Basic Knowledge
You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.
Problem 1. Basic knowledge
- Consider the following code about static members. Which statements are true?
class Counter {
static int count;
public:
static void increment() { ++count; }
static int get() { return count; }
};
int Counter::count = 0;
{{ multiselect(1) }}
countis shared among all instances ofCounter.- The initialization of
Counter::countmust be done outside the class definition. increment()can access non-static member variables ofCounter.Counter::get()can be called without anyCounterobject instance.- Static member functions have no
thispointer.
- Read the following code about move semantics. Which statements are true?
class Data {
int* ptr;
public:
Data(Data&& other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
Data& operator=(Data&& other) noexcept {
delete ptr;
ptr = other.ptr;
other.ptr = nullptr;
return *this;
}
};
{{ multiselect(2) }}
- The moved-from object will be immediately destructed after calling move assignment/constructor.
- The move assignment operator should check for self-assignment.
- Move operations typically don't throw exceptions and are marked
noexcept. - If we define a move constructor, the compiler will automatically generate a copy constructor.
- The expression
other.ptr = nullptrin move constructor prevents double deletion.
- Which statements about smart pointers are correct?
{{ multiselect(3) }}
std::unique_ptrallows multiple pointers to manage the same object.std::make_sharedis preferred overnewfor creatingstd::shared_ptr.std::shared_ptruses reference counting to manage resources.std::unique_ptrcan be returned from a function by moving.std::shared_ptrhas no overhead compared to raw pointers.
- Consider the following code about operator overloading. Which statements are true?
class Vector {
int x, y;
public:
Vector operator+(const Vector& rhs) const {
return Vector(x + rhs.x, y + rhs.y);
}
friend std::ostream& operator<<(std::ostream& os, const Vector& v);
};
{{ multiselect(4) }}
- The
+operator is overloaded as a member function. operator+can access private members ofrhs.operator<<must be declared as a friend to access private members.- The expression
v1 + v2is equivalent tov1.operator+(v2). operator+returns a reference to a temporary object.
- Which statements about type conversions are correct?
{{ multiselect(5) }}
explicitconstructors prevent implicit conversions.- Type conversion can only occur through (implicit/explicit) conversion opeator function call.
operator bool()should usually beexplicit.- A constructor with multiple parameters can be a conversion function.
- Contextual conversion to
boolworks even withexplicitoperators.
- Read the following code about destructors. Which statements are true?
class ResourceHolder {
int* ptr;
public:
~ResourceHolder() { delete ptr; }
ResourceHolder(ResourceHolder&& other) : ptr(other.ptr) { other.ptr = nullptr; }
};
{{ multiselect(6) }}
- The destructor ensures no memory leak occurs.
- The move constructor leaves the source object in a destructible state.
- If we define a destructor, the compiler will delete move operations.
- The class follows the Rule of Five.
ptrshould be checked for nullptr before deletion in destructor.
- Which statements about rvalue references are correct?
{{ multiselect(7) }}
std::moveconverts an lvalue to an rvalue reference.- An rvalue reference parameter can bind to lvalues.
int&& x = 5;is valid.- After moving an object, its state is unspecified.
- Rvalue references extend the lifetime of temporary objects.
- Consider the following code about friend functions. Which statements are true?
class Box {
int width;
friend void printWidth(const Box&);
};
void printWidth(const Box& b) { std::cout << b.width; }
{{ multiselect(8) }}
printWidthcan access private members ofBox.printWidthis a member function ofBox.- Friend declarations can appear in any section (public/private) of the class.
printWidthmust be defined inside the class.- Friendship is inherited in derived classes.
- Which statements about
std::unique_ptrare correct?
{{ multiselect(9) }}
- It can be copied if the managed object is const.
- It automatically releases memory when going out of scope.
std::unique_ptr<T[]>usesdelete[]for deallocation.- Moving a
unique_ptrtransfers ownership.
- Consider the following code about type aliases. Which statements are true?
class Container {
public:
using size_type = std::size_t;
using value_type = int;
};
{{ multiselect(10) }}
size_typecan be accessed asContainer::size_type.- Type alias members are independent of access specifiers(private, protected, public).
value_typecan be modified by class users.- Type aliases can be used within member functions.
usingdeclarations can replacetypedefcompletely in C++.
CS100 Spring2025 Homework 6
- Status
- Done
- Problem
- 4
- Open Since
- 2025-4-27 18:00
- Deadline
- 2025-5-11 23:45
- Extension
- 0 hour(s)