Engineering Questions with Answers - Multiple Choice Questions
Home » MCQs » Computer Science » MCQs on Repetition Structure – Do Loop Statement
MCQs on Repetition Structure – Do Loop Statement
Programmers use __________ known as loops.
a) Repetition structure
b) Conditional structure
c) Goto
d) Unconditional structure
View Answer
Answer: a
Explanation: Programmers use the repetition structure, referred to more simply as a loop, when they need the computer to repeatedly process one or more program instructions. If and for how long the instructions are repeated is determined by the loop’s condition.
The requirement for repeating the instructions is referred to as the __________
a) Looping condition
b) Conditional statement
c) Iterative statement
d) Initialization statement
View Answer
Answer: a
Explanation: The requirement for repeating the instructions is referred to as the looping condition, because it indicates when the computer should continue “looping” through the instructions.
The requirement for not repeating the instructions is referred to as the __________
a) Loop exit condition
b) Looping condition
c) Conditional statement
d) Iterative statement
View Answer
Answer: a
Explanation: The requirement for not repeating the instructions is referred to as the loop exit condition, because it tells the computer when to exit (or stop) the loop. Every looping condition has an opposing loop exit condition; in other words, one is the opposite of the other.
In a __________ loop the condition is evaluated before the instructions within the loop are processed.
a) Posttest
b) Pretest
c) Conditional loop
d) Unconditional loop
View Answer
Answer: b
Explanation: In a pretest loop, the condition is evaluated before the instructions within the loop are processed. For example,
repeat while balance < 250,000 interest = balance * rate add interest to balance add 1 to number of years end repeat while
In a __________ loop the condition is evaluated before the instructions within the loop are processed.
a) Posttest
b) Pretest
c) Conditional loop
d) Unconditional loop
View Answer
Answer: a
Explanation: In a posttest loop, the condition is evaluated after the instructions within the loop are processed. For example,
repeat interest = balance * rate add interest to balance add 1 to number of years end repeat while balance < 250,000
The __________ is used to code both pretest and posttest loops.
a) Do loop statement
b) For loop statement
c) While loop statement
d) If statement
View Answer
Answer: a
Explanation: The do..loop statement is used to code both pretest and posttest loops. For example,
Pretest:
Do {While | Until} condition loop body instructions, which will be processed either while the condition is true or until the condition becomes true Loop
Posttest:
Do loop body instructions, which will be processed either while the condition is true or until the condition becomes true Loop {While | Until} condition
The __________ in a Do…Loop statement can contain variables, methods, constants, properties and operators.
a) Exit statement
b) Condition
c) Statement
d) Iteration statement
View Answer
Answer: b
Explanation: The condition in a Do . . . Loop statement can contain variables, constants, properties, methods, keywords, and operators; it also must evaluate to a Boolean value. The condition is evaluated with each repetition of the loop and determines whether the computer processes the loop body.
How many times will the MessageBox.Show method in the following code be processed?
intCount =0 Do While intCount > 3 MessageBox.Show("Hello") intCount = intCount + 1 Loop
a) 0
b) 1
c) 3
d) 4
View Answer
Answer: a
Explanation: Since intCount is initialized to zero, the condition in the Do while statement will be false, and since the body of the loop will be executed, only if the condition is true, thus it will not be executed even once. Thus Messagebox.Show method will not be processed even once.
How many times will the MessageBox.Show method in the following code be processed?
intCount =0; Do MessageBox.Show("Hello") intCount += 1 Loop While intCount > 3
a) 0
b) 1
c) 3
d) 4
View Answer
Explanation: Since it is posttest, thus the body of the loop will be executed at least once, even if the condition is false. Thus, here since the condition is false, the body of the loop will be executed once, and thus MessageBox.Show will be processed only once.
The instructions in a __________ loop are always processed at least once, whereas the instructions in a __________ loop might not be processed at all.
a) Posttest, pretest
b) Pretest, posttest
c) Pretest, pretest
d) Posttest, posttest
View Answer
Answer: a
Explanation: The instructions in a posttest loop are always processed at least one, since the condition is checked after the loop body is executed at least one. So even if the condition is false, the loop body is executed at least once. Whereas the instructions in a pretest loop is not processed if the condition is false, since the condition is checked before the body of the loop is executed.