Operators and control flow
28 questions in 9 subtopics.
Operator precedence
What do these print?int x = 5;
System.out.println(x++ + ++x);
System.out.println(1 + 2 + "3" + 4 + 5);
System.out.println(10 - 2 * 3 % 4);
if (a & MASK == 0) and return flags << 1 + shift;. What is wrong, and what rule would you set for the team?Short-circuit evaluation
Explain the difference between && and & (and between || and |) on boolean operands, with an example where choosing the wrong one causes aNullPointerException.A validation method is written as return checkA() || checkB() || checkC(); where each check also records metrics. Dashboards show checkC is almost never recorded. Is this a bug?A shared validation library evaluates many predicate rules combined with && and ||. Product teams want rule ordering to be configurable. What design concerns do you raise?Java defines strict left-to-right evaluation of operands and short-circuit && and ||, while C and C++ leave much evaluation order unspecified. Why did Java choose this, and what does that predictability mean for how you write coding standards about side effects in expressions?Bitwise and shift operators
Explain >> versus >>> and what these give: -8 >> 1, -8 >>> 28, 1 << 33 for an int, and 1L << 33.Write a method that returns true if an int n is a positive power of two, without loops, and explain why it works.You need to store 40 independent feature flags per user record in a high-volume service. Compare a long bitmask, aBitSet, an EnumSet and a Set<String>.Several services encode status flags as bit positions in an int column shared through a database and in message payloads. Teams keep adding bits and two teams have reused the same bit. How do you redesign this and migrate safely?Switch statements and switch expressions
What does this print when day is 2, and how would you rewrite it as a switch expression?switch (day) {
case 1: System.out.print("Mon");
case 2: System.out.print("Tue");
case 3: System.out.print("Wed");
break;
default: System.out.print("?");
}
Loops, labeled break and continue
Find the first pair (i, j) with grid[i][j] == target and stop both loops. Show a labeled break, then explain when extracting a method is better.This code throwsConcurrentModificationException. Why, and how do you fix it?for (String s : names) {
if (s.isBlank()) names.remove(s);
}
Ternary and compound assignment pitfalls
What does this print and why?Object o = true ? 1 : "a";
Object p = true ? Integer.valueOf(1) : Double.valueOf(2.0);
System.out.println(o + " " + p);
short hits is updated with hits += delta; where delta is an int from a request. Monitoring shows negative hit counts. Explain why the compiler did not stop this, and how you would review for it.Syntax basics: keywords, comments and statements
Which of these compile as identifiers in current Java and why: int goto; int var; String record; int yield; int _; Explain reserved keywords, contextual keywords and literals.This code fails to compile even though the problem line is inside a comment: // see C:\users\dev. Another line with a comment containing \u000a followed by code changes program behaviour. Explain both.Explain the difference between statements and expressions in Java, including expression statements, the empty statement, and why switch can be both. Give a bug caused by an empty statement.Java added contextual keywords such as var, record, yield and sealed instead of new reserved words. What does this tell you about how the language evolves, and how should a company plan for language changes in long-lived codebases?Conditional statements
Rewrite this nested conditional using guard clauses and pattern matching for instanceof:if (o != null) {
if (o instanceof Order) {
Order ord = (Order) o;
if (ord.total() > 0) {
return ord.total();
}
}
}
return 0;
Integer a = 127, b = 127, c = 128, d = 128;
String s = new String("x");
if (a == b) System.out.println("ab");
if (c == d) System.out.println("cd");
if (s == "x") System.out.println("s");
else if (s.equals("x")) System.out.println("eq");
Unreachable code rules
Which of these compile, and why? (1)return 1;
System.out.println("x");
while (false) {
x++;
}
if (false) {
x++;
}
while (true) { }
System.out.println("after");
try { } catch (java.io.IOException e) { }Explain how the compiler's 'can complete normally' analysis (JLS reachability rules) interacts with definite assignment, constant expressions and catch clause ordering. Give an example where changing a static final field to a non-constant changes whether code compiles.No question matches these choices.