OOPS
August 29, 2024OOPS
August 29, 2024OOPS
|
Question 92
|
What is the output of the following Java program?
Class Test
{
public static void main (String [] args)
{
int x = 0;
int y = 0;
for (int z = 0; z < 5; z++)
{
if((++x > 2) || (++y > 2))
{
x++;
}
}
System.out.println( x + ” ” + y);
}
}
Class Test
{
public static void main (String [] args)
{
int x = 0;
int y = 0;
for (int z = 0; z < 5; z++)
{
if((++x > 2) || (++y > 2))
{
x++;
}
}
System.out.println( x + ” ” + y);
}
}
|
8 2
|
|
|
8 5
|
|
|
8 3
|
|
|
5 3
|
Question 92 Explanation:
Initial values of x and y is 0
For the given code, the loop will execute 5 times.
Step-1: For z = 0,
The following condition will execute
if((++x > 2) || (++y > 2))
Here pre-increment operation will be performed on x and y. Then x and y values are 1 and the condition false
Similar operation will be performed for z values 1,2,3 and 4
Step-2: For z = 1, Then x and y values become 2 and the condition false.
Step-3: For z = 2, “||” operator is present in the expression, if first operand is true then no need to check the second operand.x value is 3, if condition true and ++y is not evaluated,again “x” value is incremented then x value is “4”.
Perform the step-3 procedure for z value 3 and 4 . Here x value is incremented “4” times. Then the final value of “x” is 8 and “y” is 2.
For the given code, the loop will execute 5 times.
Step-1: For z = 0,
The following condition will execute
if((++x > 2) || (++y > 2))
Here pre-increment operation will be performed on x and y. Then x and y values are 1 and the condition false
Similar operation will be performed for z values 1,2,3 and 4
Step-2: For z = 1, Then x and y values become 2 and the condition false.
Step-3: For z = 2, “||” operator is present in the expression, if first operand is true then no need to check the second operand.x value is 3, if condition true and ++y is not evaluated,again “x” value is incremented then x value is “4”.
Perform the step-3 procedure for z value 3 and 4 . Here x value is incremented “4” times. Then the final value of “x” is 8 and “y” is 2.
Correct Answer: A
Question 92 Explanation:
Initial values of x and y is 0
For the given code, the loop will execute 5 times.
Step-1: For z = 0,
The following condition will execute
if((++x > 2) || (++y > 2))
Here pre-increment operation will be performed on x and y. Then x and y values are 1 and the condition false
Similar operation will be performed for z values 1,2,3 and 4
Step-2: For z = 1, Then x and y values become 2 and the condition false.
Step-3: For z = 2, “||” operator is present in the expression, if first operand is true then no need to check the second operand.x value is 3, if condition true and ++y is not evaluated,again “x” value is incremented then x value is “4”.
Perform the step-3 procedure for z value 3 and 4 . Here x value is incremented “4” times. Then the final value of “x” is 8 and “y” is 2.
For the given code, the loop will execute 5 times.
Step-1: For z = 0,
The following condition will execute
if((++x > 2) || (++y > 2))
Here pre-increment operation will be performed on x and y. Then x and y values are 1 and the condition false
Similar operation will be performed for z values 1,2,3 and 4
Step-2: For z = 1, Then x and y values become 2 and the condition false.
Step-3: For z = 2, “||” operator is present in the expression, if first operand is true then no need to check the second operand.x value is 3, if condition true and ++y is not evaluated,again “x” value is incremented then x value is “4”.
Perform the step-3 procedure for z value 3 and 4 . Here x value is incremented “4” times. Then the final value of “x” is 8 and “y” is 2.
