Q1. Given the following classes:
Which two options fail to compile when placed at line n1 of the main method?
A. employee.salary = 50_000;
B. director.salary = 80_000;
C. employee.budget = 200_000;
D. manager.budget = 1_000_000;
E. manager.stockOption = 500;
F. director.stockOptions = 1_000;
Answer: C,E
Q2. Given:
What is the result?
A. 97 98 99 100 null null null
B. 91 98 99 100 101 102 103
C. Compilation rails.
D. A NullPointerException is thrown at runtime.
E. An ArraylndexOutOfBoundsException is thrown at runtime.
Answer: A
Q3. 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: A
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)
Q4. Given the fragment:
String[][] arra = new String[3][];
arra[0] = new String[]{"rose", "lily"};
arra[1] = new String[]{"apple", "berry","cherry","grapes"};
arra[0] = new String[]{"beans", "carrot","potato"};
// insert code fragment here
Which code fragment when inserted at line '// insert code fragment here', enables the code to successfully change arra elements to uppercase?
A. String[][] arra = new String[3][];
arra[0] = new String[]{"rose", "lily"};
arra[1] = new String[]{"apple", "berry","cherry","grapes"};
arra[0] = new String[]{"beans", "carrot","potato"};
for (int i = 0; i < arra.length; i++) {
for (int j=0; j < arra[i].length; j++) {
arra[i][j] = arra[i][j].toUpperCase();
}
}
B. for (int i = 0; i < 3; i++) {
for (int j=0; j < 4; j++) {
arra[i][j] = arra[i][j].toUpperCase();
}
}
C. for (String a[]:arra[][]) {
for (String x:a[]) {
D. toUpperCase();
}
}
E. for (int i:arra.length) {
for (String x:arra) {
arra[i].toUpperCase();
}
}
Answer: C
Explanation:
Incorrect:
not A: arra.length is 3, but the subarrays have 2, 3 and 4 elements. Index will be out of
bound.
not B: The subarrys are of different lengths. Index will be out of bound.
not D: Compile error.
Q5. Given the code fragment
Which code fragments, inserted independently, enable the code compile?
A. t.fvar = 200;
B. cvar = 400;
C. fvar = 200; cvar = 400;
D. this.fvar = 200; this.cvar = 400;
E. t.fvar = 200; Test2.cvar = 400;
F. this.fvar = 200; Test2.cvar = 400;
Answer: B
Q6. Given the code fragment:
What is the result?
A. A B C Work done
B. A B C D Work done
C. A Work done
D. Compilation fails
Answer: C
Q7. Given:
public class X {
static int i;
int j;
public static void main(String[] args) {
X x1 = new X();
X x2 = new X();
x1.i = 3;
x1.j = 4;
x2.i = 5;
x2.j = 6;
System.out.println(
x1.i + " "+
x1.j + " "+
x2.i + " "+
x2.j);
}
}
What is the result?
A. 3 4 5 6
B. 3 4 3 6
C. 5 4 5 6
D. 3 6 4 6
Answer: C
Q8. Given the code fragment:
What is the result?
A. 1:2:3:4:5:
B. 1:2:3:
C. Compilation fails.
D. An ArrayoutofBoundsException is thrown at runtime.
Answer: A
Q9. Given:
What is the result?
A. Shining Sun Shining Sun Shining Sun
B. Shining Sun Twinkling Star Shining Sun
C. Compilation fails
D. A ClassCastException is thrown at runtime
Answer: D
Q10. Given the code fragment:
What is the result?
A. Values are : [EE, ME]
B. Values are : [EE, EE, ME]
C. Values are : [EE, ME, EE]
D. Values are : [SE, EE, ME, EE]
E. Values are : [EE, ME, SE, EE]
Answer: E