JavaQuizzes

Quiz Guidelines

 

Descriptive Question

QID: 
537

Identify the Exceptions that will be received when the code snippets on the left hand side are executed.

Multiple Choice Question

QID: 
538

What will be the output when the following program is run?

package exceptions; 
public class TestClass{    
public static void main(String[] args) {        
try{            
hello();        }        
catch(MyException me){            
System.out.println(me);        }    }        
static void hello() throws MyException{        
int[] dear = new int[7];        
dear[0] = 747;        
foo();    }        
static void foo() throws MyException{        
throw new MyException("Exception from foo");    } } 
class MyException extends Exception {    
public MyException(String msg){        
super(msg);    }
 } 

(Assume that line numbers printed in the messages given below are correct)

Select 1 option 

A. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at exceptions.TestClass.doTest(TestClass.java:24)    
at exceptions.TestClass.main(TestClass.java:14) 
B. Error in thread "main" java.lang.ArrayIndexOutOfBoundsException 
C. exceptions.MyException: Exception from foo 
D. exceptions.MyException: Exception from foo    
at exceptions.TestClass.foo(TestClass.java:29)    
at exceptions.TestClass.hello(TestClass.java:25)    
at exceptions.TestClass.main(TestClass.java:14) 

Multiple Choice Question

QID: 
539

Consider the following

public class TestClass {
public static void main(String[] args) { 
TestClass tc = new TestClass(); 
tc.myMethod();
}
public void myMethod() { 
yourMethod();
}
public void yourMethod() { 
throw new Exception();
}
}

What changes can be done to make the above code compile?

Select 1 option

A. Change declaration of main to 
          public static void main(String[] args) throws Exception
B. Change declaration of myMethod to
          public void myMethod throws Exception
C. Change declaration of yourMethod to
          public void yourMethod throws Exception
D. Change declaration of main and yourMethod to 
          public static void main(String[] args) throws Exception and
          public void yourMethod throws Exception
E. Change declaration of all the three method to include throws Exception.

Multiple Choice Question

QID: 
540

Java Exceptions is a mechanism...

Select 2 options

A. for dealing with unexpected user inputs.
B. that you can use to determine what to do when something unexpected happens.
C. for logging unexpected behavior.
D. to ensure that the program runs even if something unexpected happens.
E. that the VM uses to exit the program when something unexpected happens.

Multiple Choice Question

QID: 
541

Consider the following code:

public class TestClass {
    public static void doStuff() throws Exception{
        System.out.println("Doing stuff...");
        if(Math.random()>0.4){
            throw new Exception("Too high!");
        }
        System.out.println("Done stuff.");
    }
    public static void main(String[] args) throws Exception {
        doStuff();
        System.out.println("Over"); 
    }
}

Which of the following are possible outputs when the above program is compiled and run? (Assume that Math. Random () returns a double between 0.0 and 1.0 not including 1.0. Further, assume that there is no mistake in the line numbers printed in the output.)

Select 2 options

A. Doing stuff...
Exception in thread "main" java.lang.Exception: Too high!
 at TestClass.doStuff(TestClass.java:29)
 at TestClass.main(TestClass.java:41)
B.Doing stuff...
Exception in thread "main" java.lang.Exception: Too high!
 at TestClass.doStuff(TestClass.java:29)
 at TestClass.main(TestClass.java:41)
Over 
C.Doing stuff...
Done stuff.
Over
D.Doing stuff...
Exception in thread "main" java.lang.Exception: Too high!
 at TestClass.doStuff(TestClass.java:29)
 at TestClass.main(TestClass.java:41)
Done stuff.

Pages