Q1. int [] array = {1,2,3,4,5};
for (int i: array) {
if ( i < 2) {
keyword1 ;
}
System.out.println(i);
if ( i == 3) {
keyword2 ;
}}
What should keyword1 and keyword2 be respectively, in oreder to produce output 2345?
A. continue, break
B. break, break
C. break, continue
D. continue, continue
Answer: D
Q2. Given:
What is the result?
A. null
B. compilation fails
C. Java.lang.NullPointerException
D. 0
Answer: A
Q3. Given the code fragment:
What is the result?
A. Found Red
B. Found Red
Found Blue
C. Found Red
Found Blue
Found White
D. Found Red
Found Blue
Found White
Found Default
Answer: B
Q4. Which two actions will improve the encapsulation of a class?
A. Changing the access modifier of a field from public to private
B. Removing the public modifier from a class declaration
C. Changing the return type of a method to void
D. Returning a copy of the contents of an array or ArrayList instead of a direct reference
Answer: AD
Q5. Given the code fragment:
A. 20
B. 25
C. 29
D. Compilation fails
E. An Array Index Out Of Bounds Exception is thrown at runtime
Answer: B
Q6. Given the code fragment:
What is the result?
A. 10 8 6 4 2 0
B. 10 8 6 4 2
C. An Arithmetic Exception is thrown at runtime
D. The program goes into an infinite loop outputting: 10 8 6 4 2 0. . .
E. Compilation fails
Answer: B
Q7. Which two may precede the word ‘class’ in a class declaration?
A. local
B. public
C. static
D. volatile
E. synchronized
Answer: BC
Q8. Which three are valid types for switch?
A. int
B. float
C. double
D. integer
E. String
F. Float
Answer: ADE
Q9. Given:
public class SampleClass {
public static void main(String[] args) {
AnotherSampleClass asc = new AnotherSampleClass(); SampleClass sc = new
SampleClass();
sc = asc;
System.out.println("sc: " + sc.getClass());
System.out.println("asc: " + asc.getClass());
}}
class AnotherSampleClass extends SampleClass {
}
What is the result?
A. sc: class Object asc: class AnotherSampleClass
B. sc: class SampleClass asc: class AnotherSampleClass
C. sc: class AnotherSampleClass asc: class SampleClass
D. sc: class AnotherSampleClass asc: class AnotherSampleClass
Answer: D
Q10. public class DoBreak1 {
public static void main(String[] args) {
String[] table = {"aa", "bb", "cc", "dd"};
for (String ss: table) {
if ( "bb".equals(ss)) {
continue;
}
System.out.println(ss);
if ( "cc".equals(ss)) {
break;
}
}
}
}
What is the result?
A. aa
B. aa bb cc
C. cc dd
D. cc
E. Compilation fails.
Answer: A
Q11. Given the code fragment:
nt a = 0;
a++;
System.out.printIn(a++);
System.out.printIn(a);
What is the result?
A. 1 2
B. 0 1
C. 1 1
D. 2 2
Answer: A
Q12. An unchecked exception occurs in a method dosomething() Should other code be added in the dosomething() method for it to compile and execute?
A. The Exception must be caught
B. The Exception must be declared to be thrown.
C. The Exception must be caught or declared to be thrown.
D. No other code needs to be added.
Answer: D
Q13. Given the code fragment:
Which code fragment, when inserted at // insert code here, enables the code to compile and and print a b c?
A. List update (String[] strs)
B. Static ArrayListupdate(String [] strs)
C. Static List update (String [] strs)
D. Static void update (String[] strs)
E. ArrayList static update(String [] strs)
Answer: E
Q14. public class ForTest {
public static void main(String[] args) {
int[] arrar = {1,2,3};
for ( foo ) {
}
}
}
Which three are valid replacements for foo so that the program will compiled and run?
A. int i: array
B. int i = 0; i < 1; i++
C. ;;
D. ; i < 1; i++
E. ; i < 1;
Answer: ABC
Q15. given:
A. ns = 50 S = 125
ns = 125 S = 125
ns = 100 S = 125
B. ns = 50 S = 125
ns = 125 S = 125
ns = 0 S = 125
C. ns = 50 S = 50
ns = 125 S = 125
ns = 100 S = 100
D. ns = 50 S = 50
ns = 125 S = 125
ns = 0 S = 125
Answer: B
Q16. What is the result?
A. Valid
B. Not valid
C. Compilation fails
D. An IllegalArgumentException is thrown at run time
Answer: C