JavaQuizzes

Quiz Guidelines

 

Multiple Choice Question

QID: 
522

Following is a supposedly robust method to parse an input for a float :

public float parseFloat(String s){
float f = 0.0f;
try{
f = Float.valueOf(s).floatValue();
return f ;
}
catch(NumberFormatException nfe){
System.out.println("Invalid input " + s);
f = Float.NaN ;
return f;
}
finally { System.out.println("finally"); }
return f ;
}

Which of the following statements about the above method are true??

Select 1 option

A. If input is "0.1" then it will return 0.1 and print finally.
B. If input is "0x.1" then it will return Float.Nan and print Invalid Input 0x.1 and finally.
C. If input is "1" then it will return 1.0 and print finally.
D. If input is "0x1" then it will return 0.0 and print Invalid Input 0x1 and finally.
E. The code will not compile.

Multiple Choice Question

QID: 
523

Consider the following code:

class A {
public void doA(int k) throws Exception {        // 0
for(int i=0; i< 10; i++) {
if(i == k) throw new Exception("Index of k is "+i);        // 1
}
}
public void doB(boolean f) {       // 2
if(f) {
doA(15); // 3
}
else return;
}
public static void main(String[] args) { // 4
A a = new A();
a.doB(args.length>0); // 5
}
}

Which of the following statements are correct?

Select 1 option

A. This will compile and run without any errors or exception.
B. This will compile if throws Exception is added at line //2
C. This will compile if throws Exception is added at line //4
D. This will compile if throws Exception is added at line //2 as well as //4
E. This will compile if line marked // 1 is enclosed in a try - catch block.

Multiple Choice Question

QID: 
524

What will be the result of attempting to compile and run the following program?

public class TestClass{
public static void main(String args[]){
Exception e = null;
throw e;
}
}

Select 1 option

A. The code will fail to compile.
B. The program will fail to compile, since it cannot throw a null.
C. The program will compile without error and will throw an Exception when run.
D. The program will compile without error and will throw
              java.lang.NullPointerException when run
E. The program will compile without error and will run and terminate without any output.

Multiple Choice Question

QID: 
525

What will be the output of the following class...

class Test{
public static void main(String[] args){
int j = 1;
try{
int i = doIt() / (j = 2);
} catch (Exception e){
System.out.println(" j = " + j);
}
}
public static int doIt() throws Exception { throw new Exception("FORGET }

Select 1 option

A. It will print j = 1;
B. It will print j = 2;
C. The value of j cannot be determined.
D. It will not compile.
E. None of the above.

Multiple Choice Question

QID: 
526

What will be the output of the following program?

class TestClass{
public static void main(String[] args) throws Exception{
try{
amethod();
System.out.println("try");
}
catch(Exception e){
System.out.println("catch");
}
finally {
System.out.println("finally");
}
System.out.println("out");
}
public static void amethod(){ }
}

Select 1 option

A. try finally
B. try finally out
C. try out
D. catch finally out
E. It will not compile because amethod() does not throw any exception.

Pages