1. 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.
A).
1. +, *, %, —
2. =, ==, !
2. The _________ operator increases a value and returns the original value, while the _______ operator decreases a value and returns the new value.
A). post-increment, pre-decrement
3. What is the result of executing the following code snippet?
A).
1. start is -128.
2. end is 12.
4. Which of the following statements about unary operators are true?
A).
1. Unary operators are always executed before any surrounding numeric binary or ternary operators
2. The post-decrement operator (–) returns the value of the variable before the decrement is applied
3. The logical complement operator (!) cannot be used on numeric values.
5. 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);
A) -9,-8,9
6. 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);
A) 2, 0, 5
7. Which of the following can be used to change the order of operation in an expression?
A) ( )
8. What change, when applied independently, would allow the following code snippet to compile?
long ear = 10;
int hearing = 2 * ear;
A)
1. Cast ear on line 2 to int.
2. Change the data type of ear on line 1 to short.
3. Cast 2 * ear on line 2 to int.
4. Change the data type of hearing on line 2 to short
9. What is the output of the following program?
public class CandyCounter {
static long addCandy(double fruit, float vegetables) {
return (int)fruit+vegetables;
}
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)); } }
A) None of these.
10. What is the output of the following code snippet?
int pig = (short)4;
pig = pig++;
long goat = (int)2;
goat -= 1.0;
System.out.print(pig + ” – ” + goat);
A) 4 – 1
11. What is the unique output of the following code snippet?
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) The code does not compile.
12. Given the following code snippet, what are the values of the variables after it is executed?
int ticketsTaken = 1;
int ticketsSold = 3;
ticketsSold += 1 + ticketsTaken++;
ticketsTaken *= 2;
ticketsSold += (long)1;
A) ticketsSold is 6.
ticketsTaken is 4.
13. Which of the following Java operators can be used with boolean variables?
A) 1. ==
2. !
3. Cast with (boolean)
14. What is the output of the following code snippet?
boolean canine = true, wolf = true;
int teeth = 20;
canine = (teeth != 10) ^ (wolf=false);
System.out.println(canine+”, “+teeth+”, “+wolf);
A) true, 20, false
15. 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-false-false
16.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-true-false
17.
Which of the following statements are correct?
A) The inequality operator (!=) can be used to compare objects.
The return value of an assignment operation expression is the value of the newly assigned variable.
The logical complement operator (!) cannot be used to flip numeric values.
18. What are the unique outputs of the following code snippet?
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
4
5
19. Which operator takes three operands or values?
A) ? :
int note = 1 * 2 + (long)3;
short melody = (byte)(double)(note *= 2);
double song = melody;
float symphony = (float)((song == 1_000f) ? song * 2L : song);
A) 1
Other Links:
See other websites for quiz:
Check on QUIZLET