Best Practices Python Programming MCQ Quiz

python MCQ_Quiz

1. Which of the following is a correct variable name in Python?
A) 1st_variable
B) my_variable
C) $variable
D) variable@name

Correct Answer: B) my_variable

2. What does the len() function do in Python?
A) Returns the length of a string or list
B) Converts a string to lowercase
C) Finds the maximum value in a list
D) Opens a file for reading

Correct Answer: A) Returns the length of a string or list

3. What is the output of 2 * 3 ** 2 in Python?
A) 18
B) 36
C) 12
D) 9

Correct Answer: A) 18

4. Which symbol is used for exponentiation in Python?
A) ^
B) **
C) #
D)

Correct Answer: B) **

5. What function is used to read input from the user in Python?
A) read_input()
B) get_input()
C) input()
D) scan()

Correct Answer: C) input()

6. How do you comment a single line in Python?
A) /* comment */
B) # comment
C) <!– comment –>
D) % comment

Correct Answer: B) # comment

7. Which statement is used for exiting a loop in Python?
A) break
B) stop
C) exit
D) end

Correct Answer: A) break

8. What is the correct way to start a Python if statement?
A) if a > b :
B) if a > b then:
C) if a > b:
D) if a > b {

Correct Answer: C) if a > b:

9. What does the += operator do in Python?
A) Subtracts the right operand from the left operand
B) Adds the right operand to the left operand and assigns the result to the left operand
C) Multiplies the left operand by the right operand
D) Concatenates strings

Correct Answer: B) Adds the right operand to the left operand and assigns the result to the left operand

10. What is the purpose of the elif statement in Python?
A) It represents the final condition in an if statement
B) It’s a combination of else and if
C) It allows for multiple conditions to be checked
D) It’s used for exception handling

Correct Answer: C) It allows for multiple conditions to be checked

11. Which of the following is a correct way to create a function in Python?
A) function my_function():
B) def my_function():
C) func my_function():
D) define my_function():

Correct Answer: B) def my_function():

12. What does the % operator do in Python?
A) Division
B) Multiplication
C) Modulus
D) Exponentiation

Correct Answer: C) Modulus

13. Which method is used to convert a string to lowercase in Python?
A) toLowerCase()
B) lower()
C) convert_to_lower()
D) casefold()

Correct Answer: B) lower()

14. What is the purpose of the range() function in Python?
A) Generates a range of numbers 
B) Returns a random number
C) Creates a list of strings
D) Retrieves data from a database

Correct Answer: A) Generates a range of numbers

15. How do you open a file in Python for reading?
A) file_open(“file.txt”, “r”)
B) open_file(“file.txt”, “read”)
C) open(“file.txt”, “r”)
D) read_file(“file.txt”)

Correct Answer: C) open(“file.txt”, “r”)

16. Which method is used to find the index of an element in a list in Python?
A) index()
B) find()
C) search()
D) index()

Correct Answer: D) index()

17. What does the join() method do in Python?
A) Combines two lists
B) Joins elements of a list into a string
C) Adds elements to a list
D) Removes elements from a list

Correct Answer: B) Joins elements of a list into a string

18. What does the pop() method do in Python?
A) Appends an element to a list
B) Removes and returns the last element of a list
C) Deletes an element from a list
D) Sorts the elements of a list

Correct Answer: B) Removes and returns the last element of a list

19. Which method is used to remove an item from a set in Python?
A) discard()
B) remove()
C) pop()
D) All of the above

Correct Answer: D) All of the above

20. What is the output of print(“Hello” + 3 * “World”) in Python?
A) HelloWorld
B) Hello3World
C) HelloWorldWorldWorld
D) Hello3

Correct Answer: C) HelloWorldWorldWorld

21. What does the pass statement do in Python?
A) Prints a message
B) Exits the program
C) Is a placeholder that does nothing
D) Raises an exception

Correct Answer: C) Is a placeholder that does nothing

22. What is the purpose of the break statement in Python?
A) Skips the current iteration and continues to the next iteration in a loop
B) Exits the loop immediately
C) Continues to the next iteration without executing further code in the loop
D) Stops the program execution

Correct Answer: B) Exits the loop immediately

23. Which function is used to get the maximum value from a list in Python?
A) max()
B) maximum()
C) get_max()
D) largest()

Correct Answer: A) max()

24. What is the output of bool(0) in Python?
A) False
B) True
C) 0
D) 1

Correct Answer: A) False

25. In Python, what does the term “PEP” stand for?
A) Python Enhancement Proposal
B) Python Execution Process
C) Python Error Processing
D) Python External Package

Correct Answer: A) Python Enhancement Proposal

26. Which method is used to convert a string to uppercase in Python?
A) upper()
B) toUpperCase()
C) to_upper()
D) convert_to_upper()

Correct Answer: A) upper()

27. What is the output of print(8 // 3) in Python?
A) 2.66666666667
B) 2.67
C) 2
D) 2

Correct Answer: D) 2

28. What does the __init__ method do in Python?
A) Initializes the class object
B) Imports a module
C) Deletes a class
D) Defines a function

Correct Answer: A) Initializes the class object

29. Which of the following is used to represent comments in Python?
A) // comment
B) /* comment */
C) # comment
D) <!– comment –>

Correct Answer: C) # comment

30. What is the purpose of the __str__ method in Python?
A) Converts an object to a string
B) Adds two strings together
C) Returns the length of a string
D) Checks if a string contains a substring

Correct Answer: A) Converts an object to a string

31. Which keyword is used for defining a function in Python?
A) define
B) func
C) def
D) function

Correct Answer: C) def

32. In Python, what is the use of the is keyword?
A) Checks if two strings are equal
B) Checks if two variables refer to the same object in memory
C) Checks if a variable exists
D) Checks if a value is in a list

Correct Answer: B) Checks if two variables refer to the same object in memory

33. What is the purpose of the del statement in Python?
A) Deletes a variable or object
B) Declares a variable
C) Defines a function
D) Imports a module

Correct Answer: A) Deletes a variable or object

34. Which method is used to remove the last element from a list in Python?
A) pop()
B) remove_last()
C) delete()
D) pop(-1)

Correct Answer: D) pop(-1)

35. What is the output of bool([]) in Python?
A) False
B) True
C) []
D) None

Correct Answer: A) False

36. What does the %s format specifier represent in Python string formatting?
A) String
B) Integer
C) Float
D) Placeholder for a string

Correct Answer: D) Placeholder for a string

37. What is the purpose of the return statement in Python?
A) Prints a value to the console
B) Terminates the program
C) Exits a loop
D) Returns a value from a function

Correct Answer: D) Returns a value from a function

38. In Python, what does the += operator do?
A) Subtracts the right operand from the left operand
B) Adds the right operand to the left operand and assigns the result to the left operand
C) Multiplies the left operand by the right operand
D) Concatenates strings

Correct Answer: B) Adds the right operand to the left operand and assigns the result to the left operand

39. Which method is used to convert a string to a list in Python?
A) list()
B) split()
C) str_to_list()
D) string()

Correct Answer: B) split()

40. What is the purpose of the lambda function in Python?
A) Defining a block of code to be called later
B) Creating anonymous functions
C) Modifying existing functions
D) Handling exceptions

Correct Answer: B) Creating anonymous functions

Feel free to utilize these questions for increase your SQL and MySQL Knowledge! We Apologies for the confusion. If found I will try do my best. 💯

Leave a Reply