Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 32
What will be the result of compiling and executing Test class?
- package com.udayan.oca;
- class M { }
- class N extends M { }
- class O extends N { }
- class P extends O { }
- public class Test {
- public static void main(String args []) {
- M obj = new O();
- if(obj instanceof M)
- System.out.print("M");
- if(obj instanceof N)
- System.out.print("N");
- if(obj instanceof O)
- System.out.print("O");
- if(obj instanceof P)
- System.out.print("P");
- }
- }
-
A
MNO
-
B
MOP
-
C
MNP
-
D
NOP
Reveal correct answer
Correct answer: A
Explanation
M
^
N
^
O [obj refers to instance of O class]
^
P
obj instanceof M -> true
obj instanceof N -> true
obj instanceof O -> true
but
obj instanceof P -> false
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
