6 Lecture
CS201
Midterm & Final Term Short Notes
Repetition Structure (Loop)
Repetition structure, also known as loops, is a programming construct that allows a program to execute a certain section of code repeatedly, based on a certain condition or set of conditions. Loops enable programmers to write code that performs
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
Which of the following is not a type of loop structure? A) For loop B) While loop C) Repeat loop D) Do-while loop Answer: C) Repeat loop
Which loop structure is best suited for situations where the loop must be executed at least once? A) For loop B) While loop C) Do-while loop D) All of the above Answer: C) Do-while loop
Which loop structure executes its body at least once before evaluating the loop condition? A) For loop B) While loop C) Do-while loop D) All of the above Answer: C) Do-while loop
Which loop structure is typically used when the number of iterations is known in advance? A) For loop B) While loop C) Do-while loop D) All of the above Answer: A) For loop
Which keyword is used to skip the rest of the current iteration in a loop structure? A) Continue B) Break C) Skip D) Exit Answer: A) Continue
Which keyword is used to exit the loop structure immediately? A) Continue B) Break C) Skip D) Exit Answer: B) Break
Which of the following statements is true regarding nested loop structures? A) Nested loop structures are not allowed in most programming languages. B) Nested loop structures can only be used with for loops. C) Nested loop structures can increase the time complexity of a program. D) None of the above. Answer: C) Nested loop structures can increase the time complexity of a program.
Which of the following is an infinite loop? A) for (int i = 0; i < 10; i++) B) while (true) {} C) do { } while (false); D) None of the above. Answer: B) while (true) {}
Which loop structure can be used to iterate over elements of an array? A) For loop B) While loop C) Do-while loop D) All of the above Answer: A) For loop
Which loop structure is best suited for situations where the number of iterations is not known in advance? A) For loop B) While loop C) Do-while loop D) All of the above Answer: B) While loop
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a loop in programming? Answer: A loop is a programming construct that allows a section of code to be executed repeatedly based on a certain condition or set of conditions.
What is the difference between a while loop and a do-while loop? Answer: In a while loop, the loop condition is evaluated before the loop body is executed, whereas in a do-while loop, the loop body is executed at least once before the loop condition is evaluated.
What is the purpose of a break statement in a loop structure? Answer: The break statement is used to immediately exit the loop structure, even if the loop condition has not been met.
What is the purpose of a continue statement in a loop structure? Answer: The continue statement is used to skip the rest of the current iteration of the loop and move on to the next iteration.
What is a nested loop and how does it work? Answer: A nested loop is a loop structure that contains another loop structure within it. The outer loop executes the inner loop multiple times, which can increase the time complexity of the program.
What is the syntax for a for loop? Answer: The syntax for a for loop is: for (initialization; condition; increment/decrement) { loop body }
What is an infinite loop and how is it created? Answer: An infinite loop is a loop structure that runs indefinitely without stopping. It can be created by using a loop condition that always evaluates to true, or by not including a break statement inside the loop body.
What is the difference between a pre-test loop and a post-test loop? Answer: A pre-test loop (such as a for loop or a while loop) evaluates the loop condition before the loop body is executed, whereas a post-test loop (such as a do-while loop) evaluates the loop condition after the loop body is executed.
What is the difference between a for loop and a foreach loop? Answer: A for loop is used to iterate a fixed number of times, whereas a foreach loop is used to iterate over the elements of a collection, such as an array.
How can you break out of multiple nested loops in one statement? Answer: You can use labeled break statements to break out of multiple nested loops in one statement.