Basic Knowledge
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
Problem 1. Basic knowledge
- Read the following code. Which of the following statements is/are true?
#include <iostream>
int main() {
std::string s1 = "Hello";
std::string s2 = "C++";
std::string s3 = s1 + s2;
s1 += s2;
std::cout << (s1 == s3) << std::endl;
}
{{ multiselect(1) }}
s1ands3will have the same content after execution.s1 += s2is equivalent tos1 = s1 + s2.- The program will output
1. s2is modified during the concatenation operation.
- Read the following code. Which of the following statements is/are true?
#include <vector>
int main() {
std::vector<int> v1(5, 10);
std::vector<int> v2 = {10, 10, 10, 10, 10};
std::vector<int> v3;
v3.resize(5, 10);
}
{{ multiselect(2) }}
v1,v2, andv3all contain exactly 5 elements with value 10.v2andv3may have capacities larger thanv2.size() * sizeof(int)after initialization.- The type of
v1isstd::vector<int>whilev2is not. v3.resize(5, 10)can be replaced withv3 = std::vector<int>(5, 10)for the same result.- All three vectors use heap memory to store their elements.
- Read the following code and select the correct statements:
class Counter {
int count = 0;
public:
Counter& increment() {
++count;
return *this;
}
Counter reset() {
count = 0;
return *this;
}
int getValue() const {
return count;
}
void setValue(int newVal) {
this->count = newVal;
}
};
int main() {
Counter c;
c.increment().increment().reset();
c.setValue(5);
const Counter& cref = c;
int val = cref.getValue(); // Line B
}
{{ multiselect(3) }}
- In the
incrementmember function,thisis of typeCounter*. - The expression
c.increment().increment()is valid andc.countwill increase by 2. - In
setValue(int newVal)function,this->countis equivalent to writingcountdirectly. - Line B will cause a compile error because
getValue()is not declaredconst. - The
reset()function allows chaining calls likec.reset().increment()andc.countwill equals to 1 in the end. - In a
constmember function (e.g.,getValue),thisis of typeconst Counter*.
- Which of the following statements about references in C++ is/are true?
{{ multiselect(4) }}
- A reference must be initialized when declared.
- References can be rebound to different objects after initialization.
- A function returning a reference to a local variable is safe.
const int& r = 5;is valid in C++ becauseris declared asconst.- References cannot be used as return types for functions.
- Read the following code. Which of the following statements is/are true?
class Rectangle {
int width, height;
public:
Rectangle(int w, int h) : width(w), height(h) {}
int area() const { return width * height; }
void setWidth(int w) { width = w; }
};
int main() {
const Rectangle r(4, 5);
std::cout << r.area() << std::endl;
r.setWidth(10);
}
{{ multiselect(5) }}
- The code will compile and output
20. r.setWidth(10)will cause a compile error.- Removing
constfromr's declaration would fix all compilation issues. area()must be declaredconstto be called on aconstobject.- The constructor initializes
widthbeforeheight.
- Which of the following statements about
std::stringis/are true?
{{ multiselect(6) }}
std::string::size()returns the number of characters excluding the null terminator.std::strings can be compared using relational operators like<.std::string s;initializessto an empty string.- We cannot modify the content of
std::stringonce we have initialized the varible. std::getline(std::cin, s)stops reading at the first whitespace character.
- Consider the following code. Which statements about constructors are true?
class Book {
std::string title;
int pages;
public:
Book() : pages(0) {}
Book(std::string t) : title(t), pages(0) {}
Book(std::string t, int p) : title(t), pages(p) {}
};
{{ multiselect(7) }}
Book b;is valid and initializespagesto 0.Book b("C++ Primer");will initializepagesto 0.Book b("Effective C++", 500);uses the third constructor.- The class has a compiler-generated default constructor.
titleis initialized beforepagesin all constructors.
- Which of the following code snippets correctly uses range-based
forloops?
{{ multiselect(8) }}
-
std::vector<int> v{1,2,3}; for (int i : v) { ... } -
int arr[3] = {4,5,6}; for (auto& x : arr) { ... } -
std::string s = "hello"; for (char c : s) { ... } -
const std::vector<float> vec; for (float f : vec) { ... }
- Which statements about
constmember functions are correct?
{{ multiselect(9) }}
constmember functions cannot modify any member variables.constmember functions can call non-constmember functions.- Non-
constobjects cannot callconstmember functions. constmember functions can return const references to member variables.- A
constpointer to a class can only callconstmember functions.