Question 16
Back to Exercise
What is the output of the following Java program?
class P {
static { System.out.print("Ps "); }
{ System.out.print("Pi "); }
P() { System.out.print("Pc "); }
}
class C extends P {
static { System.out.print("Cs "); }
{ System.out.print("Ci "); }
C() { System.out.print("Cc "); }
}
public class Main {
public static void main(String[] args) {
new C();
System.out.println();
new C();
}
}
- A Ps Cs Pi Pc Ci Cc Pi Pc Ci Cc Correct
- B Ps Pi Pc Cs Ci Cc Ps Pi Pc Cs Ci Cc
- C Cs Ps Ci Cc Pi Pc Ci Cc Pi Pc
- D Ps Cs Pi Ci Pc Cc Pi Ci Pc Cc
Correct Answer: Option A
Static parts run once, parent first. For each object: the parent's instance block and constructor, then the child's.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!