What will be the output of the following code?
x = [1, 2, 3]
x.append([4, 5])
print(len(x))
append() adds the entire list [4, 5] as a single element. So x becomes [1, 2, 3, [4, 5]] with length 4.
What is the output of: print(2 3 2)?
The operator is right-associative. So 2 3 2 = 2 (3 2) = 2 9 = 512.
What will be the output of:
x = [1, 2, 3]
y = x
x.append(4)
print(y)
y = x creates a reference to the same list, not a copy. When x is modified, y reflects the changes.
Which of the following will raise an IndexError?
A list with 3 elements has valid indices 0, 1, 2. Index 3 is out of range and raises IndexError.
Consider the following code:
x = [1, 2, 3]
y = x[::-1]
print(y)
What will be the output?
The slice notation [::-1] reverses the list. [1, 2, 3] reversed is [3, 2, 1].
Advertisement
What will be the output of: print({1, 2, 3} | {3, 4, 5})?
The | operator performs set union, combining all unique elements from both sets. Result is {1, 2, 3, 4, 5}.
Which of the following will raise a TypeError in Python?
int('10.5') raises TypeError because '10.5' is a string representation of a float, not a valid integer string.
What will be the output of: d = {'a': 1, 'b': 2}; print(list(d.items()))
dict.items() returns key-value pairs as tuples, converted to a list: [('a', 1), ('b', 2)].
Consider: x = [i**2 for i in range(4)]. What is x?
List comprehension: i**2 for i in [0,1,2,3] produces [0, 1, 4, 9].
What will be the output of: print(any([False, False, True]))
any() returns True if at least one element in the iterable is True.
Consider: try: int('abc'); except: print('error'). What will be printed?
int('abc') raises a ValueError. The except block catches it and prints 'error' (without quotes).
What is the output of: print(*[1, 2, 3])?
The * operator unpacks the list. print(*[1, 2, 3]) becomes print(1, 2, 3) which prints '1 2 3' with spaces.
Consider: f = lambda x: x**2. What is f(5)?
Lambda creates an anonymous function. f = lambda x: x2 creates a function that returns x squared. f(5) = 52 = 25.
What will be the result of: set([1, 2, 2, 3, 3, 3])?
Sets remove duplicate elements. set([1, 2, 2, 3, 3, 3]) returns {1, 2, 3}.
Consider: *a, b = [1, 2, 3]. What is a?
The * operator in unpacking collects remaining elements. a gets [1, 2] and b gets 3.
What is the output of: print(eval('2+3*4'))?
eval() evaluates the string as Python code following operator precedence. 2+3*4 = 2+12 = 14.
Consider the code: with open('file.txt') as f: data = f.read(). What happens when the block exits?
The 'with' statement ensures the file is automatically closed when the block exits, even if an error occurs.
What will be the output of: print({x: x**2 for x in range(3)})?
Dictionary comprehension creates {0: 0, 1: 1, 2: 4} where keys are from range(3) and values are their squares.
Consider: class A: x = 5. What is A.x and how can it be modified?
x = 5 is a class variable. It can be accessed via A.x and modified via A.x = new_value or instance modification.
What is the output of: print(type(lambda x: x))?
Lambda functions are functions in Python. type(lambda x: x) returns <class 'function'>.