Chapter 2 Operators Flashcards
Review Questions
1. Which of the following Java operators can be used with boolean variables? (Choose all that apply.)
A. ==
B. +
C. –
D. !
E. %
F. ~
G. Cast with (boolean)
A, D
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.
2. What data type (or types) will allow the following code snippet to compile? (Choose all that apply.)
byte apples = 5; short oranges = 10; \_\_\_\_\_ bananas = apples + oranges;
A. int
B. long
C. boolean
D. double
E. short
F. byte
A, B, D
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.
3. What change, when applied independently, would allow the following code snippet to compile? (Choose all that apply.)
3: long ear = 10; 4: int hearing = 2 * ear;
A. No change; it compiles as is.
B. Cast ear on line 4 to int.
C. Change the data type of ear on line 3 to short.
D. Cast 2 * ear on line 4 to int.
E. Change the data type of hearing on line 4 to short.
F. Change the data type of hearing on line 4 to long.
B, C, D, F
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.
4. What is the output of the following code snippet?
3: boolean canine = true, wolf = true; 4: int teeth = 20; 5: canine = (teeth != 10) ^ (wolf=false); 6: System.out.println(canine+", "+teeth+", "+wolf);
A. true, 20, true
B. true, 20, false
C. false, 10, true
D. false, 20, false
E. The code will not compile because of line 5.
F. None of the above.
B
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.
5. Which of the following operators are ranked in increasing or the same order of precedence? Assume the + operator is binary addition, not the unary form. (Choose all that apply.)
A. +, *, %, --
B. ++, (int), *
C. =, ==, !
D. (short), =, !, *
E. *, /, %, +, ==
F. !, ||, &
G. ^, +, =, +=
B, E
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.
6. What is the output of the following program?
1: public class CandyCounter { 2: static long addCandy(double fruit, float vegetables) { 3: return (int)fruit+vegetables; 4: } 5: 6: public static void main(String[] args) { 7: System.out.print(addCandy(1.4, 2.4f) + ", "); 8: System.out.print(addCandy(1.9, (float)4) + ", "); 9: System.out.print(addCandy((long)(int)(short)2, (float)4)); } }
A. 4, 6, 6.0
B. 3, 5, 6
C. 3, 6, 6
D. 4, 5, 6
E. The code does not compile because of line 9.
F. None of the above.
F
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.
7. What is the output of the following code snippet?
int ph = 7, vis = 2; boolean clear = vis> 1 & (vis < 9 || ph < 2); boolean safe = (vis> 2) && (ph++> 1); boolean tasty = 7 <= --ph; System.out.println(clear + "-" + safe + "-" + tasty);
A. true-true-true
B. true-true-false
C. true-false-true
D. true-false-false
E. false-true-true
F. false-true-false
G. false-false-true
H. false-false-false
D
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.
8. What is the output of the following code snippet?
4: int pig = (short)4; 5: pig = pig++; 6: long goat = (int)2; 7: goat -= 1.0; 8: System.out.print(pig + " - " + goat);
A. 4 - 1
B. 4 - 2
C. 5 - 1
D. 5 - 2
E. The code does not compile due to line 7.
F. None of the above.
A
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.
9. What are the unique outputs of the following code snippet? (Choose all that apply.)
int a = 2, b = 4, c = 2; System.out.println(a> 2 ? --c : b++); System.out.println(b = (a!=c ? a : b++)); System.out.println(a> b ? b < c ? b : 2 : 1);
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
G. The code does not compile.
A, D, E,
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.
10. What are the unique outputs of the following code snippet? (Choose all that apply.)
short height = 1, weight = 3; short zebra = (byte) weight * (byte) height; double ox = 1 + height * 2 + weight; long giraffe = 1 + 9 % height + 1; System.out.println(zebra); System.out.println(ox); System.out.println(giraffe);
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
G. The code does not compile.
G
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.
11. What is the output of the following code?
11: int sample1 = (2 * 4) % 3; 12: int sample2 = 3 * 2 % 3; 13: int sample3 = 5 * (1 % 2); 14: System.out.println(sample1 + ", " + sample2 + ", " + sample3);
A. 0, 0, 5
B. 1, 2, 10
C. 2, 1, 5
D. 2, 0, 5
E. 3, 1, 10
F. 3, 2, 6
G. The code does not compile.
D
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.
12. The \_\_\_\_\_\_\_\_\_\_\_\_
operator increases a value and returns the original value, while the \_\_\_\_\_\_\_\_\_\_\_\_
operator decreases a value and returns the new value.
A. post-increment, post-increment
B. pre-decrement, post-decrement
C. post-increment, post-decrement
D. post-increment, pre-decrement
E. pre-increment, pre-decrement
F. pre-increment, post-decrement
D
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.
13. What is the output of the following code snippet?
boolean sunny = true, raining = false, sunday = true; boolean goingToTheStore = sunny & raining ^ sunday; boolean goingToTheZoo = sunday && !raining; boolean stayingHome = !(goingToTheStore && goingToTheZoo); System.out.println(goingToTheStore + "-" + goingToTheZoo + "-" +stayingHome);
A. true-false-false
B. false-true-false
C. true-true-true
D. false-true-true
E. false-false-false
F. true-true-false
G. None of the above
F
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.
14. Which of the following statements are correct? (Choose all that apply.)
A. The return value of an assignment operation expression can be void.
B. The inequality operator (!=) can be used to compare objects.
C. The equality operator (==) can be used to compare a boolean value with a numeric value.
D. During runtime, the & and | operators may cause only the left side of the expression to be evaluated.
E. The return value of an assignment operation expression is the value of the newly assigned variable.
F. In Java, 0 and false may be used interchangeably.
G. The logical complement operator (!) cannot be used to flip numeric values.
B, E, G
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 (!).
15. Which operators take three operands or values? (Choose all that apply.)
A. =
B. &&
C. *=
D. ? :
E. &
F. ++
G. /
D
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.