Right Place For Right PersonTM 
Sponsored Ads
Buy This Website(This website is for Sale)
Home Tutorials Articles Forums Source Code Books Certifications Interviews Questions

Previous Home Next

Sun Java Certification Program

Flow Control and Exception handling

Questions 1 The following method is designed to convert an input string to a floating point number while detecting a bad format. [I think factor must have been declared as float member variable or something]
public boolean strCvt(String s) {
try {
factor = Float.valueOf(s).floatValue();
return true;
} catch (NumberFormatException e) {
System.out.println(“Bad number ” + s);
factor = Float.NaN;
} finally {
System.out.println(“Finally”);
}
return false;
}
Which of the following descriptions of the results of various inputs to the method are correct?
(a) Input = “0.234” – Result: factor = 0.234, “Finally” is printed, true is returned.
(b) Input = “0.234” – Result: factor = 0.234, “Finally” is printed, false is returned.
(c) Input = null – Result: factor = NaN, “Finally” is printed, false is returned.
(d) Input = null – Result: factor = unchanged, “Finally” is printed, NullPointerException is thrown.

Questions 2 Here is the hierarchy of exceptions related to array index and string index errors:
Exception
+-- RuntimeException
+-- IndexOutOfBoundsException
+-- ArrayIndexOutOfBoundsException
+-- StringIndexOutOfBoundsException
Suppose you had a method X that could throw both array index and string index exceptions. Assuming that X does not have any try-catch statements, which of the following statements are correct?
(a) The declaration for X must include “throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException”.
(b) If a method calling X catches IndexOutOfBoundsException, both array and string index exceptions will be caught.
(c) If the declaration for X includes “throws IndexOutOfBoundsException”, any calling method must use a try-catch block.
(d) The declaration for X does not have to mention exceptions.

Questions 3 Which will be the first line to cause an error in the following code?
Select one correct answer.
1 class Char
2 {
3 public static void main(String arg[])
4 {
5 while(false)
6 {
7 System.out.println("Hello");
8 }
9 while(false)
10 {
11 }
12 do;
13 while(false);
14 do
15 {
16 ;
17 }
18 while(false);
19 }
20 }
(a) Line no. 5
(b) Line no. 9
(c) Line no. 12
(d) Line no. 16

Questions 4 What will be the result of compiling and running the given program?
Select one correct answer.
1 public class exception
2 {
3 public static void main(String args[])
4 {
5 System.out.println("A");
6 try
7 {
8 }
9 catch(java.io.IOException t)
10 {
11 System.out.println("B");
12 }
13 System.out.println("C");
14 }
15 }
(a) Compile time error.
(b) Program compiles correctly and prints "A" when executed.
(c) Program compiles correctly and prints "A" and "C" when executed.
(d) Run time error.

Questions 5 What will be the result of compiling and running the given program?
Select one correct answer.
1 public class exception
2 {
3 public static void main(String args[])
4 {
5 System.out.println("A");
6 try
7 {
8 return;
9 }
10 catch(Exception e)
11 {
12 System.out.println("B");
13 }
14 System.out.println("C");
15 }
16 }
(a) Compile time error in line no. 8 as main() method is declared void.
(b) Program compiles correctly and prints "A" when executed.
(c) Program compiles correctly and prints "A" and "C" when executed.
(d) Compile time error at line no.14 due to statement not reached.

Questions 6 What will be the result of compiling and running the given program?
Select one correct answer.
1 public class exception
2 {
3 public static void main(String args[])
4 {
5 System.out.println("A");
6 try
7 {
8 return;
9 }
10 catch(Exception e)
11 {
12 System.out.println("B");
13 }
14 finally
15 {
16 System.out.println("C");
17 }
18 }
19 }
(a) Compile time error in line no. 8 as main() method is declared void.
(b) Program compiles correctly and prints "A" when executed.
(c) Program compiles correctly and prints "A" and "C" when executed.
(d) Program compiles correctly and prints "A","B" and "C" when executed.

Questions 7 What will be the result of compiling and running the given program?
Select one correct answer.
1 public class exception
2 {
3 public static void main(String args[])
4 {
5 System.out.println("A");
6 try
7 {
8 System.out.println("B");
9 System.exit(0);
9 }
10 catch(Exception e)
11 {
12 System.out.println("C");
13 }
14 finally
15 {
16 System.out.println("D");
17 }
18 }
19 }
(a) Program compiles correctly and prints "A" when executed.
(b) Program compiles correctly and prints "A" and "B" when executed.
(c) Program compiles correctly and prints "A" and "C" when executed.
(d) Program compiles correctly and prints "A","B" and "C" when executed.

Answers:1 (a), (d)
(b) is wrong because the return value in line 4 is used. (c) is wrong because a NullPointerException is thrown in line 3 and is not caught in the method, line 7 is never reached.

Answers:2 (b), (d)
(b) is correct because exceptions obey a hierarchy just like other objects. Because these exceptions descend from RuntimeException, they do not have to be declared. Therefore, answer (d) is correct. The significant word here is “must”. Because these exceptions descend from RuntimeException, they do not have to be declared. Therefore, answer (a) is incorrect. Answer (c) is incorrect for a similar reason, because these exceptions descend from RuntimeException. They do not have to be caught even if declared by method X.

Answers:3 (a)
It will give you error for unreached statement.All other statements are valid.

Answers:4 (a)
You cannot use any checked exception in a catch block if it never thrown.

Answers:5 (b)
You can use return(without any value) in a method which is declared void.As return is used it will not print "C" but at the same time there will be no error for that.

Answers:6 (c)
As this time we have used finally so it will print "A" and "C".Finaly will always execute irrespective of whether exception is thrown or not.

Answers:7 (b)
System.exit() will restrict the finally block to be executed.

Previous Home Next
Sponsored Ads
Sponsored Ads
Sponsored Ads
Sponsored Ads
Sponsored Ads
Interview Questions And Answers
Struts interview questions and answers (Subjective)
500 Java Objective Questions and Answer
Core Java Objective Questions And Answers
Core Java Subjective Questions And Answers
Core Java Interview Questions And Answers
Core Java Interview Questions and Answers (Subjective)
Core Java Interview Questions and Answers( Objective)
50 Servlet interview questions
155 Java Interview Questions
EJB Interview Questions and Answers(Subjective)
R4R,JSP Interview Questions and Answer(Subjective)
R4R,Java Servlets Interview Questions and Answers(Subjective)
Core Java Subjective ,Objective and Interview Questions And Answers
275 Core java interview questions
Java Objective Questions and Answer
Java Architect Interview Questions
Applet Interview Questions and Answers
Core Java example
Servlet Objective Questions And Answers
Servlet Subjective Questions And Answers
Servlet Interview Questions And Answers
JSP Objective Questions And Answers
JSP Subjective Questions And Answers
JSP Interview Questions And Answers
JDBC Objective Questions And Answers
JDBC Subjective Questions And Answers
JDBC Interview Questions And Answers
JDBC Interview questions and answers
Networking Interview questions and answers
Servlets Interview questions and answers
EJB Objective Questions And Answers
EJB Subjective Questions And Answers
EJB Interview Questions And Answers
Hibernate Objective Questions And Answers
Hibernate Subjective Questions And Answers
Hibernate Interview Questions And Answers
Spring Objective Questions And Answers
Spring Subjective Questions And Answers
Spring Interview Questions And Answers
Struts Objective Questions And Answers
Struts Subjective Questions And Answers
Struts Interview Questions And Answers
Ant Objective Questions And Answers
Ant Subjective Questions And Answers
Ant Interview Questions And Answers
PHP Objective Questions And Answers
PHP Subjective Questions And Answers
PHP Interview Questions And Answers 1
ASP.net Objective Questions And Answers
ASP.net Subjective Questions And Answers
ASP.net Interview Questions And Answers 1
ASP.net interview questions ,ASP.net interview answers,ASP.net interview questions and answers
PHP interview questions ,PHP interview answers,PHP interview questions and answers
Testing Objective Questions And Answers
Testing Subjective Questions And Answers
Testing Interview Questions And Answers
Ajax Tutorials
Ajax Objective Questions And Answers
Ajax Subjective Questions And Answers
Ajax Interview Questions And Answers
Linux Objective Questions And Answers
Linux Subjective Questions And Answers
Unix Subjective Questions And Answers
Unix Interview Questions And Answers
HR interview questions and answers
HR Interview Questions Tips
HR Objective Questions And Answers
HR Subjective Questions And Answers
HR Interview Questions And Answers
Learn C Language with in a day
 C /C++ Questions
C Objective Questions And Answers
C Subjective Questions And Answers
C Interview Questions And Answers
C Objective Interview Questions And Answers(10)
C Subjective Interview Questions And Answers(100) 
C syntax,semantics and simple programming questions(61) 

Linux Interview Questions And Answers
Unix Objective Questions And Answers
C Aptitude Questions(179)
C++ Interview Questions And Answers    
C++ Objective Questions And Answers
C++ Subjective Questions And Answers
C++ Interview Questions And Answers
C,C++ objective Interview Questions and answers
C Interview Questions And Answers( Objective and Subjective)
C Objective Interview Questions And Answers(10)

C Subjective Interview Questions And Answers(100)

C,C++ Interview Questions

35 C++ Interview Questions And Answers(Subjective)

109 C++ Interview Questions

Java Script Interview Question and Answer(1-10).
 Java Script Interview Question and Answer(11-20).
Java Script  Interview Question and Answer(21-30).
Java Script Interview Question and Answer(31-40).
Javascript Objective Questions And Answers
Javascript Subjective Questions And Answers
Javascript Interview Questions And Answers
MFC Interview Questions And Answers(Subjective)

MFC Interview Questions And Answers(Subjective)

ATL Interview Questions And Answers(Subjective)

COM DCOM Interview Questions And Answers(Subjective)

Win32API Interview Questions And Answers(Subjective)

ActiveX Interview Questions And Answers(Subjective)

R4R, VC++ AllOther Interview Questions And Answers(Subjective)

PL/SQL Interview Questions And Answers(Subjective)

152 PL/SQL Interview Questions And Answers(Subjective)

Asp Objective Questions And Answers
Asp Subjective Questions And Answers
Asp Interview Questions And Answers 1

VB Objective Questions And Answers

Contact US:

Your Name:


Your Email:

Message:

Comments:

Give Your Comments:


Advertiser PRIVACY POLICY ||User PRIVACY POLICY || R4R Group Srvices