Computer Applications
To execute a loop 10 times, which of the following is correct?
- for (int i=11;i<=30;i+=2)
- for (int i=11;i<=30;i+=3)
- for (int i=11;i<20;i++)
- for (int i=11;i<=21;i++)
Answer
for (int i=11;i<=30;i+=2)
Reason — To execute a loop 10 times, the total number of iterations should match 10, based on the condition and increment values. Let's analyse each option:
1. for (int i = 11; i <= 30; i += 2)
- The loop starts at
11
and increments by2
. - The sequence is:
11, 13, 15, 17, 19, 21, 23, 25, 27, 29
. - This loop executes 10 times.
2. for (int i = 11; i <= 30; i += 3)
- The loop starts at
11
and increments by3
. - The sequence is:
11, 14, 17, 20, 23, 26, 29
. - This loop executes only 7 times, so it is incorrect.
3. for (int i = 11; i < 20; i++)
- The loop starts at
11
and increments by1
. - The sequence is:
11, 12, 13, 14, 15, 16, 17, 18, 19
. - This loop executes only 9 times, so it is incorrect.
4. for (int i = 11; i <= 21; i++)
- The loop starts at
11
and increments by1
. - The sequence is:
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
. - This loop executes 11 times, so it is incorrect.
Related Questions
Which of the following is not true with regards to a switch statement?
- checks for an equality between the input and the case labels
- supports floating point constants
- break is used to exit from the switch block
- case labels are unique
Consider the array given below:
char ch[] = {'A','E','I','O', 'U'};
Write the output of the following statements:
System.out.println(ch[0]*2);:
- 65
- 130
- 'A'
- 0
A single dimensional array has 50 elements, which of the following is the correct statement to initialize the last element to 100.
- x[51]=100
- x[48]=100
- x[49]=100
- x[50]=100
Method prototype for the method compute which accepts two integer arguments and returns true/false.
- void compute (int a, int b)
- boolean compute (int a, int b)
- Boolean compute (int a,b)
- int compute (int a, int b)