Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
runtime ?
try
{
tryThis();
return;
}
catch(IOException x1)
{
System.out.println("exception 1 ");
return;
}
catch(Exception x2)
{
System.out.println("exception 2");
return;
}
finally
{
System.out.println("finally");
}
Optons
a exception 1 followed by finally
b exception 2 followed by finally
c exception 1
d exception 2
ans a
Q2 static class A {
void process() throws Exception { throw new Exception(); }
}
static class B extends A
{
void process() { System.out.println("B");
}
}
public static void main(String ar[]) {
new B().process();
}
Options
a B
b The code runs with no output
c Compilation error
Ans a
Q3 What will be the result of attempting to compile and run following code ?
public class Foo {
static int[] a;
static { a[0]=2;}
public static void main(String[] ar) {}
Options
a. java.lang.StackOverflowError
b. java.lang.ExceptionInitializerError
c. java.lang.ArrayIndexOutOfBoundsException
d. java.lang.NullPointerException
Ans b
Q4
Which is the most appropriate way to handle invalid method passed to public meth
od ?
a Throw InvalidArgumentException
b Thow IllegalArgumentException
c Throw IllegalStateException
ans b Thow IllegalArgumentException
Q5 What will be the result of attempting to compile and run following code ?
class MyException extends Exception {}
class Test
{
void f() throws MyException
{
throw new MyException();
}
public static void main(String ar[]) throws MyException
{
MyException e1 = null;
Test t = new Test();
try
{
t.f();
}
catch(MyException e)
{
e1 = e;
System.out.println("catch");
}
finally
{
System.out.println("finally");
throw e1;
}
System.out.println("End");
}
}
Options
a Prints catch
b Prints catch ,finally,end
c Does not compile
d Print catch ,end
ans b
options
a Compilation succeds
b Class A does not compile
c TestA compiles if line 10 is enclosed in a try/catch block that catches TestEx
ception
d RuntimeException
ans c
Q11 What will be the result of attempting to compile and run following code ?
class MyException extends Exception {}
class Test
{
void f() throws MyException
{
throw new MyException();
}
public static void main(String ar[]) throws MyException
{
MyException e1 ;
Test t = new Test();
try
{
t.f();
}
catch(MyException e)
{
e1 = e;
System.out.println("catch");
}
finally
{
System.out.println("finally");
throw e1;
}
System.out.println("End");
}
}
Options
a Prints catch
b Prints catch ,finally,end
c Does not compile
d b Prints catch ,finally
ans c
Q12 What is the result of compiling and running the following code ?
class Test
{
public static void main(String args[])
{
int i=0;
try
{
for(;true;i++) //1
{
if(i/i++ >0)
break;//2
}
}
catch(RuntimeException e)
{
System.out.println("RuntimeException ");
}
catch(ArithmeticException e)
{
System.out.println("RuntimeException ");
}
catch(Exception e)
{
System.out.println("RuntimeException ");
}
finally
{
System.out.println("finally ");
}
}
}
Options
a compiles and run,no exceptions are thrown Prints "finally"
b Compiler error at line 1
c Compiler error at line 2
d Compiles and run ,prints Arithmetic Exception" and finally
e Compiles and run ,prints RuntimeException Arithmetic Exception" and fina
lly
f None of these
g Compiles and run ,prints RuntimeException" and finally
h g Compiles and run ,prints Exception" and finally
Ans f None of these
Q15 what will be the result of an attempt to compile and run the following progr
am ?
class Test
{
void f() throws NullPointerException
{
throw new RuntimeException();
}
public static void main(String ar[])
{
Test t = new Test();
try
{
t.f();
}
catch(Exception e)
{
System.out.println("catch");
}
finally
{
System.out.println("finally");
throw e;
}
}
}
Options
a Code does not compile because f() does not declare that it throws RuntimeExcep
tion
b compiles and prints catch finally
c Compiles and prints catch
d None of the above
Options
a throw new Exception() at line 1
b. throw new MyException() at line 4
c throw new MyException() at line 6
d. throws new Exception() at line 3
Ans b
Q17
class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
public class Test{
void thrower() throws ExceptionB{
throw new ExceptionB();
}
public static void main(String[] args){
Test t = new Test();
try{t.thrower();}
catch(ExceptionA e) {System.out.println("ExceptionA"); }
catch(ExceptionB e) {System.out.println("ExceptionB");}
}
}
a Prints ExceptionA
b Prints ExceptionB
c Prints ExceptionA ,ExceptionB
d compilation Error
Q18 Given :
try { int x = Integer.parseInt("two"); }
What could be used to create an appropriate catch block ?
a. ClassCastException
b. NumberFormatException
c.IllegalStateException
d ArithmeticException
Ans b
Q19 Given :
class Test
{
static String s = "-";
public static void main(String ar[])
{
try
{
throw new Exception();
}
catch(Exception e) {
try{
try { throw new Exception();
}catch(Exception ex){ s+= "ic";}
throw new Exception(); }
catch(Exception ex) {s+="mc";}
}
System.out.println(s);
}
}
Options
a -
b -ic
c icmc
d -icmc
Ans d
Q20 Given
1. class SubException extends Exception {}
2. class SubSubException extends SubException {}
3. class CC {
4. void doStuff() throws SubException
5. {}
6. }
7.class CC2 extends CC
8.{
9. void doStuff() throws SubSubException {}
10.}
11.class CC3 extends CC2
12.{
13. void doStuff() throws Exception {}
14.}
Options
a. Compilation error at line 4
b Executes sucessfully
c Compilation error at line 9.
d Compilation error at line 13.
Ans d
Q21
Given:
public class Test {
static String s = " ";
public static void main(String ar[]) {
try {
s+="1";
throw new Exception();
}catch(Exception e) { s+="2";
} finally { s+="3"; dostuff(); s+=4;
}
System.out.println(s);
}
static void doStuff() {int x = 0; int y =7/x; }
}
Options
a 12
b 13
c 1234
d Compilation Error
Ans d
Q24 Given
public class Aquestion
{
private void method1() throws Exception
{
throw new RuntimeException();
}
public void method2()
{
try
{
method1();
}
catch(RuntimeException e)
{
System.out.println("Caught Runtime Exception");
}
catch(Exception e)
{
System.out.println("Caught Exception");
}
}
public static void main(String args[])
{
Aquestion a = new Aquestion();
a.method2();
}
}
Options
a will not compile.
b will compile and show - "Caught Runtime Exception".
c will compile and show - "Caught Exception".
d will compile and show both the messages one after another in the order they ap
pear.
Ans b
Q25 Given
1. interface AnInterface
2. {
3. public void methodOne() throws Exception;
4. }
5. public class Aquestion implements AnInterface
6. {
7. public void methodOne()
8. {
9. System.out.println("I will never throw an exception");
10. }
11. public static void main(String ar[])
12. {
13. Aquestion a = new Aquestion();
14. a.methodOne();
15. }
16.
17. }
Options
a. compilation error at line 3
b. compilation error at line 5
c. compilation error at line 7
d. Print I will never throw an exception
Ans d
Q26 Assuming a method contains code which may raise an Exception (but not a Ru
ntimeException), what is the correct way for a method to indicate that it expect
s the caller to handle that exception:
a.throw Exception
b. throws Exception
c. new Exception
d. Don't need to specify anything
Ans b
Ans d
Options
a. Compiler error
b. It will print "Error A"
c. It will print "Error B"
d. The exception will go uncaught by both catch blocks
Ans a
Options
a Compilation error
b Print hello world
c Compiles and run prints nothing
Ans a
Q32 Which of the following require explicit try/catch exception handling by the
programmer?
Q40 Given
class MyException extends Exception
{
class Tier {
void doStuff() {}
}
public lcass Retread extends Tire {
public static void main(String[] ar) {
new Retread().doStuff();
}
//insert code here
System.out.println(7/0);
}
}
Add given the following four code fragments :
1 void doStuff()
2 void doStuff() throws MyException {
3 void doStuff() throws RuntimeException {
4 void doStuff() throws ArithmeticException {
When fragments 1-4 are added at line 10 which is true
a. None will compile
b. They will compile
c. Some ,but not all will compile
d. None of those that compile will throw an exception at runtime
Ans c
Options
a. The value 4 is printed at runtime
b. Compilation error occurs
c. NullPointerException occurs at runtime
d. NumberFormatException occurs at runtime
ans c
Q42 given
class Test
{
public static void main(String[] ar) {
System.out.format("%d",Math.PI);
}
}
Options
a Compilation Fails
b. Prints 3
c. Prints 3.14
d Prints java.util.IllegalFormatConversionException
Ans d
Q43 The statements following the throw keyword in a program are not executed.
Ans a True
b False
c none of these
Ans : a
Q44 The subclass exception should precede the base class exception when used wi
thin the catch clause.
a True
b False
c none of these
Ans a
Options
a throw new Exception() at line 1
b. throw new MyException() at line 4
c throw new MyException() at line 6
d. throws new Exception() at line 3
Ans b
Q48
Which exception or error will be thrown when a programmer attempts to run this c
ode ?
A java.lang.StackOverflowError
B java.lang.illegalStateException
C java.lang.ExceptionInIntializerError
D java.lang.ArrayIndexOutOfBoundsException
Ans C