Menu
Topics Index
...
`

Arithmetic Operators Increment Decrement Compound Assignment Output36


If you need explanation Read this topic

If you need Answer Take test on this topic

Arithmetic Operators Increment Decrement Compound Assignment Output36
What will be the output of the following program.
class Addition
{
    public static void main(String s[])
    {
        int a = 4;
        a -= 10;
        a = -a;
        a = 7 + a;
        System.out.println(" a = " + a );
        a %= a;
        a = a * a - 3 ;
        System.out.println(" a = " + a );
    }
}

A. a = 13
a = -3
B. a = 33
a = 163
C. a = 33
a = -3
D. Program does not compile
Topic: Arithmetic Compound Assignment Operators In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

A is the answer a-10 gives -6 then -a gives +6 + 7 ie 13 and next op gives a as -3

Posted by Asad Ahmed    2014-12-22 04:08:30


Answer is A..

Here we have the concept of Arithmetic Compound Assignment operator. These operators are used to combine an arithmetic operation with an assignment operation. For example :

marks += 5 ; will be used instead of
Marks = marks + 5;

The general syntax of the compound assignment operators is

var op= expression;

Coming to the program, initially we have initialized and declared variable 'a' by assigning value 4 to it. Next step a -= 10 leads to a = -10+4 = -6. Now this -6 value stored in a. Next step a = -a leads to a = -(-6) = 6. Now this result stored in a. In final step a = 7+a = 7+6 = 13 and the result stored in a and print method prints the a value to 13. In the next step a %= a leads to a = 13 % 13 = 0 now the result stored in a. In the next step, a = a * a - 3 = 0*0-3 = -3 will be stored and print method will prints the value -3. Final output will be a = 13 and a = -3.

Posted by Sai Ram    2014-12-22 04:21:26


This dose is now closed and the winners are Asad Ahmed,  for 'First Correct Comment', Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-23 04:02:05


© meritcampus 2019

All Rights Reserved.

Open In App