Search your topic in Java2share
Home > SCJP > QuestionBank
eXTReMe Tracker


4. Flow Control (SCJP)

Q: 01 Given:
10. public class Bar {
11. static void foo( int... x ) {
12. // insert code here
13. }
14. }
Which two code fragments, inserted independently at line 12, will allow the class to compile? (Choose
two.)

A. foreach( x ) System.out.println(z);
B. for( int z : x ) System.out.println(z);
C. while( x.hasNext() ) System.out.println( x.next() );
D. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);

Answer: B, D

Q: 02 Click the Task button.

Solution:
int [ ] y={1,2,4,8,16,32};
System.out.print("output  : ");
for(int x : y ) {
System.out.println(x);
System.out.println("  ");

 

Q: 03 Given:
25. int x = 12;
26. while (x < 10) {
27. x--;
28. }
29. System.out.print(x);
What is the result?

A. 0
B. 10
C. 12
D. Line 29 will never be reached.

Answer: C

Q: 04 Given:
11. public static void main(String[] args) {
12. Object obj = new int[] { 1, 2, 3 };
13. int[] someArray = (int[])obj;
14. for (int i : someArray) System.out.print(i + " ");
15. }
What is the result?

A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A ClassCastException is thrown at runtime.

Answer: A

5. Exception Handling