Welcome Guest | Sign in | Register
Static Concept - Discussion Page For Q.2931 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Static Concept > Questions and Answers

Exercise:

Section 1

Q: class J{
Static int I;
Static
{
System.out.println(i);
I=100;
}
Public static void main(String args[])
{
System.out.println(i);
}
Static
{
System.out.println(i);
I=200;
}

}
A. 0 200 100 B. 0 100 200
C. compile time error D. 200 100 0

Answer and Explanation

Answer:0 100 200

Explanation:
here we have declared two SIB(static initialization block), JVM always executes SIB first before the main() method , then it executes main() method,if you have more than one SIB then JVM will execute in the order in which they appear the program , and if you declare a global variable and don’t initialize it then it will take default value depending on the datatype. So in first SIB default value of I will be displayed , and I will be initialized to 100, and in second SIB 100 will displayed and initialized to 200 then in main() 200 will be displayed. So ans b) is correct.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.