Welcome Guest | Sign in | Register
Flow Control - Discussion Page For Q.2800 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Flow Control > Questions and Answers

Q: What is the Output of below code after compilation :

1 .public class Question3{
2 .public static void main(String[] args) {
3 .try {
4 .int a= (int)(Math.random()*5);
5 .if(a<=2.5)
6 .System.out.println("a="+a);
7 .else
8 .throw new Exception("a>2.5");
9. } catch (Exception e){

10. System.err.println(e.getMessage() );
11. System.err.println("Value of a="+a);
12. } } }

Find the output of the following code :
A. An Exception is raised during compilation. B. An Exception is thrown at run time.
C. Unresolved compilation problem. D. a=2 or a=1 or i >2.5 Value of i =null

Answer and Explanation

Answer:Unresolved compilation problem.

Explanation:
This code will not compile because variable "a" at line number 11 , is not recognize by compiler since "a" is a local variable declared inside "Try" block. This code can be corrected by applying code line "static String a = null ;"at line number2(or immediately after declaration of class & before 'main( )').

Comment on this Question

Name:

Loading...




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