(32) What is the exact output of the following program? Show your work on the si
ID: 3859210 • Letter: #
Question
(32) What is the exact output of the following program? Show your work on the side.
public class PrintValues {
public static void main (String[] args) {
int x = 25;
PrintValues(x);
System.out.println("------------");
}
public static void PrintValues(int n) {
for (int value = n; value > 0; value = value - 1)
if (value % 4 == 0)
System.out.println("value = " + value);
}
}
(31)
What is the exact output of the following code segment?
int counter;
for (counter = 7; counter <= 16; counter = counter + 1)
{
switch ((counter % 10))
{
case 0: System.out.print("Hello "); break;
case 1: System.out.print("Google; "); break;
case 2: System.out.print("What "); break;
case 3: System.out.print("is "); break;
case 4: System.out.print("My "); break;
case 5: System.out.print("Name"); break;
case 6: System.out.print("?"); break;
case 7: System.out.print("I "); break;
case 8: System.out.print("am "); break;
case 9: System.out.print("Waiting"); break;
default: System.out.print("to hear from you!");
}
}
System.out.println();
Explanation / Answer
32)
value = 24
value = 20
value = 16
value = 12
value = 8
value = 4
------------
Reason:
for (int value = n; value > 0; value = value - 1)
This for loop will continue to execute until the value of the variable is greater than zero.
if (value % 4 == 0)
checking whether the number when divided with 4 the remainder is zero or not.
The if statement will be execute only for the multiples of 4
The value inside the variable value is 25.which when divided by 4 the remainder is not zero
So value=value-1=24
Which when divided by 4 the remainder is zero.
Then the System.out.println("value = " + value); will be executed which displays
Value=24
In the same way 23,22,21 when divided by 4 the remainder is not zero.
Next is 20
Which when divided by 4 the remainder is zero.
Then the System.out.println("value = " + value); will be executed which displays
Value=20
In the same way 19,18,17 when divided by 4 the remainder is not zero.
Next is 16
Which when divided by 4 the remainder is zero.
Then the System.out.println("value = " + value); will be executed which displays
Value=16
In the same way 15,14,13 when divided by 4 the remainder is not zero.
Next is 12
Which when divided by 4 the remainder is zero.
Then the System.out.println("value = " + value); will be executed which displays
Value=12
In the same way it displays
Value=8
Value=4
Next the value 3,2,1 Which when divided by 4 the remainder is not zero.
As the value of the variable is 0 then the control will come out of the loop.
Then the control will goes to the main method
Then the statement will be executed System.out.println("------------");
_____________________
31)
output:
I am WaitingHello Google; What is My Name?
_____________________
Reason:
As 7%10=7
then case 7 will be executed .So it prints "I"
Now the value of couter incremented by 1.So it becomes 8
As 8%10=8
then case 8 will be executed .So it prints "am "
Now the value of couter incremented by 1.So it becomes 9
In this way it printed up to case 10.
later the value of counter will become 10
As 10%10=0
Then case 0 will be executed so the statement System.out.print("Hello "); will be executed
then counter will become 11
11%10= 1
Case 1 will be executed.
In this way the cases upto case 6 will be executed.
____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.