Showing 1–10 of 47 questions
in Basics & Syntax
Consider the following code snippet:
python
x = [1, 2, 3]
y = x
y.append(4)
print(x)
What will be the output?
A
[1, 2, 3, 4]
B
[1, 2, 3]
C
Error: list object does not support append
D
[4]
Correct Answer:
A. [1, 2, 3, 4]
EXPLANATION
In Python, when you assign y = x, both variables reference the same list object in memory. When you modify the list using y.append(4), the original list x is also modified because they point to the same object. Therefore, print(x) outputs [1, 2, 3, 4].
What is the output of: print(None == False)?
A
True
B
False
C
None
D
Error
EXPLANATION
None and False are distinct objects. None == False evaluates to False. None is a unique singleton object, not equivalent to False or 0.
Consider the code: x = (1, 2, 3); x[0] = 5. What happens?
A
x becomes (5, 2, 3)
B
TypeError is raised
C
x remains (1, 2, 3)
D
IndexError is raised
Correct Answer:
B. TypeError is raised
EXPLANATION
Tuples are immutable. Attempting to modify a tuple element with x[0] = 5 raises a TypeError: 'tuple' object does not support item assignment.
Consider: d = {'a': 1, 'b': 2}; d['c'] = 3. What is d?
A
{'a': 1, 'b': 2}
B
{'a': 1, 'b': 2, 'c': 3}
C
Error
D
None
Correct Answer:
B. {'a': 1, 'b': 2, 'c': 3}
EXPLANATION
Dictionaries are mutable. The assignment d['c'] = 3 adds a new key-value pair to the dictionary. d becomes {'a': 1, 'b': 2, 'c': 3}.
Consider: x = [1, 2, 3, 4, 5]; y = x[1:4]. What is y?
A
[1, 2, 3]
B
[2, 3, 4]
C
[2, 3, 4, 5]
D
[1, 2, 3, 4]
Correct Answer:
B. [2, 3, 4]
EXPLANATION
List slicing x[1:4] includes indices 1, 2, 3 (but excludes 4). Elements are 2, 3, 4 from the list [1, 2, 3, 4, 5].
Consider the code: def func(x, y=5): return x + y. What is func(3)?
EXPLANATION
y has a default value of 5. When func(3) is called, x=3 and y=5 (default). The function returns 3 + 5 = 8.
Consider: a = 'Hello'; b = 'Hello'. Are a and b the same object?
A
Always True
B
Always False
C
Depends on interpreter implementation
D
Cannot be determined
Correct Answer:
C. Depends on interpreter implementation
EXPLANATION
Due to string interning in Python, small strings may be cached and refer to the same object. However, this is an implementation detail. a == b is True, but a is b may vary.
What will be printed: print(3 ** 2 ** 2)?
EXPLANATION
Exponentiation is right-associative. 3 2 2 = 3 (2 2) = 3 ** 4 = 81.
Consider the code: x = [1, 2, 3]; y = x; y.append(4). What is x?
A
[1, 2, 3]
B
[1, 2, 3, 4]
C
[4]
D
Error
Correct Answer:
B. [1, 2, 3, 4]
EXPLANATION
y = x creates a reference to the same list object. When y.append(4) is called, it modifies the original list. Both x and y point to [1, 2, 3, 4].
What will be the output of: print('abc' * 3)?
A
'abc' 'abc' 'abc'
B
'abcabcabc'
C
abcabcabc
D
Error
Correct Answer:
C. abcabcabc
EXPLANATION
String multiplication repeats the string. 'abc' * 3 produces 'abcabcabc' and print displays it without quotes.