Quiz
04-Objectives-Wise-Handling
This quiz has a time limit, so it opens in the usual player.
Questions in this quiz (40)
1Which ofthese statements are true?
- AIfa RuntimeException is not caught, the method will terminate and normal
execution ofthe thread will resume. - BAn overriding method must declare that it throws the same exception classes as the
method it overrides - CThe main method ofa program can declare that it throws checked exceptions.
- DA method declaring that it throws a certain exception class may throw instances of
any subclass ofthat exception class - Efinally blocks are executed ifand only ifan exception gets thrown while inside the
corresponding try block
- AIfa RuntimeException is not caught, the method will terminate and normal
2Assume that a method named 'method1' contains code which may raise a non-runtime
(checked) Exception.
What is the correct way to declare that method so that it indicates that it expects the
caller to handle that exception?- Apublic void method1( ) throws Throwable
- Bpublic void method1( ) throw Exception
- Cpublic void method1( ) throw new Exception
- Dpublic void method1( ) throws Exception
- Epublic void method1( )
3Which ofthe following can be thrown using a throw statement?
- AEvent
- BObject
- CThrowable
- DException
- ERuntimeException
4What will be the result ofcompiling and running the following program ?
class NewException extends Exception { }
class AnotherException extends Exception { }
public class ExceptionTest{
public static void main( String [ ] args) throws Exception{
try{
m2( ) ;
}f
inally{ m3( ) ; }
}p
ublic static void m2( ) throws NewException{ throw new NewException( ) ; }
public static void m3( ) throws AnotherException{ throw new AnotherException( ) ; }
}- AIt will compile but will throw AnotherException when run.
- BIt will compile but will throw NewException when run.
- CIt will compile and run without throwing any exceptions.
- DIt will not compile.
- ENone ofthe above.
5What will be the result ofcompiling and running the following program ?
class NewException extends Exception { }
class AnotherException extends Exception { }
public class ExceptionTest{
public static void main( String[ ] args) throws Exception{
try{
m2( ) ;
}f
inally{
m3( ) ;
}c
atch ( NewException e) { }
}p
ublic static void m2( ) throws NewException { throw new NewException( ) ; }
public static void m3( ) throws AnotherException{ throw new AnotherException( ) ; }
}- AIt will compile but will throw AnotherException when run.
- BIt will compile but will throw NewException when run.
- CIt will compile and run without throwing any exceptions.
- DIt will not compile.
- ENone ofthe above.
6Which ofthe following are standard Java exception classes?
- AFileNotFoundException
- BInputException
- CCPUError
- DMemoryException
- ESecurityException
7What will be the output when the following program is run?
package exceptions;
public class TestClass {
public static void main( String[ ] args) {
try{
doTest( ) ;
}c
atch( MyException me) {
System. out. println( me) ;
}
}s
tatic void doTest( ) throws MyException{
int[ ] array = new int[ 10] ;
array[ 10] = 1000;
doAnotherTest( ) ;
}s
tatic void doAnotherTest( ) throws MyException{
throw new MyException( " Exception from doAnotherTest" ) ;
}
}c
lass MyException extends Exception {
public MyException( String msg) {
super( msg) ;
}
}- AException in thread " main"
j ava. lang. ArrayIndexOutOfBoundsException: 10
at exceptions. TestClass. doTest( TestClass. j ava: 24)
at exceptions. TestClass. main( TestClass. j ava: 14) - BError in thread " main" j ava. lang. ArrayIndexOutOfBoundsException
- Cexceptions. MyException: Exception from doAnotherTest
- Dexceptions. MyException: Exception from doAnotherTest
at exceptions. TestClass. doAnotherTest( TestClass. j ava: 29)
at exceptions. TestClass. doTest( TestClass. j ava: 25)
at exceptions. TestClass. main( TestClass. j ava: 14)
Expl
- AException in thread " main"
8What will the following code print when compiled and run?
abstract class Calculator{
abstract void calculate( ) ;
public static void main( String[ ] args) {
System. out. println( " calculating" ) ;
Calculator x = null;
x. calculate( ) ;
}
}- AIt will not compile.
- BIt will not print anything and will throw NullPointerException
- CIt will print calculating and then throw NullPointerException.
- DIt will print calculating and will throw NoSuchMethodError
- EIt will print calculating and will throw MethodNotImplementedException
9A Java programmer is developing a desktop application. Which ofthe following
exceptions would be appropriate for him to throw explicitly from his code?- ANullPointerException
- BClassCastException
- CArrayIndexOutofBoundsException
- DException
10What will the following code print?
public class Test{
public int luckyNumber( int seed) {
if( seed > 10) return seed%10;
int x = 0;
try{
if( seed%2 == 0) throw new Exception( " No Even no. " ) ;
else return x;
}c
atch( Exception e) {
return 3;
}f
inally{
return 7;
}
}
public static void main( String args[ ] ) {
int amount = 100, seed = 6;
switch( new Test( ) . luckyNumber( 6) ) {
case 3: amount = amount * 2;
case 7: amount = amount * 2;
case 6: amount = amount + amount;
default :
}
System. out. println( amount) ;
}
}- AIt will not compile.
- BIt will throw an exception at runtime.
- C800
- D200
- E400
11Consider the following code...
public class TestClass{
class MyException extends Exception { }
public void myMethod( ) throws XXXX{
throw new MyException( ) ;
}
}Wha
t can replace XXXX?- AMyException
- BException
- CNo throws clause is necessary
- DThrowable
- ERuntimeException
12A try statement must always have a ............. associated with it.
- Acatch
- Bthrows
- Cfinally
- Dcatch, finally or both
13Consider the following code:
class A {
public void doA( int k) throws Exception { // 0
for( int i=0; i< 10; i++) {
if( i == k) throw new Exception( " Index of k is " +i) ; // 1
}
}p
ublic void doB( boolean f) { // 2
if( f) {
doA( 15) ; // 3
}e
lse return;
}p
ublic static void main( String[ ] args) { // 4
A a = new A( ) ;
a. doB( args. length>0) ; // 5
}
} Whi
ch ofthe following statements are correct?- AThis will compile and run without any errors or exception.
- BThis will compile ifthrows Exception is added at line //2
- CThis will compile ifthrows Exception is added at line //4
- DThis will compile ifthrows Exception is added at line //2 as well as //4
- EThis will compile if line marked // 1 is enclosed in a try - catch block.
14What will the following code snippet print:
Float f = null;
try{
f = Float. valueOf( " 12. 3" ) ;
String s = f. toString( ) ;
int i = Integer. parseInt( s) ;
System. out. println( " i = " +i) ;
}c
atch( Exception e) {
System. out. println( " trouble : " +f) ;
}C- A12
- B13
- Ctrouble : null
- Dtrouble : 12.3
- Etrouble : 0.0
15What will the following program print when run using the command line: j ava
TestClass
public class TestClass {
public static void methodX( ) throws Exception {
throw new AssertionError( ) ;
}p
ublic static void main( String[ ] args) {
try{
methodX( ) ;
}c
atch( Exception e) {
System. out. println( " EXCEPTION" ) ;
}
}
}- AIt will throw AssertionError out ofthe main method.
- BIt will print EXCEPTION.
- CIt will not compile because ofthe throws clause in methodX( ) .
- DIt will end without printing anything because assertions are disabled by default.
16What is wrong with the following code written in a single file named TestClass.java?
class SomeThrowable extends Throwable { }
class MyThrowable extends SomeThrowable { }
public class TestClass{
public static void main( String args[ ] ) throws SomeThrowable{
try{
m1( ) ;
} catch( SomeThrowable e) {
throw e;
} finally{
System. out. println( " Done" ) ;
}
}p
ublic static void m1( ) throws MyThrowable{
throw new MyThrowable( ) ;
}
}- AThe main declares that it throws SomeThrowable but throws MyThrowable.
- BYou cannot have more than 2 classes in one file.
- CThe catch block in the main method must declare that it catches MyThrowable
rather than SomeThrowable. - DThere is nothing wrong with the code.
- EDone will be printed.
17Consider the following method public float parseFloat( String s ) {
float f = 0. 0f;
try{
f = Float. valueOf( s ) . floatValue( ) ;
return f ;
}c
atch( NumberFormatException nfe) {
f = Float. NaN ;
return f;
}f
inally{
f = 10. 0f;
return f;
}
}Wha
t will it return ifthe method is called with the input "0.0" ?- AIt will not compile.
- BIt will return 10.0
- CIt will return Float.Nan
- DIt will return 0.0
- ENone ofthe above.
18hat will the following code print when run?
public class Test{
static String j = " " ;
public static void method( int i) {
try{
if( i == 2) {
throw new Exception( ) ;
}j
+= " 1" ;
}c
atch ( Exception e) {
j += " 2" ;
return;
}f
inally{
j += " 3" ;
}j
+= " 4" ;
}p
ublic static void main( String args[ ] ) {
method( 1) ;
method( 2) ;
System. out. println( j ) ;
}
}- A13432
- B13423
- C14324
- D12434
- E12342
19What will the following program print when run?
public class TestClass{
public static void main( String[ ] args) {
try{
System. exit( 0) ;
}f
inally{
System. out. println( " finally is always executed! " ) ;
}
}
}- AIt will print "finally is always executed!"
- BIt will not compile as there is no catch block.
- CIt will not print anything.
- DAn exception will be thrown
- ENone ofthe above.
20What two changes can you do, independent ofeach other, to make the following code
compile:
//assume appropriate imports
class PortConnector {
public PortConnector( int port) {
if ( Math. random( ) > 0. 5) {
throw new IOException( ) ;
}t
hrow new RuntimeException( ) ;
}
}p
ublic class TestClass {
public static void main( String[ ] args) {
try {
PortConnector pc = new PortConnector( 10) ;
} catch ( RuntimeException re) {
re. printStackTrace( ) ;
}
}
}- Aadd throws IOException to the main method.
- Badd throws IOException to PortConnector constructor.
- Cadd throws IOException to the main method as well as to PortConnector
constructor. - DChange RuntimeException to j ava. io. IOException.
- Eadd throws Exception to PortConnector constructor and change
catch( RuntimeException re) to catch( Exception re) in the main method
21Considering the following program, which ofthe options are true?
public class FinallyTest{
public static void main( String args[ ] ) {
try{
if ( args. length == 0) return;
else throw new Exception( " Some Exception" ) ;
}c
atch( Exception e) {
System. out. println( " Exception in Main" ) ;
}f
inally{
System. out. println( " The end" ) ;
}
}
}- AIfrun with no arguments, the program will only print 'The end'.
- BIfrun with one argument, the program will only print 'The end'.
- CIfrun with one argument, the program will print 'Exception in Main' and 'The end'.
- DIfrun with one argument, the program will only print 'Exception in Main'.
- EOnly one ofthe above is correct.
22Consider the following hierarchy ofException classes :
j ava. lang. RuntimeException
+-------- IndexOutOfBoundsException
+---------ArrayIndexOutOfBoundsException,
StringIndexOutOfBoundsException
Which ofthe following statements are correct for a method that can throw
ArrayIndexOutOfBounds as well as StringIndexOutOfBounds Exceptions but does
not have try catch blocks to catch the same?- AThe method calling this method will either have to catch these 2 exceptions or
declare them in its throws clause. - BIt is ok ifit declares just throws ArrayIndexOutOfBoundsException
- CIt must declare throws ArrayIndexOutOfBoundsException,
StringIndexOutOfBoundsException - DIt is ok ifit declares just throws IndexOutOfBoundsException
- EIt does not need to declare any throws clause.
- AThe method calling this method will either have to catch these 2 exceptions or
23Identify the exceptions that are usually thrown by the JVM and the exceptions usually
thrown by an application.- AJVM : IllegalStateException, IllegalArgumentException
Application : ClassCastException, NullPointerException, SecurityException - BJVM : IllegalStateException, IllegalArgumentException, ClassCastException,
Application : NullPointerException, SecurityException - CJVM : IllegalStateException, IllegalArgumentException, ClassCastException,
NullPointerException
Application : SecurityException - DJVM : ClassCastException, NullPointerException, SecurityException
Application : IllegalStateException, IllegalArgumentException
- AJVM : IllegalStateException, IllegalArgumentException
24What can be the type ofa catch argument ?
- AAny class that extends j ava. lang. Exception
- BAny class that extends j ava. lang. Exception except any class that extends
j ava. lang. RuntimeException - CAny class that is-a Throwable.
- DAny Object
25What is wrong with the following code?
class MyException extends Exception { }
public class TestClass{
public static void main( String[ ] args) {
TestClass tc = new TestClass( ) ;
try{
tc. m1( ) ;
}c
atch ( MyException e) {
tc. m1( ) ;
}f
inally{
tc. m2( ) ;
}
}p
ublic void m1( ) throws MyException{
throw new MyException( ) ;
}p
ublic void m2( ) throws RuntimeException{
throw new NullPointerException( ) ;
}
}- AIt will not compile because you cannot throw an exception in finally block.
- BIt will not compile because you cannot throw an exception in catch block.
- CIt will not compile because NullPointerException cannot be created this way.
- DIt will not compile because ofunhandled exception.
- EIt will compile but will throw an exception when run.
26Identify the correct constructs.
- Atry {
for( ; ; ) ;
} finally { } - Btry {
File f = new File( " c: \a. txt" ) ;
} catch { f = null; } - Cint k = 0;
try {
k = callValidMethod( ) ;
}S
ystem. out. println( k) ;
catch { k = -1; } - Dtry {
try {
Socket s = new ServerSocket( 3030) ;
} catch( Exception e) {
s = new ServerSocket( 4040) ;
}
} - Etry {
s = new ServerSocket( 3030) ;
}c
atch( Exception t) { t. printStackTrace( ) ; }
catch( IOException e) {
s = new ServerSocket( 4040) ;
}c
atch( Throwable t) { t. printStackTrace( ) ; } - Fint x = validMethod( ) ;
try {
if( x == 5) throw new IOException( ) ;
else if( x == 6) throw new Exception( ) ;
} finally {
x = 8;
}
int x = validMethod( ) ;
try {
if( x == 5) throw new IOException( ) ;
else if( x == 6) throw new Exception( ) ;
} finally {
x = 8;
}
- Atry {
27Consider the following code snippet:
void m1( ) throws Exception{
try{
// line1
}c
atch ( IOException e) {
throw new SQLException( ) ;
}c
atch( SQLException e) {
throw new InstantiationException( ) ;
}f
inally{
throw new CloneNotSupportedException( ) ; // this is not a RuntimeException.
}
}Whi
ch ofthe following statements are true?- AIfIOException gets thrown at line1, then the whole method will end up throwing
SQLException. - BIfIOException gets thrown at line1, then the whole method will end up throwing
CloneNotSupportedException. - CIfIOException gets thrown at line1, then the whole method will end up throwing
InstantiationException() - DIfno exception is thrown at line1, then the whole method will end up throwing
CloneNotSupportedException. - EIfSQLException gets thrown at line1, then the whole method will end up throwing
InstantiationException()
- AIfIOException gets thrown at line1, then the whole method will end up throwing
28A new Java programmer has written the following method that takes an array of
integers and sums up all the integers that are less than 100.
public void processArray( int[ ] values) {
int sum = 0;
int i = 0;
try{
while( values[ i] <100) {
sum = sum +values[ i] ;
i++;
}
}c
atch( Exception e) { }
System. out. println( " sum = " +sum) ;
}
Which ofthe following are best practices to improve this code?- AUse ArrayIndexOutOfBoundsException for the catch argument.
- BUse ArrayIndexOutOfBoundsException for the catch argument and add code in
the catch block to log or print the exception. - CAdd code in the catch block to handle the exception.
- DUse flow control to terminate the loop.
29Which digits and in what order will be printed when the following program is run?
public class TestClass{
public static void main( String args[ ] ) {
int k = 0;
try{
int i = 5/k;
}c
atch ( ArithmeticException e) {
System. out. println( " 1" ) ;
}c
atch ( RuntimeException e) {
System. out. println( " 2" ) ;
return ;
}c
atch ( Exception e) {
System. out. println( " 3" ) ;
}f
inally{
System. out. println( " 4" ) ;
}S
ystem. out. println( " 5" ) ;
}
}- AThe program will print 5.
- BThe program will print 1 and 4, in that order.
- CThe program will print 1, 2 and 4, in that order.
- DThe program will print 1, 4 and 5, in that order.
- EThe program will print 1,2, 4 and 5, in that order.
30What class ofobjects can be declared by the throws clause?
- AException
- BError
- CEvent
- DObject
- ERuntimeException
31What will the following class print ?
class Test{
public static void main( String[ ] args) {
int[ ] [ ] a = { { 00, 01 } , { 10, 11 } } ;
int i = 99;
try {
a[ val( ) ] [ i = 1] ++;
} catch ( Exception e) {
System. out. println( i+" , " +a[ 1] [ 1] ) ;
}
}s
tatic int val( ) throws Exception {
throw new Exception( " unimplemented" ) ;
}
}- A99 , 11
- B1 , 11
- C1 and an unknown value.
- D99 and an unknown value.
- EIt will throw an exception at Run time.
32What will be the result ofattempting to compile and run the following program?
public class TestClass{
public static void main( String args[ ] ) {
Exception e = null;
throw e;
}
}- AThe code will fail to compile.
- BThe program will fail to compile, since it cannot throw a null.
- CThe program will compile without error and will throw an Exception when run.
- DThe program will compile without error and will throw
j ava. lang. NullPointerException when run - EThe program will compile without error and will run and terminate without any
output.
33What will the following program print?
public class TestClass{
public static void main( String[ ] args) {
int x = 1;
int y = 0;
if( x/y ) System. out. println( " Good" ) ;
else System. out. println( " Bad" ) ;
}
}- AGood
- BBad
- CException at runtime saying division by Zero.
- DIt will not compile.
34What will be the output when the following code is compiled and run?
//in file Test. j ava
class E1 extends Exception{ }
class E2 extends E1 { }
class Test{
public static void main( String[ ] args) {
try{
throw new E2( ) ;
}c
atch( E1 e) {
System. out. println( " E1" ) ;
}c
atch( Exception e) {
System. out. println( " E" ) ;
}f
inally{
System. out. println( " Finally" ) ;
}
}
}- AIt will not compile.
- BIt will print E1 and Finally.
- CIt will print E1, E and Finally.
- DIt will print E and Finally.
- EIt will print Finally.
35What will be the output ofthe following program:
public class TestClass{
public static void main( String args[ ] ) {
try{
m1( ) ;
} catch( IndexOutOfBoundsException e) {
System. out. println( " 1" ) ;
throw new NullPointerException( ) ;
} catch( NullPointerException e) {
System. out. println( " 2" ) ;
return;
} catch ( Exception e) {
System. out. println( " 3" ) ;
} finally{
System. out. println( " 4" ) ;
}S
ystem. out. println( " END" ) ;
}/
/ IndexOutOfBoundsException is a subclass of RuntimeException.
static void m1( ) {
System. out. println( " m1 Starts" ) ;
throw new IndexOutOfBoundsException( " Big Bang " ) ;
}
}- AThe program will print m1 Starts.
- BThe program will print m1 Starts, 1 and 4, in that order.
- CThe program will print m1 Starts, 1 and 2, in that order.
- DThe program will print m1 Starts, 1, 2 and 4 in that order.
- E. END will not be printed.
36Which statements regarding the following code are correct ?
class Base{
void method1( ) throws j ava. io. IOException, NullPointerException{
someMethod( " arguments" ) ;
// some I/O operations
}i
nt someMethod( String str) {
if( str == null) throw new NullPointerException( ) ;
else return str. length( ) ;
}
}p
ublic class NewBase extends Base{
void method1( ) {
someMethod( " args" ) ;
}
}- Amethod1 in class NewBase does not need to specify any exceptions.
- BThe code will not compile because RuntimeExceptions cannot be given in throws
clause. - Cmethod1 in class NewBase must at least give IOException in its throws clause.
- Dmethod1 in class NewBase must at least give NullpointerException in its
throws clause. - EThere is no problem with the code.
37What will be the output ofthe following program?
class TestClass{
public static void main( String[ ] args) throws Exception{
try{
amethod( ) ;
System. out. println( " try" ) ;
}c
atch( Exception e) {
System. out. println( " catch" ) ;
}f
inally {
System. out. println( " finally" ) ;
}S
ystem. out. println( " out" ) ;
}p
ublic static void amethod( ) { }
}- Atry finally
- Btry finally out
- Ctry out
- Dcatch finally out
- EIt will not compile because amethod( ) does not throw any exception.
38What will be the output when the following program is run?
package exceptions;
public class TestClass{
public static void main( String[ ] args) {
try{
hello( ) ;
}c
atch( MyException me) {
System. out. println( me) ;
}
}s
tatic void hello( ) throws MyException{
int[ ] dear = new int[ 7] ;
dear[ 0] = 747;
foo( ) ;
}s
tatic void foo( ) throws MyException{
throw new MyException( " Exception from foo" ) ;
}
}c
lass MyException extends Exception {
public MyException( String msg) {
super( msg) ;
}
}(
Assume that line numbers printed in the messages given below are correct.)- AException in thread " main"
j ava. lang. ArrayIndexOutOfBoundsException: 10]
at exceptions. TestClass. doTest( TestClass. j ava: 24)
at exceptions. TestClass. main( TestClass. j ava: 14) - BError in thread " main" j ava. lang. ArrayIndexOutOfBoundsException
- Cexceptions. MyException: Exception from foo
- Dexceptions. MyException: Exception from foo
at exceptions. TestClass. foo( TestClass. j ava: 29)
at exceptions. TestClass. hello( TestClass. j ava: 25)
at exceptions. TestClass. main( TestClass. j ava: 14)
m
- AException in thread " main"
39Given the class
// Filename: Test. j ava
public class Test{
public static void main( String args[ ] ) {
for( int i = 0; i< args. length; i++) {
System. out. print( " " +args[ i] ) ;
}
}
}N
ow consider the following 3 options for running the program:
a: j ava Test
b: j ava Test param1
c: j ava Test param1 param2
Which ofthe following statements are true?- AThe program will throw j ava. lang. ArrayIndexOutofBoundsException on
option a. - BThe program will throw j ava. lang. NullPointerException on option a.
- CThe program will print Test param1 on option b.
- DIt will print param1 param2 on option c.
- EIt will not print anything on option a.
- AThe program will throw j ava. lang. ArrayIndexOutofBoundsException on
40What is the result ofcompiling and running this code?
class MyException extends Throwable{ }
class MyException1 extends MyException{ }
class MyException2 extends MyException{ }
class MyException3 extends MyException2{ }
public class ExceptionTest{
void myMethod( ) throws MyException{
throw new MyException3( ) ;
}p
ublic static void main( String[ ] args) {
ExceptionTest et = new ExceptionTest( ) ;
try{
et. myMethod( ) ;
}c
atch( MyException me) {
System. out. println( " MyException thrown" ) ;
}c
atch( MyException3 me3) {
System. out. println( " MyException3 thrown" ) ;
}f
inally{
System. out. println( " Done" ) ;
}
}
}- AMyException thrown
- BMyException3 thrown
- CMyException thrown Done
- DMyException3 thrown Done
- EIt fails to compile
Information
- Description
- OCA-SE-7 Objective-Wise-Ethuware-OCA-SE-7 Quiz on 04-Objectives-Wise-Handling, created by Wong Fei Hung on 01/08/2017.
- Author
- Wong Fei Hung
- Language
- Português
- Views
- 0
- Tags
- Rating
- No ratings yet Rate this quiz