OCP Java SE 8 Exam
Sample Questions
Exceptions and Assertions
Hari Kiran & S G Ganesh
Question
Consider the following program:
class ChainedException {
public static void foo() {
try {
throw new ArrayIndexOutOfBoundsException();
} catch(ArrayIndexOutOfBoundsException oob) {
RuntimeException re = new RuntimeException(oob);
re.initCause(oob);
throw re;
}
}
public static void main(String []args) {
try {
foo();
} catch(Exception re) {
System.out.println(re.getClass());
} } }
https://ocpjava.wordpress.com
When executed this program from
main() by calling foo() method,
prints which of the following?
A. class
java.lang.RuntimeException
B. class
java.lang.IllegalStateException
C. class java.lang.Exception
D. class
java.lang.ArrayIndexOutOfBou
ndsException
Answer
Consider the following program:
class ChainedException {
public static void foo() {
try {
throw new ArrayIndexOutOfBoundsException();
} catch(ArrayIndexOutOfBoundsException oob) {
RuntimeException re = new RuntimeException(oob);
re.initCause(oob);
throw re;
}
}
public static void main(String []args) {
try {
foo();
} catch(Exception re) {
System.out.println(re.getClass());
} } }
https://ocpjava.wordpress.com
When executed this program from
main() by calling foo() method,
prints which of the following?
A. class
java.lang.RuntimeException
B. class
java.lang.IllegalStateException
C. class java.lang.Exception
D. class
java.lang.ArrayIndexOutOfBoun
dsException
Explanation
B. class java.lang.IllegalStateException
In the expression new RuntimeException(oob);, the
exception object oob is already chained to the
RuntimeException object. The method initCause() cannot be
called on an exception object that already has an exception
object chained during the constructor call.
Hence, the call re.initCause(oob); results in initCause()
throwing an IllegalStateException .
https://ocpjava.wordpress.com
Question
Consider the following program:
class EHBehavior {
public static void main(String []args) {
try {
int i = 10/0; // LINE A
System.out.print("after throw -> ");
} catch(ArithmeticException ae) {
System.out.print("in catch -> ");
return;
} finally {
System.out.print("in finally -> ");
}
System.out.print("after everything");
} }
Which one of the following options best describes the behaviour of this program?
A. The program prints the following: in catch -> in finally -> after everything
B. The program prints the following: after throw -> in catch -> in finally -> after
everything
C. The program prints the following: in catch -> after everything
D. The program prints the following: in catch -> in finally ->
https://ocpjava.wordpress.com
Answer
Consider the following program:
class EHBehavior {
public static void main(String []args) {
try {
int i = 10/0; // LINE A
System.out.print("after throw -> ");
} catch(ArithmeticException ae) {
System.out.print("in catch -> ");
return;
} finally {
System.out.print("in finally -> ");
}
System.out.print("after everything");
} }
Which one of the following options best describes the behaviour of this program?
A. The program prints the following: in catch -> in finally -> after everything
B. The program prints the following: after throw -> in catch -> in finally -> after
everything
C. The program prints the following: in catch -> after everything
D. The program prints the following: in catch -> in finally ->
https://ocpjava.wordpress.com
Explanation
D . The program prints the following: in catch -> in finally ->
The statement println("after throw -> "); will never be
executed since the line marked with the comment LINE A
throws an exception. The catch handles ArithmeticException ,
so println("in catch -> "); will be executed.
Following that, there is a return statement, so the function
returns. But before the function returns, the finally statement
should be called, hence the statement println("in finally -> ");
will get executed. So, the statement println("after
everything"); will never get executed
https://ocpjava.wordpress.com
Question
Consider the following program:
import java.util.Scanner;
class AutoCloseableTest {
public static void main(String []args) {
try (Scanner consoleScanner = new Scanner(System.in)) {
consoleScanner.close(); // CLOSE
consoleScanner.close();
}
}
}
Which one of the following statements is correct?
A. This program terminates normally without throwing any exceptions
B. This program throws an IllegalStateException
C. This program throws an IOException
D. This program throws an AlreadyClosedException
https://ocpjava.wordpress.com
Answer
Consider the following program:
import java.util.Scanner;
class AutoCloseableTest {
public static void main(String []args) {
try (Scanner consoleScanner = new Scanner(System.in)) {
consoleScanner.close(); // CLOSE
consoleScanner.close();
}
}
}
Which one of the following statements is correct?
A. This program terminates normally without throwing any exceptions
B. This program throws an IllegalStateException
C. This program throws an IOException
D. This program throws an AlreadyClosedException
https://ocpjava.wordpress.com
Explanation
A . This program terminates normally without throwing any
exceptions
The try-with-resources statement internally expands to call
the close() method in the finally block. If the resource is
explicitly closed in the try block, then calling close()
again does not have any effect. From the description of the
close() method in the AutoCloseable interface: “Closes this
stream and releases any system resources associated with it.
If the stream is already closed, then invoking this method has
no effect.”
https://ocpjava.wordpress.com
Question
Consider the following program:
class AssertionFailure {
public static void main(String []args) {
try {
assert false;
} catch(RuntimeException re) {
System.out.println("RuntimeException");
} catch(Exception e) {
System.out.println("Exception");
} catch(Error e) { // LINE A
System.out.println("Error" + e);
} catch(Throwable t) {
System.out.println("Throwable");
}
}
}
https://ocpjava.wordpress.com
This program is invoked from the
command line as follows:
java AssertionFailure
Choose one of the following options
describes the behaviour of this
program:
A. Prints "RuntimeException" in
console
B. Prints "Exception"
C. Prints "Error"
D. Prints "Throwable"
E. Does not print any output on
console
Answer
Consider the following program:
class AssertionFailure {
public static void main(String []args) {
try {
assert false;
} catch(RuntimeException re) {
System.out.println("RuntimeException");
} catch(Exception e) {
System.out.println("Exception");
} catch(Error e) { // LINE A
System.out.println("Error" + e);
} catch(Throwable t) {
System.out.println("Throwable");
}
}
}
https://ocpjava.wordpress.com
This program is invoked from the
command line as follows:
java AssertionFailure
Choose one of the following options
describes the behaviour of this
program:
A. Prints "RuntimeException" in
console
B. Prints "Exception"
C. Prints "Error"
D. Prints "Throwable"
E. Does not print any output on
console
Explanation
E . Does not print any output on the console
By default, assertions are disabled. If -ea (or the -
enableassertions option to enable assertions), then the
program would have printed "Error" since the exception
thrown in the case of assertion failure is
java.lang.AssertionError , which is derived from
the Error class
https://ocpjava.wordpress.com
Question
Consider the following class hierarchy from the package
javax.security.auth.login and answer the questions.
Which of the following handlers that makes use of multi-catch exception handler
feature will compile without errors?
A. catch (AccountException | LoginException exception)
B. catch (AccountException | AccountExpiredException exception)
C. catch (AccountExpiredException | AccountNotFoundException exception)
D. catch (AccountExpiredException exception1 | AccountNotFoundException
exception2)
https://ocpjava.wordpress.com
Answer
Consider the following class hierarchy from the package
javax.security.auth.login and answer the questions.
Which of the following handlers that makes use of multi-catch exception handler
feature will compile without errors?
A. catch (AccountException | LoginException exception)
B. catch (AccountException | AccountExpiredException exception)
C. catch (AccountExpiredException | AccountNotFoundException exception)
D. catch (AccountExpiredException exception1 | AccountNotFoundException
exception2)
https://ocpjava.wordpress.com
Explanation
C . catch (A ccountExpiredException | A
ccountNotFoundException exception)
For A and B, the base type handler is provided with the
derived type handler, hence the multi-catch is incorrect.
For D, the exception name exception1 is redundant and will
result in a syntax error. C is the correct option and this will
compile fine without errors.
https://ocpjava.wordpress.com
• Check out our latest book for
OCPJP 8 exam preparation
• http://amzn.to/1NNtho2
• www.apress.com/9781484218358
(download source code here)
• https://ocpjava.wordpress.com
(more ocpjp 8 resources here)
http://facebook.com/ocpjava

OCJP Samples Questions: Exceptions and assertions

  • 1.
    OCP Java SE8 Exam Sample Questions Exceptions and Assertions Hari Kiran & S G Ganesh
  • 2.
    Question Consider the followingprogram: class ChainedException { public static void foo() { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException oob) { RuntimeException re = new RuntimeException(oob); re.initCause(oob); throw re; } } public static void main(String []args) { try { foo(); } catch(Exception re) { System.out.println(re.getClass()); } } } https://ocpjava.wordpress.com When executed this program from main() by calling foo() method, prints which of the following? A. class java.lang.RuntimeException B. class java.lang.IllegalStateException C. class java.lang.Exception D. class java.lang.ArrayIndexOutOfBou ndsException
  • 3.
    Answer Consider the followingprogram: class ChainedException { public static void foo() { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException oob) { RuntimeException re = new RuntimeException(oob); re.initCause(oob); throw re; } } public static void main(String []args) { try { foo(); } catch(Exception re) { System.out.println(re.getClass()); } } } https://ocpjava.wordpress.com When executed this program from main() by calling foo() method, prints which of the following? A. class java.lang.RuntimeException B. class java.lang.IllegalStateException C. class java.lang.Exception D. class java.lang.ArrayIndexOutOfBoun dsException
  • 4.
    Explanation B. class java.lang.IllegalStateException Inthe expression new RuntimeException(oob);, the exception object oob is already chained to the RuntimeException object. The method initCause() cannot be called on an exception object that already has an exception object chained during the constructor call. Hence, the call re.initCause(oob); results in initCause() throwing an IllegalStateException . https://ocpjava.wordpress.com
  • 5.
    Question Consider the followingprogram: class EHBehavior { public static void main(String []args) { try { int i = 10/0; // LINE A System.out.print("after throw -> "); } catch(ArithmeticException ae) { System.out.print("in catch -> "); return; } finally { System.out.print("in finally -> "); } System.out.print("after everything"); } } Which one of the following options best describes the behaviour of this program? A. The program prints the following: in catch -> in finally -> after everything B. The program prints the following: after throw -> in catch -> in finally -> after everything C. The program prints the following: in catch -> after everything D. The program prints the following: in catch -> in finally -> https://ocpjava.wordpress.com
  • 6.
    Answer Consider the followingprogram: class EHBehavior { public static void main(String []args) { try { int i = 10/0; // LINE A System.out.print("after throw -> "); } catch(ArithmeticException ae) { System.out.print("in catch -> "); return; } finally { System.out.print("in finally -> "); } System.out.print("after everything"); } } Which one of the following options best describes the behaviour of this program? A. The program prints the following: in catch -> in finally -> after everything B. The program prints the following: after throw -> in catch -> in finally -> after everything C. The program prints the following: in catch -> after everything D. The program prints the following: in catch -> in finally -> https://ocpjava.wordpress.com
  • 7.
    Explanation D . Theprogram prints the following: in catch -> in finally -> The statement println("after throw -> "); will never be executed since the line marked with the comment LINE A throws an exception. The catch handles ArithmeticException , so println("in catch -> "); will be executed. Following that, there is a return statement, so the function returns. But before the function returns, the finally statement should be called, hence the statement println("in finally -> "); will get executed. So, the statement println("after everything"); will never get executed https://ocpjava.wordpress.com
  • 8.
    Question Consider the followingprogram: import java.util.Scanner; class AutoCloseableTest { public static void main(String []args) { try (Scanner consoleScanner = new Scanner(System.in)) { consoleScanner.close(); // CLOSE consoleScanner.close(); } } } Which one of the following statements is correct? A. This program terminates normally without throwing any exceptions B. This program throws an IllegalStateException C. This program throws an IOException D. This program throws an AlreadyClosedException https://ocpjava.wordpress.com
  • 9.
    Answer Consider the followingprogram: import java.util.Scanner; class AutoCloseableTest { public static void main(String []args) { try (Scanner consoleScanner = new Scanner(System.in)) { consoleScanner.close(); // CLOSE consoleScanner.close(); } } } Which one of the following statements is correct? A. This program terminates normally without throwing any exceptions B. This program throws an IllegalStateException C. This program throws an IOException D. This program throws an AlreadyClosedException https://ocpjava.wordpress.com
  • 10.
    Explanation A . Thisprogram terminates normally without throwing any exceptions The try-with-resources statement internally expands to call the close() method in the finally block. If the resource is explicitly closed in the try block, then calling close() again does not have any effect. From the description of the close() method in the AutoCloseable interface: “Closes this stream and releases any system resources associated with it. If the stream is already closed, then invoking this method has no effect.” https://ocpjava.wordpress.com
  • 11.
    Question Consider the followingprogram: class AssertionFailure { public static void main(String []args) { try { assert false; } catch(RuntimeException re) { System.out.println("RuntimeException"); } catch(Exception e) { System.out.println("Exception"); } catch(Error e) { // LINE A System.out.println("Error" + e); } catch(Throwable t) { System.out.println("Throwable"); } } } https://ocpjava.wordpress.com This program is invoked from the command line as follows: java AssertionFailure Choose one of the following options describes the behaviour of this program: A. Prints "RuntimeException" in console B. Prints "Exception" C. Prints "Error" D. Prints "Throwable" E. Does not print any output on console
  • 12.
    Answer Consider the followingprogram: class AssertionFailure { public static void main(String []args) { try { assert false; } catch(RuntimeException re) { System.out.println("RuntimeException"); } catch(Exception e) { System.out.println("Exception"); } catch(Error e) { // LINE A System.out.println("Error" + e); } catch(Throwable t) { System.out.println("Throwable"); } } } https://ocpjava.wordpress.com This program is invoked from the command line as follows: java AssertionFailure Choose one of the following options describes the behaviour of this program: A. Prints "RuntimeException" in console B. Prints "Exception" C. Prints "Error" D. Prints "Throwable" E. Does not print any output on console
  • 13.
    Explanation E . Doesnot print any output on the console By default, assertions are disabled. If -ea (or the - enableassertions option to enable assertions), then the program would have printed "Error" since the exception thrown in the case of assertion failure is java.lang.AssertionError , which is derived from the Error class https://ocpjava.wordpress.com
  • 14.
    Question Consider the followingclass hierarchy from the package javax.security.auth.login and answer the questions. Which of the following handlers that makes use of multi-catch exception handler feature will compile without errors? A. catch (AccountException | LoginException exception) B. catch (AccountException | AccountExpiredException exception) C. catch (AccountExpiredException | AccountNotFoundException exception) D. catch (AccountExpiredException exception1 | AccountNotFoundException exception2) https://ocpjava.wordpress.com
  • 15.
    Answer Consider the followingclass hierarchy from the package javax.security.auth.login and answer the questions. Which of the following handlers that makes use of multi-catch exception handler feature will compile without errors? A. catch (AccountException | LoginException exception) B. catch (AccountException | AccountExpiredException exception) C. catch (AccountExpiredException | AccountNotFoundException exception) D. catch (AccountExpiredException exception1 | AccountNotFoundException exception2) https://ocpjava.wordpress.com
  • 16.
    Explanation C . catch(A ccountExpiredException | A ccountNotFoundException exception) For A and B, the base type handler is provided with the derived type handler, hence the multi-catch is incorrect. For D, the exception name exception1 is redundant and will result in a syntax error. C is the correct option and this will compile fine without errors. https://ocpjava.wordpress.com
  • 17.
    • Check outour latest book for OCPJP 8 exam preparation • http://amzn.to/1NNtho2 • www.apress.com/9781484218358 (download source code here) • https://ocpjava.wordpress.com (more ocpjp 8 resources here) http://facebook.com/ocpjava