You are on page 1of 2

National Public School, Koramangala 

Term 1-2023-24
Computer Science Worksheet - 1 
                                                                                                                                                     
Grade:10 
Topic: While – Do While -- Nested For Loop

Learning Objective: 1. Learn the use of while, do while and nested for loop.
2. Write programs using the while, do while and nested for loop
_______________________________________________________________________________________

1. Select option(s) which are syntactically correct.

i) int i =0 iii) for(int j==5;j<=7;j++)


ii) for(int j=3;j>=0; j-1)
for( ;i<=5;i++)
{ ; }
{ ; }
{ }

2. Identify 2 block of codes from the options given below which produce the same output.
a.
a) for(int i=1;i<5;i++)

{ System.out.println(2*i+1);}

b) for(int i=3;i<=9;i=i+2)

{ System.out.println(i);}

c) for(int i=1;i<=9;i=i+2)

{ System.out.println(i);}

3. Tick the option(s) where the loop terminates without even executing once.
a) for (int k=0;k>=10; k--) {System.out.println(“Hello”); }
b) for (int k=0; k<0; k++) { System.out.println(“Hello”); }
c) for (int j=0;j>=0;j++); { System.out.println(“Hello”); }

1
4. Write the outputs for the following code snippets:

a) b)
int i = 0; int k = 1;
while (i <= 5) { do {
System.out.println(i); System.out.print(k + " ");
k += 2;
i++;
} while (k < 10);
}

c) int x = 10; d) for (int i = 1; i <= 3; i++) {


while (x > 0) { for (int j = 1; j <= 3; j++) {
System.out.print(x + " "); System.out.print(i + "" + j + " ");
x -= 3; }
} }

5. Write a program to generate the following output.

1
31
531
7531
97531

6. Identify the errors in the below codes. How can it be rectified?


a) b)
int i = 1; int x = 10;
while (i <= 5) { do {
System.out.println("Hello!"); System.out.println(x);
} } while (x < 5);

*********

You might also like