skip to content

Operators

Java's full operator set and the precedence and associativity rules that decide how an expression parses. Interviewers use operator questions for quick puzzles about integer division, short-circuiting and shifts.

part ofJavaoverview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

In Java, what does an expression like 1 << 33 evaluate to, and why? Explain shift-distance modulo behavior.

level: seniorimportance: should knowfreq 40%

basics

~20 s

The shift amount is reduced using a remainder. For int it's the amount mod 32, for long it's mod 64. So 1 << 33 is the same as 1 << 1 = 2, not 0.

open as a page

What surprising results arise from integer promotion and sign extension when applying bitwise and shift operators to byte, short, and char, and how do you handle them safely?

level: seniorimportance: should knowfreq 35%

basics

~20 s

byte, short, and char are widened to int before any bitwise or shift operation. A negative byte gets its high bits filled with 1s (sign extension), so masking with & 0xFF is often needed to get the value you expect.

open as a page

When does an instanceof expression fail to compile rather than simply evaluate to false?

level: seniorimportance: should knowfreq 40%

basics

~20 s

If the compiler can prove the test could never possibly be true — because the two types have no possible relationship — instanceof is a compile error, not a false result. It also won't compile with a primitive on the left.

open as a page

Why are long instanceof / type-check chains often considered a design smell, and what should you reach for instead?

level: seniorimportance: should knowfreq 48%

basics

~20 s

Chains of instanceof checks scatter type-specific logic and must be edited every time a new subtype is added. Prefer polymorphism (an overridden method on each type) so each type owns its behavior. For closed sets, sealed types + pattern-matching switch give safe, exhaustive handling.

open as a page

How does short-circuit evaluation influence how you order operands in a compound condition, and what pitfalls arise from relying on it?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Because && and || stop early, the order of checks matters: put guards (like null checks) and cheap, likely-decisive tests first so the expensive or unsafe ones run only when needed. The pitfall is hiding required side effects inside an operand that may get skipped.

open as a page

How does operator precedence differ from operand evaluation order in Java? Use an expression with side effects to illustrate.

level: seniorimportance: should knowfreq 45%

basics

~20 s

Precedence decides how operators group their operands; evaluation order decides which operand runs first in time. In Java operands always run left to right, no matter the precedence, so a higher-precedence operator can still have its operands computed later.

open as a page

Why can == sometimes return true and sometimes false when comparing two Integer objects with the same value?

level: seniorimportance: should knowfreq 55%

basics

~20 s

Integer is an object, so == compares references, not the number. Java caches small boxed values (-128 to 127), so two boxed ints in that range can share one object and be == ; outside that range they are different objects and == is false. Always use .equals() or unbox to int.

open as a page

When ordering objects, why can't you use relational operators, and what do you use instead?

level: seniorimportance: should knowfreq 45%

basics

~10 s

Relational operators (<, >) only work on numbers, not objects. To order objects you implement Comparable's compareTo (or pass a Comparator) and check whether its result is negative, zero, or positive.

open as a page

When both branches of a ternary are reference (object) types rather than numerics, how does Java decide the result type?

level: seniorimportance: should knowfreq 35%

basics

~20 s

For object branches, Java finds a common parent type both branches share — roughly their nearest shared supertype. If one branch is null, the result takes the other branch's type. The result is the most specific type that can hold either value.

open as a page

As a tech lead, how would you set a team policy around operator precedence to prevent subtle bugs, and where do you draw the line between relying on the table and using explicit parentheses?

level: principalimportance: nice to knowfreq 25%

basics

~20 s

Let obvious arithmetic precedence stand (a * b + c), but require parentheses whenever you mix different operator families — especially bitwise with comparison, or assignment/ternary in chains. Back it with linter rules so it's automatic, not memory.

open as a page

showing 31–40 of 40