Python Core Syntax 2 — Questions and Answers
Question 1: What is the output of `print(type(3/2))` in Python 3?
- <class 'int'>
- <class 'float'> (Correct answer)
- <class 'complex'>
- <class 'decimal'>
Correct answer: <class 'float'>
In Python 3, the `/` operator always performs true division, returning a float even when dividing two integers.
Question 2: Which statement correctly creates a multi-line string in Python?
- s = 'line1\nline2'
- s = '''line1\nline2'''
- s = """line1 line2""" (Correct answer)
- s = (line1, line2)
Correct answer: s = """line1 line2"""
Triple-quoted strings (""" or ''') can span multiple lines and preserve newlines literally.
Question 3: What does the `//` operator do in Python?
- Divides and returns a float
- Performs floor (integer) division (Correct answer)
- Computes the modulus
- Raises to a power
Correct answer: Performs floor (integer) division
The `//` operator performs floor division, rounding the result down to the nearest integer.
Question 4: What is the value of `bool('')` in Python?
- True
- False (Correct answer)
- None
- TypeError is raised
Correct answer: False
An empty string is falsy in Python, so `bool('')` evaluates to `False`.
Question 5: Which of the following is a valid variable name in Python?
- 2count
- my-var
- _total (Correct answer)
- class
Correct answer: _total
Variable names can start with a letter or underscore, making `_total` valid; `2count` starts with a digit, `my-var` contains a hyphen, and `class` is a reserved keyword.
Question 6: What is the result of `'5' + '3'` in Python?
- 8
- 53
- '8'
- '53' (Correct answer)
Correct answer: '53'
The `+` operator on strings performs concatenation, so `'5' + '3'` yields the string `'53'`.
Question 7: Which keyword is used to define an anonymous function in Python?
- def
- func
- lambda (Correct answer)
- anon
Correct answer: lambda
The `lambda` keyword creates a small anonymous function that can have any number of arguments but only one expression.
What is the output of `print(type(3/2))` in Python 3?