ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
// C++17
struct V {
int d[2] = {1, 2};
int& operator[](int i) { cout << "N"; return d[i]; }
int operator[](int i) const { cout << "C"; return d[i]; }
};
int main() {
V v;
const V& cv = v;
v[0] = 5;
cout << cv[0];
}- A. NC5
- B. NN5
- C. CC5
- D. NC1
Correct answer: A. NC5
The call is resolved on the object's constness: v is non-const (N, returns a reference so the assignment writes d[0] = 5), cv is a const view of the same object (C). It reads the updated 5.
More OOP with C++ and Java questions
- Which lines fail to compile? cpp struct E { explicit E(int) { } }; int main() { E a(1); // L1 E b{1}; // L2 E c = 1; // L3 E d = {1}; //…
- Which line fails to compile? cpp struct P { const int id; }; int main() { P a{1}, b{2}; P c = a; // L1 a = b; // L2 }
- How many of these statements about implicitly generated special members in C++11 and later are correct? (i) If a class declares a copy…
- What happens when this is compiled? cpp struct A { A(int) { } }; int main() { A a; }
- What is printed? cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void f() override { cout << "D"; } }; int main() {…
- What is printed? cpp template <class T> struct Base { void hi() { staticcast<T>(this)->impl(); } void impl() { cout << "B"; } }; struct X…