Python Medium
- Get link
- X
- Other Apps
1. What will be the output of the following code?
x = [1, 2, 3]
y = x
y.append(4)
print(x)
A) [1, 2, 3]
B) [1, 2, 3, 4] ✅
C) [4]
D) Error
Explanation: Lists are mutable, and y references the same object as x. So changes in y affect x.
2. Which of the following is not a valid keyword in Python?
A) pass
B) assert
C) eval
D) then ✅
Explanation: then is not a Python keyword.
3. What is the output?
print(bool(0), bool(3.5), bool(-1))
A) False True True ✅
B) False False False
C) True True True
D) Error
Explanation: In Python, 0 is False. Any non-zero value (positive or negative) is True.
4. Which of these data types is immutable?
A) list
B) dict
C) tuple ✅
D) set
Explanation: Tuples are immutable. Lists, dicts, and sets are mutable.
5. What will the following code output?
def f(a, b=2, c=3):
return a + b + c
print(f(1, c=5))
A) 6
B) 8 ✅
C) 10
D) Error
Explanation: a=1, b=2 (default), c=5 (overridden) → 1+2+5 = 8.
6. Which of the following is correct about sets?
A) Sets allow duplicates
B) Sets are ordered
C) Sets are mutable ✅
D) Sets are indexed
Explanation: Sets are unordered and mutable, and they only store unique elements.
7. What is the output?
a = [1, 2, 3]
print(a * 2)
A) [2, 4, 6]
B) [1, 2, 3, 1, 2, 3] ✅
C) [1, 2, 3, 2]
D) Error
Explanation: List multiplication repeats the elements.
8. Function to get dictionary length?
A) count()
B) size()
C) len() ✅
D) length()
Explanation: len(dict) gives number of key-value pairs.
9. Output?
try:
print(10/0)
except ZeroDivisionError:
print("Error")
finally:
print("Done")
A) Error
B) Error
C) Error Done ✅
D) Done Error
Explanation: ZeroDivisionError is caught → prints “Error”. Finally always executes → “Done”.
10. Which is not a valid import?
A) import math
B) import math as m
C) from math import *
D) include math ✅
Explanation: Python uses import, not include.
11. Output?
x = {1, 2, 3}
y = {3, 4, 5}
print(x & y)
A) {1, 2, 3, 4, 5}
B) {3} ✅
C) {}
D) Error
Explanation: & gives intersection → {3}.
12. id() function returns:
A) Identity of an object ✅
B) Memory size
C) RAM address
D) Data type
Explanation: Returns unique identity (usually memory reference).
13. Output?
print("Python"[-2])
A) o ✅
B) n
C) h
D) Error
Explanation: -2 index gives second last char → o.
14. Which creates a generator?
A) (x for x in range(5)) ✅
B) [x for x in range(5)]
C) {x for x in range(5)}
D) tuple(x for x in range(5))
Explanation: Parentheses comprehension creates generator.
15. strip() does what?
A) Removes whitespace at both ends ✅
B) Removes all characters
C) Splits string
D) Removes right spaces only
Explanation: strip() trims leading and trailing whitespace.
16. Function for max in a list?
A) largest()
B) max() ✅
C) biggest()
D) top()
Explanation: max(list) returns maximum value.
17. Output?
for i in range(2, 10, 2):
print(i, end=" ")
A) 2 4 6 8 ✅
B) 2 3 4 5 6 7 8 9
C) 2 10
D) Error
18. Difference between is and ==?
A) No difference
B) is compares values, == compares identity
C) is compares identity, == compares values ✅
D) Both check values
19. Output?
def func(x=[]):
x.append(1)
return x
print(func(), func())
A) [1] [1]
B) [1,1] [1]
C) [1] [1,1]
D) [1,1] [1,1] ✅
Explanation: Default list is reused across calls.
20. Valid decorator?
A) @staticmethod ✅
B) @override
C) @synchronized
D) @virtual
21. Which operator is used for floor division?
A) /
B) // ✅
C) %
D) div
22. Output?
print(type(lambda x: x+1))
A) function
B) lambda
C) <class 'function'> ✅
D) error
23. Which of the following is true for Python’s with statement?
A) Used for loops
B) Ensures proper resource management ✅
C) Used only with files
D) Same as try-except
24. Which function is used to read a single line from a file?
A) readline() ✅
B) read()
C) readlines()
D) line()
25. Output?
a = (1, 2, 3)
a[0] = 5
A) (5,2,3)
B) [5,2,3]
C) Error ✅
D) None
Explanation: Tuples are immutable.
26. Which function gives absolute value?
A) abs() ✅
B) absolute()
C) fabs()
D) mag()
27. What is the output?
print(all([True, 1, "abc"]))
A) False
B) True ✅
C) Error
D) None
Explanation: all() returns True if all elements are truthy.
28. Which one is used for docstring access?
A) help() ✅
B) info()
C) doc()
D) getdoc()
29. Output?
print({i:i**2 for i in range(3)})
A) {1:1, 2:4, 3:9}
B) {0:0, 1:1, 2:4} ✅
C) [0,1,4]
D) Error
30. Which keyword is used to define anonymous blocks?
A) def
B) lambda ✅
C) func
D) block
31. Output?
print(type({}))
A) dict ✅
B) set
C) list
D) tuple
32. Which function is used to convert string to list of words?
A) split() ✅
B) listify()
C) tokenize()
D) explode()
33. Which is false about Python modules?
A) A module is a file with .py extension
B) Modules can contain functions & classes
C) Modules cannot be reused ✅
D) Modules can be imported
34. Output?
a = [1,2,3]
print(a[::-1])
A) [1,2,3]
B) [3,2,1] ✅
C) Error
D) None
35. Function to check datatype?
A) datatype()
B) type() ✅
C) class()
D) dtype()
36. Output?
print(any([0, "", False, 5]))
A) False
B) True ✅
C) None
D) Error
Explanation: any() is True if at least one element is truthy.
37. Which of these is not a Python data structure?
A) list
B) tuple
C) stack ✅ (not built-in, must be implemented)
D) dict
38. Output?
print(2 ** 3 ** 2)
A) 64
B) 512 ✅
C) 16
D) Error
Explanation: Exponentiation is right-associative → 3^2=9, then 2^9=512.
39. Which library is used for scientific computing in Python?
A) NumPy ✅
B) pandas
C) math
D) scipy
40. Output?
x = [1,2,3]
print(sum(x,10))
A) 6
B) 16 ✅
C) Error
D) 10
Explanation: sum([1,2,3],10) = 10+1+2+3=16.
- Get link
- X
- Other Apps
Comments
Post a Comment