[go: up one dir, main page]

0% found this document useful (0 votes)
41 views31 pages

python 150 question

The document consists of a set of beginner and intermediate level Python programming questions and answers. It covers various topics such as variable naming, data types, functions, control structures, and object-oriented programming concepts. Each question is followed by multiple-choice options and the correct answer is provided.

Uploaded by

abcdfakeuse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views31 pages

python 150 question

The document consists of a set of beginner and intermediate level Python programming questions and answers. It covers various topics such as variable naming, data types, functions, control structures, and object-oriented programming concepts. Each question is followed by multiple-choice options and the correct answer is provided.

Uploaded by

abcdfakeuse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Beginner Level (50 Questions)

1. Which of the following is a valid variable name in Python?

o a) 1variable

o b) _variable1

o c) @variable

o d) variable-1
Answer: b) _variable1

2. What is the output of the following code: print(2 * 3 + 5)?

o a) 16

o b) 11

o c) 21

o d) Error
Answer: b) 11

3. Which keyword is used to define a function in Python?

o a) func

o b) function

o c) def

o d) define
Answer: c) def

4. What is the output of: print(type(3.14))?

o a) <class 'int'>

o b) <class 'float'>

o c) <class 'double'>

o d) <class 'decimal'>
Answer: b) <class 'float'>

5. Which of the following is not a core data type in Python?

o a) List

o b) Dictionary

o c) Tuples

o d) Class
Answer: d) Class

6. What does the len() function do?


o a) Returns the length of an object

o b) Returns the sum of elements

o c) Returns the largest element

o d) Sorts a list
Answer: a) Returns the length of an object

7. Which operator is used to raise a number to a power in Python?

o a) ^

o b) **

o c) *

o d) %
Answer: b) **

8. What is the result of 3 // 2?

o a) 1.5

o b) 2

o c) 1

o d) 0
Answer: c) 1

9. Which function is used to convert an integer to a string in Python?

o a) str()

o b) int()

o c) float()

o d) list()
Answer: a) str()

10. How do you comment out a single line in Python?

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

12. What is the output of the following code: print("Hello" * 3)?

o a) Hello Hello Hello

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

16. Which of the following is used to terminate a loop in Python?

o a) continue

o b) end

o c) terminate
o d) break
Answer: d) break

17. Which of the following is not a comparison operator in Python?

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()

19. What is the output of: print(10 > 5)?

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]

21. How do you create an empty dictionary in Python?

o a) []

o b) {}

o c) ()

o d) dict()
Answer: b) {}

22. Which method is used to add elements to a list in Python?


o a) append()

o b) add()

o c) insert()

o d) push()
Answer: a) append()

23. What will be the result of the following code: 5 + 4 * 3 // 2 - 1?

o a) 10

o b) 11

o c) 9

o d) 7
Answer: b) 11

24. What does range(5) do in Python?

o a) Creates a list of numbers from 1 to 5

o b) Creates a list of numbers from 0 to 5

o c) Creates a list of numbers from 0 to 4

o d) Creates a list of numbers from 1 to 4


Answer: c) Creates a list of numbers from 0 to 4

25. Which keyword is used to create a class in Python?

o a) def

o b) class

o c) create

o d) function
Answer: b) class

26. How do you initialize a tuple in Python?

o a) []

o b) {}

o c) ()

o d) <>
Answer: c) ()

27. Which of the following is immutable in Python?

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()

29. What is the result of: len('Python')?

o a) 5

o b) 6

o c) 7

o d) 4
Answer: b) 6

30. Which of the following is not a logical operator in Python?

o a) and

o b) or

o c) not

o d) none
Answer: d) none

31. What will be the output of the following code: print(bool(0))?

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 d) initial() Answer: a) startswith()


33. Which function is used to generate random numbers in Python?

o a) random()

o b) randint()

o c) rand()

o d) shuffle()
Answer: b) randint()

34. What is the output of the following code: print('a' in 'apple')?

o a) True

o b) False

o c) Error

o d) None
Answer: a) True

35. Which function is used to round off a float value in Python?

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]

37. What is the output of 3 == 3.0 in Python?

o a) True

o b) False

o c) Error

o d) None
Answer: a) True

38. What will be the output of print(10 % 3)?

o a) 1
o b) 0

o c) 3

o d) 10
Answer: a) 1

39. Which method is used to count the occurrences of a value in a list?

o a) count()

o b) find()

o c) search()

o d) occur()
Answer: a) count()

40. How do you check if a key exists in a dictionary?

o a) in

o b) exists

o c) contains

o d) search
Answer: a) in

41. What is the output of 2 ** 3 in Python?

o a) 6

o b) 8

o c) 9

o d) 16
Answer: b) 8

42. Which keyword is used to create a lambda function in Python?

o a) lambda

o b) func

o c) function

o d) def
Answer: a) lambda

43. What does the is operator check in Python?

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

46. Which method is used to get a list of all keys in a dictionary?

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

49. How can you create a set in Python?


o a) set = {1, 2, 3}

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

Intermediate Level (50 Questions)

1. Which of the following is used to create an alias for a module in Python?

o a) module as alias

o b) import alias from module

o c) alias module

o d) import module as alias


Answer: d) import module as alias

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:

3. What is the purpose of the self parameter in a class method?

o a) It refers to the instance of the class

o b) It refers to the class itself

o c) It is used to define class variables

o d) It refers to the parent class


Answer: a) It refers to the instance of the class

4. Which of the following is the correct way to handle exceptions in Python?


o a) try ... except

o b) try ... catch

o c) catch ... throw

o d) throw ... except


Answer: a) try ... except

5. What will happen if you don't handle an exception in Python?

o a) The program will terminate with an error message

o b) The program will continue execution

o c) The program will skip the error

o d) The program will freeze


Answer: a) The program will terminate with an error message

6. Which function is used to open a file in Python?

o a) read()

o b) open()

o c) file()

o d) write()
Answer: b) open()

7. What is the default mode of the open() function if no mode is specified?

o a) Read ('r')

o b) Write ('w')

o c) Append ('a')

o d) Binary ('b')
Answer: a) Read ('r')

8. Which of the following is not a type of method in Python classes?

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__()

11. What is the purpose of the global keyword in Python?

o a) It is used to define a global function

o b) It is used to declare global variables inside functions

o c) It is used to declare a variable as read-only

o d) It is used to access system-level variables


Answer: b) It is used to declare global variables inside functions

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

13. What will be the output of print({1, 2, 3} == {3, 2, 1})?

o a) True

o b) False

o c) Error

o d) None
Answer: a) True

14. Which of the following is true about a Python set?

o a) It is unordered

o b) It is mutable

o c) It does not allow duplicate values

o d) All of the above


Answer: d) All of the above
15. How do you handle multiple exceptions in Python?

o a) Using multiple except blocks

o b) Using a single except block with multiple exception types

o c) Using finally block

o d) Both a and b
Answer: d) Both a and b

16. What is the purpose of the __repr__() method in Python classes?

o a) To provide an official string representation of the object

o b) To provide an informal string representation of the object

o c) To create an object

o d) To delete an object
Answer: a) To provide an official string representation of the object

17. What will be the output of print(sorted([3, 1, 4, 2]))?

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 d) All of the above


Answer: d) All of the above

19. Which of the following is a mutable data type in Python?

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

21. Which of the following is a valid generator expression in Python?

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 d) gen(x * 2 for x in range(10))


Answer: a) (x * 2 for x in range(10))

22. Which method is used to write data to a file in Python?

o a) write()

o b) print()

o c) store()

o d) append()
Answer: a) write()

23. What is the purpose of the zip() function in Python?

o a) To combine two or more sequences element-wise

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

24. What will be the output of print('Python'[1:3])?

o a) yt

o b) yth

o c) Py

o d) yt
Answer: d) yt

25. What does the pass statement do in Python?

o a) Skips the current loop iteration

o b) Does nothing; it's a placeholder

o c) Terminates the loop


o d) Throws an error
Answer: b) Does nothing; it's a placeholder

26. Which of the following is not a valid dictionary method?

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()

28. What will be the output of print([i for i in range(3)])?

o a) [0, 1, 2]

o b) [1, 2, 3]

o c) [2, 3, 4]

o d) Error
Answer: a) [0, 1, 2]

29. Which method is used to find the index of an item in a list?

o a) index()

o b) find()

o c) locate()

o d) position()
Answer: a) index()

30. What is the purpose of the assert statement in Python?

o a) To raise an AssertionError if a condition is not met

o b) To check if a value exists in a list

o c) To check for syntax errors

o d) To print debugging information


Answer: a) To raise an AssertionError if a condition is not met

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

32. What will be the output of print([x * 2 for x in range(4)])?

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]

33. How do you create a tuple with a single element in Python?

o a) (element,)

o b) (element)

o c) [element]

o d) {element}
Answer: a) (element,)

34. Which of the following is not a valid way to create a list?

o a) list()

o b) [1, 2, 3]

o c) list([1, 2, 3])

o d) {1, 2, 3}
Answer: d) {1, 2, 3}

35. What is the difference between remove() and pop() in lists?

o a) remove() deletes the first occurrence of a value, pop() removes the last item

o b) remove() deletes by index, pop() deletes by value

o c) remove() deletes by value, pop() removes by index

o d) remove() removes all occurrences of a value, pop() removes all occurrences of a


value
Answer: c) remove() deletes by value, pop() removes by index

36. What will be the result of bool([]) in Python?

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 a) raise Exception("Error message")

o b) throw Exception("Error message")

o c) exception("Error message")

o d) error Exception("Error message")


Answer: a) raise Exception("Error message")

38. How do you check the type of a variable in Python?

o a) typeof()

o b) gettype()

o c) type()

o d) varType()
Answer: c) type()

39. Which of the following is true about Python functions?

o a) They must return a value

o b) They can return multiple values

o c) They can only return one value

o d) They cannot return values


Answer: b) They can return multiple values

40. What is the purpose of the map() function in Python?

o a) To apply a function to each element of a sequence

o b) To combine two sequences

o c) To filter elements from a sequence

o d) To sort elements in a sequence


Answer: a) To apply a function to each element of a sequence

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

43. Which of the following is not a valid string method in Python?

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

45. What will be the output of print(type(10/5))?

o a) int

o b) float

o c) double

o d) complex
Answer: b) float

46. What does the __name__ == "__main__" check do in Python scripts?

o a) Ensures that the script is being run directly and not imported as a module

o b) Checks the name of the main class

o c) Starts the execution of the script

o d) Imports the main function


Answer: 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[:]

48. What will be the result of print('python'.capitalize())?

o a) python

o b) Python

o c) PYTHON

o d) None
Answer: b) Python

49. Which of the following is true about Python sets?

o a) They are ordered collections

o b) They allow duplicate elements

o c) They are immutable

o d) They do not allow duplicate elements


Answer: d) They do not allow duplicate elements

50. What will be the output of print(bool('')) in Python?

o a) True

o b) False

o c) None

o d) Error
Answer: b) False

Advanced Level (50 Questions)

1. What will be the output of the following code:

python

CopyEdit

def func(a, b=[]):

b.append(a)

return b

print(func(1))

print(func(2, []))
print(func(3))

o a) [1], [2], [3]

o b) [1], [2], [3, 1]

o c) [1], [2], [3]

o d) [1], [], [3]


Answer: b) [1], [2], [3, 1]

2. Which of the following methods is used to prevent modification of an object in Python?

o a) @final

o b) final()

o c) @staticmethod

o d) @classmethod
Answer: a) @final

3. Which function in Python is used to get the memory address of an object?

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):

5. What is the output of print(1 + 2 * 3 ** 2)?

o a) 13

o b) 19

o c) 17

o d) 11
Answer: a) 13

6. How do you define a metaclass in Python?

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):

7. What is the purpose of the yield keyword in Python?

o a) To stop the function execution

o b) To return multiple values from a function

o c) To create a generator

o d) To create an infinite loop


Answer: 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

9. What will be the result of the following code?

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

10. What will be the output of the following code?


python

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 c) [1, 2, 3, [4, 5]]

o d) [1, 2, 3]
Answer: b) [1, 2, 3, 4, 5]

11. Which of the following is used for multithreading in Python?

o a) threading module

o b) multiprocessing module

o c) concurrent.futures module

o d) All of the above


Answer: d) All of the above

12. What will be the output of print(list(range(0, 5, 2)))?

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]

13. How would you initialize a generator in Python?

o a) By using the yield keyword

o b) By using a list comprehension

o c) By using a for loop

o d) By defining a function with return


Answer: a) By using the yield keyword

14. What will be the output of the following code?

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]

15. Which of the following decorators is used to define a class method?

o a) @classmethod

o b) @staticmethod

o c) @property

o d) @final
Answer: a) @classmethod

16. What is the purpose of the @staticmethod decorator in Python?

o a) To create a class method

o b) To create an instance method

o c) To define a method that doesn't depend on instance or class

o d) To override a method
Answer: c) To define a method that doesn't depend on instance or class

17. What will the following code output?

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

20. What is the output of the following code?

python

CopyEdit

def f(a, b=[]):

b.append(a)

return b

print(f(1))

print(f(2, []))

print(f(3))

o a) [1], [2], [3]

o b) [1], [2], [3, 1]

o c) [1], [2], [3]

o d) [1], [], [3]


Answer: b) [1], [2], [3, 1]

21. What is the purpose of the finally block in Python exception handling?

o a) To execute code if no exception is raised

o b) To catch exceptions

o c) To execute code regardless of whether an exception is raised

o d) To log the exception


Answer: c) To execute code regardless of whether an exception is raised
22. What is the output of print(sorted([5, 3, 9, 1]))?

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]

23. What will be the output of print(len('Hello'))?

o a) 5

o b) Hello

o c) 6

o d) Error
Answer: a) 5

24. What is the output of print(2 ** 3 ** 2)?

o a) 512

o b) 64

o c) 128

o d) 81
Answer: a) 512

25. What is the purpose of the __init__ method in Python?

o a) To initialize the object’s properties

o b) To create a static method

o c) To define the class method

o d) To override a built-in method


Answer: a) To initialize the object’s properties

26. What will be the result of the following code?

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]

27. How do you implement a singleton pattern in Python?

o a) By using a global variable to store the instance

o b) By using the __new__ method to ensure a single instance

o c) By using a decorator

o d) By using a class variable


Answer: b) By using the __new__ method to ensure a single instance

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()

29. What will be the output of print({1, 2} | {2, 3})?

o a) {1, 2, 3}

o b) {1, 2}

o c) {2}

o d) {3}
Answer: a) {1, 2, 3}

30. What is the output of print('ab' * 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()

32. What is the output of the following code?

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)

35. What is the output of print('python'[::-1])?

o a) python

o b) nothyp

o c) nohtyp
o d) None
Answer: c) nohtyp

36. Which of the following statements is true about Python dictionaries?

o a) They are ordered collections

o b) Keys must be unique

o c) They do not allow nested dictionaries

o d) Values must be immutable


Answer: b) Keys must be unique

37. What will be the output of the following code?

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

38. Which of the following is used to install a package in Python?

o a) pip install <package_name>

o b) python install <package_name>

o c) import <package_name>

o d) setup.py install <package_name>


Answer: a) pip install <package_name>

39. Which of the following is true about Python’s garbage collection?

o a) Python uses reference counting for garbage collection

o b) Python does not have garbage collection

o c) Python only uses manual memory management

o d) Python uses memory pools instead of garbage collection


Answer: a) Python uses reference counting for garbage collection

40. What will be the output of print('Python'.index('o'))?


o a) 4

o b) 3

o c) 2

o d) 1
Answer: b) 3

41. What is the purpose of the __del__ method in Python?

o a) To initialize the object’s properties

o b) To destroy an object when it is no longer needed

o c) To execute code before the program ends

o d) To define a custom exception


Answer: b) To destroy an object when it is no longer needed

42. What does the break statement do in Python?

o a) It terminates the loop and continues the next iteration

o b) It terminates the loop and breaks out of the function

o c) It skips to the next iteration of the loop

o d) It terminates the loop and exits the program


Answer: b) It terminates the loop and breaks out of the function

43. What will the following code output?

python

CopyEdit

print(2 < 3 < 4)

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

46. Which of the following is true about Python’s filter() function?

o a) It filters out values based on a condition

o b) It filters out the even numbers from a list

o c) It filters out the odd numbers from a list

o d) It allows for sorting elements based on a condition


Answer: a) It filters out values based on a condition

47. What will be the output of print(3 ** 2 ** 3)?

o a) 6561

o b) 512

o c) 128

o d) 81
Answer: a) 6561

48. What is the purpose of the @property decorator in Python?

o a) To define a class method

o b) To define an instance method

o c) To make an attribute of the class accessible like a method

o d) To define a static method


Answer: c) To make an attribute of the class accessible like a method

49. What is the output of print((lambda x: x * 2)(5))?

o a) 5
o b) 10

o c) 2

o d) None
Answer: b) 10

50. What will be the output of print([i for i in range(5) if i % 2 == 0])?

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]

You might also like