Menu
Topics Index
...
`

Rearrange the following


If you need explanation Read this topic

If you need Answer Take test on this topic

Rearrange the following
Which order should the lines M, N, O, P be changed such that the value of a is 10 and b is 70 at the end of the program. You can remove any unwanted lines.
class Operators
{
    public static void main(String s[])
    {
        int a = 31, b = 40;
        int c = b - a;
        b = c % a;        // LINE M
        b = b / a;        // LINE N
        a = ++c;        // LINE O
        b = (a - --b) * c;    // LINE P

    }
}

A. M, O, N
B. N, P, M, O
C. P, M, N, O
D. O, N, P
E. N, M, O
Topic: Modulus Operator 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.

Ans is D.
Initially a = 31, b = 40, c = 9
At line O, a will become 10 =>a=10,b=40,c=10
At line N, b becomes b/a = 40/10 = 4
=>a=10,b=4,c=10
At line P, b becomes 70
b = (a - --b)*c = (10 - 3)*10 = 70

a= 10 and b = 70

Posted by Gopinath Manchikanti    2014-10-17 12:39:10


Ans is D. Initially a = 31, b = 40, c = b-a=9 At line O, a will become 10 =>a=10,b=40,c=10 At line N, b becomes b/a = 40/10 = 4 =>a=10,b=4,c=10 At line P, b becomes 70 b = (a - --b)*c = (10 - 3)*10 = 70 a= 10 and b = 70

Posted by Shubham Bansal    2014-10-17 13:09:15


ans d.
this programs goal is to change the values of a and b to 10 and 70.

in main method a b variables declaration and initialized with 31 and 40.

c value assigned with subtraction value of a from b.

in order to get program goal we must use following order. O,N,P.

line m not needed.

o line-
a value is reassigned with pre incremented c value ,so a=10.

n line-
b variable reassigned with b/a .it return quotient value .so b=4.

p line
a- --b= 10-pre decremented b value=10-3=7.
thn b= 7*10=70.

so finally a b values changed using ONP order of line and removing M line.

Posted by Maheshwari Natarajan    2014-10-17 15:33:29


Ans is D..here we have  three local varibles..a,b,c a=31 and b=40
c=b-a ,means c=9 here if we remove the line M than we have 3 statments..
now we have N,O,P.. if we arrange these O,N,P than the statements Like this..

a=++c //O
b=b/a   // n
b = (a - --b) * c; //P

after executing the O a and c values wil be a=10,b=40,c=10

here we we used pre increment which is unary operator..here 1st values is incresed than assigned to a

after executing the N than b value is 3..here b/a--(Int/Int) always result in Integer only so here b wil become b=4..

next here( -,--) unary operators always get high precedense so b wil (10-3)*7 it wil b 70 si b wil become 70 and a 10

Posted by Uday Kumar    2014-10-17 15:52:50


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. 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-10-20 13:03:54


© meritcampus 2019

All Rights Reserved.

Open In App