Page 221 - CA_Blue( J )_Class10
P. 221
Previous Years' Questions
SECTION A
1. 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");
} [2017]
(i) How many times does the inner loop execute?
(ii) Write the output of the program segment.
When i = 3:
- The inner loop runs with j starting at 2 and goes while j < 3.
- The values of j will be 2 (the inner loop executes 1 time).
When i = 4:
- The inner loop runs with j starting at 2 and goes while j < 4.
- The values of j will be 2 and 3 (the inner loop executes 2 times).
Total Executions of the Inner Loop:
For i = 3: 1 time
For i = 4: 2 times
Total = 1 + 2 = 3 times
Ans. (i) Loop executes 3 times
(ii) WIN
WIN
2. Study the method and answer the given questions:
public void sampleMethod(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 2; j++){
int number = (int)(Math.random() * 10);
System.out.println(number); }
}
}
(i) How many times does the loop execute?
(ii) What is the range of possible values stored in the variable number? [2014]
Ans. The loop will execute 6 times.
The range is 0 to 9.
3. What is the final value of ctr when the iteration process given below, executes? [2013]
int ctr = 0;
for(int i = 1; i <= 5; i++)
for(int j = 1; j <= 5; j += 2)
++ctr;
Ans. ctr = 15
SECTION B
4. Using the switch statement, write a menu-driven program for the following:
(i) To print the Floyd’s triangle given below:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
219
Nested Loop 219

