Submitted by c-admin on Wed, 06/05/2019 - 04:04
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.
Submitted by c-admin on Wed, 06/05/2019 - 03:59
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.
Submitted by c-admin on Wed, 06/05/2019 - 03:55
Considering the following program, which of the options are true?
public class FinallyTest{
public static void main(String args[]){
try{
if (args.length == 0) return;
else throw new Exception("Some Exception");
}
catch(Exception e){
System.out.println("Exception in Main");
} finally{
System.out.println("The end");
}
} }
Select 2 options
A. If run with no arguments, the program will only print 'The end'.
B. If run with one argument, the program will only print 'The end'.
C. If run with one argument, the program will print 'Exception in Main' and 'The end'.
D. If run with one argument, the program will only print 'Exception in Main'.
E. Only one of the above is correct.
Submitted by c-admin on Wed, 06/05/2019 - 03:50
What will be the output of the following program:
public class TestClass{
public static void main(String args[]){
try{
m1();
}catch(IndexOutOfBoundsException e){
System.out.println("1");
throw new NullPointerException();
}catch(NullPointerException e){
System.out.println("2");
return;
}catch (Exception e) {
System.out.println("3");
}finally{
System.out.println("4");
}
System.out.println("END");
} // IndexOutOfBoundsException is a subclass of RuntimeException.
static void m1(){
System.out.println("m1 Starts");
throw new IndexOutOfBoundsException( "Big Bang " );
}
}
Select 3 options
A. The program will print m1 Starts.
B. The program will print m1 Starts, 1 and 4, in that order.
C. The program will print m1 Starts, 1 and 2, in that order.
D. The program will print m1 Starts, 1, 2 and 4 in that order.
E. END will not be printed.
Submitted by c-admin on Wed, 06/05/2019 - 03:46
What is wrong with the following code written in a single file named TestClass.java?
class SomeThrowable extends Throwable { }
class MyThrowable extends SomeThrowable { }
public class TestClass{
public static void main(String args[]) throws SomeThrowable{
try{
m1();
}catch(SomeThrowable e){
throw e;
}finally{
System.out.println("Done");
}
}
public static void m1() throws MyThrowable{
throw new MyThrowable();
}
}
Select 2 options
A. The main declares that it throws SomeThrowable but throws MyThrowable.
B. You cannot have more than 2 classes in one file.
C. The catch block in the main method must declare that it catches MyThrowable rather than SomeThrowable.
D. There is nothing wrong with the code.
E. Done will be printed.
Pages