← Quiz: OOP Interview Questions: all 10 questions
Quiz: OOP Interview Questions · Question 2 of 10
What does this print? (code)
What does this print?
class A { int x = 1; int get() { return x; } }
class B extends A { int x = 2; int get() { return x; } }
public class Main {
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.x + " " + obj.get());
}
}
- A.2 2
- B.1 2
- C.1 1
- D.2 1
Answer & explanation
Correct answer: 1 2
Fields use the declared type (A → 1); methods use the real object (B → 2).
Can you get the rest right without peeking?
Take the timed Quiz: OOP Interview Questions quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →