Hackerrank Scalar Types and Operators 2 โ Questions and Answers
Question 1: What is the result of `7 // 2` in Python?
- 3 (Correct answer)
- 3.5
- 4
- 2
Correct answer: 3
The `//` operator performs floor division, truncating the decimal portion and returning an integer.
Question 2: Which of the following correctly converts the string `'42'` to an integer?
- int('42') (Correct answer)
- integer('42')
- str(42)
- float('42')
Correct answer: int('42')
The built-in `int()` function converts a string representation of a whole number to an integer.
Question 3: What does `type(3.0)` return?
- <class 'float'> (Correct answer)
- <class 'int'>
- <class 'double'>
- <class 'decimal'>
Correct answer: <class 'float'>
Any number written with a decimal point is treated as a `float` in Python, and `type()` returns the class object.
Question 4: What is the value of `True + True + False`?
- 2 (Correct answer)
- 1
- True
- Error
Correct answer: 2
In Python, `bool` is a subclass of `int`; `True` equals `1` and `False` equals `0`, so the sum is `2`.
Question 5: What does the expression `not (3 > 2)` evaluate to?
- False (Correct answer)
- True
- None
- Error
Correct answer: False
`3 > 2` is `True`, and `not True` returns `False`.
Question 6: Which operator is used for exponentiation in Python?
- ** (Correct answer)
- ^
- ^^
- exp
Correct answer: **
Python uses `**` for exponentiation; `^` is the bitwise XOR operator.
Question 7: What is the result of `10 % 3`?
- 1 (Correct answer)
- 3
- 0
- 3.33
Correct answer: 1
The modulo operator `%` returns the remainder after division; `10 รท 3` has remainder `1`.
What is the result of `7 // 2` in Python?