Java Chapter 6

4.0 (4 reviews)
A switch statement must have a default clause.
Click the card to flip 👆
1 / 26
Terms in this set (26)
F.

It is true that while and do loops can be infinite loops, but it is also true that Java for loops can be infinite loops. This is not true in some other programming languages where for loops have a set starting and ending point, but Java for loops are far more flexible than most other language's for loops.
You might choose to use a switch statement instead of nested if-else statements if a. the variable being tested might equal one of several hundred integer values b. the variable being tested might equal one of a few integer values c. there are two or more integer variables being tested, each one of which could be one of several hundred values d. there are two or more integer variables being tested, each one of which could be one of a few values e. None of these; you would never choose a switch statement instead of nested if-else statements under any circumstances.B. The switch statement can only be used if there is a single variable being tested and it is an integer type (an int or a char in Java). Further, because you have to enumerate each possible value being tested, the switch statement only makes sense if the number of values being tested is a small number.If a switch statement contains no break statements at all, a. a syntax error will occur b. each of the case clauses will be executed every time the switch statement is encountered c. this is equivalent to having the switch statement always take the default clause, if one is present d. this is not a syntax error but nothing within the switch statement will ever be executed e. None of theseE. Although writing such a switch statement is unusual, it is entirely legal. The normal rules for the switch statement execution apply. The matching case clause being executed after the switch statement is evaluated and, following that, all subsequent clauses are executed, in order, since there are no break statements to terminate execution.A continue statement a. may be used within a while or a do-while loop but not a for loop b. is identical to a break statement within Java loops c. may be used within any Java loop statement d. may be used within a for loop but not within a while or a do-while loop e. None of theseC. Altough use of continue statements should be avoided if possible, they may be used within any of Java's three types of loops.How can the following statement be rewritten using a conditional operator? if(x <0) y = x; else y = 0; a. y = (x < 0) ? x : 0; b. x = (x < 0) ? y : 0; c. (x < 0) ? y = x : y = 0; d. y = (x < 0); e. y = if(x < 0) x : 0;A. The conditional operator of Java tests a condition, (x < 0) in this case, and if true, returns the value after the ? (x in this case) and if false, returns the value after the : (0) in this case). The original if statement is to assign y to x if (x < 0) and 0 otherwise. This will be accomplished by assigning y to be x or 0 based on (x < 0), as shown in A. In B, x is assigned the value of y or 0 which is backward. In C, the conditional operator is syntactically invalid. In D, y would be set to either true or false depending on (x < 0), and the statement in E is syntactically invalid.Given the following code, where x = 0, what is the resulting value of x after the for loop terminates? for(int i = 0; i < 5; i++) x += i; a. 0 b. 4 c. 5 d. 10 e. 15D. Each pass through the for loop results with the current value of the loop index, i, being added to x. The first time through the loop, i = 0 so x = x + 0 = 0. The second time through the loop i = 1 so x = x + 1 = 1. The third time through the loop i = 2 so x = x + 2 = 3. The fourth time through the loop i = 3 so x = x + 3 = 6. The fifth and final time through the loop i = 4 so x = x + 4 = 10.The do loop differs from the while loop in that a. the while loop will always execute the body of the loop at least once b. the do loop will always execute the body of the loop at least once c. the do loop will continue to loop while the condition in its while statement is false and the while loop will continue to loop while the condition in its while statement is true d. the while loop will continue to loop while the condition in its while statement is false and the do loop will continue to loop while the condition in its while statement is true e. None of these; there is no difference between the two types of loopsB. Because the do loop does not test the condition until after the body of the loop executes, the body will always execute at least one time, but the while loop tests the condition first, and so if the condition is false the first time, the body does not execute even one time.How many times will the following loop iterate? int x = 10; do { System.out.println(x); x--; } while(x > 0); a. 0 times b. 1 time c. 9 times d. 10 times e. 11 timesE. The variable x starts at 10. Each pass through the loop, x is decremented and the loop finally exits once x is no longer greater than 0, which in this case means once x becomes 0. So the loop body executes for x = 10, x = 9, x = 8, and so forth down to x = 0. This is 11 times.How many times will the following nested loop structure execute the innermost statement (x++;)? for(int j = 0; j < 100; j++) for(int k = 100; k > 0; k--) x++; a. 100 b. 200 c. 10,000 d. 20,100 e. 1,000,000C. The outer loop iterates 100 times. Each time it iterates, the inner loop, and the x++; statement, execute 100 times. Therefore, the statement x++; executes 100*100 = 10,000 times.Given that s is a String, what does the following loop do? for(int j = s.length(); j > 0; j--) System.out.print(s.charAt(j-1); a. It prints s out backwards b. It prints s out forwards c. It prints s out backwards after skipping the last character d. It prints s out backwards but does not print the first character e. It yields a run-time error because there is no character at s.charAt(j-1) for j = 0A The variable j counts down from the length of the String to 1, each time printing out the character at position j-1. The character at length-1 is the first character printed and this is the last character of the String. It continues until it reaches j = 1, and prints out the character at position j-1, or the 0th character, so it prints the entire String backwards.What does the break statement do? a. It ends a program. b. It transfers control out of the current control structure such as a loop or switch statement. c. It ends the current line of output, returning the cursor. d. It denotes the end of a switch statement. e. It indicates the end of a line when using System.out.print.B. This is the exit statement that allows the programmer to exit a control structure from inside of that control structure.If a break occurs within the innermost loop of a nested loop that is three levels deep, a. just the innermost loop will be "broken" b. all loops are "broken" and execution continues from after the end of the loop c. all but the outermost loops are "broken" and execution continues from the next iteration of the outermost loop d. a syntax error is generated unless there are break or continue statements at each loop level e. None of these are trueA The innermost loop is broken, and execution continues from just after the end of that loop.Which of the following statements are true about Java loops? a. All three loop types are functionally equivalent. b. while loops and do loops are essentially the same, but while loops always execute at least once. c. If you know the number of times that a loop is to be performed, the best type of loop to use is a while loop. d. Loops may be replaced by an appropriate combination of if-else and switch statements. e. None of theseA In Java, as in most languages, the looping statements are all essentially equivalent (and almost interchangeable). Their principal difference(s) relate to when the controlling condition is evaluated and whether there is syntax for incrementation/update.How many times will the System.out.println("*"); statement execute inside of the following nested for loops? for(j = 0; j < 10; j++) for(k = 10; k > j; k--) System.out.println("*"); a. 50 b. 100 c. 55 d. 10 e. 20C The first iteration of the outer loop has j = 0, so the inner loop iterates from 10 down to 1, or 10 times. The next iteration of the outer loop has j = 1, so the inner loop iterates from 10 down to 2, or 9 times. This continues until the outer loop has j = 9, in which case the inner loop iterates for k = 10 only, or 1 time. Thus, the System.out.println("*"); statement executes a total of 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 55 times.Which of the following would rotate an Ellipse named lipse 30 degrees counterclockwise? a. lipse.setRotate(30); b. lipse.setRotate(370); c. lipse.setRotate(-30); d. Ellipse.lipse.setRotate(30); e. Ellipse.lipse.setRotate(-10*3);CIn the following example, x is an int: switch (x) { case 3 : x += 1; case 4 : x += 2; case 5 : x += 3; case 6 : x++; case 7 : x += 2; case 8 : x--; case 9 : x++ } If x is equal to 5, what will the value of x be after the switch statement executes? a. 8 b. 6 c. 11 d. 5 e. 10C. Since there are no break statements, the flow falls through any cases following the one that is true. The first case to be true is 5 since x is 5 prior to the switch statement. This changes x to 8, then to 9, then to 11, then to 10, and then finally back to 11In the following example, x is an int: switch (x) { case 3 : x += 1; case 4 : x += 2; case 5 : x += 3; case 6 : x++; case 7 : x += 2; case 8 : x--; case 9 : x++ } If x is equal to 3, what will the value of x be after the switch statement executes? a. 5 b. 6 c. 11 d. 10 e. 12E Since there are no break statements, the flow falls through any cases following the one that is true. The first case to be true is 3 since x is 3 prior to the switch statement. This changes x to 4, then to 6, then to 9, then to 10, then to 12, then to 11, and then finally back to 12.