ProgrammingHardTCS NQT · Infosys interview pattern
What is printed?
What is printed?
struct A {
A() { cout << "D"; }
A(const A&) { cout << "C"; }
A& operator=(const A&) { cout << "="; return *this; }
};
int main() {
A a;
A b = a;
A c;
c = a;
}- A. D=D=
- B. DCDC
- C. DCD=
- D. DDD=
Correct answer: C. DCD=
A b = a initialises a new object, so it calls the copy constructor (C), despite the = sign. c = a assigns to an already constructed object, so it calls operator= (=). Output: D, C, D, =.
More OOP with C++ and Java questions
- What is printed? java String s = "b"; switch (s) { case "a": System.out.print("A"); case "b": System.out.print("B"); case "c"…
- What happens when this Java code is compiled? java try { FileReader r = new FileReader("a.txt"); } catch (Exception e) {…
- This class is meant to be immutable. What change is still needed? java public final class Team { private final List<String> members…
- What is printed? cpp struct V { int x; V(int a) : x(a) {} V operator+(const V& o) const { return V(x + o.x 2); } }; int main() { V a(1)…
- What is printed? java class A { void m(A a) { System.out.print("AA"); } } class B extends A { void m(A a) { System.out.print("BA"); } void…
- Class P is in package a and declares protected int x. Class C is in package b and extends P. Inside an instance method of C, which of…