Q21. Given the classes: 

* AssertionError 

* ArithmeticException 

* ArrayIndexOutofBoundsException 

* FileNotFoundException 

* IllegalArgumentException 

* IOError 

* IOException 

* NumberFormatException 

* SQLException 

Which option lists only those classes that belong to the unchecked exception category? 

A. AssertionError, ArrayIndexOutOfBoundsException, ArithmeticException 

B. AssertionError, IOError, IOException 

C. ArithmeticException, FileNotFoundException, NumberFormatException 

D. FileNotFoundException, IOException, SQLException 

E. ArrayIndexOutOfBoundException, IllegalArgumentException, FileNotFoundException 

Answer:

Explanation: Not B: IOError and IOException are both checked errors. 

Not C, not D, not E: FileNotFoundException is a checked error. 

Note: 

Checked exceptions: 

* represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files) 

* are subclasses of Exception 

* a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it 

somehow) 

Note: 

Unchecked exceptions: 

* represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes: "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time." 

* are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException 

* method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so) 


Q22. Given the code fragments: 

Which code fragment, when inserted at line ni, enables the code to print Hank? 

A. checkAge (iList, ( ) -> p. get Age ( ) > 40); 

B. checkAge(iList, Person p -> p.getAge( ) > 40); 

C. checkAge (iList, p -> p.getAge ( ) > 40); 

D. checkAge(iList, (Person p) -> { p.getAge() > 40; }); 

Answer:


Q23. Which two statements correctly describe checked exception? 

A. These are exceptional conditions that a well-written application should anticipate and recover from. 

B. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. 

C. These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. 

D. Every class that is a subclass of RuntimeException and Error is categorized as checked exception. 

E. Every class that is a subclass of Exception, excluding RuntimeException and its subclasses, is categorized as checked exception. 

Answer: B,D 

Explanation: Checked exceptions: 

* (B) represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files) 

* are subclasses of Exception It's somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked). 

* a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow) 

Reference: Checked versus unchecked exceptions 


Q24. Given the code fragment: 

public class Test { 

static String[][] arr =new String[3][]; 

private static void doPrint() { 

//insert code here 

public static void main(String[] args) { 

String[] class1 = {"A","B","C"}; 

String[] class2 = {"L","M","N","O"}; 

String[] class3 = {"I","J"}; 

arr[0] = class1; 

arr[1] = class2; 

arr[2] = class3; 

Test.doPrint(); 

Which code fragment, when inserted at line //insert code here, enables the code to print 

COJ? 

A. int i = 0; 

for (String[] sub: arr) { 

int j = sub.length -1; 

for (String str: sub) { 

System.out.println(str[j]); 

i++; 

B. private static void doPrint() { 

for (int i = 0;i < arr.length;i++) { 

int j = arr[i].length-1; 

System.out.print(arr[i][j]); 

C. int i = 0; 

for (String[] sub: arr[][]) { 

int j = sub.length; 

System.out.print(arr[i][j]); 

i++; 

D. for (int i = 0;i < arr.length-1;i++) { 

int j = arr[i].length-1; 

System.out.print(arr[i][j]); 

i++; 

Answer:

Explanation: 

Incorrect: 

not A: The following line causes a compile error: 

System.out.println(str[j]); 

Not C: Compile erro line: 

for (String[] sub: arr[][]) 

not D: Output: C 


Q25. Given: 

public class ScopeTest { 

int j, int k; 

public static void main(String[] args) { 

ew ScopeTest().doStuff(); } 

void doStuff() { 

nt x = 5; 

oStuff2(); 

System.out.println("x"); 

void doStuff2() { 

nt y = 7; 

ystem.out.println("y"); 

or (int z = 0; z < 5; z++) { 

ystem.out.println("z"); 

ystem.out.println("y"); 

Which two items are fields? 

A. j 

B. k 

C. x 

D. y 

E. z 

Answer: A,B 


Q26. Given the code fragment: 

Which code fragment, when inserted at line 9, enables the code to print true? 

A. String str2 = str1; 

B. String str2 = new String (str1); 

C. String str2 = sb1. toString (); 

D. String str2 = "Duke"; 

Answer:


Q27. Given: 

What is the result? 

A. Null 

B. Compilation fails 

C. An exception is thrown at runtime 

D. 0 

Answer:


Q28. Given the code fragment: 

What is the result? 

A. Match 1 

B. Match 2 

C. No Match 

D. A NullPointerException is thrown at runtime. 

Answer:

Explanation: 

it will compare the string contents of the StringBuilder with string object. 


Q29. Given: 

class Cake { 

int model; 

String flavor; 

Cake() { 

model = 0; 

flavor = "Unknown"; 

public class Test { 

public static void main(String[] args) { 

Cake c = new Cake(); 

bake1(c); 

System.out.println(c.model + " " + c.flavor); 

bake2(c); 

System.out.println(c.model + " " + c.flavor); 

public static Cake bake1(Cake c) { 

c.flavor = "Strawberry"; 

c.model = 1200; 

return c; 

public static void bake2(Cake c) { 

c.flavor = "Chocolate"; 

c.model = 1230; 

return; 

What is the result? 

A. 0 unknown 0 unknown 

B. 1200 Strawberry 1200 Strawberry 

C. 1200 Strawberry 1230 Chocolate 

D. Compilation fails 

Answer:

Explanation: 1200 Strawberry 1230 Chocolate 


Q30. Consider 

Integer number = Integer.valueOff 808.1"); 

Which is true about the above statement? 

A. The value of the variable number will be 808.1 

B. The value of the variable number will be 808 

C. The value of the variable number will be 0. 

D. A NumberFormatException will be throw. 

E. It will not compile. 

Answer:

Explanation: 

The Integer class value of 0 returns an Integer from given string. But we need to pass string which has correct format for integer otherwise it will throw a NumberFormatException. In this case we have passed string which is not an integer value (since what we passed is fractional number), so option D is correct.