Question

QUESTION 8 What is the exact output of the following nested loop? for (int row = 2; row<10; row += 4){ int column=1; while (c
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Given code

for(int row=2;row<10;row+=4){
   int column=1;
   while(column<=4){
   System.out.print(row-column+"");
column+=2;
   }
System.out.println();
}

out of all the 4 given options for the above code the 3rd option is correct that is

1 -1

5 3

Because

Initally row=2 and row<10 it is true then column=1 and column <=4 it is also true so it prints row-column as 2-1=1.

column increment by 2 so column value=3 and it is less than 4 so again it prints row-column as 2-3=-1

then column value incremented by 2 and becomes 5 so the condition in while loop is not true so it terminates.

System.out.println()nprints a new line. again the row value incremented by 4

row=2+4=6 and row<10 it is true then column=1 and column <=4 it is also true so it prints row-column as 6-1=5.

column increment by 2 so column value=3 and it is less than 4 so again it prints row-column as 6-3=3

then column value incremented by 2 and becomes 5 so the condition in while loop is not true so it terminates.

System.out.println()nprints a new line. again the row value incremented by 4

row=6+4=10 and the condition row<10 becomes false loop terminated.

so finally the exact output is

1 -1

5 3

Tested output image is attached below

C. C:\Windows\system32\cmd.exe - O X C:\Users\Kumar Reddi\Desktop>javac op.java C:\Users\Kumar Reddi\Desktop>java op 1 -1 5

Add a comment
Know the answer?
Add Answer to:
QUESTION 8 What is the exact output of the following nested loop? for (int row =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT