All Exams  >   Software Development  >   Basics of Python  >   All Questions

All questions of Loops for Software Development Exam

What will be the output of the following code snippet?
for i in range(5):
    if i == 2:
        break
    print(i, end=" ")
else:
    print("Done")
  • a)
    0 1 2 Done
  • b)
    0 1
  • c)
    0 1 2
  • d)
    0 1 2 3 4 Done
Correct answer is option 'A'. Can you explain this answer?

The code is incomplete and will result in a syntax error. The code is missing the closing parentheses for the "end" parameter in the print statement.

The correct code should be:

```python
for i in range(5):
if i == 2:
break
print(i, end='')
```

With the correct code, the output will be:
```
01
```
1 Crore+ students have signed up on EduRev. Have you? Download the App

What is the output of the following code?
for i in range(2, 6):
    if i % 2 == 0:
        continue
    print(i, end=" ")
  • a)
    2 4
  • b)
    2 3 4 5
  • c)
    3 5
  • d)
    2 3 4
Correct answer is option 'C'. Can you explain this answer?

The code you provided is incomplete. The line `print(i, end=` is missing the closing parenthesis and the desired output is not specified. Please provide the complete code and specify the desired output so that I can assist you further.

What is the output of the following code?
for i in range(5):
    if i == 2:
        continue
    print(i, end=" ")
else:
    print("Loop completed")
  • a)
    0 1 3 4 Loop completed
  • b)
    0 1 2 3 4 Loop completed
  • c)
    0 1 2 3 4
  • d)
    Loop completed
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The loop iterates over the range range(5), which generates the numbers 0, 1, 2, 3, and 4. When i is equal to 2, the continue statement is encountered, skipping the print statement for 2. The else block is executed after the loop completes, printing "Loop completed."

What is the output of the following code snippet?
for i in range(5):
    if i == 3:
        pass
    print(i, end=" ")
  • a)
    0 1 2 3 4
  • b)
    0 1 2 4
  • c)
    0 1 2 3
  • d)
    0 1 2 3 4 4 4 ...
Correct answer is option 'A'. Can you explain this answer?

The code snippet provided is incomplete and contains a syntax error. The "end=" argument of the print() function is missing its value.

To provide a complete answer, the code snippet should be modified as follows:

```python
for i in range(5):
if i == 3:
pass
print(i, end=" ")
```

With this modification, the output of the code snippet will be:

```
0 1 2 3 4
```

How many times will the following loop execute? for i in range(5):
  • a)
    0
  • b)
    4
  • c)
    5
  • d)
    6
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The range(5) generates a sequence of numbers from 0 to 4 (5 numbers). The loop will execute five times, once for each number in the range.

What is the output of the following code?
num = 10
for i in range(2, num):
    if num % i == 0:
        print("Not a prime number")
        break
else:
    print("Prime number")
  • a)
    Not a prime number
  • b)
    Prime number
  • c)
    Not a prime number Prime number
  • d)
    No output
Correct answer is option 'B'. Can you explain this answer?

The code is incomplete as it does not have a closing parenthesis. The correct code should be:

num = 10
for i in range(2, num):
if num % i == 0:
print(i)

The output of this code would be:
2
5

This is because the code is checking for factors of 10. It starts at 2 and checks if 10 is divisible by 2, which it is. Therefore, it prints 2. It then continues to check if 10 is divisible by 3, 4, 5, 6, 7, 8, or 9, but it is not divisible by any of these numbers. So, it does not print any additional numbers.

Which statement is used to exit a loop prematurely in Python?
  • a)
    next
  • b)
    break
  • c)
    pass
  • d)
    continue
Correct answer is option 'B'. Can you explain this answer?

Explanation:

The correct statement used to exit a loop prematurely in Python is the break statement.

Break Statement:
The break statement is used to exit or terminate a loop prematurely. When the break statement is encountered inside a loop, the loop is immediately terminated, and the program execution continues with the next statement after the loop.

Usage:
The break statement can be used with the following loop structures in Python:
- for loop
- while loop

Example:
Let's consider an example where we want to print numbers from 1 to 5, but exit the loop if the number 3 is encountered. We can achieve this using the break statement as follows:

```
for i in range(1, 6):
print(i)
if i == 3:
break
```

Explanation of the Example:
- The for loop is used to iterate over the numbers from 1 to 5 (inclusive).
- Inside the loop, we print the current value of the variable i.
- We check if the value of i is equal to 3 using the if statement.
- If the condition is true, we encounter the break statement, which terminates the loop immediately.
- As a result, the program execution continues with the next statement after the loop.

Output:
The above example will output the following:
```
1
2
3
```

Conclusion:
The break statement is used to exit a loop prematurely in Python. It is useful when we want to terminate a loop based on a certain condition.

What is the output of the following code snippet?
for i in range(3):
    for j in range(3):
        if j == 1:
            break
        print(i, j)
  • a)
    0 0
  • b)
    0 1
  • c)
    0 0 1 1 2 2
  • d)
    0 0 0 1 1 1 2 2 2
Correct answer is option 'A'. Can you explain this answer?

Explanation:

The code snippet provided uses nested loops to iterate over the values of `i` and `j`. The outer loop runs three times, while the inner loop runs three times for each iteration of the outer loop.

Iteration 1:
- `i = 0`
- Inner loop:
- `j = 0`: The condition `j == 1` is not satisfied, so the code inside the inner loop is executed.
- Output: `0 0`
- `j = 1`: The condition `j == 1` is satisfied, so the `break` statement is executed, causing the inner loop to terminate.

Iteration 2:
- `i = 1`
- Inner loop:
- `j = 0`: The condition `j == 1` is not satisfied, so the code inside the inner loop is executed.
- Output: `1 0`
- `j = 1`: The condition `j == 1` is satisfied, so the `break` statement is executed, causing the inner loop to terminate.

Iteration 3:
- `i = 2`
- Inner loop:
- `j = 0`: The condition `j == 1` is not satisfied, so the code inside the inner loop is executed.
- Output: `2 0`
- `j = 1`: The condition `j == 1` is satisfied, so the `break` statement is executed, causing the inner loop to terminate.

Final Output:
The code snippet will output the following values:
- `0 0`
- `1 0`
- `2 0`

Therefore, the correct answer is option 'A'.

What happens if the condition in a while loop is initially False?
  • a)
    The loop is skipped entirely.
  • b)
    The loop runs once and then exits.
  • c)
    The loop runs indefinitely.
  • d)
    An error is thrown.
Correct answer is option 'A'. Can you explain this answer?

If the condition in a while loop is initially False:

If the condition in a while loop is initially False, the loop is skipped entirely. The code inside the while loop is not executed, and the program moves on to the next line of code after the while loop.

Explanation:

A while loop is a control flow statement that allows a certain block of code to be executed repeatedly as long as a specified condition is True. The condition is checked before each iteration of the loop, and if it evaluates to False, the loop is terminated and the program continues with the next line of code after the loop.

When the condition in a while loop is initially False, it means that the loop will never execute because the condition is not met from the start. The program skips the entire block of code inside the while loop and moves on to the next line of code.

This behavior is useful when we want to ensure that a loop is executed only when a certain condition is True. If the condition is initially False, it indicates that the loop is not necessary, and the program can proceed without executing the loop.

Example:

Let's consider an example to illustrate this behavior:

```
x = 0

while x > 5:
print("Inside the while loop")
x += 1

print("Outside the while loop")
```

In this example, the condition `x > 5` is initially False because `x` is assigned a value of 0. Therefore, the while loop is skipped entirely, and the program directly moves to the line `print("Outside the while loop")`. As a result, the output will be:

```
Outside the while loop
```

Conclusion:

When the condition in a while loop is initially False, the loop is skipped entirely, and the program continues with the next line of code after the loop. It is important to ensure that the condition in a while loop is appropriately initialized to prevent infinite loops or unnecessary iterations.

What is the output of the following code snippet?
x = 5
while x > 0:
    print(x)
    x -= 1
  • a)
    5 4 3 2 1
  • b)
    1 2 3 4 5
  • c)
    1 2 3 4
  • d)
    5 4 3
Correct answer is option 'A'. Can you explain this answer?

Hamad Al Azizi answered
Explanation:
- Initialization: The variable x is initialized with a value of 5.
- Loop: The while loop runs as long as the value of x is greater than 0.
- Output: Inside the loop, the current value of x is printed and then decremented by 1.
- Termination: The loop continues until x becomes 0.

Step-by-step Execution:
1. x = 5, x is greater than 0, print 5, x becomes 4
2. x = 4, x is greater than 0, print 4, x becomes 3
3. x = 3, x is greater than 0, print 3, x becomes 2
4. x = 2, x is greater than 0, print 2, x becomes 1
5. x = 1, x is greater than 0, print 1, x becomes 0
6. x = 0, exit the loop

Final Output:
The code snippet will output:
5
4
3
2
1
Therefore, the correct answer is option A) 5 4 3 2 1.

What is the output of the following code snippet?
while True:
    print("Hello")
    break
print("World")
  • a)
    Hello World
  • b)
    Hello (infinite repetition)
  • c)
    Hello
  • d)
    World
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Code Explanation:
- The code snippet contains a while loop that will run indefinitely because the condition "True" is always true.
- Inside the loop, the statement "print('Hello')" will be executed repeatedly.

Output:
- The output of the code will be "Hello" printed repeatedly.

How the Output is Achieved:
- The code enters the while loop and prints "Hello" indefinitely.
- However, the loop is terminated using the "break" statement after the first iteration.
- As a result, the "Hello" is printed once before breaking out of the loop.
- After breaking out of the loop, the statement "print('World')" is executed, printing "World" after the "Hello".
Therefore, the final output of the code snippet is:
"Hello
World"

What is the output of the following code snippet?
for i in range(5):
    if i == 3:
        break
    print(i)
else:
    print("Finished")
  • a)
    0 1 2
  • b)
    0 1 2 3 4
  • c)
    0 1 2 3 4 Finished
  • d)
    Finished
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The for loop iterates over the range of numbers from 0 to 4. When i equals 3, the break statement is executed, causing the loop to exit prematurely. Since the else block is associated with the for loop, it is not executed.

What is the output of the following code snippet?
for i in range(3):
    for j in range(3):
        if j == 1:
            continue
        print(i, j)
  • a)
    0 0 1 0 2 0
  • b)
    0 0 1 1 2 2
  • c)
    0 0 1 2 2 2
  • d)
    0 0 1 2
Correct answer is option 'A'. Can you explain this answer?

Explanation:

The code snippet provided consists of nested for loops. The outer loop iterates over the range from 0 to 2 (inclusive), and the inner loop also iterates over the range from 0 to 2 (inclusive).

Iteration 1:
- The outer loop variable `i` is 0.
- The inner loop variable `j` is 0.
- Since `j` is not equal to 1, the `continue` statement is not executed.
- The print statement `print(i, j)` is executed, and the output is `0 0`.

Iteration 2:
- The outer loop variable `i` is 0.
- The inner loop variable `j` is 1.
- Since `j` is equal to 1, the `continue` statement is executed.
- The `continue` statement skips the remaining code in the inner loop and goes to the next iteration of the inner loop.
- Therefore, the print statement is not executed in this iteration.

Iteration 3:
- The outer loop variable `i` is 0.
- The inner loop variable `j` is 2.
- Since `j` is not equal to 1, the `continue` statement is not executed.
- The print statement `print(i, j)` is executed, and the output is `0 2`.

The same process repeats for the next iterations of the outer loop, with `i` incrementing from 0 to 1 and then to 2.

Overall Output:
- The code snippet will produce the following output:
```
0 0
0 2
1 0
1 2
2 0
2 2
```

Therefore, the correct answer is option A: `0 0 0 2 1 0 1 2 2 0 2`.

What is the output of the following code?
for i in range(3):
    for j in range(2):
        print(i + j, end=" ")
  • a)
    0 1 1 2 2 3
  • b)
    0 1 1 2 2 3 3 4
  • c)
    1 2 3 4 5 6
  • d)
    0 1 2 0 1 2
Correct answer is option 'A'. Can you explain this answer?

Hani Al Wahidi answered
The code provided is incomplete and contains a syntax error. The end= parameter is missing a value.

The corrected code is as follows:

```python
for i in range(3):
for j in range(2):
print(i, j, end="")
```

The output of this code will be:

```
00 01 10 11 20 21
```

Each pair of `i` and `j` values will be printed together with no space in between.

What is the output of the following code snippet?
for i in range(1, 6):
    if i == 3:
        continue
    print(i, end=" ")
  • a)
    1 2 3 4 5
  • b)
    1 2 3
  • c)
    1 2 4 5
  • d)
    1 2 4
Correct answer is option 'C'. Can you explain this answer?

Explanation:
- The code snippet provided uses a for loop to iterate through the range of numbers from 1 to 6.
- When the value of i is equal to 3, the continue statement is executed, which skips the remaining code inside the loop for that particular iteration.
- The print statement is used to display the value of i, separated by a space and without a new line character.

Output:
- When the code is executed, the output will be as follows:
- 1 2 4 5
- The number 3 is skipped due to the continue statement, resulting in the sequence of numbers being printed without 3.

What is the output of the following code snippet?
i = 0
while i < 5:
    print(i)
    i += 1
    if i == 3:
        break
else:
    print("Finished")
  • a)
    0 1 2
  • b)
    0 1 2 Finished
  • c)
    0 1 2 3 Finished
  • d)
    0 1 2 3 4 Finished
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The while loop iterates as long as i is less than 5. When i equals 3, the break statement is executed, causing the loop to exit prematurely. Since the else block is associated with the while loop, it is not executed.

What is the output of the following code?
for i in range(5):
    if i == 3:
        break
    print(i)
else:
    print("Loop completed")
  • a)
    0 1 2
  • b)
    0 1 2 Loop completed
  • c)
    1 2 3 Loop completed
  • d)
    Loop completed
Correct answer is option 'A'. Can you explain this answer?

The code provided is incomplete and contains a syntax error. The closing parentheses and quotation mark are missing after the last "print(" statement. Here is the corrected code:

```python
for i in range(5):
if i == 3:
break
print(i)
else:
print("Finished")
```

The output of the corrected code would be:

```
0
1
2
```

The "for" loop iterates over the range from 0 to 4. When "i" equals 3, the "break" statement is executed, which terminates the loop. The "print(i)" statement is then skipped for the value of 3. Since the loop is not completed normally, the "else" block is not executed.

What is the output of the following code?
for i in range(2, 10, 3): print(i, end=" ")
  • a)
    2 5 8
  • b)
    2 3 4 5 6 7 8 9
  • c)
    2 4 6 8
  • d)
    3 6 9
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The range(2, 10, 3) generates a sequence of numbers starting from 2 and incrementing by 3 at each step until less than 10. The loop prints each number in the sequence.

What is the output of the following code?
for i in range(3):
    print(i)
else:
    print("Loop completed")
  • a)
    0 1 2
  • b)
    0 1 2 Loop completed
  • c)
    1 2 3 Loop completed
  • d)
    Loop completed
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The loop iterates over the range range(3), which generates the numbers 0, 1, and 2. The else block is executed after the loop completes, printing "Loop completed."

What is the output of the following code?
i = 1
while i < 5:
    print(i)
    if i == 3:
        break
    i += 1
  • a)
    1 2 3
  • b)
    1 2 3 4
  • c)
    1 2 3 4 5
  • d)
    Infinite loop
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The while loop executes until the condition i < 5 is false. Inside the loop, i is printed, and if it becomes equal to 3, the break statement is encountered, terminating the loop.

What is the output of the following code snippet?
for i in range(5):
    if i == 2:
        break
    print(i)
  • a)
    0 1 2
  • b)
    0 1
  • c)
    0 1 3 4
  • d)
    0 1 3
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The loop iterates over the range of numbers from 0 to 4. When i equals 2, the break statement is executed, causing the loop to exit prematurely.

What is the output of the following code?
num = 5
for i in range(num):
    print(i, end=" ")
    if i == 2:
        break
  • a)
    0 1 2
  • b)
    0 1 2 3 4
  • c)
    0 1 2 0 1 2 0 1 2 0 1 2
  • d)
    0 1 2 0 1 2
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The loop iterates over the range range(num), which generates the numbers 0, 1, 2, 3, and 4. When i is equal to 2, the break statement is encountered, terminating the loop.

What is the output of the following code?
num = 10
if num % 2 == 0:
    print("Even")
else:
    print("Odd")
  • a)
    Even
  • b)
    Odd
  • c)
    10
  • d)
    No output
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code checks if the number num is divisible by 2 using the modulus operator (%). If the remainder is 0, it is an even number and "Even" is printed.

Which loop construct allows the execution of a set of statements at least once, even if the loop condition is false?
  • a)
    for loop
  • b)
    while loop
  • c)
    do-while loop
  • d)
    nested loop
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
In Python, there is no direct construct for a do-while loop. However, we can achieve the same behavior by using a while loop with an initial condition and then checking the loop condition.

How many times will the following loop execute? while True:
  • a)
    0
  • b)
    1
  • c)
    Infinite loop
  • d)
    Depends on the loop condition
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The condition True is always true, so the while loop will continue executing indefinitely unless there is a break statement or an external interrupt.

What is the output of the following code?
num = 5
for i in range(num):
    if i == 3:
        continue
    print(i, end=" ")
  • a)
    0 1 2 3 4
  • b)
    0 1 2 4
  • c)
    0 1 2 3
  • d)
    0 1 2 3 4 5
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The loop iterates over the range range(num), which generates the numbers 0, 1, 2, 3, and 4. When i is equal to 3, the continue statement is encountered, skipping the print statement for 3.

Which loop is ideal to use when the number of iterations is unknown?
  • a)
    for loop
  • b)
    while loop
  • c)
    do-while loop
  • d)
    All of the above
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The while loop is ideal to use when the number of iterations is unknown because it repeatedly executes a block of code as long as a given condition is true.

Chapter doubts & questions for Loops - Basics of Python 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Loops - Basics of Python in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of Python

49 videos|38 docs|18 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev