Review Questions – Making Decisions with the Ternary Operator – 1Z0-829 Study Guide

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.)
  1. +, *, %, —
  • ++, (int), *
  • =, ==, !
  • (short), =, !, *
  • *, /, %, +, ==
  • !, ||, &
  • ^, +, =, +=
  • What is the output of the following program?
  1. 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)); } }
  1. 4, 6, 6.0
  • 3,5,6
  • 3,6,6
  • 4,5,6
  • The code does not compile because of line 9.
  • None of the above.
  • 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);

  1. true-true-true
  • true-true-false
  • true-false-true
  • true-false-false
  • false-true-true
  • false-true-false
  • false-false-true
  • false-false-false
  • 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);
  1. 4- 1
  1. 4- 2
  • 5- 1
  • 5- 2
  • The code does not compile due to line 7.
  • None of the above.
  • 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. 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • The code does not compile.
  1. 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. 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • The code does not compile.
  1. What is the output of the following code?
  1. int sample1 = (2 * 4) % 3;
  1. int sample2 = 3 * 2 % 3;
  1. int sample3 = 5 * (1 % 2);
  1. System.out.println(sample1 + “, ” + sample2 + “, ” + sample3);
  1. 0,0,5
  1. 1, 2, 10
  • 2,1,5
  • 2,0,5
  • 3, 1, 10
  • 3,2,6
  • The code does not compile.
  1. The _________ operator increases a value and returns the original value, while the _______

operator decreases a value and returns the new value.

  1. post-increment, post-increment
  • pre-decrement, post-decrement
  • post-increment, post-decrement
  • post-increment, pre-decrement
  • pre-increment, pre-decrement
  • pre-increment, post-decrement
  1. 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);
  1. true-false-false
  1. false-true-false
  • true-true-true
  • false-true-true
  • false-false-false
  • true-true-false
  • None of the above
  1. Which of the following statements are correct? (Choose all that apply.)
  1. 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.
  1. Which operators take three operands or values? (Choose all that apply.)
  1. =
  • &&
  • *=
  • ? :
  • &
  • ++
  • /
  1. 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);

  1. 0
  • 1
  • 2
  • 3
  • 4
  1. 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;

  1. ticketsSold is 8.
  • ticketsTaken is 2.
  • ticketsSold is 6.
  • ticketsTaken is 6.
  • ticketsSold is 7.
  • ticketsTaken is 4.
  • The code does not compile.
  1. Which of the following can be used to change the order of operation in an expression? (Choose all that apply.)
  1. [ ]
  • < >
  • ( )
  • \ /
  • { }
  • ” “
  1. What is the result of executing the following code snippet? (Choose all that apply.)
  • int start = 7;
  • int end = 4;
  • end += ++start;
  • start = (byte)(Byte.MAX_VALUE + 1);
  1. start is 0.
  1. start is -128.
  • start is 127.
  • end is 8.
  • end is 11.
  • end is 12.
  • 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.)
  1. 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.
  • None of the above
  • 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);

  1. -7,-8,9
  • -7,-8,10
  • -8,-8,4
  • -8,-8,5
  • -9,-8,9
  • -9,-8,10
  • None of the above