Sei sulla pagina 1di 19

Q1 What will appear in the standard output if tryThis() throws an IOException at

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

Q6 What will be printed on standard output if following class is executed using


command "java Test 1 two 3" ?
public class Test
{
static public void main(string ar[])
{
try
{
int k = Integer.parseInt(arg[1]);
System.out.println(ar[k]);
}
catch(Exception e)
{
System.out.println(e);
Options
a two
b NumberFormatException
c ArrayIndexOutOfBoundsException
d Print 1
Ans b
Q7 Given :
static void test() throws RuntimeException {
try
{
System.out.println("test");
throw new RuntimeException();
}
catch(Exception ex) { System.out.print("exception");}
}
public static void main(String[] ar) {
try { test(); }
catch(RuntimeException ex) { System.out.print("runtime"); }
System.out.print("End");
}
options
a test end
b compilation fals
c test exception end
d test runtime end
ans c
Q8 What will be printed on standard output if following class is executed ?
1.class TestException extends Exception {}
2. class A {
3. public String sayHello(String name) throws TestException {
4. if(name == null) throw new TestException();
5. return "Hello" + name;
6. }
7. }
8. public class TestA {
9. public static void main(String ar[]) {
10. new A().sayHello("Aiko");
11. }
12. }

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

Q9 What is the result of compiling and running the following code ?


class Test
{
void f()
{
throw new RuntimeException();
}
public static void main(String ar[])
{
Test t = new Test();
try
{
t.f();
}
catch(Throwable e)
{
try
{
throw (Exception) e;
}
catch(Exception ex)
{
System.out.println("catch1");
}
finally
{
System.out.println("finally1");
}
}
finally
{
System.out.println("finally2");
}
}
}
Options
a Compiler error
b Prints catch1 finally1
c Prints catch1 finally1 finally2
d Prints finally2
e None of the above
Ans c

Q10 class ExceptionA extends Exception {}


class ExceptionB extends ExceptionA {}
public class Test{
void thrower() throws ExceptionA{
throw new ExceptionA();
}
public static void main(String[] args){
Test t = new Test();
try{
System.out.println("hello");
t.thrower();}
catch(ExceptionB e) { System.out.println("Exception B");}
}
}
Options
a Exception B
b hello
c hello Exception B
d Compilation Error

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

Q13 Select the exception that matches the given description


Thorws when an attempt is made to cast an object to a subclass of which it is n
ot an instance
Options
a IllegalCastException
b ClassCastException
c IllegalArgumentException
d NullPointerException
ans b

Q14 Exception1 and Exception2 are subclasses of Exception


Exception3 is the subclass of RuntimeException. Given the follwoing classes, wh
ich of these exceptions can be thrown
by the test() method in class A3 ?
class A1
{
void test() throws Exception1,Exception2 {}
}
class A2 extends A1
{
void test() throws Exception2 {}
}
class A3 extends A2
{
void test() throws ___________{}
}
a.Exception1 ,Exception2 and RuntimeException
b.Exception1 and Exception2 only
c.Exception2 and RuntimeException only
d Exception1 only
and c

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

Q16 Suppose MyException (which is a subclass of Exception) should be thrown if c


ondition (which is a boolean variable)
is true, which statement would you insert ?
1. public aMethod throws MyException
2. {
3. if(Condition) {
4. }
5. }

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

Q22 public class AQuestion


{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Throwable t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
Options
a Compiler error complaining about the catch block where no IOException object c
an ever be thrown.
b Compiler error - IOException not found. It must be imported in the first line
of the code.
c No compiler error. The lines "Before Try" and "At the end" are printed on the
screen.
d.None of these
Ans c

Q23 The class java.lang.Exception


a Is public
b Extends Throwable
c implements serializable
d All the above
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

Q27 public class Aquestion {


static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo"); }
public static void main(String args[]) {
try { throwMethod(); }
catch (IllegalAccessException e) {
System.out.println("Caught " + e); }
} } // ThrowsDemo
Options
a. Compilation error
b. Runtime error
c. Compile successfully, nothing is printed
d. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: dem
o
Ans a

Q28 public class Aquestion {


static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new RuntimeException("demo"); }
public static void main(String args[]) {
try { throwMethod(); }
catch (RuntimeException e) {
System.out.println("Caught Exception"); }
} } // ThrowsDemo
Options
a. Compilation error
b. Runtime error
c. Compile successfully, nothing is printed
d. Inside throwMethod. followed by caught: java.lang.RuntimeException: demo

Ans d

Q29 Which of the is a legal definition of a method named m assuming it throws IO


Exception, and returns void. Also assume that the method does not take any argum
ents. Select the one correct answer.
a.void m() throws IOException{}
b.void m() throw IOException{}
c.void m(void) throws IOException{}
d.m() throws IOException{}
e. void m() {} throws IOException
ans a
Q30 What is the result of attempting to compile and run this code ?
class A extends Exception{}
class B extends A{}
class C extends B{}
public class Test {
static void aMethod() throws C{ throw new C(); }
public static void main(String[] args){
int x = 10;
try { aMethod(); }
catch(A e) { System.out.println("Error A");}
catch(B e) { System.out.println("Error B");}
}
}

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

Q31 class ExceptionA extends Exception {}


class ExceptionB extends ExceptionA {}
public class Test{
void thrower() throws ExceptionB{
System.out.println("hello world");
throw new ExceptionB();
}
public static void main(String[] args){
Test t = new Test();
try{t.thrower();}
catch(ExceptionA e) {}
catch(ExceptionB e) {}
}
}

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?

a Traversing each member of an array


b Attempting to open a file
c Accessing a method in same class
d Accessing a method in other class
Ans b

Q33 An ArithmeticException is a checked exception. True Or False?


a True
b False
ans b

Q34 Which of the following exception is thrown by JVM


a IllegalArgumentException
b IllegalStateException
c ArrayIndexOutOfBoundsException
d NumberformatException
Ans c

Q35 Which of the following exception is not thrown by JVM


a NumberFormatException
b ArrayIndexOutOfBoundsException
c IllegalArgumentException
Ans a

Q36 Select the exception that matches the given description


Thrown when the JVM is out of memory when trying to allocate an Object
a OutOfMemoryError
b OutOfMemoryException
c IllegalCastException
d DivideByZeroError
Ans a

Q39 FileNotFoundException is a checked Exception


a. True
b. False
Ans a

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

Q41 givenclass Test


{
Integer I;
int x;
void calc(int y)
{
x = I + y;
System.out.println(x);
}
public static void main(String[] ar) {
Test t = new Test();
t.calc(3);
}
}

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

Q45 ExceptionInInitializerError is thrown when :


a Thrown when attempting to initialize a static variable or an initialization b
lock.
b Thrown when a method that converts a String to a number receives a String tha
t it cannot convert
c Thrown when attempting to access an object with a reference variable whose cur
rent value is null.
ans a

Q46 public class Aquestion {


static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo"); }
public static void main(String args[]) {
try { throwMethod(); }
catch (IllegalAccessException e) {
System.out.println("Caught " + e); }
} } // ThrowsDemo
Options
a. Compilation error
b. Runtime error
c. Compile successfully, nothing is printed
d. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: dem
o
Ans a

q47 Suppose MyException (which is a subclass of Exception) should be thrown if c


ondition (which is a boolean variable)
is true, which statement would you insert ?
1. public aMethod throws MyException
2. {
3. if(Condition) {
4. }
5. }

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

public static void parse(String str) {


try {
float f = Float.parseFloat(str);
}catch(NumberFormatException nfe) {
f=0;
}
finally {
System.out.println(f);
}
}
public static void main(String ar[]) {
parse("invalid");
}
What is the result ?
A 0.0
B Compilation fails
C A ParseException is thrown by the parse method at runtime.
D A NumberFormatException is thrown by the parse method at runtime.
Ans B
Q49 Given
33 try {
//some code here
} catch (NullPointerException e1) {
System.out.println("a");
}catch(Exception e2) {
System.out.println("b");
} finally {
System.out.println("c");
}
A a
B b
C c
D ac
Ans d
Q50 Given
10 public class Foo {
11 static int[] a;
12 static { a[0]=2;}
13 public static void main(String ar[]) {}
14 }

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

Potrebbero piacerti anche