Multiple choice technology programming languages

What gets printed when the following program is compiled and run? class test { public static void main(String args[]) { int i,j,k,l=0; k = l++; j = ++k; i = j++; System.out.println(i); } }

  1. 1

  2. 3

  3. 2

  4. 0

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

l starts at 0. k = l++ assigns 0 to k then increments l to 1. j = ++k increments k to 1 then assigns 1 to j. i = j++ assigns 1 to i then increments j to 2. So i = 1.

AI explanation

To determine the output of the program, let's go through the code step by step:

  1. Initialize the variables i, j, k, and l to 0.
  2. Assign the value of l to k and then increment the value of l by 1. So, k becomes 0 and l becomes 1.
  3. Increment the value of k by 1 and assign the result to j. So, j becomes 1 and k becomes 1.
  4. Assign the value of j to i and then increment the value of j by 1. So, i becomes 1 and j becomes 2.
  5. Print the value of i which is 1.

Therefore, the output of the program will be 1.

Hence, the correct answer is option A.