OCP Java17 SE Developers 复习题03

======================答案=========================

A, B, C, E, F, G.  A switch expression supports only the primitives intbyteshort, and char, along with their associated wrapper classes IntegerByteShort, and Character, respectively, making options B, C, and F correct and ruling out options D and H. It also supports enum and String, making options A and E correct. Finally, switch supports var if the type can be resolved to a supported switch data type, making option G correct.

======================答案=========================

B.  The code compiles and runs without issue, so options D, E, and F are incorrect. Even though two consecutive else statements on lines 7 and 8 look a little odd, they are associated with separate if statements on lines 5 and 6, respectively. The value of humidity on line 4 is equal to -4 + 12, which is 8. The first if statement evaluates to true on line 5, so line 6 is executed and evaluates to false. This causes the else statement on line 7 to run, printing Just Right and making option B the correct answer.

======================答案=========================

A, D, F, H.  A for-each loop supports arrays, making options A and F correct. For Double[][], each element of the for-each loop would be a Double[]. A for-each loop also supports classes that implement java.lang.Iterable. Although this includes many of the Collections Framework classes, not all of them implement java.lang.Iterable. For this reason, option C is incorrect, and options D and H are correct. Options B, E, and G are incorrect, as they do not implement java.lang.Iterable. Although a String is a list of ordered characters, the class does not implement the required interface for a for-each loop.

======================答案=========================

F.  The code does not compile because the switch expression requires all possible case values to be handled, making option F correct. If a valid default statement was added, then the code would compile and print Turtle at runtime. Unlike traditional switch statements, switch expressions execute exactly one branch and do not use break statements between case statements.

======================答案=========================

E.  The second for-each loop contains a continue followed by a print() statement. Because the continue is not conditional and always included as part of the body of the for-each loop, the print() statement is not reachable. For this reason, the print() statement does not compile. As this is the only compilation error, option E is correct. The other lines of code compile without issue.

======================答案=========================

C, D, E. A for-each loop can be executed on any Collections object that implements java.lang.Iterable, such as List or Set, but not all Collections classes, such as Map, so option A is incorrect. The body of a do/while loop is executed one or more times, while the body of a while loop is executed zero or more times, making option E correct and option B incorrect. The conditional expression of for loops is evaluated at the start of the loop execution, meaning the for loop may execute zero or more times, making option C correct. A switch expression that takes a String requires a default branch if the result is assigned to a variable, making option D correct. Finally, each if statement has at most one matching else statement, making option F incorrect.

======================答案=========================

B, D. Option A is incorrect because on the first iteration, it attempts to access weather[weather.length] of the nonempty array, which causes an ArrayIndexOutOfBoundsException to be thrown. Option B is correct and will print the elements in order. Option C doesn't compile as i is undefined in weather[i]. For this to work, the body of the for-each loop would have to be updated as well. Option D is also correct and is a common way to print the elements of an array in reverse order. Option E does not compile and is therefore incorrect. You can declare multiple elements in a for loop, but the data type must be listed only once, such as in for(int i=0, j=3; …). Finally, option F is incorrect because the first element of the array is skipped. Since the conditional expression is checked before the loop is executed the first time, the first value of i used inside the body of the loop will be 1.

======================答案=========================

G.  The first two pattern matching statements compile without issue. The variable bat is allowed to be used again, provided it is no longer in scope. Line 36 does not compile, though. Due to flow scoping, if s is not a Long, then bat is not in scope in the expression bat <= 20. Line 38 also does not compile as default cannot be used as part of an if/else statement. For these two reasons, option G is correct.

======================答案=========================

B, C, E.  The code contains a nested loop and a conditional expression that is executed if the sum of col + row is an even number; otherwise, count is incremented. Note that options E and F are equivalent to options B and D, respectively, since unlabeled statements apply to the most inner loop. Studying the loops, the first time the condition is true is in the second iteration of the inner loop, when row is 1 and col is 1. Option A is incorrect because this causes the loop to exit immediately with count only being set to 1. Options B, C, and E follow the same pathway. First, count is incremented to 1 on the first inner loop, and then the inner loop is exited. On the next iteration of the outer loop, row is 2 and col is 0, so execution exits the inner loop immediately. On the third iteration of the outer loop, row is 3 and col is 0, so count is incremented to 2. In the next iteration of the inner loop, the sum is even, so we exit, and our program is complete, making options B, C, and E each correct. Options D and F are both incorrect, as they cause the inner and outer loops to execute multiple times, with count having a value of 5 when done. You don't need to trace through all the iterations; just stop when the value of count exceeds 2.

 

======================答案=========================

E.  This code contains numerous compilation errors, making options A and H incorrect. Line 15 does not compile, as continue cannot be used inside a switch statement like this. Line 16 is not a compile-time constant since any int value can be passed as a parameter. Marking it final does not change this, so it doesn't compile. Line 18 does not compile because Sunday is not marked as final. Being effectively final is insufficient. Finally, line 19 does not compile because DayOfWeek.MONDAY is not an int value. While switch statements do support enum values, each case statement must have the same data type as the switch variable otherDay, which is int. The rest of the lines do compile. Since exactly four lines do not compile, option E is the correct answer.

======================答案=========================

A.  The code compiles and runs without issue, printing 3 at runtime and making option A correct. The default statement on line 17 is optional since all the enum values are accounted for and can be removed without changing the output.

======================答案=========================

C.  Prior to the first iteration, sing = 8squawk = 2, and notes = 0. After the iteration of the first loop, sing is updated to 7squawk to 4, and notes to the sum of the new values for sing + squawk7 + 4 = 11. After the iteration of the second loop, sing is updated to 6squawk to 6, and notes to the sum of itself plus the new values for sing + squawk11 + 6 + 6 = 23. On the third iteration of the loop, sing > squawk evaluates to false, as 6 > 6 is false. The loop ends and the most recent value of sing23, is output, so the correct answer is option C.

======================答案=========================

G.  This example may look complicated, but the code does not compile. Line 8 is missing the required parentheses around the boolean conditional expression. Since the code does not compile and it is not because of line 6, option G is the correct answer. If line 8 was corrected with parentheses, then the loop would be executed twice, and the output would be 11.

======================答案=========================

B, D, F.  The code does compile, making option G incorrect. In the first for-each loop, the right side of the for-each loop has a type of int[], so each element penguin has a type of int, making option B correct. In the second for-each loop, ostrich has a type of Character[], so emu has a data type of Character, making option D correct. In the last for-each loop, parrots has a data type of List<Integer>. Since the generic type of Integer is used in the Listmacaw will have a data type of Integer, making option F correct.

======================答案=========================

F.  The code does not compile, although not for the reason specified in option E. The second case statement contains invalid syntax. Each case statement must have the keyword case—in other words, you cannot chain them with a colon (:). For this reason, option F is the correct answer. This line could have been fixed to say case 'B', 'C' or by adding the case keyword before 'C'; then the rest of the code would have compiled and printed great good at runtime.

 

 

======================答案=========================

A, B, D.  To print items in the wolf array in reverse order, the code needs to start with wolf[wolf.length-1] and end with wolf[0]. Option A accomplishes this and is the first correct answer. Option B is also correct and is one of the most common ways a reverse loop is written. The termination condition is often m>=0 or m>-1, and both are correct. Options C and F each cause an ArrayIndexOutOfBoundsException at runtime since both read from wolf[wolf.length] first, with an index that is passed the length of the 0-based array wolf. The form of option C would be successful if the value was changed to wolf[wolf.length-z-1]. Option D is also correct, as the j is extraneous and can be ignored in this example. Finally, option E is incorrect and produces an infinite loop, as w is repeatedly set to r-1, in this case 4, on every loop iteration. Since the update statement has no effect after the first iteration, the condition is never met, and the loop never terminates.

======================答案=========================

B, E.  The code compiles without issue and prints two distinct numbers at runtime, so options G and H are incorrect. The first loop executes a total of five times, with the loop ending when participants has a value of 10. For this reason, option E is correct. In the second loop, animals starts out not less than or equal to 1, but since it is a do/while loop, it executes at least once. In this manner, animals takes on a value of 3 and the loop terminates, making option B correct. Finally, the last loop executes a total of two times, with performers starting with -1, going to 1 at the end of the first loop, and then ending with a value of 3 after the second loop, which breaks the loop. This makes option B a correct answer twice over.

======================答案=========================

C, E.  Pattern matching with an if statement is implemented using the instanceof operator, making option C correct and options A and B incorrect. Option D is incorrect as it is possible to access a pattern variable outside the if statement in which it is defined. Option E is a correct statement about flow scoping. Option F is incorrect. Pattern matching does not support declaring variables in else statements as else statements do not have a boolean expression.

======================答案=========================

E.  The variable snake is declared within the body of the do/while statement, so it is out of scope on line 7. For this reason, option E is the correct answer. If snake were declared before line 3 with a value of 1, then the output would have been 1 2 3 4 5 -5.0, and option G would have been the correct answer.

======================答案=========================

A, E.  The most important thing to notice when reading this code is that the innermost loop is an infinite loop. Therefore, you are looking for solutions that skip the innermost loop entirely or that exit that loop. Option A is correct, as break L2 on line 8 causes the second inner loop to exit every time it is entered, skipping the innermost loop entirely. For option B, the first continue on line 8 causes the execution to skip the innermost loop on the first iteration of the second loop but not the second iteration of the second loop. The innermost loop is executed, and with continue on line 12, it produces an infinite loop at runtime, making option B incorrect. Option C is incorrect because it contains a compiler error. The label L3 is not visible outside its loop. Option D is incorrect, as it is equivalent to option B since the unlabeled break and continue apply to the nearest loop and therefore produce an infinite loop at runtime. Like option A, the continue L2 on line 8 allows the innermost loop to be executed the second time the second loop is called. The continue L2 on line 12 exits the infinite loop, though, causing control to return to the second loop. Since the first and second loops terminate, the code terminates, and option E is a correct answer.

======================答案=========================

E.  Line 22 does not compile because Long is not a compatible type for a switch statement or expression. Line 23 does not compile because it is missing a semicolon after "Jane" and a yield statement. Line 24 does not compile because it contains an extra semicolon at the end. Finally, lines 25 and 26 do not compile because they use the same case value. At least one of them would need to be changed for the code to compile. Since four lines need to be corrected, option E is correct.

======================答案=========================

E.  The code compiles without issue, making options F and G incorrect. Remember, var is supported in both switch and while loops, provided the compiler determines that the type is compatible with these statements. In addition, the variable one is allowed in a case statement because it is a final local variable, making it a compile-time constant. The value of tailFeathers is 3, which matches the second case statement, making 5 the first output. The while loop is executed twice, with the pre-increment operator (--) modifying the value of tailFeathers from 3 to 2 and then to 1 on the second loop. For this reason, the final output is 5 2 1, making option E the correct answer.

======================答案=========================

F.  Line 19 starts with an else statement, but there is no preceding if statement that it matches. For this reason, line 19 does not compile, making option F the correct answer. If the else keyword was removed from line 19, then the code snippet would print Success.

======================答案=========================

G.  The statement is not a valid for-each loop (or a traditional for loop) since it uses a nonexistent in keyword. For this reason, the code does not compile, and option G is correct. If the in was changed to a colon (:), then Setint[], and Collection would be correct.

 

======================答案=========================

D.  The code compiles without issue, so option F is incorrect. The viola variable created on line 8 is never used and can be ignored. If it had been used as the case value on line 15, it would have caused a compilation error since it is not marked final. Since "violin" and "VIOLIN" are not an exact match, the default branch of the switch statement is executed at runtime. This execution path increments p a total of three times, bringing the final value of p to 2 and making option D the correct answer.

======================答案=========================

F.  The code snippet does not contain any compilation errors, so options D and E are incorrect. There is a problem with this code snippet, though. While it may seem complicated, the key is to notice that the variable r is updated outside of the do/while loop. This is allowed from a compilation standpoint, since it is defined before the loop, but it means the innermost loop never breaks the termination condition r <= 1. At runtime, this will produce an infinite loop the first time the innermost loop is entered, making option F the correct answer.

======================答案=========================

F.  Line 27 does not compile because the case block does not yield a value if name is not equal to Frog. For this reason, option F is correct. Every path within a case block must yield a value if the switch expression is expected to return a value.

======================答案=========================

F.  Based on flow scoping, guppy is in scope after lines 41–42 if the type is not a String. In this case, line 43 declares a variable guppy that is a duplicate of the previously defined local variable defined on line 41. For this reason, the code does not compile, and option F is correct. If a different variable name was used on line 43, then the code would compile and print Swim! at runtime with the specified input.

======================答案=========================

C.  Since the pre-increment operator was used, the first value that will be displayed is -1, so options A and B are incorrect. On the second-to-last iteration of the loop, y will be incremented to 5, and the loop will output 5. The loop will continue since 5 <= 5 is true, and on the last iteration, 6 will be output. At the end of this last iteration, the boolean expression 6 <= 5 will evaluate to false, and the loop will terminate. Since 6 was the last value output by the loop, the answer is option C.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值