Friday, February 4, 2011

Self Test: Chapters 22 to 26

Question 1:
Given:
class TestTernaryOp {
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?
A. null
B. life
C. universe
D. everything
E. Compilation fails
F. An exception is thrown at runtime

Question 2:
Given:
1. class TestEqualityOps {
2. public static void main(String[] args) {
3. float f1 = 2.3f;
4. float[][] f2 = {{42.0f}, {1.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?
A. One of them will compile, only one will be true
B. Two of them will compile, only one will be true
C. Two of them will compile, two will be true
D. Three of them will compile, only one will be true
E. Three of them will compile, exactly two will be true
F. Three of them will compile, exactly three will be true

Question 3:
Given:
class TestShortCrctOr {
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 TestShortCrctOr live2

What is the result?
A. test case
B. production live2
C. test case live2
D. Compilation fails
E. An exception is thrown at runtime

Question 4:
Given:
class TestStringConcatOp {
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?
A. 9 foo47 86foo
B. 9 foo47 4244foo
C. 9 foo425 86foo
D. 9 foo425 4244foo
E. 72 foo47 86foo
F. 72 foo47 4244foo
G. 72 foo425 86foo
H. 72 foo425 4244foo
I. Compilation fails


Question 5:
Given:
3. public class TestIncrementOp {
4. { index = 1; }
5. int index;
6. public static void main(String[] args) {
7. new TestIncrementOp().go();
8. }
9. void go() {
10. int [][] dd = {{9,8,7}, {6,5,4}, {3,2,1,0}};
11. System.out.println(dd[index++][index++]);
12. }
13. }

What is the result? (Choose all that apply.)
A. 1
B. 2
C. 4
D. 6
E. 8
F. Compilation fails
G. An exception is thrown at runtime

Question 6:
Given:
3. public class EnumTest {
4. public static void main(String[] args) {
5. Days d1 = Days.TH;
6. Days d2 = Days.M;
7. for(Days d: Days.values()) {
8. if(d.equals(Days.F)) break;
9. d2 = d;
10. }
11. System.out.println((d1 == d2)?"same old" : "newly new");
12. }
13. enum Days {M, T, W, TH, F, SA, SU};
14. }

What is the result?
A. same old
B. newly new
C. Compilation fails due to multiple errors
D. Compilation fails due only to an error on line 7
E. Compilation fails due only to an error on line 8
F. Compilation fails due only to an error on line 11
G. Compilation fails due only to an error on line 13

Question 7:
Given:
4. public class OperatorsTest {
5. public static void main(String[] args) {
6. String s = "";
7. Boolean b1 = true;
8. boolean b2 = false;
9. if((b2 = false) | (21%5) > 2) s += "x";
10. if(b1 || (b2 == true)) s += "y";
11. if(b2 == true) s += "z";
12. System.out.println(s);
13. }
14. }

Which are true? (Choose all that apply.)
A. Compilation fails
B. x will be included in the output
C. y will be included in the output
D. z will be included in the output
E. An exception is thrown at runtime

Question 8:
Given:
3. public class TestOROperators {
4. public static void main(String[] args) {
5. int mask = 0;
6. int count = 0;
7. if( ((5<7) || (++count < 10)) | mask++ < 10 ) mask = mask + 1; 8. if( (6 > 8) ^ false) mask = mask + 10;
9. if( !(mask > 1) && ++count > 1) mask = mask + 100;
10. System.out.println(mask + " " + count);
11. }
12. }

Which two are true about the value of mask and the value of count at line 10? (Choose two.)
A. mask is 0
B. mask is 1
C. mask is 2
D. mask is 10
E. mask is greater than 10
F. count is 0
G. count is greater than 0
10.

Self Test Answers

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

Question 2:

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.

Question 3:

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

A, B, C, and D are incorrect based on the above.

Question 4:

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.

Question 5:

C is correct. Multidimensional arrays’ dimensions can be inconsistent, the code uses an initialization block, and the increment operators are both post-increment operators.

A, B, D, E, F, and G are incorrect based on the above.

Question 6:

A is correct. All of this syntax is correct. The for-each iterates through the enum using the values() method to return an array. Enums can be compared using either equals() or ==. Enums can be used in a ternary operator’s Boolean test.

B, C, D, E, F, and G are incorrect based on the above.

Question 7:

C is correct. First of all, boxing takes care of the Boolean. Line 9 uses the modulus operator, which returns the remainder of the division, which in this case is 1. Also, line 9 sets b2 to false, and it doesn’t test b2's value. Line 10 sets b2 to true, and it doesn’t test its value; however, the short circuit operator keeps the expression b2 = true from being executed.

A, B, D, and E are incorrect based on the above.

Question 8:

C and F are correct. At line 7 the || keeps count from being incremented, but the | allows mask to be incremented. At line 8 the ^ returns true only if exactly one operand is true. At line 9 mask is 2 and the && keeps count from being incremented.

A, B, D, E, and G are incorrect based on the above.

Previous Chapter: Quick Recap - Chapters 22 to 26

Next Chapter: If and Switch Statements

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers