Back to the 2026 paper

Module 1: Introduction to Java

20267m

Consider the following code segment:

int a = 5;
int b = 2;
System.out.println(a++ + ++b * a);

Determine the value printed by the program and show the evaluation sequence according to Java's operator and increment rules.

Worked SolutionAI Assisted

Evaluate the expression

int a = 5;
int b = 2;
System.out.println(a++ + ++b * a);

Java evaluates operands left-to-right; multiplication has higher precedence than addition.

  1. a++ yields the old value 5, then a becomes 6.
  2. ++b increments b from 2 to 3, and yields 3.
  3. The later a is now 6, so 3 * 6 = 18.
  4. Addition gives 5 + 18 = 23.

Output: 23. Final values are a = 6, b = 3.

Similar questions

AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.