c2cedge
C, Java & Python · A — Foundations

Operators & Output Prediction

Integer division, increment operators, precedence and type coercion behave differently in each language — and 'predict the output' questions live right here.

Test weight: Very highSkill: Read code preciselyDifficulty: Medium

A large share of MCQ screening rounds are 'what does this print?' questions, and most of them hinge on operators: how division rounds, how increment works, operator precedence, and implicit type conversion. The same expression can give different results across C, Java and Python, so you must read carefully and know the language.

Division is the classic trap

In C and Java, dividing two integers does integer division — it truncates the fractional part, so 5/2 is 2. In Python 3, / always gives a float (5/2 is 2.5), and // is floor division (5//2 is 2).

Division across the three
/* C */    printf("%d", 5 / 2);   // 2  (integer division truncates)
// Java     System.out.print(5 / 2); // 2
# Python    print(5 / 2)             # 2.5  (true division)
# Python    print(5 // 2)            # 2    (floor division)
Pre- vs post-increment (C/Java)
int i = 5;
printf("%d", i++);  // prints 5, then i becomes 6  (post-increment)
int j = 5;
printf("%d", ++j);  // j becomes 6, then prints 6  (pre-increment)
// Python has no ++ operator; use i += 1
⚡ The edge
  • Integer division truncates in C/Java; Python's / returns a float and // floors. This single difference is behind a huge fraction of output-prediction questions — always check the language and the operand types.
  • Post-increment i++ uses the old value then increments; pre-increment ++i increments then uses the new value. Python has no ++ at all — you write i += 1.
Worked example
What does printf("%d", 7 / 2) print in C, and print(7 / 2) in Python?
  1. In C, 7 and 2 are integers, so 7/2 is integer division — it truncates to 3.
  2. In Python 3, / is true division, so 7/2 is 3.5 (a float).
  3. If you wanted 3 in Python, you'd use 7 // 2.
Worked example
In C, what does this print: int i = 5; printf("%d %d", i++, ++i)?
  1. This relies on i++ (post: use 5, then 6) and ++i (pre: increment then use).
  2. The exact result is actually undefined in C because i is modified twice without a sequence point — a known trap.
  3. The interview lesson: know post vs pre, and know that modifying a variable twice in one expression is undefined behaviour in C.
⚠ Watch out
  • 5/2 differs by language: 2 in C/Java, 2.5 in Python (use // for 2).
  • Operator precedence: * and / bind before + and -; use parentheses when unsure.
  • Floating-point isn't exact: 0.1 + 0.2 is not exactly 0.3 in any of these languages.
Practice this — take a timed mock →
1,300+ questions, scored, with a weak-area report.
Know who's ready. Not who finished.
HomeLibraryPrivacyTerms