Review Questions
- 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.)
- +, *, %, —
- What is the output of the following program?
- public class CandyCounter {
- static long addCandy(double fruit, float vegetables) {
- return (int)fruit+vegetables;
5:
- public static void main(String[] args) {
- System.out.print(addCandy(1.4, 2.4f) + “, “);
- System.out.print(addCandy(1.9, (float)4) + “, “);
- System.out.print(addCandy((long)(int)(short)2, (float)4)); } }
- 4, 6, 6.0
- The code does not compile because of line 9.
- 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);
- true-true-true
- What is the output of the following code snippet?
- System.out.print(pig + ” -” + goat);
- 4- 1
- 4- 2
- The code does not compile due to line 7.
- 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);
- 1
- The code does not compile.
- 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);
- 1
- The code does not compile.
- What is the output of the following code?
- int sample1 = (2 * 4) % 3;
- int sample2 = 3 * 2 % 3;
- int sample3 = 5 * (1 % 2);
- System.out.println(sample1 + “, ” + sample2 + “, ” + sample3);
- 0,0,5
- 1, 2, 10
- The code does not compile.
- The _________ operator increases a value and returns the original value, while the _______
operator decreases a value and returns the new value.
- post-increment, post-increment
- pre-decrement, post-decrement
- post-increment, post-decrement
- post-increment, pre-decrement
- pre-increment, pre-decrement
- pre-increment, post-decrement
- 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
- true-false-false
- false-true-false
- Which of the following statements are correct? (Choose all that apply.)
- The return value of an assignment operation expression can be void.
- The inequality operator (!=) can be used to compare objects.
- The equality operator (==) can be used to compare a boolean value with a numeric value.
- During runtime, the & and | operators may cause only the left side of the expression to be evaluated.
- The return value of an assignment operation expression is the value of the newly assigned variable.
- In Java, 0 and false may be used interchangeably.
- The logical complement operator (!) cannot be used to flip numeric values.
- Which operators take three operands or values? (Choose all that apply.)
- =
- How many lines of the following code contain compiler errors?
int note = 1 * 2 + (long)3;
short melody = (byte)(double)(note *= 2); double song = melody;
float symphony = (float)((song == 1_000f) ? song * 2L : song);
- 0
- Given the following code snippet, what are the values of the variables after it is executed? (Choose all that apply.)
int ticketsTaken = 1;
int ticketsSold = 3;
ticketsSold += 1 + ticketsTaken++;
ticketsTaken *= 2;
ticketsSold += (long)1;
- ticketsSold is 8.
- The code does not compile.
- Which of the following can be used to change the order of operation in an expression? (Choose all that apply.)
- [ ]
- What is the result of executing the following code snippet? (Choose all that apply.)
- start = (byte)(Byte.MAX_VALUE + 1);
- start is 0.
- start is -128.
- The code does not compile.
- The code compiles but throws an exception at runtime.
- Which of the following statements about unary operators are true? (Choose all that apply.)
- Unary operators are always executed before any surrounding numeric binary or ternary operators.
- The -operator can be used to flip a boolean value.
- The pre-increment operator (++) returns the value of the variable before the increment is applied.
- The post-decrement operator (–) returns the value of the variable before the decrement is applied.
- The ! operator cannot be used on numeric values.
- What is the result of executing the following code snippet?
int myFavoriteNumber = 8;
int bird = ~myFavoriteNumber;
int plane = -myFavoriteNumber;
var superman = bird == plane ? 5 : 10; System.out.println(bird + “,” + plane + “,” + --superman);
- -7,-8,9