JavaQuizzes

Quiz Guidelines

 

Multiple Choice Question

QID: 
542

You have a method that currently does not handle any exception thrown from the code contained in its method body. You are now changing this method to call another method that throws IO Exception.

What changes, independent of each other, can you make to your method so that it will compile?

Select 2 options

A. Set the exception to null and don't rethrow it.
B. Declare IO Exception in the throws clause of your method.
C. Wrap the call to another method within a try-catch block that catches RuntimeException.
D. Wrap the call to another method within a try-catch block that catches Exception.

Multiple Choice Question

QID: 
543

Identify the exceptions that will be received when the following code snippets are executed.

1. int factorial(int n){
       if(n==1) return 1;
      else return n*factorial(n-1);
   }
Assume that it is called with a very large integer.

2. void printMe(Object[] oa){
       for(int i=0; i<=oa.length; i++)
       System.out.println(oa[i]);
    }
Assume that it is called as such: printMe(null);

3. Object m1(){
       return new Object(); 
    }
    void m2(){
       String s = (String) m1();
    }

Select 1 option

A. Class Cast Exception
Array Index out of Bounds Exception
Stack Overflow Error

B. Class Cast Exception
Array Index Out Of Bounds Exception
Security Exception

C. No Exception Will Be Thrown
Security Exception
Will Not Compile
 
D. Stack Over flow Error
NullPointerException
No Exception Will Be Thrown 

E. Stack Over flow Error
Array Index Out Of Bounds Exception
Class Cast Exception

F. Stack Over flow Error
NullPointerException
NullPointerException
 
G. Security Exception
Null Pointer Exception
No Exception Will Be Thrown
 
H. Stack Overflow Error
Null Pointer Exception
Class Cast Exception

Multiple Choice Question

QID: 
544

Given that SomeException is a checked exception, consider the following code:

//in file A.java
public class A{ 
   protected void m() throws SomeException{} 
}

//in file B.java
public class B extends A{ 
   public void m(){ } 
}

//in file TestClass.java
public class TestClass{
   public static void main(String[] args){
      // insert code here. // 1
   }
}

Which of the following options can be inserted at //1?

Select 1 option

A. B b =  new A();
b.m();
B. A a = new B();
a.m();
C. A a = new B();
( ( B) a ).m(); 
D. Object o = new B();
o.m();
E. None of these.

Multiple Choice Question

QID: 
545

What can be done to get the following code to compile and run? (Assume that the options are independent of each other.)

public float parseFloat( String s ){
     float f = 0.0f;      // 1
     try{
          f = Float.valueOf( s ).floatValue();    // 2
          return f ;      // 3
     }
     catch(NumberFormatException nfe){
        f = Float.NaN ;    // 4
       return f;     // 5
     }
     finally {
         return f;     // 6
     }
     return f ;    // 7
 }

Select 4 options

A. Remove line 3, 6
B. Remove line 5
C. Remove line 5, 6 
D. Remove line 7
E. Remove line 3, 7

Multiple Choice Question

QID: 
546

What will the following code print when run?

public class Test {
    static String s = "";
    public static void m0(int a, int b) {
        s += a;
        m2();
        m1(b);
    }
    public static void m1(int i) {
        s += i;
    }
    public static void m2() {
        throw new NullPointerException("aa");
    }
    public static void m() {
        m0(1, 2);
        m1(3);
    }
    public static void main(String args[]) {
        try {
            m();
        } catch (Exception e) {
        }
        System.out.println(s);
    }
}

Select 1 option

A. 1
B. 12
C. 123
D. 2 
E. It will throw exception at runtime.

Pages