โ† All HACKERRANK Flashcard Decks

Control Flow and Looping Flashcards

6 cards from real HACKERRANK practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Control Flow and Looping flashcards as text
  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='') ```

    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.

  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?

    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.

  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') ```

    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.

  4. Which of the following scenarios is the most appropriate use case for a `for` loop with an `else` block in Python?

    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.

  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('*') ```

    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.

  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') ```

    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.