Computer Science, asked by sonty1007, 1 year ago

What will be the output of the following Java code snippet? Test(int x) switch (x % 2) case 0: System.out.print("Odd"); break; case 1: System.out.print("Even"); break; Test n = new Test(2); a. Odd b. Even c. Error d. OddEven

Answers

Answered by paulaiskander2
2

"Test" method takes an integer and performs a switch statement on the integer modulus '2'. Basically, a modulus operator gets the remainder after the division of two numbers.

In this code, the user creates a Test object, passing the integer 2 into the arguments in this line: Test n = new Test(2).

Therefore, the operation of 2%2 will be performed. 2%2 gives zero, because it has no remainder. Therefore, case 0 will be executed.

Therefore, the program will print out the string "Odd".

So the correct answer is a) Odd.

Answered by topanswers
1

Given:

Test(int x)

switch (x % 2)

case 0: System.out.print("Odd");

break;

case 1: System.out.print("Even");

break;

To find:

Test n = new Test(2);

Solution:

As the cases passes 2,

The ( x % 2 ) returns the remainder of the integer x.

Hence,

2 % 2 = 0

So, it enters case 0.

Case 0 functions by printing ( "Odd" )

The program prints "Odd" as it enters in the switch case 0.

Hence, ( a ) Odd is the answer.

Similar questions