Wednesday, August 5, 2015

Programs on Expressions



Expressions & Operator Precedence

Q1. What is the output of the following code snippet?
int a, b, x = 5;
a = ++x;
b = x++;
System.out.println("a: " + a);
System.out.println("b: " + b);       
System.out.println("x: " + x);
A1.
a: 6
b: 6
x: 7
Explanation:
a = ++x = ++5 = 6 (x is now 6)
b = x++ = 6++ = 6 (x is now 7)
Q2. What is the output of the following code snippet?
int a, b, x = 5;
a = x++;
b = ++x;
System.out.println("a: " + a);
System.out.println("b: " + b);       
System.out.println("x: " + x);
A2.
a: 5
b: 7
x: 7
Explanation:
a = x++ = 5++ = 5 (x is now 6)
b = ++x = ++6 = 7 (x is now 7)
Q3. What is the output of the following code snippet?
int a, x = 5;
a = x++ + ++x;
System.out.println("a: " + a);
System.out.println("x: " + x);
A3.
a: 12
x: 7
Explanation:
a = x++ + ++x = 5++ + ++x = 5 + ++6 = 5 + 7 = 12
Q4. What is the output of the following code snippet?
int a, b, x = 5, y = 4;
a = x++ + ++y;
b = ++x + --y;
System.out.println("a: " + a);
System.out.println("b: " + b);
A4.
a: 10
b: 11
Explanation:
a = x++ + ++y = 5++ + ++y = 5 + ++y = 5 + ++4 = 5 + 5 = 10 (x is now 6, y is now 5)
b = ++x + –y = ++6 + –y = 7 + –5 = 7 + 4 = 11 (x is now 7, y is now 4)
Q5.
int a, x = 12, y = 6;
a = x++ % ++y;
System.out.println("a: " + a);
A5.
a: 5
Explanation:
a = x++ % ++y = 12++ % ++y = 12 % ++6 = 12 % 7 = 5
Q6. What is the output of the following code snippet?
        int a = 3, b = 4, c = 5;
        int x = a+b * c;
        System.out.println("x: " + x);
A6.
x: 23
Explanation:
x=a+b * c = a+(b*c) = a+(4*5) = a+20=3+20=23
Q7. What is the output of the following code snippet?
int a = 3, b = 4, c = 7, d = 8;
int x = a*b+c*d;
System.out.println("x: " + x);
A7.
x: 68
Explanation:
x=a*b+c*d=(a*b)+(c*d) = (3*4)+(c*d) = 12+(7*8)=12+56=68
Q8. What is the output of the following code snippet?
int a = 3, b = 4;
int x = a++*++b+a++*++b;
System.out.println("x: " + x);
A8.
x: 39
Explanation:
x=a++*++b+a++*++b = (a++*++b)+(a++*++b)
= (3++*++4)+(a++*++b)=(3*5)+(a++*++b) [witha =4, b=5]
= 15+(4++*++5) = 15+(4*6) = 15+24=39
Q9. What is the output of the following code snippet?
int a = 3, b = 4;
int x = a>b?a:b;
System.out.println("x: " + x);
A9.
x: 4
Explanation: This is a ternary operator. The condition a>b is false, and hence the second value (b=4) gets returned.
Q10. What is the output of the following code snippet?
int a = 3, b = 4, c=5;
int x = c*a>b?a:b;
System.out.println("x: " + x);
A10.
x: 3
Explanation: As per the operator precedence table, * has more priority over ?:. Therefore,
x = c*a>b?b:a = (c*a)>b?a:b = (5*3)>4?3:4 = (15 > 4)?3:4 = (true) ? 3:4 = 3
Q11. What is the output of the following code snippet?
int a = 3, b = 4, c=5;
int x = c+a>b?a:b;
System.out.println("x: " + x);
A11.
x: 3
Explanation: As per the operator precedence table, + has more priority over ?:. Therefore,
x = c+a>b?b:a = (c+a)>b?a:b = (5+3)>4?3:4 = (8 > 4)?3:4 = (true) ? 3:4 = 3
Q12. What is the output of the following code snippet?
int p = 3, q = 4, r=5, s=5;
r += p>q?p:q; // (1)
s = s+p>q?p:q; // (2)
System.out.println("r: " + r);
System.out.println("s: " + s);
A12.
r: 9
s: 3
Explanation:
(1) The = and shortcut operators (+=. /=, -=, *=, etc) are the only operators that have a lower precendence than the ternary operator ?:. Hence, the += happens last. So:
r += p>q:p:q results in p>q?p:q being evaluated first.
p>q?p:q = 3>4?3:4 = false?3:4 = 4
Then, r+=4 means r=r+4=5+4=9
(2) s=s+p>q?p:q. Here, + has a higher precendence than ?:. So, s+p happens first which is 5+3=8. Then, 8>q?p:q which is 8>4?3:4 which is true?3:4 which is 3. Hence, s=3.
Q13. What is the output of the following code snippet?
int p = 3, q = 4, r=5, s;
s = ++p%--q + ++r/--q;
System.out.println("s: " + s);
A13.
s: 4
Explanation:
s = ++p%–q + ++r/–q = Part-A + Part-B
Part-A = ++p%–q = ++3 % –4 = 4 % 3 = 1 (and p is now 4, q is now 3)
Part-B = ++r / –q = ++5 / –3 = 6 / 2 = 3
s = Part-A + Part-B = 1 + 3 = 4
Q14. What is the output of the following code snippet?
int p = 15, q = 12, s;
s = p/q*++p%(++p%--q);
System.out.println("s: " + s);
A14.
s: 4
Explanation:
s = p/q*++p%(++p%–q)
First, p/q = 15/12 = 1
This gets multiplied by ++p%(++p%–q)
= ++15 % (++p%–q)
= 16 % (++p%–q) (with p now 16)
= 16 % (++16 % –12)
= 16 % (17 % 11) = 16 % 6 = 4
So, the answer s = 1 * 4 = 4
Q15. What is the output of the following code snippet?
int x=4, y=4;
x+= ++x + ++x;
y *= ++y + ++y;
System.out.println(x + ", " + y);
Q15.
15, 44
Explanation: As per the precedence table, the shortcut operators have the lowest priority and hence execute last. However, when executed, the value to be used is the original value of that shortcut operator and not the newly modified one. Therefore:
x += ++x + ++x becomes
x = x + (++x + ++x) = 4 + (++4 + ++x) = 4 + (5 + ++5) = 4 + (5 + 6) = 4 + 11 = 15
and
y *= ++y + ++y becomes
y = y * (++y + ++y) = 4 * (++y + ++y) = 4 * (++4 + ++y) = 4 * (5 + ++5) = 4 * (5 + 6) = 4 * 11 = 44


No comments:

Post a Comment