KnowledgeBoat Logo

Computer Applications

A student executes the following code to increase the value of a variable ‘x’ by 2.

He has written the following statement, which is incorrect.

x = +2;

What will be the correct statement?

A. x +=2;
B. x =2;
C. x = x +2;

  1. Only A
  2. Only C
  3. All the three
  4. Both A and C

Java Operators

ICSE Sp 2025

4 Likes

Answer

Both A and C

Reason — The student's code x = +2; is incorrect because it doesn't actually increment x by 2. Instead, it simply assigns the value +2 to x.

The correct way to increment the value of x by 2 is:

  1. Option A: x += 2;
    This is shorthand for x = x + 2; and correctly increments x by 2.

  2. Option C: x = x + 2;
    This is the full, explicit version of incrementing x by 2. It is also correct.

Option B: x = 2;
This assigns 2 to x, which is not incrementing its value. Hence, this is incorrect.

Answered By

2 Likes


Related Questions