English

What does the following program output to the monitor: int value = 0; int count = 1; value = count++; System.out.println("value:" + value "+ count:" + count);

Advertisements

Question

What does the following program output to the monitor:

int value = 0;
int count = 1;
value = count++;
System.out.println("value:" + value "+ count:" + count);
Code Writing
Advertisements

Solution

Initially, value = 0 and count = 1.

The statement value = count++; uses the post-increment operator (++). It assigns the original value of count to value and then increases count by 1.

Therefore,

  • value = 1
  • count = 2

However, the given System.out.println() statement contains a syntax error because the + operator is missing after value.

class PostIncrement
{
    public static void main(String[] args)
    {
        int value = 0;
        int count = 1;
        value = count++;
        System.out.println("value:" + value + " count:" + count);
    }
}

Output:

value:1 count:2

shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Variables and Expressions - EXERCISE [Page 132]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 40. | Page 132
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×