1) int x=5;
int y=x++;
2) int p=0;
p--;
q=(p++)+p;
3) int a=-10, b=++a;
b=++a;
a=++b + --b;
4) int x=1,y=2,z=++x + ++y;
x + =y;
z += y--;
5) double x1 = 1.1;
double x2 = --x1;
x2 = x1-- + x1-- + x1;
6) int m = 10, n = 20, p = 21;
n--;
m += n;
boolean res = (m >= p) || (n <= p) && !(m==n);
Plz find the output of thr follwing sums with a proper method
Answers
Question 1:
int x=5;
int y=x++;
Answer 1:
Given, x=5
therefore, y= x++
Note : x++ is postfix increment (first calculate then increases the value)
so, y= 5++
y=5. (Now x will become 6)
________
Question 2:
int p=0;
p--;
q=(p++)+p;
Answer 2:
- Initially, p=0
- p--; here p remain 0, it will not decrease
- q=(p++)+p (Now p changes to -1)
- q= (-1++)+p. (now p changes to 0 (p++))
- q=-1+0
- q=-1
________
Question 3:
int a=-10, b=++a;
b=++a;
a=++b + --b;
Answer 3:
- Initially, a=-10
- b=++a. (a will immediately change to -9)
- b=-9
- b=++a (again a will change to -8)
- b=-8
- a= ++b + --b
- Here b will first change to -7
- a= -7 + --b
- Now b will change to -8
- a= -7-8 = -15
- a=-15
_____
Question 4:
int x=1,y=2,z=++x + ++y ;
x + =y;
z += y--;
Answer 4:
- Initially, x=1,y=2
z=++x + ++y
z= 2 + 3
z= 5. (Now x=2,y=3)
x+=y
x=x+y
x= 2+3= 5
z += y--;
z= z+ y--
z= 5+ 3 Now y will become 2
z= 8
_______
Question 5:
double x1 = 1.1;
double x2 = --x1;
x2 = x1-- + x1-- + x1;
Answer 5:
- x1= 1.1
- x2= --x1 = --1.1 = 1.0
- x2 = x1-- + x1-- + x1
- x2= 1.0 +0.9 + 0.8
- x2= 2.7
_________
Question 6:
int m = 10, n = 20, p = 21;
n--;
m += n;
boolean res = (m >= p) || (n <= p) && !(m==n);
Answer 6:
- m=10
- n=20
- p=21
- n--;
n will become 19
- m+=n
- m=m+n
- m= 10 +19
- m= 29
boolean res = (m >= p) || (n <= p) && !(m==n) ;
Note: Boolean variable returns the answer in True/False
boolean res = (m >= p) || (n <= p) && !(m==n)
Order of Logical operator is NAO 1.not 2.and 3.or
Therefore,
(m >= p) || (n <= p) && !(m==n)
(29>=21 ) || (19<=21) && !(m==n)
(29>=21 ) || (19<=21) && !(F)
(29>=21 ) || T && T
T || T && T
T|| T
T
Therefore, res will return True.
Note: F= False, T=True