โ† All HACKERRANK Flashcard Decks

Scalar Types and Operators 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 Scalar Types and Operators flashcards as text
  1. What is the output of the following Python code snippet? x = 15 y = 4 result = x // y + (x % y) print(result)

    Answer: 6

    The floor division operator `//` calculates the quotient, discarding the remainder. `15 // 4` results in `3`. The modulo operator `%` calculates the remainder of the division. `15 % 4` results in `3`. The expression then becomes `3 + 3`, which equals `6`.

  2. Which of the following correctly describes the `None` object in Python?

    Answer: It is a keyword used to signify the absence of a value and its type is `NoneType`.

    In Python, `None` is a special constant that represents the absence of a value or a null value. It is not the same as `0`, `False`, or an empty string. `None` is the sole value of the `NoneType` data type, and it is immutable.

  3. A developer needs to ensure a function is only called if a list is not empty to avoid an `IndexError`. Which code snippet uses short-circuiting to achieve this safely? `my_list = []` `def process_list(data): print(data[0])`

    Answer: `if my_list and process_list(my_list): pass`

    The `and` operator in Python uses short-circuit evaluation. It evaluates the expression from left to right. If the first operand (`my_list`) is falsy (an empty list is falsy), it stops and does not evaluate the second operand (`process_list(my_list)`), thus preventing the `IndexError`. The other options would either still call the function or use incorrect logic.

  4. What is the value of the variable `z` after this code executes? `z = 10 + 4 * 2 ** 3 / 8 - 1`

    Answer: 13.0

    Python follows the PEMDAS/BODMAS order of operations. The exponentiation `2 ** 3` is calculated first (8). Then, multiplication and division are performed from left to right: `4 * 8` is 32, and `32 / 8` is 4.0. Finally, addition and subtraction are performed: `10 + 4.0` is 14.0, and `14.0 - 1` is 13.0.

  5. In Python, boolean values `True` and `False` are instances of which scalar type?

    Answer: `bool`

    The values `True` and `False` are literals of the `bool` data type. While they can behave like the integers `1` and `0` in arithmetic contexts, their fundamental type is `bool`.

  6. Given the expression `result = 5 > 3 < 4`, what is the final value of `result`?

    Answer: True

    Python allows for chaining comparison operators. The expression `5 > 3 3) and (3 < 4)`. Both of these comparisons are `True`, and `True and True` results in `True`.