Tests Flashcards
What will happen if you add the statement System.out.println(5 / 0); to a working
main() method?
- *A.** It will not compile.
- *B.** It will not run.
- *C.** It will run and throw an ArithmeticException.
- *D.** It will run and throw an IllegalArgumentException.
- *E.** None of the above.
C. The compiler tests the operation for a valid type but not a valid result, so the code
will still compile and run. At runtime, evaluation of the parameter takes place before
passing it to the print() method, so an ArithmeticException object is raised.
What is the output of the following application?
1: public class CompareValues {
2: public static void main(String[] args) {
3: int x = 0;
4: while(x++ 5: String message = x > 10 ? “Greater than” : false;
6: System.out.println(message+”,”+x);
7: }
8: }
* *A.** Greater than,10
* *B.** false,10
* *C.** Greater than,11
* *D.** false,11
* *E.** The code will not compile because of line 4.
* *F.** The code will not compile because of line 5.
F. In this example, the ternary operator has two expressions, one of them a String and the other a boolean value. The ternary operator is permitted to have expressions that don’t have matching types, but the key here is the assignment to the String reference.
The compiler knows how to assign the first expression value as a String, but the second boolean expression cannot be set as a String; therefore, this line will not compile.
What change would allow the following code snippet to compile? (Choose all that apply)
3: long x = 10;
4: int y = 2 * x;
* *A.** No change; it compiles as is.
* *B.** Cast x on line 4 to int.
* *C.** Change the data type of x on line 3 to short.
* *D.** Cast 2 * x on line 4 to int.
* *E.** Change the data type of y on line 4 to short.
* *F.** Change the data type of y on line 4 to long.
B, C, D, F. The code will not compile as is, so option A is not correct. The value 2 * x
is automatically promoted to long and cannot be automatically stored in y, which is
in an int value. Options B, C, and D solve this problem by reducing the long value to
int. Option E does not solve the problem and actually makes it worse by attempting
to place the value in a smaller data type. Option F solves the problem by increasing the
data type of the assignment so that long is allowed.
What is the output of the following code?
3: byte a = 40, b = 50;
4: byte sum = (byte) a + b;
5: System.out.println(sum);
* *A.** 40
* *B.** 50
* *C.** 90
* *D.** The code will not compile because of line 4.
* *E.** An undefined value.
D. Line 4 generates a possible loss of precision compiler error. The cast operator has
the highest precedence, so it is evaluated first, casting a to a byte. Then, the addition is
evaluated, causing both a and b to be promoted to int values. The value 90 is an int
and cannot be assigned to the byte sum without an explicit cast, so the code does not
compile. The code could be corrected with parentheses around (a + b), in which case
option C would be the correct answer.
What is the result of the following code snippet?
3: int m = 9, n = 1, x = 0;
4: while(m > n) {
5: m–;
6: n += 2;
7: x += m + n;
8: }
9: System.out.println(x);
* *A.** 11
* *B.** 13
* *C.** 23
* *D.** 36
* *E.** 50
* *F.** The code will not compile because of line 7.
D. Prior to the first iteration, m = 9, n = 1, and x = 0. After the iteration of the first loop, m is updated to 8, n to 3, and x to the sum of the new values for m + n, 0 + 11 = 11. After the iteration of the second loop, m is updated to 7, n to 5, and x to the sum of the new values for m + n, 11 + 12 = 23. After the iteration of the third loop, m is updated to 6, n to 7, and x to the sum of the new values for m + n, 23 + 13 = 36. On the fourth iteration of the loop, m > n evaluates to false, as 6
What is the output of the following code?
1: public class TernaryTester {
2: public static void main(String[] args) {
3: int x = 5;
4: System.out.println(x > 2 ? x 5: }}
* *A.** 5
* *B.** 4
* *C.** 10
* *D.** 8
* *E.** 7
* *F.** The code will not compile because of line 4.
D. As you learned in the section “Ternary Operator,” although parentheses are not
required, they do greatly increase code readability, such as the following equivalent
statement:
System.out.println((x > 2) ? ((x We apply the outside ternary operator fi rst, as it is possible the inner ternary expression
may never be evaluated. Since (x>2) is true, this reduces the problem to:
System.out.println((x Since x is greater than 2, the answer is 8, or option D in this case.
What data type (or types) will allow the following code snippet to compile? (Choose all that apply)
byte x = 5;
byte y = 10;
_____ z = x + y;
A. int
B. long
C. boolean
D. double
E. short
F. byte
A, B, D. The value x + y is automatically promoted to int, so int and data types that
can be promoted automatically from int will work. Options A, B, D are such data
types. Option C will not work because boolean is not a numeric data type. Options E
and F will not work without an explicit cast to a smaller data type.
What is the output of the following code snippet?
3: int c = 7;
4: int result = 4;
5: result += ++c;
6: System.out.println(result);
* *A.** 8
* *B.** 11
* *C.** 12
* *D.** 15
* *E.** 16
* *F.** The code will not compile because of line 5.
C. The code compiles successfully, so option F is incorrect. On line 5, the pre-increment operator is used, so c is incremented to 4 and the new value is returned to the expression. The value of result is computed by adding 4 to the original value of 8, resulting in a new value of 12, which is output on line 6. Therefore, option C is the correct answer.
What is the output of the following code snippet?
3: boolean x = true, z = true;
4: int y = 20;
5: x = (y != 10) ^ (z=false);
6: System.out.println(x+”, “+y+”, “+z);
* *A.** true, 10, true
* *B.** true, 20, false
* *C.** false, 20, true
* *D.** false, 20, false
* *E.** false, 20, true
* *F.** The code will not compile because of line 5.
B. This example is tricky because of the second assignment operator embedded in line
5. The expression (z=false) assigns the value false to z and returns false for the
entire expression. Since y does not equal 10, the left-hand side returns true; therefore,
the exclusive or (^) of the entire expression assigned to x is true. The output reflects
these assignments, with no change to y, so option B is the only correct answer. The
code compiles and runs without issue, so option F is not correct.
Which of the following exceptions are thrown by the JVM? (Choose all that apply)
- *A.** ArrayIndexOutOfBoundsException
- *B.** ExceptionInInitializerError
- *C.** java.io.IOException
- *D.** NullPointerException
- *E.** NumberFormatException
A, B, D. java.io.IOException is thrown by many methods in the java.io package,
but it is always thrown programmatically. The same is true for NumberFormatException; it is thrown programmatically by the wrapper classes of java.lang. The other three exceptions are all thrown by the JVM when the corresponding problem arises.
Which exception will the following throw?
Object obj = new Integer(3); String str = (String) obj; System.out.println(str);
- *A.** ArrayIndexOutOfBoundsException
- *B.** ClassCastException
- *C.** IllegalArgumentException
- *D.** NumberFormatException
- *E.** None of the above.
B. The second line tries to cast an Integer to a String. Since String does not extend
Integer, this is not allowed and a ClassCastException is thrown.
Which of the following Java operators can be used with boolean variables? (Choose all that apply)
- *A.** ==
- *B**. +
- *C.** –
- *D.**!
- *E.** %
- *F.**
A, D. Option A is the equality operator and can be used on numeric primitives, boolean values, and object references. Options B and C are both arithmetic operators and cannot be applied to a boolean value. Option D is the logical complement operator and is used exclusively with boolean values. Option E is the modulus operator, which can only be used with numeric primitives. Finally, option F is a relational operator that compares the values of two numbers.
What is the output of the following snippet, assuming a and b are both 0?
3: try {
4: return a / b;
5: } catch (RuntimeException e) {
6: return -1;
7: } catch (ArithmeticException e) {
8: return 0;
9: } finally {
10: System.out.print(“done”);
11: }
- *A.** -1
- *B.** 0
- *C.** done-1
- *D.** done0
- *E.** The code does not compile.
- *F.** An uncaught exception is thrown.
E. The order of catch blocks is important because they’re checked in the order they
appear after the try block. Because ArithmeticException is a child class of Runtime-
Exception, the catch block on line 7 is unreachable. (If an ArithmeticException is
thrown in try try block, it will be caught on line 5.) Line 7 generates a compiler error
because it is unreachable code.
What is printed besides the stack trace caused by the NullPointerException from line 16?
1: public class DoSomething {
2: public void go() {
3: System.out.print(“A”);
4: try {
5: stop();
6: } catch (ArithmeticException e) {
7: System.out.print(“B”);
8: } finally {
9: System.out.print(“C”);
10: }
11: System.out.print(“D”);
12: }
13: public void stop() {
14: System.out.print(“E”);
15: Object x = null;
16: x.toString();
17: System.out.print(“F”);
18: }
19: public static void main(String[] args) {
20: new DoSomething().go();
21: }
22: }
- *A.** AE
- *B.** AEBCD
- *C.** AEC
- *D**. AECD
- *E.** No output appears other than the stack trace.
C. The main() method invokes go and A is printed on line 3. The stop method is
invoked and E is printed on line 14. Line 16 throws a NullPointerException, so stop
immediately ends and line 17 doesn’t execute. The exception isn’t caught in go, so the go method ends as well, but not before its finally block executes and C is printed on line 9. Because main() doesn’t catch the exception, the stack trace displays and no further output occurs, so AEC was the output printed before the stack trace.
What is the output of the following code snippet?
3: do {
4: int y = 1;
5: System.out.print(y++ + “ “);
6: } while(y A. 1 2 3 4 5 6 7 8 9
* *B.** 1 2 3 4 5 6 7 8 9 10
* *C.** 1 2 3 4 5 6 7 8 9 10 11
* *D.** The code will not compile because of line 6.
* *E.** The code contains an infinite loop and does not terminate.
D. The variable y is declared within the body of the do-while statement, so it is out of scope on line 6. Line 6 generates a compiler error, so option D is the correct answer.