Python MCQs
Python MCQs
2. What will be the output if you print 'Hello, World!' in Python using the print function?
a) Hello, World!
b) 'Hello, World!'
c) print Hello, World!
d) SyntaxError
Answer: a) Hello, World!
23. What is the result of using a variable without assigning a value to it first?
a) 0
b) Null
c) NameError
d) False
Answer: c) NameError
24. Which of these is the correct behavior in Python when assigning a value to a variable?
a) Python assigns and allocates memory during declaration
b) Python allocates memory only after first use
c) Python does not allocate memory
d) Python assigns a fixed memory type
Answer: a) Python assigns and allocates memory during declaration
25. What is the output if you print a variable that was not defined?
a) Undefined
b) SyntaxError
c) RuntimeError
d) NameError
Answer: d) NameError
2. Which Python data type would be used to store a value like 3 + 4j?
a) int
b) float
c) complex
d) boolean
Answer: c) complex
13. Which data type is typically used to store a collection of values of any type, and allows duplicate
elements?
a) dictionary
b) list
c) set
d) tuple
Answer: b) list
14. Which data type does not allow duplicates and is unordered?
a) list
b) tuple
c) dictionary
d) set
Answer: d) set
16. Which data type allows indexing and slicing but cannot be changed?
a) list
b) dictionary
c) tuple
d) set
Answer: c) tuple
19. Which of these supports key-value pairs and allows keys to be of any immutable type?
a) dictionary
b) list
c) set
d) string
Answer: a) dictionary
20. Which of the following is NOT a correct pair of data type and its mutability?
a) list – mutable
b) tuple – immutable
c) dictionary – mutable
d) string – mutable
Answer: d) string – mutable
21. What happens if you try to assign a new item to an index of a string?
a) It updates successfully
b) It appends the value
c) It throws a TypeError
d) It ignores the change
Answer: c) It throws a TypeError
23. Which of the following allows duplicate values and is ordered but mutable?
a) set
b) list
c) tuple
d) dictionary
Answer: b) list
24. Which data type would be most appropriate to store a phone book (names and phone numbers)?
a) tuple
b) list
c) dictionary
d) string
Answer: c) dictionary
25. What type of data is returned when you perform an arithmetic operation between two integers
in Python?
a) float
b) complex
c) boolean
d) int
Answer: d) int
27. Which of these types can be both values and keys in a dictionary?
a) Only string
b) Only int
c) Any immutable type as key, any type as value
d) Only tuple
Answer: c) Any immutable type as key, any type as value
7. Which operator is used to check if two variables refer to the same object?
a) ==
b) is
c) :=
d) in
Answer: b) is
9. What is the result of the logical operator 'not' if the operand is True?
a) Error
b) False
c) True
d) None
Answer: b) False
14. Which operator divides and returns only the integer part?
a) /
b) %
c) //
d) **
Answer: c) //
16. Which relational operator checks if one value is not equal to another?
a) ==
b) !=
c) =!
d) not equal
Answer: b) !=
17. Which of the following evaluates to True for the expression: value1 in list1?
a) When value1 is not a part of list1
b) When value1 is in list1
c) Always False
d) Only if list1 is empty
Answer: b) When value1 is in list1
22. Which logical operator gives True if at least one operand is True?
a) and
b) or
c) not
d) is
Answer: b) or
29. Which operator assigns a value only if the variable is not already assigned?
a) =
b) :=
c) is
d) +=
Answer: b) :=
9. What will happen if you convert a float with value 7.9 to int using explicit conversion?
a) It becomes 8
b) It becomes '7.9'
c) It becomes 7
d) It throws an error
Answer: c) It becomes 7
10. Which method is used to accept input from the user in Python?
a) get()
b) input()
c) read()
d) enter()
Answer: b) input()
12. What is the default type of value returned by the input function?
a) integer
b) string
c) boolean
d) float
Answer: b) string
18. What will be the result type of adding an int and float in Python?
a) int
b) float
c) string
d) error
Answer: b) float
19. Which function is used to convert a string into a floating point number?
a) float()
b) convert()
c) toFloat()
d) double()
Answer: a) float()
21. If you input '5' from the console and add it to 3 directly, what happens?
a) 8
b) 53
c) Error
d) 5.0
Answer: c) Error
22. How can you make the above input addition work correctly?
a) By converting the input to int before addition
b) Using a different input method
c) By converting to string
d) It will never work
Answer: a) By converting the input to int before addition
23. What happens if you use int() on a string that is not a number like 'abc'?
a) Returns 0
b) Returns 'abc'
c) Throws ValueError
d) Converts to ASCII
Answer: c) Throws ValueError
24. What type of expression is: not (True and False)?
a) Arithmetic
b) Logical
c) Relational
d) Assignment
Answer: b) Logical
27. Which function helps to check the type of any object in Python?
a) typeof()
b) checktype()
c) type()
d) datatype()
Answer: c) type()
30. Which of the following will display the output without a newline?
a) print with end=''
b) print()
c) input()
d) display()
Answer: a) print with end=''
2. Which type of error occurs when a program runs but produces incorrect results?
a) Syntax error
b) Logical error
c) Runtime error
d) Compilation error
Answer: b) Logical error
7. Which type of error can be detected only when the program is running?
a) Syntax error
b) Runtime error
c) Logical error
d) Typing error
Answer: b) Runtime error
8. Which of the following will most likely cause a logical error?
a) Misspelled variable name
b) Missing bracket
c) Wrong formula used
d) Using print without parentheses
Answer: c) Wrong formula used
10. What type of error will occur if you try to access an index that does not exist in a list?
a) SyntaxError
b) NameError
c) IndexError
d) ValueError
Answer: c) IndexError
12. If your program runs without crashing but gives the wrong output, it is a:
a) Runtime error
b) Syntax error
c) Logical error
d) None of these
Answer: c) Logical error
17. What is the result of writing a print statement without parentheses in Python 3?
a) RuntimeError
b) IndentationError
c) SyntaxError
d) No output
Answer: c) SyntaxError
18. If a program crashes when the user provides wrong input, this is a:
a) Compilation error
b) Syntax error
c) Runtime error
d) Logical error
Answer: c) Runtime error
20. Which error is difficult to detect because it doesn't crash the program?
a) Runtime error
b) Syntax error
c) Logical error
d) Type error
Answer: c) Logical error
23. An error that results in incorrect program output but no interruption is called:
a) Runtime error
b) Syntax error
c) Logical error
d) ValueError
Answer: c) Logical error
25. If you write 'if x > 5 print(x)', what kind of error will be raised?
a) TypeError
b) Logical error
c) SyntaxError
d) NameError
Answer: c) SyntaxError
10. Which keyword is used to repeat a block of code while a condition is true?
a) repeat
b) do
c) for
d) while
Answer: d) while
11. Which loop is used to iterate over a range or a sequence?
a) while
b) for
c) if
d) do
Answer: b) for
17. If there are no conditions or loops, the code executes in which manner?
a) Random
b) Reverse
c) Sequential
d) Recursive
Answer: c) Sequential
18. Which part of the control structure must be correctly indented in Python?
a) Only if statements
b) Only loops
c) All block structures
d) Indentation is optional
Answer: c) All block structures
22. Which control structure is best suited for executing a set of statements a fixed number of times?
a) if
b) for
c) while
d) pass
Answer: b) for
24. Which statement is used to define multiple decision branches in one structure?
a) if-else
b) nested if
c) elif
d) switch
Answer: c) elif
25. What happens when a for loop finishes all iterations?
a) It breaks
b) It starts again
c) It stops automatically
d) It throws an error
Answer: c) It stops automatically
27. Which control flow structure is useful when the number of iterations is unknown?
a) for loop
b) while loop
c) if statement
d) elif statement
Answer: b) while loop
28. What is the default increment behavior in a for loop using range?
a) 0
b) 2
c) 1
d) Infinite
Answer: c) 1
29. Which statement will be executed regardless of whether the condition is true or false?
a) elif
b) continue
c) else
d) None
Answer: c) else
5. Which control statement is best for checking whether a number is divisible by both 3 and 5?
a) for
b) if
c) while
d) break
Answer: b) if
6. What happens if all if and elif conditions are false and there is no else?
a) Error
b) The last if is executed
c) Nothing happens
d) All conditions run
Answer: c) Nothing happens
10. Which of the following is best used to check whether a number is even or odd?
a) if-elif
b) if
c) if-else
d) while
Answer: c) if-else
17. What is printed if the condition in the if block is false and there is an else?
a) The if block
b) Nothing
c) The else block
d) An error
Answer: c) The else block
20. Which value will be printed for absolute value of -7 using conditionals?
a) -7
b) 0
c) 7
d) Error
Answer: c) 7
21. Which block will execute if none of the if or elif conditions are true?
a) First if
b) Last elif
c) else
d) pass
Answer: c) else
22. To check divisibility of a number by 3 and 5 together, which logical operator is used?
a) or
b) and
c) not
d) is
Answer: b) and
23. Which of the following checks whether a number is not divisible by 2?
a) number % 2 == 0
b) number % 2 != 0
c) number / 2 == 1
d) number * 2 == 0
Answer: b) number % 2 != 0
26. What is the output if a condition is true and both if and else blocks contain print statements?
a) Only if block is executed
b) Only else block is executed
c) Both are executed
d) None are executed
Answer: a) Only if block is executed
29. To find the smallest of 3 numbers using conditional statements, what is the ideal approach?
a) while loop
b) if-else
c) if-elif-else
d) range function
Answer: c) if-elif-else
30. Which of the following helps organize the logic in multi-branch conditions?
a) Indentation
b) Brackets
c) Semicolons
d) Loop counters
Answer: a) Indentation
2. Which function is commonly used with for loop to generate a sequence of numbers?
a) range()
b) loop()
c) sequence()
d) series()
Answer: a) range()
3. What is the default starting value in the range() function if not specified?
a) 1
b) -1
c) 0
d) Depends on input
Answer: c) 0
5. Which loop is preferred when the number of iterations is not known in advance?
a) for
b) while
c) if
d) switch
Answer: b) while
15. What is the value of the loop variable on the first iteration of range(3, 9)?
a) 2
b) 3
c) 9
d) 1
Answer: b) 3
17. To find the sum of the first N numbers, which kind of loop is commonly used?
a) if
b) for
c) try
d) pass
Answer: b) for
23. Which statement can be used inside a loop to avoid an infinite loop?
a) continue
b) break
c) pass
d) return
Answer: b) break
26. What happens if a break is used inside an inner loop in a nested structure?
a) Both loops end
b) Only the inner loop breaks
c) Outer loop ends
d) Loop restarts
Answer: b) Only the inner loop breaks
27. To compute the sum of even numbers from 1 to 10, what should the step be in range()?
a) 1
b) 2
c) 3
d) 0
Answer: b) 2
28. Which of the following patterns can be created using nested loops?
a) Star pyramid
b) Number triangle
c) Floyd’s triangle
d) All of the above
Answer: d) All of the above
29. Which keyword causes the loop to skip remaining code and recheck the condition?
a) break
b) exit
c) continue
d) pass
Answer: c) continue
31. What will be the output of summing values using range(1, 6)?
a) 10
b) 15
c) 6
d) 5
Answer: b) 15
33. Which condition will stop a while loop that increments a variable from 1 to 10?
a) When value > 10
b) When value = 1
c) When value != 10
d) When loop restarts
Answer: a) When value > 10
34. Which error may occur if loop condition is not properly set in a while loop?
a) NameError
b) ValueError
c) Infinite loop
d) IndexError
Answer: c) Infinite loop
35. Which statement is needed in a loop to calculate the total sum of entered numbers until zero is
entered?
a) if-else
b) for loop
c) while loop
d) try-except
Answer: c) while loop
11. Which method returns the number of times a character appears in a string?
a) count()
b) len()
c) find()
d) index()
Answer: a) count()
12. Which method returns the index of the first occurrence of a character?
a) find()
b) locate()
c) count()
d) check()
Answer: a) find()
15. Which method checks if the string starts with a specific substring?
a) startswith()
b) begin()
c) prefix()
d) header()
Answer: a) startswith()
16. Which method returns True if all characters in the string are alphabets or numbers?
a) isalpha()
b) isdigit()
c) isalnum()
d) isnumeric()
Answer: c) isalnum()
22. Which method removes whitespace from the beginning of the string?
a) ltrim()
b) strip()
c) lstrip()
d) trimleft()
Answer: c) lstrip()
23. Which method removes whitespace from the end of the string?
a) rtrim()
b) strip()
c) rstrip()
d) trimright()
Answer: c) rstrip()
24. Which method removes whitespace from both ends of the string?
a) trim()
b) clean()
c) strip()
d) delete()
Answer: c) strip()
25. Which method replaces part of the string with another value?
a) replace()
b) change()
c) update()
d) edit()
Answer: a) replace()
30. What will happen if you try to change a character in a string by index?
a) It changes
b) It is ignored
c) It gives error
d) It prints nothing
Answer: c) It gives error
33. Which method is useful to count how many times a word appears in a sentence?
a) find()
b) index()
c) count()
d) title()
Answer: c) count()
38. Which function can be used to convert a list of words into a single string separated by spaces?
a) join()
b) append()
c) merge()
d) strip()
Answer: a) join()
12. Which method returns how many times a value appears in a list?
a) count()
b) find()
c) index()
d) length()
Answer: a) count()
13. Which method returns the index of the first occurrence of an element?
a) locate()
b) find()
c) index()
d) position()
Answer: c) index()
14. Which method removes the first occurrence of a value from a list?
a) delete()
b) pop()
c) remove()
d) cut()
Answer: c) remove()
15. Which method removes and returns the last item of a list by default?
a) delete()
b) remove()
c) pop()
d) cut()
Answer: c) pop()
16. Which method reverses the order of elements in the list in place?
a) reverse()
b) sort(reverse=True)
c) rev()
d) flip()
Answer: a) reverse()
18. Which function returns a new sorted list without changing the original?
a) arrange()
b) sort()
c) sorted()
d) order()
Answer: c) sorted()
27. To calculate the mean of numbers in a list, which two functions are useful?
a) min() and max()
b) len() and sum()
c) append() and sort()
d) remove() and sum()
Answer: b) len() and sum()
29. Which algorithm checks for the presence of a value one-by-one in a list?
a) Binary search
b) Bubble sort
c) Linear search
d) Insertion sort
Answer: c) Linear search
30. What will list.count(x) return?
a) Total elements
b) Total numeric values
c) Number of times x appears
d) Index of x
Answer: c) Number of times x appears
31. Which method will cause an error if the element is not in the list?
a) remove()
b) count()
c) append()
d) insert()
Answer: a) remove()
39. Which method is used to add one list into another at a particular position?
a) append()
b) insert()
c) extend()
d) add()
Answer: b) insert()
40. Which data structure is best suited for storing student marks and finding highest score?
a) string
b) list
c) set
d) dictionary
Answer: b) list
10. Which method returns how many times an element appears in a tuple?
a) index()
b) count()
c) repeat()
d) find()
Answer: b) count()
11. Which method returns the index of the first occurrence of a value in a tuple?
a) locate()
b) find()
c) index()
d) search()
Answer: c) index()
20. Which type of assignment allows extracting multiple values from a tuple?
a) Chained assignment
b) Tuple unpacking
c) Auto assignment
d) Mapping assignment
Answer: b) Tuple unpacking
23. Which function is used to find the number of times an item exists in a tuple?
a) index()
b) len()
c) count()
d) repeat()
Answer: c) count()
29. Which method is NOT available for tuples but available for lists?
a) count()
b) index()
c) append()
d) len()
Answer: c) append()
6. Dictionaries are:
a) Immutable
b) Mutable
c) Static
d) Read-only
Answer: b) Mutable
12. Which method removes a specific key and returns its value?
a) pop()
b) remove()
c) del()
d) discard()
Answer: a) pop()
14. Which method removes and returns the last inserted item?
a) pop()
b) popitem()
c) clear()
d) remove()
Answer: b) popitem()
17. Which method returns the value for a key or inserts it with a default if not found?
a) get()
b) update()
c) setdefault()
d) append()
Answer: c) setdefault()
21. Which of the following will return a sorted list of dictionary keys?
a) sorted(dict)
b) dict.sort()
c) sort(dict)
d) dict.sorted()
Answer: a) sorted(dict)
25. Which method is used to traverse through all dictionary keys and values?
a) keys()
b) items()
c) get()
d) len()
Answer: b) items()
29. What will happen if you pop a key that doesn’t exist and no default is given?
a) None is returned
b) Error
c) Empty string
d) 0 is returned
Answer: b) Error
30. Which of the following returns True if a key exists in a dictionary?
a) in
b) has()
c) exists()
d) check()
Answer: a) in
31. Which method can safely fetch a key's value even if the key doesn't exist?
a) get()
b) pop()
c) values()
d) clear()
Answer: a) get()
34. Which method is suitable for counting the frequency of items in a dictionary?
a) update()
b) count()
c) setdefault()
d) popitem()
Answer: c) setdefault()
5. Which module provides mathematical functions like square root and trigonometry?
a) random
b) statistics
c) math
d) numpy
Answer: c) math
10. Which function returns the largest integer less than or equal to a given number?
a) ceil()
b) floor()
c) round()
d) trunc()
Answer: b) floor()
11. Which function raises one number to the power of another in math module?
a) exp()
b) raise()
c) pow()
d) power()
Answer: c) pow()
19. Which function returns a random number from a range with a step?
a) randrange()
b) randint()
c) randomstep()
d) step_random()
Answer: a) randrange()
21. Which function returns the average of numbers in the statistics module?
a) average()
b) mean()
c) mid()
d) avg()
Answer: b) mean()
27. What happens if you use pow() from math on two numbers?
a) Returns sum
b) Returns power
c) Returns multiplication
d) Returns exponent base e
Answer: b) Returns power
21. What will happen if a local variable has the same name as a global one?
a) Global is modified
b) Error
c) Local overrides within the function
d) Both are removed
Answer: c) Local overrides within the function
24. If a function has both positional and default arguments, which must come first?
a) Default
b) Positional
c) Either
d) Keyword
Answer: b) Positional
25. Which keyword exits the function and sends back a value?
a) exit
b) stop
c) return
d) end
Answer: c) return
26. What is the return type when a function returns two values?
a) List
b) Set
c) Tuple
d) Dictionary
Answer: c) Tuple
40. What is the default return value of a function with no return statement?
a) 0
b) ""
c) None
d) False
Answer: c) None
14. Which exception is raised for invalid value conversion (like converting 'abc' to int)?
a) ValueError
b) TypeError
c) IndexError
d) KeyError
Answer: a) ValueError
17. Which keyword is used to define a block where exception might occur?
a) handle
b) try
c) block
d) catch
Answer: b) try
18. What happens if an exception occurs inside try and is not matched in except?
a) Error is ignored
b) Program continues
c) Program crashes
d) finally is skipped
Answer: c) Program crashes
19. Which exception is raised when accessing a dictionary with a non-existing key?
a) KeyError
b) NameError
c) TypeError
d) ValueError
Answer: a) KeyError
20. Which exception is raised when the input is not as expected?
a) TypeError
b) ValueError
c) IndexError
d) IOError
Answer: b) ValueError
23. What happens if both try and finally blocks exist and no error occurs?
a) try is skipped
b) finally is skipped
c) both run
d) only finally runs
Answer: c) both run
25. Can there be multiple except blocks with one try block?
a) No
b) Yes
c) Only two
d) Only one
Answer: b) Yes