πŸ“Š JAVA
Q. 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;
}
}
  • (A) Compilation error.
  • (B) IllegalArgumentException is thrown at runtime.
  • (C) InsideDemo 10 Hello2
  • (D) InsideDemo 10 Hello2 D. Hello2 InsideDemo 10
πŸ’¬ Discuss
βœ… Correct Answer: (C) InsideDemo 10 Hello2

Explanation: 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.

Explanation by: Vijay Sangwan
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
90
Total Visits
πŸ“½οΈ
1 y ago
Published
πŸŽ–οΈ
Vijay Sangwan
Publisher
πŸ“ˆ
86%
Success Rate