Determine output:
public class InitDemo{
static int i = demo();
static{
System.out.print(i);
}
InitDemo(){
System.out.print("hello1");
}
public static void main(String... args){
System.out.print("Hello2");
}
static int demo(){
System.out.print("InsideDemo");
return 10;
}
}
public class InitDemo{
static int i = demo();
static{
System.out.print(i);
}
InitDemo(){
System.out.print("hello1");
}
public static void main(String... args){
System.out.print("Hello2");
}
static int demo(){
System.out.print("InsideDemo");
return 10;
}
}
A. Compilation error.
B. IllegalArgumentException is thrown at runtime.
C. InsideDemo 10 Hello2
D. Hello2 InsideDemo 10
E. InsideDemo 10 Hello2 hello1
Answer: Option C
Solution(By Examveda Team)
As soon as the class are loaded static variables are initialized first. To initialize it demo must be called first then then static block executes and then main method is called.
Join The Discussion
Comments ( 1 )
Related Questions on Declaration and Access Control
What is the default access modifier for class members in Java if no access modifier is specified?
A. public
B. protected
C. private
D. package-private
A. private
B. public
C. protected
D. package-private
What keyword is used to declare a constant in Java, which is implicitly final and static?
A. final
B. const
C. static
D. constant
A. protected
B. private
C. public
D. default
Before the loading of main , static block is called.but here static block is not executed at first.so I think it's the wrong answer