โ† All HACKERRANK Flashcard Decks

Control Flow and Looping Flashcards

7 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 7 Control Flow and Looping flashcards as text
  1. What is the output of: ```python for i in range(3): print(i) else: print('done') ```

    Answer: 0 1 2 done

    The loop prints 0, 1, 2, and then the else clause prints 'done' because no break occurred.

  2. Which of the following creates an infinite loop?

    Answer: while 1 == 1: pass

    1 == 1 is always True, creating an infinite loop with no break condition.

  3. What is the result of this nested conditional? ```python a, b = 3, 7 if a > b: print('A') elif a == b: print('B') elif a < b: print('C') else: print('D') ```

    Answer: C

    3 < 7 is True, so the third elif branch executes and prints 'C'.

  4. How many times does 'hello' print? ```python i = 10 while i > 0: print('hello') i -= 3 ```

    Answer: 4

    i takes values 10, 7, 4, 1 (all > 0), so 'hello' prints 4 times before i becomes -2.

  5. What does `zip()` return when used with two lists of different lengths in a for loop?

    Answer: It stops at the shorter list's length

    zip() stops producing tuples when the shortest iterable is exhausted.

  6. What is the output of: ```python for x in range(1, 6, 2): print(x, end=' ') ```

    Answer: 1 3 5

    range(1, 6, 2) generates 1, 3, 5 (start=1, stop=6 exclusive, step=2).

  7. Which is the correct way to use a `for` loop to iterate in reverse over a list `lst`?

    Answer: Both A and B are correct

    Both slicing with step -1 and reversed() correctly iterate a list in reverse order.