Self Test


1. 

Given:

 class Hexy {   public static void main(String[] args) {     Integer i = 42;     String s = (i<40)?"life":(i>50)?"universe":"everything";     System.out.println(s);   } } 

What is the result?

  1. null

  2. life

  3. universe

  4. everything

  5. Compilation fails.

  6. An exception is thrown at runtime.

image from book

2. 

Given:

 1. class Example { 2.   public static void main(String[] args) { 3.     Short s = 15; 4.     Boolean b; 5.     // insert code here 6.   } 7. } 

Which, inserted independently at line 5, will compile? (Choose all that apply.)

  1. b = (Number instanceof s);

  2. b = (s instanceof Short);

  3. b = s.instanceof(Short);

  4. b = (s instanceof Number);

  5. b = s.instanceof(Object);

  6. b = (s instanceof String);

image from book

3. 

Given:

  1. class Comp2 {  2.   public static void main(String[] args) {  3.     float f1 = 2.3f;  4.     float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}};  5.     float[] f3 = {2.7f};  6.     Long x = 42L;  7.     // insert code here  8.       System.out.println("true");  9.   } 10. } 

And the following five code fragments:

 F1.  if (f1 == f2) F2.  if (f1 == f2[2][1]) F3.  if (x == f2[0][0]) F4.  if (f1 == f2 [1,1] ) F5.  if (f3 == f2 [2] ) 

What is true?

  1. One of them will compile, only one will be true.

  2. Two of them will compile, only one will be true.

  3. Two of them will compile, two will be true.

  4. Three of them will compile, only one will be true.

  5. Three of them will compile, exactly two will be true.

  6. Three of them will compile, exactly three will be true.

image from book

4. 

Given:

 class  Fork {   public static void main(String[] args)    {     if(args.length == 1 | args[1] .equals("test")) {       System.out.println ("test  case");     } else {       System.out.println("production " + args[0]) ;     }   } } 

And the command-line invocation:

 java Fork live2 

What is the result?

  1. test case

  2. production

  3. test case live2

  4. Compilation fails.

  5. An exception is thrown at runtime.

image from book

5. 

Given:

 class Foozit {   public static void main(String[] args) {     Integer x = 0;     Integer y = 0;     for(Short z = 0; z < 5; z++)       if((++x > 2) || (++y > 2))         X++ ;     System.out.println (x + " " + y);   } } 

What is the result?

  1. 5 1

  2. 5 2

  3. 5 3

  4. 8 1

  5. 8 2

  6. 8 3

  7. 10 2

  8. 10 3

image from book

6. 

Given:

 class Titanic {   public static void main(String[] args) {     Boolean bl = true;     boolean b2 = false;     boolean b3 = true;     if((bl & b2) | (b2 & b3) & b3)       System.out.print("alpha ");     if((bl = false) | (b1 & b3) | (bl | b2))       System.out.print("beta "};   } } 

What is the result?

  1. beta

  2. alpha

  3. alpha beta

  4. Compilation fails.

  5. No output is produced.

  6. An exception is thrown at runtime.

image from book

7. 

Given:

 class Feline {   public static void main(String[] args) {     Long x = 42L;     Long y = 44L;     System.out.print (" " + 7 + 2 + " ") ;     System.out.print(foo () + x + 5 + " ");     System.out.println(x + y + foo());   }   static String foo() { return "foo"; } } 

What is the result?

  1. 9 foo47 86foo

  2. 9 foo47 4244foo

  3. 9 foo425 86foo

  4. 9 foo425 4244foo

  5. 72 foo47 86foo

  6. 72 foo47 4244foo

  7. 72 foo425 86foo

  8. 72 foo425 4244foo

  9. Compilation fails.

image from book

8. 

Place the fragments into the code to produce the output 33. Note, you must use each fragment exactly once.

CODE:

 class Incr {   public static void main(String[] args) {     Integer x = 7;     int y = 2 ;     X    ___  ___;     ___  ___  ___;     ___  ___  ___;     ___  ___  ___;     System.out.println(x);   } } 

FRAGMENTS:

Y

Y

Y

Y

y

x

x

 

-=

*=

*=

*=

image from book

9. 

Given:

 1. class Maybe { 2.   public static void main(String[] args) { 3.     boolean bl = true; 4.     boolean b2 = false; 5.     System.out.print(!false ^ false); 6.     System.out.print(" " + (!b1 & (b2 = true))); 7.     System.out.println(" " + (b2 ^ b1)); 8.   } 9. } 

Which are true?

  1. Line 5 produces true.

  2. Line 5 produces false.

  3. Line 6 produces true.

  4. Line 6 produces false.

  5. Line 7 produces true.

  6. Line 7 produces false.

image from book

10. 

Given:

 class Sixties {   public static void main(String[] args) {     int x = 5;  int y = 7 ;     System.out.print(((y * 2) % x));     System.out.print(" " + (y % x));   } } 

What is the result?

  1. 1 1

  2. 1 2

  3. 2 1

  4. 2 2

  5. 4 1

  6. 4 2

  7. Compilation fails.

  8. An exception is thrown at runtime.

image from book

Answers

1. 

þ  

D is correct. This is a ternary nested in a ternary with a little unboxing thrown in. Both of the ternary expressions are false.

ý  

A, B, C, E, and F are incorrect based on the above. (Objective 7.6)

2. 

þ  

B and D correctly use boxing and instanceof together.

ý  

A is incorrect because the operands are reversed. C and E use incorrect instanceof syntax. F is wrong because Short isn't in the same inheritance tree as String. (Objective 7,6)

3. 

þ  

D is correct, Fragments F2, F3, and F5 will compile, and only F3 is true.

ý  

A, B, C, E, and F are incorrect. F1 is incorrect because you can't compare a primitive to an array. F4 is incorrect syntax to access an element of a two-dimensional array. (Objective 7.6)

4. 

þ  

E is correct. Because the short circuit (| |) is not used, both operands are evaluated, Since args[1] is past the args array bounds, an ArrayIndexOutOf BoundsException is thrown.

ý  

A, B, C, and D are incorrect based on the above. (Objective 7.6)

5. 

þ  

E is correct. The first two times the if test runs, both x and y are incremented once (the x++ is not reached until the third iteration). Starting with the third iteration of the loop, y is never touched again, because of the short-circuit operator.

ý  

A, B, C, D, F, G, and H are incorrect based on the above. (Objective 7.6)

6. 

þ  

E is correct. In the second if test, the leftmost expression is an assignment, not a comparison. Once bl has been set to false, the remaining tests are all false.

ý  

A, B, C, D, and F are incorrect based on the above. , (Objective 7.6)

7. 

þ  

G is correct. Concatenation runs from left to right, and if either operand is a String, the operands are concatenated. If both operands are numbers they are added together. Unboxing works in conjunction with concatenation.

ý  

A, B, C, D, E, F, H, and I are incorrect based on the above. (Objective 7.6)

8. 

 class Incr{   public static void main(String[] args){     Integer.x = 7;     int Y = 2;     x *_ X;     Y *= Y;     Y = Y;     x -= Y;     System.out.println(x);   } } 

Yeah, we know it's kind of puzzle-y, but you might encounter something like it on the real exam. (Objective 7.6)

9. 

þ  

A, D, and F are correct. The ^ (xor) returns true if exactly one operand is true. The ! inverts the operand's boolean value. On line 6 b2 = true is an assignment not a comparison, and it's evaluated because & does not short-circuit it.

ý  

B, C, and E are incorrect based on the above. (Objective 7.6)

10. 

þ  

F is correct. The % (remainder a.k.a. modulus) operator returns the remainder of a division operation.

ý  

A, B, C, D, E, G, and H .are incorrect based on the above. (Objective 7.6)




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net