ProgrammingHardAccenture · Cognizant pattern
What is printed?
What is printed?
struct A {
A() { cout << "C"; }
A(const A &) { cout << "K"; }
~A() { cout << "D"; }
};
void f(A a) { }
int main() {
A x;
f(x);
}- A. CKDKD
- B. CKD
- C. CKDD
- D. CDD
Correct answer: C. CKDD
A x runs the default constructor (C). Passing x by value copy-constructs the parameter (K), which is destroyed when the call ends (D). x is destroyed at the end of main (D).
More OOP with C++ and Java questions
- What is printed? cpp class A { public: static int count; A() { count++; } ~A() { count--; } }; int A::count = 0; int main() { A a, b; { A…
- Which line fails to compile? cpp struct Shape { virtual double area() = 0; }; struct Circle : Shape { }; struct Square : Shape { double…
- class D : private B { ... }; where B has public int x, protected int y and private int z. Which is correct?
- Which of these is a checked exception in Java?
- A parent class declares protected void run(). Which overriding declaration in a subclass compiles?
- Parent declares void read() throws IOException. Which override in a subclass compiles?