Syntax Quiz - Languages Python
Free practice quiz: Syntax (Languages Python). 43 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 43 questions freeSample questions
Which of the following is a valid Python variable name?
- 2_variable
- _my_var
- my-variable
- my variable
Show answer
Answer: B. _my_var
Python variable names can start with a letter or an underscore, followed by letters, numbers, or underscores. They cannot start with a number, contain hyphens, or spaces.
Which of the following is NOT a Python data type?
- float
- boolean
- string
- array
Show answer
Answer: D. array
Python has built-in data types like `float` (for floating-point numbers), `bool` (for boolean values), and `str` (for strings). While Python has lists which are similar to arrays, 'array' itself is not a primary built-in data type in the same way. The `array` module exists for a more C-style array, but 'array' as a general term isn't a fundamental type like `int`, `float`, `str`, `list`, `tuple`, `dict`, `set`.
Which of the following is used to define a class in Python?
- class
- new
- define
- object
Show answer
Answer: A. class
The `class` keyword is used to declare a new class. It is followed by the class name and a colon. The class body, which contains attributes and methods, is indented.
Which of the following is used to create a set in Python?
- `my_set = (1, 2, 3)`
- `my_set = [1, 2, 3]`
- `my_set = {1, 2, 3}`
- `my_set = {1: 'a', 2: 'b'}`
Show answer
Answer: C. `my_set = {1, 2, 3}`
Sets in Python are unordered collections of unique elements and are defined using curly braces `{}`. If you use `{}` with key-value pairs, it creates a dictionary. `()` creates a tuple, and `[]` creates a list.
What is the result of `bool('')`?
- True
- False
- None
- Error
Show answer
Answer: B. False
An empty string (`''`) is considered 'falsy' in Python, so `bool('')` evaluates to `False`.