jobs4timesLogo jobs4timesLogo

Flow Control Certification Programs

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.)

  1. foreach( x ) System.out.println(z);
  2. for( int z : x ) System.out.println(z);
  3. while( x.hasNext() ) System.out.println( x.next() );
  4. 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 ?
  1. 0
  2. 10
  3. 12
  4. 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?
  1. 1 2 3
  2. Compilation fails because of an error in line 12.
  3. Compilation fails because of an error in line 13.
  4. Compilation fails because of an error in line 14.
  5. A ClassCastException is thrown at runtime.
Answer: A

Q: 05 Given:

11. public static void main(String[] args) { 
12. for (int i = 0; i <= 10; i++) { 
13. if (i > 6) break; 
14. } 
15. System.out.println(i); 
16. } 
What is the result ?
  1. 6
  2. 7
  3. 10
  4. 11
  5. Compilation fails.
  6. An exception is thrown at runtime.
Answer: E

Q: 06 Given:

11. public static void main(String[] args) { 
12. Integer i = new Integer(1) + new Integer(2); 
13. switch(i) { 
14. case 3: System.out.println("three"); break; 
15. default: System.out.println("other"); break;
16. } 
17. } 
What is the result ?
  1. three
  2. other
  3. An exception is thrown at runtime.
  4. Compilation fails because of an error on line 12.
  5. Compilation fails because of an error on line 13.
  6. Compilation fails because of an error on line 15.
Answer: A

Q: 07 Given:

10. public class ClassA { 
11. public void count(int i) { 
12. count(++i); 
13. } 
14. } 
And: 
20. ClassA a = new ClassA(); 
21. a.count(3); 
Which exception or error should be thrown by the virtual machine ?
  1. StackOverflowError
  2. NullPointerException
  3. NumberFormatException
  4. IllegalArgumentException
  5. ExceptionInInitializerError
Answer: A

Q: 08 Given:

22. public void go() { 
23. String o = ""; 
24. z: 
25. for(int x = 0; x < 3; x++) { 
26. for(int y = 0; y < 2; y++) { 
27. if(x==1) break; 
28. if(x==2 && y==1) break z; 
29. o = o + x + y; 30. } 
31. } 
32. System.out.println(o); 
33. }
What is the result when the go() method is invoked ?
  1. 00
  2. 0001
  3. 000120
  4. 00012021
  5. Compilation fails.
  6. An exception is thrown at runtime.
Answer: C

Q: 09 Given:

3. public class Breaker { 
4. static String o = ""; 
5. public static void main(String[] args) { 
6. z: 
7. o = o + 2; 
8. for(int x = 3; x < 8; x++) { 
9. if(x==4) break; 
10. if(x==6) break z; 
11. o = o + x; 
12. } 
13. System.out.println(o); 
14. } 
15. } 
What is the result ?
  1. 23
  2. 234
  3. 235
  4. 2345
  5. 2357
  6. 23457
  7. Compilation fails.
Answer: G

Q: 10 Given:

35. int x = 10; 
36. do { 
37. x--; 
38. } while (x < 10); 
How many times will line 37 be executed?
  1. ten times
  2. zero times
  3. one to nine times
  4. more than ten times
Answer: D

11. Given the following code:

public class OrtegorumFunction { 
 public int computeDiscontinuous(int x) { 
  int r = 1; r += x; 
  if ((x > 4) && (x < 10)) { 
   r += 2 * x; 
  } 
  else (x <= 4) { 
   r += 3 * x; 
  } 
  else {
     r += 4 * x; 
   } 
   
  r += 5 * x; 
  return r; 
 } 

public static void main(String [] args) {
OrtegorumFunction o = new OrtegorumFunction(); 
System.out.println("OF(11) is: " + o.computeDiscontinuous(11));
}
}
What is the result?
  1. OF(11) is: 45
  2. OF(11) is: 56
  3. OF(11) is: 89
  4. OF(11) is: 111
  5. Compilation fails.
  6. An exception is thrown at runtime.
Answer:
  • E is correct. The if statement is illegal.
    The if-else-else must be changed to if-else if-else, which would result in OF(11) is: 111.
  • A, B, C, D, and F are incorrect based on the above.

12. Given:

1. class Crivitch { 
2. public static void main(String [] args) { 
3. int x = 0; 
4. // insert code here 
5. do { } while (x++ < y); 
6. System.out.println(x); 
7. } 
8. } 
Which, inserted at line 4, produces the output 12 ?
  1. int y = x;
  2. int y = 10;
  3. int y = 11;
  4. int y = 12;
  5. int y = 13;
  6. None of the above will allow compilation to succeed.
Answer:
  • C is correct. x reaches the value of 11, at which point the while test fails. x is then incremented (after the comparison test!), and the println() method runs.
  • A, B, D, E, and F are incorrect based on the above.

13. Given:

 
class Swill { 
 public static void main(String[] args) { 
  String s = "-"; 
  switch(TimeZone.CST) { 
   case EST: s += "e"; 
   case CST: s += "c"; 
   case MST: s += "m"; 
   default: s += "X"; 
   case PST: s += "p"; 
  } 
 System.out.println(s); 
 } 
}
enum TimeZone {EST, CST, MST, PST } What is the result ?
  1. -c
  2. -X
  3. -cm
  4. -cmp
  5. -cmXp
  6. Compilation fails.
  7. An exception is thrown at runtime.
Answer:
  • E is correct. It's legal to use enums in a switch, and normal switch fall-through logic applies; i.e., once a match is made the switch has been entered, and all remaining blocks will run if no break statement is encountered.
  • Note: default doesn‘t have to be last.
  • A, B, C, D, and F are incorrect based on the above.

14. Given:

class Circus { 
public static void main(String[] args) { 
int x = 9; int y = 6; 
for(int z = 0; z < 6; z++, y--) { 
 if(x > 2) x--; 
 label: if(x > 5) { 
 System.out.print(x + " ");
   --x; 
  continue label; 
  } 
  x--; 
 }
}
} 
What is the result ?
  1. 8
  2. 8 7
  3. 8 7 6
  4. Compilation fails.
  5. An exception is thrown at runtime.
Answer:
  • D is correct. A labeled continue works only with loops. In this case, although the label is legal, label is not a label on a loop statement, it's a label on an if statement.
  • A, B, C, and E are incorrect based on the above.

15. Given:

1. class Loopy { 
2. public static void main(String[] args) { 
3. int[] x = {7,6,5,4,3,2,1}; 
4. // insert code here 
5. System.out.print(y + " "); 
6. }
7. } }
Which, inserted independently at line 4, compiles? (Choose all that apply.)
  1. for(int y : x) {
  2. for(x : int y) {
  3. int y = 0; for(y : x) {
  4. for(int y=0, z=0; z<x.length; z++) { y = x[z];
  5. for(int y=0, int z=0; z<x.length; z++) { y = x[z];
  6. int y = 0; for(int z=0; z<x.length; z++) { y = x[z];
Answer:
  • A , D, and F are correct. A is an example of the enhanced for loop. D and F are examples of the basic for loop.
  • B is incorrect because its operands are swapped. C is incorrect because the enhanced for must declare its first operand. E is incorrect syntax to declare two variables in a for statement.

16. Given:

1. class Ring { 
2. final static int x2 = 7; 
3. final static Integer x4 = 8; 
4. public static void main(String[] args) { 
5. Integer x1 = 5; 
6. String s = "a"; 
7. if(x1 < 9) s += "b"; 
8. switch(x1) { 
9. case 5: s += "c"; 
10. case x2: s += "d"; 
11. case x4: s += "e"; 
12. } 
13. System.out.println(s); 
14. } 
15. }
What is the result ?
  1. abc
  2. abcde
  3. Compilation fails due only to an error on line 7.
  4. Compilation fails due only to an error on line 8.
  5. Compilation fails due only to an error on line 10.
  6. Compilation fails due only to an error on line 11.
  7. Compilation fails due to errors on multiple lines.
Answer:
  • F is correct. A switch statement requires its case expressions to be constants, and wrapper variables (even final static ones) aren't considered constants. The rest of the code is correct.
  • A, B, C, D, E, and G are incorrect based on the above.


BACK