Submitted by c-admin on Wed, 06/05/2019 - 06:17
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
Submitted by c-admin on Wed, 06/05/2019 - 06:15
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.
Submitted by c-admin on Wed, 06/05/2019 - 06:13
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.
Submitted by c-admin on Wed, 06/05/2019 - 06:09
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.
Submitted by c-admin on Wed, 06/05/2019 - 05:57
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.
Pages