Scrivio Interview library

Operators and control flow

28 questions in 9 subtopics.

Operator precedence

CodingFoundations5 min read
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);
LeadershipIntermediate6 min read
In code review you see if (a & MASK == 0) and return flags << 1 + shift;. What is wrong, and what rule would you set for the team?

Short-circuit evaluation

ConceptFoundations5 min read
Explain the difference between && and & (and between || and |) on boolean operands, with an example where choosing the wrong one causes a NullPointerException.
DebuggingIntermediate7 min read
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?
DesignAdvanced7 min read
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?
ConceptPrincipal6 min read
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

CodingFoundations7 min read
Explain >> versus >>> and what these give: -8 >> 1, -8 >>> 28, 1 << 33 for an int, and 1L << 33.
CodingFoundations6 min read
Write a method that returns true if an int n is a positive power of two, without loops, and explain why it works.
DesignIntermediate7 min read
You need to store 40 independent feature flags per user record in a high-volume service. Compare a long bitmask, a BitSet, an EnumSet and a Set<String>.
DesignAdvanced7 min read
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

CodingFoundations6 min read
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("?");
}
InternalsIntermediate7 min read
How does the compiler implement a switch on a String, and on an enum, and how do tableswitch and lookupswitch differ?
LeadershipAdvanced6 min read
Your organization is moving from Java 11 to Java 21 or 25. What guidance would you give teams about adopting switch expressions and pattern switches in existing code?
LeadershipPrincipal5 min read
Java carried C-style switch fall-through for 25 years before adding arrow-form switch expressions. In a codebase of millions of lines, would you ban the old colon-form switch? Explain the cost and benefit over several years.

Loops, labeled break and continue

CodingFoundations6 min read
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.
DebuggingFoundations6 min read
This code throws ConcurrentModificationException. Why, and how do you fix it?
for (String s : names) {
if (s.isBlank()) names.remove(s);
}
ConceptPrincipal6 min read
Java kept C-style syntax for loops, switch fall-through and operators while adding streams, switch expressions and pattern matching. Why does Java evolve by adding forms rather than removing old ones, and what does that mean for organization-wide coding standards?

Ternary and compound assignment pitfalls

CodingFoundations6 min read
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);
DebuggingIntermediate6 min read
A counter 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

ConceptFoundations6 min read
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.
DebuggingFoundations7 min read
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.
ConceptIntermediate6 min read
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.
LeadershipPrincipal6 min read
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

CodingFoundations6 min read
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;
DebuggingFoundations6 min read
What does this print and why?
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");
DesignIntermediate7 min read
A pricing service has a 300-line if-else chain over customer type, region and product category that grows every month and causes regressions. How would you restructure it, and when is a long conditional actually fine?

Unreachable code rules

ConceptFoundations6 min read
Which of these compile, and why? (1)
return 1;
System.out.println("x");
(2)
while (false) {
x++;
}
(3)
if (false) {
x++;
}
(4)
while (true) { }
System.out.println("after");
(5) try { } catch (java.io.IOException e) { }
InternalsIntermediate7 min read
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.