Welcome Guest | Sign in | Register
Language Fundamentals - Discussion Page For Q.3009 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Language Fundamentals > Questions and Answers

Q: What will happen if you try to compile and run this ?
public class Test{
static{
print(10);
}
static void print(int x){
System.out.println(x);
System.exit(0);
}
}
A. Compiler error. B. Will throw a NoSuchMethod error at runtime.
C. It will compile and run printing out "10" D.  It will run with no output.
E. It will run and print "10" and then crash with an error.      

Answer and Explanation

Answer:It will compile and run printing out "10"

Explanation:
This will run, print a message and terminate gracefully.
The runtime system needs to load the class before it can look
for the main method. So the static initializer will run first
and print "10". Immediately after that System.exit(0) will be called
terminating the program before an error can be thrown. 

Comment on this Question

Name:

Loading...




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