What is the result of: true && false?
false
What keyword follows an if statement to provide another conditional branch?
else if
Why does false && (x / 0 > 1) not crash?
Short-circuiting stops the second part from evaluating.
Which compares object contents in Java to determine if that are aliases?
==
What is the only metal that is liquid at standard room temperature?
Mercury
What is the result of: !(false || true)?
false
In an if / else if / else chain, how many branches will execute?
One.
Technically at most one. If there is no else statement and all of the if and else if statements are false, then zero branches will execute!
Rewrite using DeMorgan’s Law:
!(x < 5 || y == 10)
(x >= 5 && y != 10)
2 PART QUESTION: What does the following evaluate to and why?
String s1 = new String("cat");
String s2 = new String("cat");
System.out.println(s1 == s2);
False! These are two different objects. The == operator checks if they have the same memory address, and these do not.
Which country has the largest number of time zones in the world when including overseas territories?
France
Evaluate: (5 < 10) || (3 > 8 && 2 == 2)
true
What is the difference between:
A) One if with many else ifs
B) Several separate if statements
A) Only one branch can run.
B) Many branches can run if all conditions are true.
What is a short-circuit evaluation in terms of Java compilers.
Java evaluates only as much of an expression as needed to determine truth.
What shortcut operator updates a variable by adding to it?
+=
Which city was the first in the world to reach a population of 1 million people?
Rome
Apply DeMorgan’s Law to: !(a && b)
!a || !b
Which selection structure should you use when you have a condition that relies on another condition?
A Nested If Statement
Identify the error:
if (str.length() > 0 && str.charAt(0) == 'A')
Which part risks a crash if str is null?
str.length() will cause the crash.
This is a NullPointerException. str.charAt would crash, but it is never encountered due to short circuit rules.
Why does "hello".equals("HELLO") return false?
.equals is case-sensitive.
What is the hardest naturally occurring substance on Earth besides diamond (meaning the next hardest)?
Moissanite
Given:
int x = 0;
boolean result = (x !=0 && (10 / x) > 1);
Why will this not cause a division-by-zero error?
Because && short-circuits. When x != 0 is false, the right side (10 / x) never executes
What does the following print?
int n = 4;
if (n > 5)
System.out.print("A");
else if (n > 3)
System.out.print("B");
else
System.out.print("C");
B
Convert to an equivalent expression using DeMorgan's Law:
!(x == 7 || y != 3)
x != 7 && y == 3
What is the value of n after:
int n = 8;
n *= 2 + 3;
40
You should resolve the right side of the equals sign before you do the multiplication! n *= 5 → 8 * 5 = 40.
In most versions of Greek mythology, which Titan is credited with creating mankind from clay?
Prometheus
(Some versions say Hephaistos)