โ† All HACKERRANK Flashcard Decks

Python Basics 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 Python Basics flashcards as text
  1. Which of the following creates a set in Python?

    Answer: set()

    `{}` creates an empty dict, so `set()` is the correct way to create an empty set.

  2. What is the output of `list(map(lambda x: x*2, [1, 2, 3]))`?

    Answer: [2, 4, 6]

    `map` applies the lambda to each element, doubling 1, 2, 3 to produce [2, 4, 6].

  3. Which exception is raised when dividing by zero in Python?

    Answer: ZeroDivisionError

    Python raises `ZeroDivisionError` specifically when the divisor is zero.

  4. What does `'abc' * 3` produce in Python?

    Answer: 'abcabcabc'

    String repetition with `*` concatenates the string the given number of times.

  5. Which of the following correctly checks if `x` is NOT in a list `lst`?

    Answer: Both A and B

    Both `x not in lst` and `not x in lst` are valid; `not in` is the idiomatic form.

  6. What is the purpose of `__init__` in a Python class?

    Answer: To initialize object attributes when an instance is created

    `__init__` is the constructor method called automatically when a new object is instantiated.

  7. Which of these is an immutable data type in Python?

    Answer: tuple

    Tuples are immutable; their elements cannot be changed after creation.