Basic Python Quiz
- Last Updated On
- ByScientech Easy
- InPython
- Read Time8 mins
1. What are Python tokens?
A type of data structure in Python
A built-in function in Python
The smallest individual units in a Python program
A type of exception in Python
c
Tokens are the basic building blocks of a Python program. They include keywords, identifiers, literals, operators, and punctuation symbols. For example, in the statement x = 10, the tokens are x, =, and 10.
2. Which of the following is not a token in Python?
Keywords
Identifiers
Statements
Literals
c
Statements are made up of tokens, but they are not tokens themselves. For example, x = 10 is a statement composed of the tokens x, =, and 10.
3. What is the purpose of identifiers in Python?
To represent reserved words
To name variables, functions, or classes
To define operators
To represent special symbols
b
Identifiers are user-defined names used to identify variables, functions, classes, or other objects. For example, in count = 5, count is an identifier.
4. Which of the following is a valid identifier in Python?
2variable
_variable
variable-name
class
b
_variable is valid because it starts with an underscore.
5. What is the maximum length of an identifier in Python?
32 characters
64 characters
79 characters
No fixed limit
d
Python does not impose a fixed limit on the length of identifiers, but it is recommended to keep them concise and meaningful.
6. Which of the following is a keyword in Python?
main
if
return
Both (b) and (c)
d
if and return are keywords in Python.
7. How many tokens are in the following code?
x = 10 + 5 print(x)
7
8
9
5
c
Tokens: x, =, 10, +, 5, print, (, x, )
8. Which symbol is used for single-line comments in Python?
//
#
/*
—
b
The # symbol is used for single-line comments. Everything after # symbol on a line is ignored by the Python interpreter.
9. How do you write a multi-line comment in Python?
Using # on each line
Using triple quotes (”’ or “””)
Using /* and */
Both (a) and (b)
d
In Python, you can use # for single line comment and triple quotes (”’ or “””) for multi-line comments.
10. Which of the following is a string literal?
123
“Scientech Easy”
True
3.14
b
A string literal is enclosed in quotes. “Scientech Easy” is a string literal, while 123, True, and 3.14 are numeric and boolean literals.
11. Which of the following statement is true in Python?
Identifiers in Python can start with a digit.
2var and var2 are valid identifiers in Python.
Docstrings are a type of comment in Python.
Variable and variable are different identifiers in Python.
d
Variable and variable are different identifiers in Python because Python tokens are case-sensitive.
12. What happens if you write a comment inside triple quotes?
It is treated as a comment.
It causes an error.
It is treated as a string.
It causes an error.
c
Triple quotes (”’ or “””) are used for multi-line strings. If you do not assign them to a variable, they are ignored, but they are not technically comments.
13. Which of the following lines of code will raise an error?
a) 2name = "Python" b) name_2 = "Python" c) print( [ ] == [ ] )
a)
b)
c)
None of them will raise an error.
a
Line a) will raise an error because identifiers cannot start with a digit.
14. Identify the literals in the following code.
name = "Alice" age = 25 is_student = True
“Alice”
25
True
All
d
Literals: “Alice”, 25, True.
15. Which escape sequence is used to represent a newline in Python?
\t
\n
\r
\b
b
The \n escape sequence is used to insert a newline in a string.
16. What is the output of the following code?
print("Hello\tWorld")
HelloWorld
Hello\tWorld
Hello World
Error
c
The \t escape sequence inserts a tab space between Hello and World.
17. Which escape sequence is used to represent a backslash in Python?
\\
\/
\b
\s
a
The \\ escape sequence is used to insert a literal backslash in a string.
18. What is the output of the following code?
print("This is a backslash: \\")
This is a backslash: \\
This is a backslash:
This is a backslash: \
This is a backslash: /
c
The \\ escape sequence is interpreted as a single backslash in the output.
19. What is the output of the following code?
print("Hello\rWorld")
Hello World
World
Hello\rWorld
Hello
b
The \r escape sequence is used to represent a carriage return, which moves the cursor to the beginning of the line. Therefore, World overwrites Hello.
20. Which of the following statement is not correct in Python?
Escape sequences are case-sensitive.
Identifiers cannot start with a digit.
Docstrings are a type of comment in Python.
Comments can be placed on the same line as code.
c
Docstrings are multi-line strings used for documentation, not comments.
21. What is the output of the following code?
x = None print(type(x))
class ‘int’
class ‘str’
class ‘None’
class ‘NoneType’
d
None is a special literal representing the absence of a value. Its type is NoneType.
22. What is the output of the following code?
x = 0b1010 y = 0o12 z = 0xA print(x, y, z)
10 10 10
9 9 9
11 11 11
10 9 10
a
0b1010 is a binary literal representing decimal 10. 0o12 is an octal literal representing decimal 10. 0xA is a hexadecimal literal representing decimal 10.
23. What is the output of the following code?
x = 10 y = "20" print(x + y)
30
10 + “20”
1020
TypeError
d
You cannot add an integer (10) and a string (“20”). This will raise a TypeError.
24. Which of the following is a valid boolean literal?
“True”
true
True
1
c
Boolean literals in Python are True and False. They must be capitalized.
25. Which of the following statement is correct in Python?
String literals must always be enclosed in double quotes.
The literal 0o12 represents a hexadecimal number.
The literal 123.45 is of type float.
The \\ escape sequence is used to insert a forward slash.
c
Any number with a decimal point is a floating-point literal.
26. What is the output of the following code?
x = 10 y = float(x) z = str(y) print(type(z), z)
b
float(x) converts 10 to 10.0. str(y) converts 10.0 to “10.0” (a string).
27. What is the output of the following code?
x = None if x: print("Hello") else: print("World")
Hello
World
TypeError
None of these
b
None evaluates to False in a boolean context.
28. What is the output of the following code?
x = """Python is awesome""" y = x.replace("\n", " ").upper() print(y)
PYTHON IS AWESOME
Python is awesome
PYTHON
IS
AWESOME
IS
AWESOME
Error
a
The variable x is assigned a multi-line string literal. Therefore, it will create a string. The x.replace(“\n”, ” “) method replaces all newline characters (\n) with a space ( ). The upper() method is then called on the modified string, converting all characters to uppercase.
29. What is the output of the following code?
x = 10 y = "20" z = x + int(y) print(z)
30
1020
TypeError
10 + “20”
a
int(y) converts the string “20” to an integer 20. x + int(y) results in 10 + 20 = 30.
30. What is the output of the following code?
x = "Hello\\nWorld" print(x)
Hello
World
World
Hello\nWorld
Hello\\nWorld
Hello\World
b
The \\ escape sequence represents a single backslash (\). The string “Hello\\nWorld” is interpreted as Hello\nWorld, where \n is not treated as a newline but as literal characters.
31. What is the output of the following code?
x = 10 y = "10" print(type(x) == type(y))
True
False
Error
None
b
The type() function returns the type of the variable, and int is not equal to str.
32. What is the output of the following code?
x = "Hello\tWorld" y = x.expandtabs(4) print(y)
Hello World
Hello\tWorld
Hello World
HelloWorld
a
The \t escape sequence represents a tab character. The x.expandtabs(4) method replaces the tab with 4 spaces, resulting in Hello World.
33. What is the output of the following code?
x = True y = False z = not x and y print(z)
True
False
1
0
b
not x evaluates to False because x is True. False and y evaluates to False (logical AND requires both operands to be True).
34. What is the output of the following code?
x = "Hello" # This is a comment y = "World" print(x + y)
Hello World
HelloWorld
Hello # This is a commentWorld
Error
b
The comment after x = “Hello” is ignored by the Python interpreter. The + operator concatenates the strings “Hello” and “World” to produce HelloWorld.
35. What is the output of the following code?
x = r"Hello\nWorld" print(x)
Hello
World
World
Hello\nWorld
Hello\\nWorld
Error
b
The r prefix creates a raw string, where escape sequences will treat as literal characters. \n is not interpreted as a newline but as the literal characters \ and n.