Which expression tests that x lies strictly between 0 and 10?
Q3
What is the value of true && false || true?
Q4
Which statement about && is correct?
Q5
Why is the order of operands important in
if (count != 0 && total / count > 50)?
Q6
!(a || b) is equivalent to:
Q7
The negation of grade >= 60 && grade <= 100 is:
Q8
What is printed?
int x = -3;
if (x > 0);
System.out.println("positive");
Q9
What is printed?
int score = 45;
if (score > 50)
System.out.println("Pass");
System.out.println("Done");
Q10
What is printed?
int n = 7;
if (n > 5)
if (n > 10) System.out.println("big");
else System.out.println("medium");
Q11
In the previous question, to which if does the else belong?
Q12
What is the value of grade after this ladder when mark is 85?
char grade;
if (mark >= 70) grade = 'C';
else if (mark >= 80) grade = 'B';
else if (mark >= 90) grade = 'A';
else grade = 'F';
Q13
What is wrong with the ladder in the previous question?
Q14
Which condition is true when ch holds an upper-case letter?
Q15
How should two String variables be compared for equal contents?
Q16
What is the output when the user's input has been stored in answer as "YES"?
if (answer.equalsIgnoreCase("yes")) System.out.print("Confirmed");
else System.out.print("Cancelled");
Q17
Which of the following may not be used as the selector of a switch?
Q18
What happens if a case block does not end with break?
Q19
What is the output?
int day = 3;
switch (day) {
case 1: System.out.print("Mon");
case 2: System.out.print("Tue");
case 3: System.out.print("Wed");
case 4: System.out.print("Thu");
default: System.out.print("?");
}
Q20
What is the output?
char c = 'b';
switch (c) {
case 'a':
case 'b': System.out.print("ab"); break;
case 'c': System.out.print("c"); break;
default: System.out.print("other");
}
Q21
Is the default label required in a switch?
Q22
Which statement is the best reason to prefer an if ladder over a switch?
Q23
What is the output?
int a = 5, b = 10;
System.out.print(a > b ? "first" : a == b ? "equal" : "second");
Q24
What is printed?
boolean flag = false;
if (flag = true) System.out.print("yes");
else System.out.print("no");
Q25
Why does if (x = 5) fail to compile when x is an int?
Q26
What is printed?
int x = 8;
if (x % 2 == 0 && x % 4 == 0) System.out.print("A");
else if (x % 2 == 0) System.out.print("B");
else System.out.print("C");
Q27
How many of the following conditions are true when x is 0?
x >= 0, x <= 0, x != 0, !(x > 0)
Q28
What is the output?
int n = 15;
String result = "";
if (n % 3 == 0) result += "Fizz";
if (n % 5 == 0) result += "Buzz";
System.out.print(result.isEmpty() ? n : result);
Q29
Which condition correctly checks that a year is a leap year (divisible by 4 but not by 100,
unless also divisible by 400)?
Q30
Which expression is true exactly when ch is not a digit?
Q31
What is the output?
String s = null;
if (s != null && s.length() > 3) System.out.print("long");
else System.out.print("short or none");
Q32
What would happen if the two operands of && in the previous question were swapped?
Rewrite the following as a single assignment statement, with no
if and no else: “if the mark is at least 50 set passed to true, otherwise set it to false”.
Q36Short answer
Explain, with an example of your own, what short-circuit evaluation
is and give one situation in which a program would crash without it.
Q37Short answer
Rewrite the following switch as an if/else if ladder,
and say which version you find clearer and why.
switch (code) {
case 'S': cost = 5; break;
case 'M': cost = 8; break;
case 'L': cost = 12; break;
default: cost = 0;
}
Q38Programming
Write a program TriangleType that reads three side lengths and
prints Not a triangle, Equilateral, Isosceles or Scalene. Remember that the
sum of any two sides must exceed the third.