python 150 question
python 150 question
o a) 1variable
o b) _variable1
o c) @variable
o d) variable-1
Answer: b) _variable1
o a) 16
o b) 11
o c) 21
o d) Error
Answer: b) 11
o a) func
o b) function
o c) def
o d) define
Answer: c) def
o a) <class 'int'>
o b) <class 'float'>
o c) <class 'double'>
o d) <class 'decimal'>
Answer: b) <class 'float'>
o a) List
o b) Dictionary
o c) Tuples
o d) Class
Answer: d) Class
o d) Sorts a list
Answer: a) Returns the length of an object
o a) ^
o b) **
o c) *
o d) %
Answer: b) **
o a) 1.5
o b) 2
o c) 1
o d) 0
Answer: c) 1
o a) str()
o b) int()
o c) float()
o d) list()
Answer: a) str()
o a) //
o b) #
o c) /* */
o d) --
Answer: b) #
11. Which of the following data structures does Python use to implement arrays?
o a) Lists
o b) Dictionaries
o c) Tuples
o d) Sets
Answer: a) Lists
o b) HelloHelloHello
o c) Hello3
o d) Error
Answer: b) HelloHelloHello
13. Which of the following functions can be used to take user input in Python?
o a) input()
o b) raw_input()
o c) scanf()
o d) get()
Answer: a) input()
14. Which method is used to remove white spaces from the beginning and end of a string in
Python?
o a) trim()
o b) strip()
o c) split()
o d) replace()
Answer: b) strip()
15. Which operator is used for checking whether a value exists in a sequence in Python?
o a) with
o b) in
o c) exists
o d) check
Answer: b) in
o a) continue
o b) end
o c) terminate
o d) break
Answer: d) break
o a) ==
o b) !=
o c) is
o d) <>
Answer: d) <>
18. Which of the following function will convert a string to a list of characters?
o a) list()
o b) strlist()
o c) split()
o d) chars()
Answer: a) list()
o a) True
o b) False
o c) Error
o d) None
Answer: a) True
20. Which of the following is used to get the last element of a list in Python?
o a) list[-1]
o b) list[0]
o c) list.pop()
o d) list[-0]
Answer: a) list[-1]
o a) []
o b) {}
o c) ()
o d) dict()
Answer: b) {}
o b) add()
o c) insert()
o d) push()
Answer: a) append()
o a) 10
o b) 11
o c) 9
o d) 7
Answer: b) 11
o a) def
o b) class
o c) create
o d) function
Answer: b) class
o a) []
o b) {}
o c) ()
o d) <>
Answer: c) ()
o a) List
o b) Dictionary
o c) Set
o d) Tuple
Answer: d) Tuple
28. Which of the following function is used to find the minimum value of a list?
o a) min()
o b) max()
o c) len()
o d) sum()
Answer: a) min()
o a) 5
o b) 6
o c) 7
o d) 4
Answer: b) 6
o a) and
o b) or
o c) not
o d) none
Answer: d) none
o a) True
o b) False
o c) Error
o d) None
Answer: b) False
32. Which of the following is the correct way to check if a string starts with a specific prefix in
Python?
o a) startswith()
o b) start()
o c) beginswith()
o a) random()
o b) randint()
o c) rand()
o d) shuffle()
Answer: b) randint()
o a) True
o b) False
o c) Error
o d) None
Answer: a) True
o a) floor()
o b) ceil()
o c) round()
o d) cut()
Answer: c) round()
36. Which of the following is the correct way to reverse a string in Python?
o a) reversed()
o b) [::-1]
o c) reverse()
o d) flip()
Answer: b) [::-1]
o a) True
o b) False
o c) Error
o d) None
Answer: a) True
o a) 1
o b) 0
o c) 3
o d) 10
Answer: a) 1
o a) count()
o b) find()
o c) search()
o d) occur()
Answer: a) count()
o a) in
o b) exists
o c) contains
o d) search
Answer: a) in
o a) 6
o b) 8
o c) 9
o d) 16
Answer: b) 8
o a) lambda
o b) func
o c) function
o d) def
Answer: a) lambda
o a) Equality of values
o b) Identity of objects
o c) Type of variables
o d) Length of strings
Answer: b) Identity of objects
44. What is the output of the following code: print('abc' < 'abcd')?
o a) True
o b) False
o c) Error
o d) None
Answer: a) True
45. What will be the output of the following code: print([1, 2, 3][1])?
o a) 1
o b) 2
o c) 3
o d) Error
Answer: b) 2
o a) keys()
o b) values()
o c) items()
o d) get()
Answer: a) keys()
47. Which of the following is a valid syntax for a list comprehension in Python?
o a) [x for x in range(10)]
o b) (x for x in range(10))
o c) {x for x in range(10)}
o d) x for x in range(10)
Answer: a) [x for x in range(10)]
48. What will be the result of the following code: print(2 + 3 * 5)?
o a) 25
o b) 17
o c) 10
o d) 15
Answer: b) 17
o b) set = [1, 2, 3]
o c) set = (1, 2, 3)
o d) set = {}
Answer: a) set = {1, 2, 3}
50. Which of the following is the correct way to import a module in Python?
o a) import module_name
o b) load module_name
o c) require module_name
o d) use module_name
Answer: a) import module_name
o a) module as alias
o c) alias module
2. Which of the following is the correct syntax for defining a class in Python?
o a) class ClassName:
o b) define ClassName:
o c) def ClassName:
o d) create ClassName:
Answer: a) class ClassName:
o a) read()
o b) open()
o c) file()
o d) write()
Answer: b) open()
o a) Read ('r')
o b) Write ('w')
o c) Append ('a')
o d) Binary ('b')
Answer: a) Read ('r')
o a) Static method
o b) Instance method
o c) Private method
o d) Class method
Answer: c) Private method
9. What will be the output of the following code: print([x for x in range(5)])?
o a) [0, 1, 2, 3, 4]
o b) [0, 1, 2, 3, 4, 5]
o c) [1, 2, 3, 4, 5]
o d) [1, 2, 3, 4]
Answer: a) [0, 1, 2, 3, 4]
10. Which method can be used to create an object from a class in Python?
o a) __init__()
o b) __new__()
o c) __object__()
o d) new()
Answer: a) __init__()
12. Which of the following is a valid syntax for defining a decorator in Python?
o a) @decorator_function
o b) #decorator_function
o c) &decorator_function
o d) decorator_function
Answer: a) @decorator_function
o a) True
o b) False
o c) Error
o d) None
Answer: a) True
o a) It is unordered
o b) It is mutable
o d) Both a and b
Answer: d) Both a and b
o c) To create an object
o d) To delete an object
Answer: a) To provide an official string representation of the object
o a) [1, 2, 3, 4]
o b) [4, 3, 2, 1]
o c) None
o d) Error
Answer: a) [1, 2, 3, 4]
18. Which of the following can be used to remove an element from a set?
o a) remove()
o b) discard()
o c) pop()
o a) List
o b) Tuple
o c) String
o d) int
Answer: a) List
20. What will be the output of the following code: print('Hello'.replace('l', 'L'))?
o a) HeLLo
o b) HeLLo
o c) Hello
o d) Error
Answer: b) HeLLo
o a) (x * 2 for x in range(10))
o b) [x * 2 for x in range(10)]
o c) {x * 2 for x in range(10)}
o a) write()
o b) print()
o c) store()
o d) append()
Answer: a) write()
o b) To compress a list
o c) To concatenate strings
o d) To unzip a file
Answer: a) To combine two or more sequences element-wise
o a) yt
o b) yth
o c) Py
o d) yt
Answer: d) yt
o a) get()
o b) keys()
o c) items()
o d) add()
Answer: d) add()
27. Which of the following methods is used to create a new dictionary with default values?
o a) fromkeys()
o b) setdefault()
o c) keys()
o d) items()
Answer: a) fromkeys()
o a) [0, 1, 2]
o b) [1, 2, 3]
o c) [2, 3, 4]
o d) Error
Answer: a) [0, 1, 2]
o a) index()
o b) find()
o c) locate()
o d) position()
Answer: a) index()
31. Which of the following is the correct syntax to create a class method in Python?
o a) @classmethod
o b) classMethod()
o c) classmethod()
o d) @method_class
Answer: a) @classmethod
o a) [0, 2, 4, 6]
o b) [1, 2, 3, 4]
o c) [2, 4, 6, 8]
o d) [0, 1, 2, 3]
Answer: a) [0, 2, 4, 6]
o a) (element,)
o b) (element)
o c) [element]
o d) {element}
Answer: a) (element,)
o a) list()
o b) [1, 2, 3]
o c) list([1, 2, 3])
o d) {1, 2, 3}
Answer: d) {1, 2, 3}
o a) remove() deletes the first occurrence of a value, pop() removes the last item
o a) True
o b) False
o c) Error
o d) None
Answer: b) False
37. Which of the following is the correct way to raise an exception in Python?
o c) exception("Error message")
o a) typeof()
o b) gettype()
o c) type()
o d) varType()
Answer: c) type()
41. Which of the following is a valid syntax for defining a lambda function in Python?
o a) lambda x: x + 1
o b) lambda x => x + 1
o c) lambda(x) -> x + 1
o d) lambda: x + 1
Answer: a) lambda x: x + 1
42. What is the output of print('5' + '5') in Python?
o a) 10
o b) 55
o c) Error
o d) 5
Answer: b) 55
o a) split()
o b) join()
o c) append()
o d) replace()
Answer: c) append()
44. Which of the following is used to delete an element from a list by index?
o a) del
o b) remove()
o c) pop()
o d) delete()
Answer: a) del
o a) int
o b) float
o c) double
o d) complex
Answer: b) float
o a) Ensures that the script is being run directly and not imported as a module
47. How do you copy a list in Python without affecting the original list?
o a) copy_list = list[:]
o b) copy_list = list()
o c) copy_list = list.copy()
o d) copy_list = list[0:]
Answer: a) copy_list = list[:]
o a) python
o b) Python
o c) PYTHON
o d) None
Answer: b) Python
o a) True
o b) False
o c) None
o d) Error
Answer: b) False
python
CopyEdit
b.append(a)
return b
print(func(1))
print(func(2, []))
print(func(3))
o a) @final
o b) final()
o c) @staticmethod
o d) @classmethod
Answer: a) @final
o a) id()
o b) address()
o c) mem()
o d) location()
Answer: a) id()
4. Which of the following will you use to create a custom exception in Python?
o a) raise Exception
o b) class CustomError(Exception):
o c) try Exception
o d) def exception():
Answer: b) class CustomError(Exception):
o a) 13
o b) 19
o c) 17
o d) 11
Answer: a) 13
o a) class MyMeta(type):
o b) class Meta(type):
o c) class MyClass(Meta):
o d) class MyMeta(class):
Answer: a) class MyMeta(type):
o c) To create a generator
8. Which of the following methods is used to check if a key exists in a Python dictionary?
o a) key()
o b) exists()
o c) in
o d) has_key()
Answer: c) in
python
CopyEdit
def check_scope():
x = 10
def inner():
x = 20
inner()
return x
print(check_scope())
o a) 10
o b) 20
o c) None
o d) Error
Answer: a) 10
CopyEdit
lst = [1, 2, 3]
def f(lst):
lst += [4, 5]
f(lst)
print(lst)
o a) [1, 2, 3]
o b) [1, 2, 3, 4, 5]
o d) [1, 2, 3]
Answer: b) [1, 2, 3, 4, 5]
o a) threading module
o b) multiprocessing module
o c) concurrent.futures module
o a) [0, 2, 4]
o b) [0, 1, 2, 3, 4]
o c) [2, 4]
o d) [0, 1, 3]
Answer: a) [0, 2, 4]
python
CopyEdit
lst = [1, 2, 3]
lst[1:] = [4, 5]
print(lst)
o a) [1, 2, 3, 4, 5]
o b) [1, 4, 5, 3]
o c) [1, 4, 5]
o d) [1, 5]
Answer: c) [1, 4, 5]
o a) @classmethod
o b) @staticmethod
o c) @property
o d) @final
Answer: a) @classmethod
o d) To override a method
Answer: c) To define a method that doesn't depend on instance or class
python
CopyEdit
def func(*args):
return sum(args)
print(func(1, 2, 3))
o a) 6
o b) None
o c) 1
o d) Error
Answer: a) 6
18. What is the output of print('abcd'.__len__())?
o a) 4
o b) abcd
o c) None
o d) Error
Answer: a) 4
19. Which of the following is the correct syntax for a Python lambda function?
o a) lambda x, y: x + y
o b) lambda x + y: x + y
o c) lambda(x, y): x + y
o d) lambda x + y = x + y
Answer: a) lambda x, y: x + y
python
CopyEdit
b.append(a)
return b
print(f(1))
print(f(2, []))
print(f(3))
21. What is the purpose of the finally block in Python exception handling?
o b) To catch exceptions
o a) [1, 3, 5, 9]
o b) [5, 3, 9, 1]
o c) [9, 5, 3, 1]
o d) Error
Answer: a) [1, 3, 5, 9]
o a) 5
o b) Hello
o c) 6
o d) Error
Answer: a) 5
o a) 512
o b) 64
o c) 128
o d) 81
Answer: a) 512
python
CopyEdit
a = [1, 2, 3]
b=a
b[0] = 100
print(a)
o a) [100, 2, 3]
o b) [1, 2, 3]
o c) [100]
o d) Error
Answer: a) [100, 2, 3]
o c) By using a decorator
28. Which of the following methods is used to add a new element to a Python set?
o a) add()
o b) insert()
o c) append()
o d) push()
Answer: a) add()
o a) {1, 2, 3}
o b) {1, 2}
o c) {2}
o d) {3}
Answer: a) {1, 2, 3}
o a) ababab
o b) ab
o c) a3b
o d) None
Answer: a) ababab
31. Which of the following is the correct syntax to create a set in Python?
o a) set[]
o b) set()
o c) {}
o d) [1, 2, 3]
Answer: b) set()
python
CopyEdit
x = 10
def func(x):
return x * 2
print(func(x))
o a) 20
o b) 10
o c) Error
o d) None
Answer: a) 20
33. Which Python function is used to get the absolute value of a number?
o a) abs()
o b) round()
o c) max()
o d) min()
Answer: a) abs()
34. How do you convert a list of strings into a single string in Python?
o a) ''.join(list)
o b) list.join('')
o c) join(list)
o d) str(list)
Answer: a) ''.join(list)
o a) python
o b) nothyp
o c) nohtyp
o d) None
Answer: c) nohtyp
python
CopyEdit
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)
o a) True
o b) False
o c) Error
o d) None
Answer: b) False
o c) import <package_name>
o b) 3
o c) 2
o d) 1
Answer: b) 3
python
CopyEdit
o a) True
o b) False
o c) Error
o d) None
Answer: a) True
44. Which of the following functions can be used to create a shallow copy of a list?
o a) copy()
o b) deepcopy()
o c) slice()
o d) clone()
Answer: a) copy()
45. What will be the result of the following code?
python
CopyEdit
x=0
y=x
x=1
print(y)
o a) 1
o b) 0
o c) None
o d) Error
Answer: b) 0
o a) 6561
o b) 512
o c) 128
o d) 81
Answer: a) 6561
o a) 5
o b) 10
o c) 2
o d) None
Answer: b) 10
o a) [0, 2, 4]
o b) [1, 3]
o c) [0, 1, 2, 3, 4]
o d) [1, 2, 3, 4]
Answer: a) [0, 2, 4]