java_2_2/27

¡Supera tus tareas y exámenes ahora con Quizwiz!

int x1 = 50, x2= 75; boolean b = x1 > x2; if(b = true) System.out.println("Success"); else System.out.println("Failure"); A. Success B. Failure C. The code will not compile because of line 4 D. The code will not compile because of line 5

The code compiles successfully, so options C and D are incorrect. The value of b after line 4 is false. However, the if-then statement on line 5 contains an assignment, not a comparison. The variable b is assigned true on line 3, and the assignment operator returns true, so line 5 executes and displays Success, so the answer is option A.

How many times will the following code print "Hello World"? for (int i=0; i<10 ; ) { i = i++; System.out.println("Hello World"); } A. 9 B. 10 C. 11 D. The code will not compile because of line 3 E. The code will not compile because of line 5 F. The code con gains an infinite loop and does not terminate

piles and runs without issue, so option F is not correct. 9. F. In this example, the update statement of the for loop is missing, which is fine as the statement is optional, so option D is incorrect. The expression inside the loop increments i but then assigns i the old value. Therefore, i ends the loop with the same value that it starts with: 0. The loop will repeat infinitely, outputting the same statement over and over again because i remains 0 after every iteration of the loop.

What is the output of the following code snippet? boolean x = true, z = true; int y = 20; x = (y != 10) ^ (z = false); 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. Does 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.

int c = 7; int result = 4; result += ++c; 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? -java.util.List<Integer> list = new java.util.ArrayList<Integer>(); -list.add(10); -list.add(14) -for(int x : list) { System.out.print(x + ", "); break; } A. 10, 14, B. 10, 14 C. 10, D. Does not compile because of line 7 E. Does not compile because of line 8 F. The code contains an infinite loop and does not terminate

C. This code does not contain any compilation errors or an infinite loop, so options D, E, and F are incorrect. The break statement on line 8 causes the loop to execute once and finish, so option C is the correct answer.

What is the output of the following code? public class TernaryTester { public static void main (String[] args) { int x = 5; System.out.println(x > 2 ? x < 4 ? 10 : 8 : 7); }} A. 5 B. 4 C. 10 D. 8 E. 7 F. Does 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 <4) ? 10 : 8) : 7) We apply the outside ternary operator first, 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 <4) ? 10 : 8) Since x is greater than 2, the answer is 8, or option D in this case.

What is the output of the following code? byte a = 40, b = 50; byte sum= (byte) a + b; 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.

int m = 9, n = 1, x = 0; while(m > n) { m--; n += 2; x += m + n; } System.out.println(x); A. 11 B. 13 C. 23 D. 36 E. 50 F. compile failure 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 <7 is not true. The loop ends and the most recent value of x, 36, is output, so the correct answer is option D.

boolean keepGoing = true; int result = 15, i = 10; do { i--; if(i == 8) keepGoing = false; result -= 2; }while(keepGoing); System.out.println(result); A. 7 B, 9 C. 10. D.11 E.15 F. compile failure line 8

D. The code compiles without issue, so option F is incorrect. After the first execution of the loop, i is decremented to 9 and result to 13. Since i is not 8, keepGoing is false, and the loop continues. On the next iteration, i is decremented to 8 and result to 11. On the second execution, i does equal 8, so keepGoing is set to false. At the conclusion of the loop, the loop terminates since keepGoing is no longer true. The value of result is 11, and the correct answer is option D.

What is the following code snippet? in x = 0; String s = null; if(x == s) System.out.println("Success"); else System.out.println("Failure"); A. Success b. Failure C. The code will not compile because of line 4 D. The code will not compile because of line 5

D. The variable x is an int and s is a reference to a String object. The two data types are incomparable because neither variable can be converted to the other variable's type. The compiler error occurs on line 5 when the comparison is attempted, so the answer is option D.

do{ int y = 1; System.out.print(y++ + " " ); } while (y <= 10); A. 1 2 3 4 5 6 7 8 9 B. 1 2 3 4 5 6 7 8 9 10 C. 1 2 34 5 6 7 8 9 10 11 D. compile failure line 6 E. infinite loop and doesn't 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.

int x = 1,y= 15; while x < 10 y--; x++; System.out.println(x+", " + y); A. 10, 5 B. 10, 6 C. 11, 5 D. compile failure due to line 3 E. compile failure due to line 4 F. infinite loop and does not terminate

E. This is actually a much simpler problem than it appears to be. The while statement on line 4 is missing parentheses, so the code will not compile, and option E is the correct answer. If the parentheses were added, though, option F would be the correct answer since the loop does not use curly braces to include x++ and the boolean expression never changes. Finally, if curly braces were added around both expressions, the output would be 10, 6 and option B would be correct.

What is the output of the following code snippet? int x = 4 long y = x * 4 - x++; if(y<10) System.out.println("Too low"); else System.out.println("Just right"); else System.out.println("Too High"); A Too Low B. Just Right C. Too High D. Compiles but throws a NullPointerException E. Does not compile because of line 6

F. The code does not compile because two else statements cannot be chained together without additional if-then statements, so the correct answer is option F. Option E is incorrect as Line 6 by itself does not cause a problem, only when it is paired with Line 7. One way to fix this code so it compiles would be to add an if-then statement on line 6. The other solution would be to remove line 7.

What is the output of the following code? public class ArithmeticSample { public static void main(String[] args) { int x = 5 * 4 % 3; System.out.println(x) }} A. 2 B.3 C. 5 D. 6 E. The code will not compile because of line 3

The * and % have the same operator precedence, so the expression is evaluated from left-to-right. The result of 5 * 4 is 20, and 20 % 3 is 2 (20 divided by 3 is 18, the remainder is 2). The output is 2 and option A is the correct answer.

int count = 0; ROW_LOOP: for(int row = 1l col <= 2; col++) for(int col = 1; col <= 2; col++) { if(row * col % 2 == 0) continue ROW_LOOP; count++ } System.out.println(count); A. 1 B. 2 C. 3 D. 4 E. 6 F. compile failure line 6

The expression on line 5 is true when row * col is an even number. On the first iteration, row =1 and col =1, so the expression on line 6 is false, the continue is skipped, and count is incremented to 1. On the second iteration, row =1 and col =2, so the expression on line 6 is true and the continue ends the outer loop with count still at 1. On the third iteration, row =2 and col =1, so the expression on line 6 is true and the continue ends the outer loop with count still at 1. On the fourth iteration, row =3 and col =1, so the expression on line 6 is false, the continue is skipped, and count is incremented to 2. Finally, on the fifth and final iteration, row =3 and col =2, so the expression on line 6 is true and the continue ends the outer loop with count still at 2. The result of 2 is displayed, so the answer is option B.


Conjuntos de estudio relacionados

MS Symptoms + Diagnosis + etiology + pathogenesis

View Set

Units of Measurement and Ratio tables

View Set

Cultural Psychology Exam-Multiple Choice

View Set

Chapter 19: Neurobiology of Schizophrenia, Mood Disorders, and Anxiety Disorders

View Set

12 Questions (Ch. 8), 4 Questions (Ch. 1), 2 Questions (Ch. 3), 3 Questions (Ch. 4), 2 Questions (Ch. 5), 4 Questions (Ch. 6), 4 Questions (Ch. 12), 4 Questions (Ch. 11), 5 Questions (Ch. 10), 4 Questions (Ch. 12)

View Set