What is the exact output of the following code segment? int counter; for (counte
ID: 3859156 • Letter: W
Question
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
The ouput of the given program is:
I am WaitingHello Google; What is My Name?
Explanation:
For loop tarts with counter=7; then switch(7%10) which is equals to switch(7), then prints the I ,
now counter=counter+1=7+1=8, switch(8%10) which is equal to switch(8), then it prints am
Same for loop iterates until counter=16.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.