Question
how and why is this the answer as well? thank you

35. What is the output of the following code? for (int j = 0; j < 7; ;=j+2) { for (int k = j; k < 4; k++) System.out.print(h
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Driver Code : (in java)

public class Main
{
   public static void main(String[] args) {
       for(int j=0;j<7;j=j+2)


  {
       for(int k=j;k<4;k++)
       {
       System.out.print("hey");
       }
       System.out.println(".");
      
       }
   }
}

Output :

Explanation :

step 1 : Here, when the control enters the first for loop initially j=0 and here it satisfies the condition j<7 and j will be incremented to 2 and as it satisfies the condition it enters into the second for loop and as of the given condition here it initializes k to j that is k=0 and as 0<4 satisfies the condition so it enters into the loop and prints "hey" and here as k will be incremented to 1 and as again it satisfies the given condition it will print "hey" and it will print "hey" until the condition is false so until k is 4 it will print "hey".Therefore, it will print "heyheyheyhey" until it exits the first for loop and then and it will exit the loop and it will print "." and here it will print "heyheyheyhey."

step 2 : Here as the given function is "println" so here the next statement will print after a "line" .So, here it again enters into the loop and here initially j=2 and here j also satisfies the condition j<7 and again it will again enter into the second for loop here k will be 2 and here as 2<4 so it will print "hey" until the condition k<4 is false .So,it prints "heyhey." and then exists the two loops

step 3 : Here j will be 4 as j=j+2 initially and here as it enters the condtion k<4 doesn't satisfy the condition so it will not enter into the second for loop so here it prints only "."

step 4 : Here j will again increment to 6 and here it again prints "." as it doesn't enter the second for loop and here now j will be 8 as its doesn't enter the first loop because j=8 doesn't satisfy the condition j<7 .So, finally it will exit from the loop by printing the above lines .Thus it will print the output lines.

For any further queries please leave a comment below.

Add a comment
Know the answer?
Add Answer to:
how and why is this the answer as well? thank you 35. What is the output...
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