OCP Java17 SE Developers 复习题02

OCP Java17 SE Developers 复习题02_第1张图片

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

A, D, G. Option A is the equality operator and can be used on primitives 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 be used only with numeric primitives. Option F is a bitwise complement operator and can only be applied to integer values. Finally, option G is correct, as you can cast a boolean variable since boolean is a type.

OCP Java17 SE Developers 复习题02_第2张图片

 

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

A, B, D. The expression apples + oranges is automatically promoted to int, so int and data types that can be promoted automatically from int will work. Options A, B, and 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.

OCP Java17 SE Developers 复习题02_第3张图片

 

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

B, C, D, F. The code will not compile as is, so option A is not correct. The value 2 * ear is automatically promoted to long and cannot be automatically stored in hearing, which is 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.

OCP Java17 SE Developers 复习题02_第4张图片

 

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

B. The code compiles and runs without issue, so option E is not correct. This example is tricky because of the second assignment operator embedded in line 5. The expression (wolf=false) assigns the value false to wolf and returns false for the entire expression. Since teeth does not equal 10, the left side returns true; therefore, the exclusive or (^) of the entire expression assigned to canine is true. The output reflects these assignments, with no change to teeth, so option B is the only correct answer.

OCP Java17 SE Developers 复习题02_第5张图片

 

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

A, C. Options A and C show operators in increasing or the same order of precedence. Options B and E are in decreasing or the same order of precedence. Options D, F, and G are in neither increasing nor decreasing order of precedence. In option D, the assignment operator (=) is between two unary operators, with the multiplication operator (*) incorrectly being in place of highest precedence. In option F, the logical complement operator (!) has the highest order of precedence, so it should be last. In option G, the assignment operators have the lowest order of precedence, not the highest, so the last two operators should be first.

OCP Java17 SE Developers 复习题02_第6张图片

 

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

F. The code does not compile because line 3 contains a compilation error. The cast (int) is applied to fruit, not the expression fruit+vegetables. Since the cast operator has a higher operator precedence than the addition operator, it is applied to fruit, but the expression is promoted to a float, due to vegetables being float. The result cannot be returned as long in the addCandy() method without a cast. For this reason, option F is correct. If parentheses were added around fruit+vegetables, then the output would be 3, 5, 6, and option B would be correct. Remember that casting floating-point numbers to integral values results in truncation, not rounding.

OCP Java17 SE Developers 复习题02_第7张图片

 

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

D. In the first boolean expression, vis is 2 and ph is 7, so this expression evaluates to true & (true || false), which reduces to true. The second boolean expression uses the conditional operator, and since (vis > 2) is false, the right side is not evaluated, leaving ph at 7. In the last assignment, ph is 7, and the pre-decrement operator is applied first, reducing the expression to 7 <= 6 and resulting in an assignment of false. For these reasons, option D is the correct answer.

OCP Java17 SE Developers 复习题02_第8张图片

 

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

A. The code compiles and runs without issue, so option E is incorrect. Line 7 does not produce a compilation error since the compound operator applies casting automatically. Line 5 increments pig by 1, but it returns the original value of 4 since it is using the post-increment operator. The pig variable is then assigned this value, and the increment operation is discarded. Line 7 just reduces the value of goat by 1, resulting in an output of 4 - 1 and making option A the correct answer.

OCP Java17 SE Developers 复习题02_第9张图片

 

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

A, D, E. The code compiles without issue, so option G is incorrect. In the first expression, a > 2 is false, so b is incremented to 5; but since the post-increment operator is used, 4 is printed, making option D correct. The --c was not applied, because only one of the right-hand expressions was evaluated. In the second expression, a!=c is false since c was never modified. Since b is 5 due to the previous line and the post-increment operator is used, b++ returns 5. The result is then assigned to b using the assignment operator, overriding the incremented value for b and printing 5, making option E correct. In the last expression, parentheses are not required, but lack of parentheses can make ternary expressions difficult to read. From the previous lines, a is 2, b is 5, and c is 2. We can rewrite this expression with parentheses as (2 > 5 ? (5 < 2 ? 5 : 2) : 1). The second ternary expression is never evaluated since 2 > 5 is false, and the expression returns 1, making option A correct.

OCP Java17 SE Developers 复习题02_第10张图片

 

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

G. The code does not compile due to an error on the second line. Even though both height and weight are cast to byte, the multiplication operator automatically promotes them to int, resulting in an attempt to store an int in a short variable. For this reason, the code does not compile, and option G is the only correct answer. This line contains the only compilation error.

OCP Java17 SE Developers 复习题02_第11张图片

 

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

D. First, * and % have the same operator precedence, so the expression is evaluated from left to right unless parentheses are present. The first expression evaluates to 8 % 3, which leaves a remainder of 2. The second expression is evaluated left to right since * and % have the same operator precedence, and it reduces to 6 % 3, which is 0. The last expression reduces to 5 * 1, which is 5. Therefore, the output on line 14 is 2, 0, 5, making option D the correct answer.

OCP Java17 SE Developers 复习题02_第12张图片

 

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

D. The pre- prefix indicates the operation is applied first, and the new value is returned, while the post- prefix indicates the original value is returned prior to the operation. Next, increment increases the value, while decrement decreases the value. For these reasons, option D is the correct answer.

OCP Java17 SE Developers 复习题02_第13张图片

 

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

F. The first expression is evaluated from left to right since the operator precedence of & and ^ is the same, letting us reduce it to false ^ sunday, which is true, because sunday is true. In the second expression, we apply the negation operator (!) first, reducing the expression to sunday && true, which evaluates to true. In the last expression, both variables are true, so they reduce to !(true && true), which further reduces to !true, aka false. For these reasons, option F is the correct answer.

OCP Java17 SE Developers 复习题02_第14张图片

 

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

B, E, G. The return value of an assignment operation in the expression is the same as the value of the newly assigned variable. For this reason, option A is incorrect, and option E is correct. Option B is correct, as the equality (==) and inequality (!=) operators can both be used with objects. Option C is incorrect, as boolean and numeric types are not comparable. For example, you can't say true == 3 without a compilation error. Option D is incorrect, as logical operators evaluate both sides of the expression. The (|) operator will cause both sides to be evaluated. Option F is incorrect, as Java does not accept numbers for boolean values. Finally, option G is correct, as you need to use the negation operator (-) to flip or negate numeric values, not the logical complement operator (!).

OCP Java17 SE Developers 复习题02_第15张图片

 

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

D. The ternary operator is the only operator that takes three values, making option D the only correct choice. Options A, B, C, E, and G are all binary operators. While they can be strung together in longer expressions, each operation uses only two values at a time. Option F is a unary operator and takes only one value.

OCP Java17 SE Developers 复习题02_第16张图片

 

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

B. The first line contains a compilation error. The value 3 is cast to long. The 1 * 2 value is evaluated as int but promoted to long when added to the 3. Trying to store a long value in an int variable triggers a compiler error. The other lines do not contain any compilation errors, as they store smaller values in larger or same-size data types, with lines 2 and 4 using casting to do so. Since only one line does not compile, option B is correct.

OCP Java17 SE Developers 复习题02_第17张图片

 

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

C, F. The starting values of ticketsTaken and ticketsSold are 1 and 3, respectively. After the first compound assignment, ticketsTaken is incremented to 2. The ticketsSold value is increased from 3 to 5; since the post-increment operator was used, the value of ticketsTaken++ returns 1. On the next line, ticketsTaken is doubled to 4. On the final line, ticketsSold is increased by 1 to 6. The final values of the variables are 4 and 6, for ticketsTaken and ticketsSold, respectively, making options C and F the correct answers. Note the last line does not trigger a compilation error as the compound operator automatically casts the right-hand operand.

OCP Java17 SE Developers 复习题02_第18张图片

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

C. Only parentheses, ( ), can be used to change the order of operation in an expression, making option C correct. The other operators, such as [ ], < >, and { }, cannot be used as parentheses in Java.

OCP Java17 SE Developers 复习题02_第19张图片

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

B, F. The code compiles and runs successfully, so options G and H are incorrect. On line 5, the pre-increment operator is executed first, so start is incremented to 8, and the new value is returned as the right side of the expression. The value of end is computed by adding 8 to the original value of 4, leaving a new value of 12 for end and making option F a correct answer. On line 6, we are incrementing one past the maximum byte value. Due to overflow, this will result in a negative number, making option B the correct answer. Even if you didn't know the maximum value of byte, you should have known the code compiles and runs and looked for the answer for start with a negative number.

OCP Java17 SE Developers 复习题02_第20张图片

 

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

A, D, E. Unary operators have the highest order of precedence, making option A correct. The negation operator (-) is used only for numeric values, while the logical complement operator (!) is used exclusively for boolean values. For these reasons, option B is incorrect, and option E is correct. Finally, the pre-increment/pre-decrement operators return the new value of the variable, while the post-increment/post-decrement operators return the original variable. For these reasons, option C is incorrect, and option D is correct.

OCP Java17 SE Developers 复习题02_第21张图片

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

E. The bitwise complement of 8 can be found by multiplying the number by negative one and subtracting one, making -9 the value of bird. By contrast, plane is -8 because it negates myFavoriteNumber. Since bird and plane are not the same, superman is assigned a value of 10. The pre-decrement operator takes superman, subtracts 1, and returns the new value, printing 9. For this reason, option E is correct.

 

你可能感兴趣的:(Java,java)