Iterative construct in java theory
Iterative construct in java theory
1
loop. for and while loops are entry-controlled loops.
2
}
4
12. Differentiate fixed and variable iterative type of loops.
Ans: Fixed type of iterative loop is created when the process is to be
repeated for defined number of times. Variable iterative loop repeats the
process till a given condition is true.
13. Distinguish between:
for and while loop
Ans; The for loop is a suitable choice when the number of iterations is fixed,
whereas the while loop is a suitable choice when the number of iterations
is not fixed.
14: State one similarity and one difference between while and do while
loop.
Ans: Similarity: Both the loops are generally used if the number of iterations
are not known.
Difference: The while loop is an entry controlled loop and the do-while is an exit
controlled loop.
15. State one similarity and one difference between while and for loop.
Ans. Similarity: Both are entry controlled loops.
Difference: Initialization, test expression and updation can be written at the
beginning of a for loop, whereas in a while loop, the initialization is written
outside the loop, and the updation inside the body of the loop.
16. Give two differences between step loop and continuous loop.
Ans: Step loop is a system of creating a loop where the control variable is updated
by a given value after each iteration. The loop starts with an initial value, repeats
execution of the statements by updating the value with the given step.
Continuous loop is a type of looping structure in which the control variable is
updated only by 1 after each iteration. The loop is terminated as soon as the
control variable exceeds the last limit.
17. What is the difference between entry controlled and exit controlled
loop?
or
What is the difference between while and do-while loop?
Ans: while loop is known as entry controlled loop and do-while loop is known as
5
exit-controlled loop. The differences between these two loops are: (1) In while
loop the test expression is evaluated at the beginning where as in do-while loop
test expression is evaluated at the bottom, after the body of the loop. (2) In while
loop if the test expression is false loop does not continued but in do-while what
ever the test expression the loop execute at least once.
18. Multiple Choice Questions
1. When the statements are repeated sequentially for a number of times in a
program, the construct is known as:
(a) iteration
(b) sequence
(c) selection
(d) none
2. Which of the following statement is an exit-controlled loop?
(a) for
(b) while
(c) do-while
(d) if-else
3. Which of the following loop does not execute even once if the condition is
false in the beginning?
(a) do-while
(b) while
(c) for
(d) nested loop
4. To execute a loop 10 times, which of the following statement satisfies?
(a) for(i = 6;i <= 26; i = i + 2)
(b) for(i = 3; i <= 30; i = i + 3)
(c) for(i = 0; i < 10; i = i++)
(d) all of the above
5. Which of the following statement uses multiple branches?
(a) loop
(b) continue
(c) switch
(d) break
6. Which of the following loop checks the condition first and then execution
begins?
(a) do-while
(b) do
6
(c) while loop
(d) for
7. To find the sum of whole numbers up to 10, a loop runs:
(a) once
(b) ten times
(c) eleven times
(d) any number of times
8. How many times the loop: for(i = 1;; i++) will execute, if there is no
statement to terminate the loop?
(a) 1
(b) 0
(c) infinite
(d) none
9. Which of the following statements uses a case called default?
(a) do-while
(b) while
(c) switch
(d) all of the above
10. A loop statement is given as:
for(i = 10; i < 10; i++){
statement
}
For how many times will the given loop statement be executed?
(a) never
(b) 1 time
(c) 10 times
(d) infinite