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) }}
s1
ands3
will have the same content after execution.s1 += s2
is equivalent tos1 = s1 + s2
.- The program will output
1
. s2
is 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
, andv3
all contain exactly 5 elements with value 10.v2
andv3
may have capacities larger thanv2.size() * sizeof(int)
after initialization.- The type of
v1
isstd::vector<int>
whilev2
is 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
increment
member function,this
is of typeCounter*
. - The expression
c.increment().increment()
is valid andc.count
will increase by 2. - In
setValue(int newVal)
function,this->count
is equivalent to writingcount
directly. - Line B will cause a compile error because
getValue()
is not declaredconst
. - The
reset()
function allows chaining calls likec.reset().increment()
andc.count
will equals to 1 in the end. - In a
const
member function (e.g.,getValue
),this
is 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++ becauser
is 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
const
fromr
's declaration would fix all compilation issues. area()
must be declaredconst
to be called on aconst
object.- The constructor initializes
width
beforeheight
.
- Which of the following statements about
std::string
is/are true?
{{ multiselect(6) }}
std::string::size()
returns the number of characters excluding the null terminator.std::string
s can be compared using relational operators like<
.std::string s;
initializess
to an empty string.- We cannot modify the content of
std::string
once 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 initializespages
to 0.Book b("C++ Primer");
will initializepages
to 0.Book b("Effective C++", 500);
uses the third constructor.- The class has a compiler-generated default constructor.
title
is initialized beforepages
in all constructors.
- Which of the following code snippets correctly uses range-based
for
loops?
{{ 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
const
member functions are correct?
{{ multiselect(9) }}
const
member functions cannot modify any member variables.const
member functions can call non-const
member functions.- Non-
const
objects cannot callconst
member functions. const
member functions can return const references to member variables.- A
const
pointer to a class can only callconst
member functions.