ProgrammingModerateProduct-company placement · C++

What is printed?

What is printed?
struct N { int v; N(int x = 7) : v(x) { } };
void show(N n) { cout << n.v; }
int main() {
    N a;
    N b = 3;
    show(4);
    show(a);
}
  1. A. 47
  2. B. 74
  3. C. 43
  4. D. 77

Correct answer: A. 47

A constructor whose parameters all have defaults is also the default constructor (a.v = 7), and as a non-explicit single-argument constructor it converts 4 into an N for show.

More OOP with C++ and Java questions

Exams that ask this topic