Hackerrank - HackerRank Python Control Flow and Looping Questions and Answers 1 — Questions and Answers
Question 1: What is the output of the following Python code snippet?
```python
for i in range(5):
if i == 3:
continue
print(i, end=' ')
else:
print('Loop finished', end='')
```
- 0 1 2 4 Loop finished (Correct answer)
- 0 1 2 3 4 Loop finished
- 0 1 2 4
- 0 1 2
Correct answer: 0 1 2 4 Loop finished
The `for` loop iterates from 0 to 4. The `continue` statement is triggered when `i` is 3, which skips the `print(i)` statement for that iteration and proceeds to the next one. The `else` block of a `for` loop executes only when the loop completes its iterations without being terminated by a `break` statement. Since no `break` occurs, 'Loop finished' is printed after the loop concludes.
Question 2: A developer is writing a function but has not yet implemented its logic. To avoid an `IndentationError` for an empty code block, which keyword should be used as a placeholder?
- break
- continue
- pass (Correct answer)
- return
Correct answer: pass
The `pass` statement in Python is a null operation. It is used when a statement is required syntactically but you do not want any command or code to execute. It's a common placeholder for function bodies or conditional blocks that are yet to be implemented.
Question 3: What will the following Python code print to the console?
```python
count = 5
while count > 1:
print(count)
count -= 1
if count == 3:
break
else:
print('Done')
```
- 5
4
3
- 5
4 (Correct answer)
- 5
4
Done
- 5
4
3
2
Done
Correct answer: 5
4
The `while` loop starts with `count` as 5. It prints 5 and then 4. When `count` becomes 3, the `if` condition is met, and the `break` statement is executed. The `break` statement immediately terminates the loop. The `else` block associated with a loop is only executed if the loop finishes normally (i.e., its condition becomes false), not when it is terminated by a `break` statement.
Question 4: Which of the following scenarios is the most appropriate use case for a `for` loop with an `else` block in Python?
- Iterating through a list to perform an action on every element.
- Executing a block of code a fixed number of times.
- Searching for an item in a list and performing an action only if the item is not found. (Correct answer)
- Creating an infinite loop that waits for user input.
Correct answer: Searching for an item in a list and performing an action only if the item is not found.
The `else` block of a `for` loop is executed if and only if the loop completes its entire sequence of iterations without encountering a `break` statement. This makes it ideal for search operations. You can use the `for` loop to look for an item, `break` if it's found, and the `else` block will then only run if the entire list was searched and the item was not found.
Question 5: Analyze the following nested loop. How many times will the `print('*')` statement be executed?
```python
for i in range(3):
for j in range(i + 1):
print('*')
```
Correct answer: 6
The outer loop iterates with `i` taking values 0, 1, and 2.
- When `i` is 0, the inner loop `range(1)` runs once (j=0).
- When `i` is 1, the inner loop `range(2)` runs twice (j=0, 1).
- When `i` is 2, the inner loop `range(3)` runs three times (j=0, 1, 2).
The total number of executions for `print('*')` is 1 + 2 + 3 = 6.
Question 6: Given the following code, what will be the output if the `status_code` is `404`?
```python
status_code = 404
match status_code:
case 200 | 201:
print('Success')
case 401:
print('Unauthorized')
case 404:
print('Not Found')
case _:
print('Unknown Error')
```
- Success
- Not Found (Correct answer)
- Unknown Error
- The code will raise a SyntaxError.
Correct answer: Not Found
The `match-case` statement, introduced in Python 3.10, compares a value against several patterns. The code will evaluate `status_code`, which is `404`. It will skip the first two cases and find a match with `case 404:`. The corresponding block is executed, printing 'Not Found'. The `_` is a wildcard that acts as a default case but is not reached here.