Output writing and loop counting – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2017-03I #JAVA#7248    siteicon   siteicon  

Problem Statement - Output writing and loop counting

Analyze the given program segment and answer the following questions:

for(int i=3;i<=4;i++ ) {
for(int j=2;j<i;j++ ) {
System.out.print(“” ); }
System.out.println(“WIN” ); }

(i) How many times does the inner loop execute?
(ii) Write the output of the program segment.

Solution

TC++ #7248

Run Output

WIN
WIN

The output loop runs twice, with i = 3 and i = 4
When i = 3, the inner loop runs once with j = 2
When i = 4, the inner loop runs twice with j = 2 and j = 3

Since outer loop runs 2 times and matter printing statement is in the outer loop to the given matter will be printed twice only.

Answer:
i) Inner loop executes 3 times
ii)output:
WIN
WIN


Share

CSKC| Created: 15-Apr-2019 | Updated: 27-Nov-2019|ICSE2017









Back