โ† 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 result = [i for i in range(10) if i % 3 == 0] print(result) ```

    Answer: [0, 3, 6, 9]

    range(10) produces 0-9; filtering by i%3==0 keeps 0, 3, 6, 9.

  2. What is the output of: ```python i = 0 while i < 3: i += 1 print(i) ```

    Answer: 3

    The loop runs while i < 3, incrementing i each time; after the loop i equals 3.

  3. Which of the following correctly uses `all()` to check that every number in a list is positive?

    Answer: all(x > 0 for x in nums)

    all() with a generator expression returns True only if every element satisfies the condition.

  4. What is the output of: ```python for char in 'abc': if char == 'b': continue print(char) ```

    Answer: a c

    continue skips 'b', so only 'a' and 'c' are printed.

  5. What does this code print? ```python found = False for n in [4, 8, 15, 16]: if n > 10: found = True break print(found, n) ```

    Answer: True 15

    The first n > 10 is 15, which sets found=True and breaks, leaving n as 15.

  6. What is the Pythonic way to loop a fixed number of times without needing the loop variable?

    Answer: for _ in range(n): ...

    Using _ as the loop variable signals that the variable is intentionally unused, which is the Python convention.

  7. What is the output of: ```python for i in range(1, 4): print(i * '*') ```

    Answer: * ** ***

    i * '*' repeats the star i times: '*', '**', '***' for i=1,2,3.