ProgrammingModerateTCS NQT · Infosys pattern
What is printed?
What is printed?
class A {
public:
A() { cout << "A"; }
~A() { cout << "a"; }
};
class B : public A {
public:
B() { cout << "B"; }
~B() { cout << "b"; }
};
int main() { B obj; }- A. BAba
- B. ABba
- C. ABab
- D. BAab
Correct answer: B. ABba
Construction runs base first, then derived: A then B. Destruction runs in exactly the reverse order: ~B then ~A. So ABba.
More OOP with C++ and Java questions
- What is printed? java class C { static int n = 0; C() { n++; } } // in main: for (int i = 0; i < 3; i++) new C(); C c = new C()…
- In Java, final applied to a class, a method and a variable means, respectively:
- What is printed? java List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(1, 5); System.out.print(list);
- What is printed? java Set<String> s = new HashSet<>(); s.add("a"); s.add("b"); s.add("a"); System.out.print(s.size() + " " + s.add("b"));
- What is printed? java try { System.out.print("T"); throw new RuntimeException(); } catch (Exception e) { System.out.print("C"); } finally…
- Why must a C++ copy constructor take its parameter by reference (A(const A &other)) rather than by value?