ProgrammingModerateWipro · Cognizant technical pattern

What is printed?

What is printed?
class K {
    K() { this(5); System.out.print("A"); }
    K(int x) { System.out.print("B" + x); }
}
// in main:
new K();
  1. A. AB5
  2. B. A
  3. C. B5
  4. D. B5A

Correct answer: D. B5A

this(5) must run first and calls K(int), printing B5; control then returns to the rest of K(), printing A.

More OOP with C++ and Java questions

Exams that ask this topic