A. Basic Knowledge

    客观题

Basic Knowledge

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

Problem 1. Basic knowledge

  1. 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 and s3 will have the same content after execution.
  • s1 += s2 is equivalent to s1 = s1 + s2.
  • The program will output 1.
  • s2 is modified during the concatenation operation.
  1. 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, and v3 all contain exactly 5 elements with value 10.
  • v2 and v3 may have capacities larger than v2.size() * sizeof(int) after initialization.
  • The type of v1 is std::vector<int> while v2 is not.
  • v3.resize(5, 10) can be replaced with v3 = std::vector<int>(5, 10) for the same result.
  • All three vectors use heap memory to store their elements.
  1. 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 type Counter*.
  • The expression c.increment().increment() is valid and c.count will increase by 2.
  • In setValue(int newVal) function, this->count is equivalent to writing count directly.
  • Line B will cause a compile error because getValue() is not declared const.
  • The reset() function allows chaining calls like c.reset().increment() and c.count will equals to 1 in the end.
  • In a const member function (e.g., getValue), this is of type const Counter*.
  1. 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++ because r is declared as const.
  • References cannot be used as return types for functions.
  1. 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 from r's declaration would fix all compilation issues.
  • area() must be declared const to be called on a const object.
  • The constructor initializes width before height.
  1. 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::strings can be compared using relational operators like <.
  • std::string s; initializes s 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.
  1. 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 initializes pages to 0.
  • Book b("C++ Primer"); will initialize pages to 0.
  • Book b("Effective C++", 500); uses the third constructor.
  • The class has a compiler-generated default constructor.
  • title is initialized before pages in all constructors.
  1. 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) { ... }
    
  1. 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 call const member functions.
  • const member functions can return const references to member variables.
  • A const pointer to a class can only call const member functions.

CS100 Spring2025 Homework 5

未认领
状态
已结束
题目
3
开始时间
2025-4-11 23:30
截止时间
2025-4-25 23:59
可延期
0 小时