What is the output of this Python code?
a = [1, 2, 3] b = a b.append(4) print(a)
Solution
a and b refer to the same list, so appending through b changes a too.
20 technical questions of moderate difficulty, as asked in campus placement and company tests. Each has a short explanation.
What is the output of this Python code?
a = [1, 2, 3] b = a b.append(4) print(a)
a and b refer to the same list, so appending through b changes a too.
What is the output of this Python code?
a = [1, 2, 3] b = a[:] b.append(4) print(a)
a[:] makes a copy, so changes to b do not affect a.
What is the output of this Python code?
print(7 // 2)
// is floor division: 7 // 2 = 3.
What is the output of this Python code?
print(7 / 2)
/ always performs true division in Python 3.
What is the output of this Python code?
print("hello"[::-1])The slice [::-1] reverses a string.
What is the output of this Python code?
print([x * x for x in range(4)])
The list comprehension squares 0, 1, 2 and 3.
What is the output of this Python code?
print(2 ** 3)
** is the power operator: 2 ** 3 = 8.
Which of these data types is immutable in Python?
Tuples cannot be changed after creation; lists, dicts and sets can.
What is the output of this Python code?
print({1, 1, 2, 3, 3})A set keeps only unique elements.
What is the output of this Python code?
def f(x, lst=[]):
lst.append(x)
return lst
print(f(1), f(1))The default list is created once and shared between calls. Both calls return that same list, which holds [1, 1] by the time print runs.
What is the output of this Python code?
print("a" + "b")Strings can be joined with +.
What is the output of this Python code?
d = {"a": 1, "b": 2, "c": 3}
print(len(d))len() of a dictionary counts its keys.
What is the output of this Python code?
a = [1, 2, 3, 4] print(a[1:3])
Slicing [1:3] takes items at index 1 and 2.
Which keyword is used to define a function in Python?
Functions are defined with def.
What is the output of this Python code?
a = None b = None print(a is b)
'is' checks identity. None is a single shared object (a singleton), so a and b refer to the same object.
What is the output of this Python code?
for i in range(3):
print(i, end=" ")range(3) gives 0, 1 and 2.
What does the 'pass' statement do in Python?
pass does nothing; it is used where a statement is required syntactically.
What is the output of this Python code?
print(type(3 + 2.0))
Adding an int and a float gives a float.
Which method adds all elements of another list to the end of a list?
extend() adds each element; append() would add the whole list as a single element.
What is the output of this Python code?
print([1, 2, 2, 3].count(2))
count() returns how many times the item appears.
Want guided preparation? Placement Papers Prep courses by Dr. Kiran Derle, with video lessons and full-length mocks.
See placement courses ↗