ProgrammingHardTCS NQT Digital · Accenture Advanced
What is printed?
What is printed?
class A {
private void f() { System.out.print("A"); }
void g() { f(); }
}
class B extends A { void f() { System.out.print("B"); } }
// main: new B().g();- A. B
- B. AB
- C. A
- D. Compile error
Correct answer: C. A
Private methods are not inherited, so B.f does not override A.f; g() inside A calls A's private f.
More OOP with C++ and Java questions
- What is printed? java class A { void f() { System.out.print("A"); g(); } void g() { System.out.print("a"); } } class B extends A { void…
- What is printed? java int x = Integer.MAXVALUE; long y = x + 1; long z = x + 1L; System.out.print(y + " " + z);
- What is printed? java class A { A() { this(1); System.out.print("0"); } A(int x) { System.out.print("1"); } } class B extends A { B() {…
- Which OOP principle bundles data with the methods that act on it and hides the internal state behind an interface?
- In Java, which keyword makes one class inherit from another class?
- What is the return type of a C++ constructor?