In Java
1. What will be the value of x after the following section of code executes:
int x = 3;
if (x > 3)
x = x – 2;
else
x = x + 2;
A. 1 B.3 C.5 D.7
2. What will be the value of y after the following section of code executes:
int y = 5, z = 3;
if (y > 4){
z = 2;
y = y – 1;
}else
z = 1;
y = z;
A. 1 B.2 C.3 D.4
Thank you for asking the question.
1. Value of x after the given code executes is 5.
Explaination:- As initially x=3.Therefore else part will execute rather than "if part" because x is not greater than 3. So the final value of x is x=x+2 => 3+2=5. So,option c is correct.
2. Value of y after executing given code is 2.
Explaination:- As initially y=5 and z=3. Therefore y>4, that's why " if " part will execute.
So, on executing "if" part, z will be 2 and y=y-1 =>y=5-1=4.
At last, y=z and z is equal to 2. So , y is assigned as 2.
Correct answer is option B. 2
# Thank you for asking the question.
# Feel free to ask any doubt and once again thank you very much.
Get Answers For Free
Most questions answered within 1 hours.