0% found this document useful (0 votes)
10 views

Python MCQs

The document contains multiple-choice questions (MCQs) covering Python basics, data types, and operators. Each question is followed by the correct answer, providing a comprehensive overview of fundamental Python concepts. Topics include features of Python, data types, variable handling, and operator functionalities.

Uploaded by

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

Python MCQs

The document contains multiple-choice questions (MCQs) covering Python basics, data types, and operators. Each question is followed by the correct answer, providing a comprehensive overview of fundamental Python concepts. Topics include features of Python, data types, variable handling, and operator functionalities.

Uploaded by

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

Python Basics MCQs

1. Which of the following is a feature of Python?


a) Compiled language
b) Statically typed
c) Interpreted and dynamically typed
d) Case-insensitive
Answer: c) Interpreted and dynamically typed

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!

3. Which mode allows immediate execution of Python commands one at a time?


a) Script mode
b) Batch mode
c) Compilation mode
d) Interactive mode
Answer: d) Interactive mode

4. What is the default file extension of a Python script file?


a) .pt
b) .py
c) .txt
d) .pyt
Answer: b) .py

5. Which of the following is a valid Python identifier?


a) 2value
b) value_1
c) value-1
d) class
Answer: b) value_1

6. Which of the following is a Python keyword?


a) for
b) input
c) print
d) range
Answer: a) for

7. What does the '#' symbol denote in Python?


a) Error
b) String
c) Comment
d) Loop
Answer: c) Comment
8. Which of the following is NOT a valid Python literal?
a) 10
b) 10.5
c) 'Python'
d) @value
Answer: d) @value

9. Which of the following is a punctuator in Python?


a) #
b) (
c) 10
d) value
Answer: b) (

10. In Python, what is the purpose of the equal sign (=)?


a) Equality check
b) Assignment operator
c) Loop control
d) Logical comparison
Answer: b) Assignment operator

11. In Python, the right-hand side of an assignment is referred to as:


a) L-value
b) Address
c) R-value
d) Constant
Answer: c) R-value

12. Which of the following would be considered an l-value in Python?


a) 25
b) x
c) "Hello"
d) 10 + 5
Answer: b) x

13. Which of the following is true about variables in Python?


a) Variable names must begin with a number
b) Variable names are case-insensitive
c) Variables are declared with data types
d) Variables are created when first assigned
Answer: d) Variables are created when first assigned

14. Which character set does Python primarily use?


a) ASCII
b) Binary
c) Unicode
d) UTF-7
Answer: c) Unicode
15. Which one is NOT a valid Python operator?
a) +
b) %
c) **
d) $$
Answer: d) $$

16. What will happen if you use a keyword as a variable name?


a) It will be treated as a normal variable
b) It will throw a runtime warning
c) It will give a syntax error
d) It will overwrite the keyword
Answer: c) It will give a syntax error

17. What is the correct way to include a multi-line comment in Python?


a) /* This is a comment */
b) // This is a comment
c) ''' This is a comment '''
d) -- This is a comment
Answer: c) ''' This is a comment '''

18. Which of the following is an example of a string literal?


a) "Hello"
b) Hello
c) 123
d) 45.6
Answer: a) "Hello"

19. Which of these represents a floating-point literal in Python?


a) 100
b) '100'
c) 100.0
d) True
Answer: c) 100.0

20. Python treats a single-line comment as starting with:


a) //
b) ;
c) #
d) !
Answer: c) #

21. Which of these operators is used for exponentiation in Python?


a) ^
b) **
c) ^^
d) exp
Answer: b) **
22. Choose the correct statement about Python variables:
a) Python needs you to declare variables with a keyword
b) Variables cannot be reassigned once created
c) The data type of a variable is fixed
d) Variables can be of different types during execution
Answer: d) Variables can be of different types during execution

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

Python Data Types – MCQs


1. Which of the following is a valid numeric data type in Python?
a) integer, double, float
b) int, float, complex
c) number, float, char
d) int, decimal, fraction
Answer: b) int, float, complex

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

3. What is the data type of the value True in Python?


a) int
b) boolean
c) string
d) float
Answer: b) boolean
4. Which of the following values represents a float in Python?
a) 10
b) '10'
c) 10.0
d) True
Answer: c) 10.0

5. Which one of the following is a sequence data type?


a) dictionary
b) float
c) list
d) boolean
Answer: c) list

6. Which of the following is mutable in Python?


a) list
b) tuple
c) string
d) integer
Answer: a) list

7. Which of the following is an immutable data type in Python?


a) list
b) dictionary
c) tuple
d) set
Answer: c) tuple

8. What is the value of None typically used for in Python?


a) Initializing an integer
b) Representing an empty string
c) Representing the absence of a value
d) Storing zero
Answer: c) Representing the absence of a value

9. Which of the following data types can store key-value pairs?


a) list
b) set
c) dictionary
d) tuple
Answer: c) dictionary

10. Which one of these is NOT a built-in Python sequence type?


a) string
b) list
c) dictionary
d) tuple
Answer: c) dictionary
11. Which of these is both ordered and immutable?
a) list
b) tuple
c) dictionary
d) set
Answer: b) tuple

12. What happens if you try to modify a tuple after creation?


a) It updates the value
b) It gives a syntax warning
c) It throws a TypeError
d) It silently fails
Answer: c) It throws a TypeError

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

15. What is the result of checking the type of None?


a) int
b) null
c) NoneType
d) string
Answer: c) NoneType

16. Which data type allows indexing and slicing but cannot be changed?
a) list
b) dictionary
c) tuple
d) set
Answer: c) tuple

17. Which of the following is a correct statement about Python strings?


a) Strings are mutable
b) Strings are lists
c) Strings are immutable
d) Strings can store only numbers
Answer: c) Strings are immutable
18. Which Python data type is a mapping type?
a) set
b) tuple
c) dictionary
d) list
Answer: c) dictionary

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

22. Which type would be most suitable to represent a sequence of characters?


a) list
b) int
c) string
d) dictionary
Answer: c) string

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

26. Which of these types can be keys in a dictionary?


a) list
b) tuple
c) dictionary
d) set
Answer: b) tuple

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

Python Operators – MCQs


1. Which of the following is an arithmetic operator in Python?
a) and
b) ==
c) +
d) is
Answer: c) +

2. Which operator is used to compare the equality of two values in Python?


a) =
b) ==
c) :=
d) !=
Answer: b) ==

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


a) &
b) ||
c) and
d) +=
Answer: c) and

4. Which operator returns True if both operands are True?


a) or
b) not
c) and
d) is
Answer: c) and
5. Which of these is an assignment operator in Python?
a) ==
b) :=
c) =
d) is
Answer: c) =

6. Which of the following is an augmented assignment operator in Python?


a) +
b) =
c) +=
d) ++
Answer: c) +=

7. Which operator is used to check if two variables refer to the same object?
a) ==
b) is
c) :=
d) in
Answer: b) is

8. Which of these is a membership operator?


a) ==
b) not
c) in
d) is
Answer: c) in

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

10. Which operator checks if a value is not present in a sequence?


a) not
b) !=
c) not in
d) is not
Answer: c) not in

11. Which of these operators has the highest precedence?


a) or
b) and
c) not
d) is
Answer: c) not
12. What will be the result of using the 'is' operator between two identical integers with same value?
a) True
b) False
c) SyntaxError
d) Depends on memory address
Answer: a) True

13. Which of the following is used for exponentiation in Python?


a) ^
b) **
c) ^^
d) exp
Answer: b) **

14. Which operator divides and returns only the integer part?
a) /
b) %
c) //
d) **
Answer: c) //

15. Which operator returns the remainder after division in Python?


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

18. Which of the following is an example of an augmented assignment operator?


a) x = x + 1
b) x := x + 1
c) x += 1
d) x + 1 = x
Answer: c) x += 1
19. Which of the following operators is used to subtract one value from another in Python?
a) –
b) ++
c) //
d) **
Answer: a) –

20. What will be the result of the expression: 10 // 3?


a) 3.33
b) 3
c) 4
d) 0
Answer: b) 3

21. The result of 10 % 3 in Python is:


a) 1
b) 0
c) 3
d) 10
Answer: a) 1

22. Which logical operator gives True if at least one operand is True?
a) and
b) or
c) not
d) is
Answer: b) or

23. Which of the following statements is True?


a) The operator '==' checks object identity
b) The operator 'is' checks value equality
c) The operator 'is' checks object identity
d) The operator '=' compares two variables
Answer: c) The operator 'is' checks object identity

24. Which of these operators cannot be overloaded in Python?


a) +
b) //
c) is
d) *
Answer: c) is

25. What is the use of 'is not' in Python?


a) It checks if variables are equal
b) It checks for membership
c) It checks if two objects are not the same
d) It returns the opposite of 'not'
Answer: c) It checks if two objects are not the same
26. Which of these is a valid chain of logical operators?
a) and or not
b) or and or
c) not not not
d) All of the above
Answer: d) All of the above

27. Which assignment operator doubles the value of a variable in place?


a) =
b) *=
c) ++
d) **=
Answer: b) *=

28. Which of the following is not a relational operator?


a) <
b) ==
c) >=
d) =
Answer: d) =

29. Which operator assigns a value only if the variable is not already assigned?
a) =
b) :=
c) is
d) +=
Answer: b) :=

30. What does the operator ‘not in’ check?


a) If value is outside a loop
b) If value is absent in a collection
c) If value is a negative number
d) If variable is declared
Answer: b) If value is absent in a collection

Python Expressions, Type Conversion, Input/Output – MCQs


1. What is an expression in Python?
a) A block of code with a name
b) A combination of values and operators that can be evaluated
c) A loop or condition
d) A comment
Answer: b) A combination of values and operators that can be evaluated

2. What is a statement in Python?


a) A variable assignment only
b) A line that performs an action or operation
c) A print command only
d) A comment line
Answer: b) A line that performs an action or operation
3. Which of the following has the highest operator precedence?
a) and
b) +
c) **
d) ==
Answer: c) **

4. What is the result of evaluating 2 + 3 * 4?


a) 20
b) 14
c) 24
d) 9
Answer: b) 14

5. Which comes first according to operator precedence in Python?


a) Multiplication
b) Logical AND
c) Addition
d) Comparison
Answer: a) Multiplication

6. Which operator has the lowest precedence in Python?


a) or
b) and
c) not
d) ==
Answer: a) or

7. Which of the following is an example of implicit type conversion?


a) Manually converting int to float
b) Python automatically converting int to float during addition
c) Using a casting function
d) None of the above
Answer: b) Python automatically converting int to float during addition

8. Which function is used for explicit type conversion to integer?


a) convert()
b) cast()
c) int()
d) toInt()
Answer: c) int()

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

11. Which function is used to display output to the console?


a) display()
b) echo()
c) output()
d) print()
Answer: d) print()

12. What is the default type of value returned by the input function?
a) integer
b) string
c) boolean
d) float
Answer: b) string

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


a) a + b
b) 5 *
c) = 10
d) print
Answer: a) a + b

14. Which of the following is NOT a valid statement in Python?


a) x = 10
b) print('Hello')
c) 5 + 5
d) if x > 5:
Answer: c) 5 + 5

15. Which of the following best describes explicit type conversion?


a) Python decides the type change
b) Manual conversion using functions
c) Automatic change in type
d) Converting string to integer without code
Answer: b) Manual conversion using functions

16. Which type conversion happens without any user intervention?


a) Explicit
b) Manual
c) Implicit
d) External
Answer: c) Implicit
17. Which of the following statements will result in implicit type conversion?
a) Adding int and float
b) Casting int using int()
c) Reading user input
d) Adding two strings
Answer: a) Adding int and float

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

20. Which is the correct behavior of the input function?


a) It returns a boolean
b) It reads numeric values directly
c) It returns a string
d) It always throws an error
Answer: c) It returns a string

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

25. *Which type of expression involves +, -, , and / operators?


a) Assignment
b) Logical
c) Arithmetic
d) Identity
Answer: c) Arithmetic

26. Which of these conversions is considered safe and automatic in Python?


a) int to float
b) float to int
c) string to int
d) float to string
Answer: a) int to float

27. Which function helps to check the type of any object in Python?
a) typeof()
b) checktype()
c) type()
d) datatype()
Answer: c) type()

28. What will type(5 + 3.2) return?


a) int
b) float
c) complex
d) string
Answer: b) float

29. In which case will Python perform implicit conversion?


a) Subtracting a string from an integer
b) Dividing two integers
c) Adding an integer to a float
d) Multiplying a string with a string
Answer: c) Adding an integer to a float

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=''

Python Errors – MCQs


1. Which of the following occurs when Python code violates the grammar rules of the language?
a) Runtime error
b) Logical error
c) Syntax error
d) Semantic error
Answer: c) Syntax error

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

3. What kind of error will occur if a variable is used before it is defined?


a) NameError
b) TypeError
c) SyntaxError
d) ValueError
Answer: a) NameError

4. Which of the following is an example of a runtime error?


a) Missing colon after if statement
b) Adding a string and integer
c) Using tabs and spaces inconsistently
d) Misspelled keyword
Answer: b) Adding a string and integer

5. What kind of error is caused by division by zero?


a) SyntaxError
b) LogicalError
c) ZeroDivisionError
d) IndexError
Answer: c) ZeroDivisionError

6. Which of the following best defines a syntax error?


a) Wrong output due to logic
b) Mistake in code structure
c) Issue with variable values
d) Error due to slow performance
Answer: b) Mistake in code structure

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

9. Which of the following is an example of a syntax error?


a) Dividing a number by zero
b) Using undefined variables
c) Missing closing quotation mark
d) Using wrong formula for area
Answer: c) Missing closing quotation mark

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

11. What type of error is caused by mismatched parentheses or colons?


a) Logical error
b) Runtime error
c) Syntax error
d) Memory error
Answer: c) Syntax error

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

13. Which error is NOT raised by Python automatically?


a) ValueError
b) SyntaxError
c) Logical error
d) TypeError
Answer: c) Logical error

14. Trying to convert a non-numeric string to an integer will raise:


a) NameError
b) SyntaxError
c) ValueError
d) IndexError
Answer: c) ValueError
15. What is the result of using an undefined function in Python?
a) SyntaxError
b) TypeError
c) NameError
d) KeyError
Answer: c) NameError

16. Which of the following cannot be caught using try-except blocks?


a) Runtime errors
b) Syntax errors
c) Index errors
d) Zero division errors
Answer: b) Syntax errors

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

19. When does a syntax error occur?


a) During execution
b) After execution
c) Before execution
d) At random times
Answer: c) Before execution

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

21. Which of the following is NOT a common runtime error in Python?


a) ZeroDivisionError
b) IndexError
c) SyntaxError
d) TypeError
Answer: c) SyntaxError
22. What type of error will result if a string is added to an integer?
a) IndexError
b) SyntaxError
c) TypeError
d) LogicalError
Answer: c) TypeError

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

24. Which of the following errors is related to indentation in Python?


a) IndentationError
b) BracketError
c) SpacingError
d) LineError
Answer: a) IndentationError

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

Python Flow of Control – MCQs


1. What is the default flow of execution in a Python program?
a) Random
b) Conditional
c) Iterative
d) Sequential
Answer: d) Sequential

2. Which structure allows decisions to be made in Python code?


a) Loop
b) Function
c) Conditional
d) Class
Answer: c) Conditional

3. Which keyword is used to execute a block of code if a condition is true?


a) loop
b) check
c) if
d) case
Answer: c) if
4. Which keyword is used when the initial condition is false but an alternate block should run?
a) elif
b) else
c) continue
d) pass
Answer: b) else

5. Which keyword is used for multiple conditions in Python?


a) else-if
b) elseif
c) elif
d) ifelse
Answer: c) elif

6. What is the role of indentation in Python control structures?


a) It helps with debugging
b) It is only for readability
c) It defines the block of code
d) It is optional
Answer: c) It defines the block of code

7. Which of the following is TRUE about indentation in Python?


a) You can use spaces or tabs interchangeably
b) Indentation is ignored by the interpreter
c) Incorrect indentation leads to errors
d) Only the first line of code needs indentation
Answer: c) Incorrect indentation leads to errors

8. What type of flow does a for loop represent?


a) Conditional
b) Sequential
c) Iterative
d) Function
Answer: c) Iterative

9. What type of flow does the if statement represent?


a) Sequential
b) Conditional
c) Iterative
d) Jump
Answer: b) Conditional

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

12. What happens if the condition in a while loop is always true?


a) Error
b) Executes once
c) Infinite loop
d) Skips loop
Answer: c) Infinite loop

13. Which of the following is NOT a control flow statement in Python?


a) if
b) for
c) def
d) while
Answer: c) def

14. Which keyword is used to skip the current iteration of a loop?


a) break
b) pass
c) stop
d) continue
Answer: d) continue

15. Which keyword is used to exit a loop completely?


a) break
b) continue
c) skip
d) exit
Answer: a) break

16. Which statement is used to do nothing in a block of code?


a) skip
b) continue
c) pass
d) null
Answer: c) pass

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

19. What happens when the indentation level of a block is incorrect?


a) The code executes partially
b) Interpreter ignores the block
c) An IndentationError occurs
d) The program runs normally
Answer: c) An IndentationError occurs

20. Which statement is true about nested if statements?


a) Not allowed in Python
b) Only 2 levels are allowed
c) Indentation defines the nesting
d) Nesting requires brackets
Answer: c) Indentation defines the nesting

21. What is the purpose of using loops in a program?


a) To make conditions
b) To define functions
c) To execute code repeatedly
d) To stop execution
Answer: c) To execute code repeatedly

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

23. What is the role of the else block in an if-else statement?


a) Executes when the condition is true
b) Skips the loop
c) Executes when the condition is false
d) Defines a loop
Answer: c) Executes when the condition is false

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

26. What is the minimal requirement to write a loop in Python?


a) Condition and colon
b) Curly braces
c) Function call
d) Print statement
Answer: a) Condition and colon

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

30. In Python, the structure of a conditional block is determined by:


a) Semicolons
b) Indentation
c) Brackets
d) Tabs only
Answer: b) Indentation

Python Conditional Statements – MCQs


1. Which keyword is used to start a conditional block in Python?
a) check
b) case
c) if
d) switch
Answer: c) if
2. What is the purpose of the else clause in Python conditionals?
a) To check multiple conditions
b) To execute code if the if condition is false
c) To skip a loop
d) To repeat the block
Answer: b) To execute code if the if condition is false

3. What is the role of the elif keyword in Python?


a) Acts like else
b) Used in loops
c) Used to check multiple conditions
d) Used to end the program
Answer: c) Used to check multiple conditions

4. Which of the following is the correct structure for multiple conditions?


a) if - else
b) if - elif - else
c) while - else
d) for - elif
Answer: b) if - elif - 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

7. Which condition should be checked to print the absolute value of a number?


a) Number is negative
b) Number is zero
c) Number is positive
d) Number is non-integer
Answer: a) Number is negative

8. How many branches can an if-elif-else chain have?


a) Only one
b) Two
c) Unlimited elifs with one if and one else
d) Only three
Answer: c) Unlimited elifs with one if and one else
9. Which statement is used when there are multiple conditions to be evaluated one after the other?
a) if
b) else
c) elif
d) pass
Answer: c) elif

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

11. In an if-else structure, which block executes if the condition is true?


a) else block
b) Both
c) if block
d) None
Answer: c) if block

12. In a flowchart, a decision is usually represented by:


a) Circle
b) Rectangle
c) Diamond
d) Arrow
Answer: c) Diamond

13. Which operator is used in conditions to check equality?


a) =
b) ==
c) :=
d) !=
Answer: b) ==

14. Which symbol represents 'not equal to' in Python?


a) /=
b) !=
c) <>
d) ^=
Answer: b) !=

15. To find the largest of three numbers, which structure is appropriate?


a) if only
b) if-else only
c) if-elif-else
d) while
Answer: c) if-elif-else
16. Which condition will help to check if a number is divisible by 2?
a) Number plus 2 equals 0
b) Remainder after dividing by 2 is 1
c) Remainder after dividing by 2 is 0
d) Number minus 2 equals 0
Answer: c) Remainder after dividing by 2 is 0

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

18. Which of the following is a valid decision structure?


a) if - pass
b) if - for
c) if - while
d) if - elif - else
Answer: d) if - elif - else

19. What type of problem is sorting 3 numbers into ascending order?


a) Looping
b) Arithmetic
c) Conditional
d) Logical
Answer: c) Conditional

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

24. Which of the following keywords starts a conditional block?


a) begin
b) check
c) if
d) end
Answer: c) if

25. What happens if you skip the colon in an if statement?


a) Runs normally
b) Prints nothing
c) SyntaxError
d) Returns 0
Answer: c) SyntaxError

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

27. In a flowchart, which element connects different blocks?


a) Arrows
b) Circles
c) Diamonds
d) Lines
Answer: a) Arrows

28. Which condition checks if one number is greater than another?


a) =
b) >=
c) >
d) <=
Answer: c) >

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

Python Iterative Statements – MCQs


1. Which keyword is used to create a loop that iterates over a sequence in Python?
a) while
b) for
c) repeat
d) do
Answer: b) for

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

4. Which loop is most suitable when the number of iterations is known?


a) while
b) do-while
c) for
d) if
Answer: c) for

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

6. Which of the following is used to exit the current loop completely?


a) exit
b) continue
c) break
d) stop
Answer: c) break
7. Which of the following skips the current iteration and continues with the next one?
a) skip
b) continue
c) pass
d) stop
Answer: b) continue

8. What will the range(5) function generate?


a) 1 to 5
b) 0 to 4
c) 0 to 5
d) 5 to 0
Answer: b) 0 to 4

9. In a while loop, the condition is checked:


a) After executing the loop once
b) At the end of the loop
c) Before each iteration
d) Only once
Answer: c) Before each iteration

10. What happens if the condition in a while loop is always true?


a) Executes once
b) Executes twice
c) Infinite loop
d) Throws an error
Answer: c) Infinite loop

11. Which keyword is used to do nothing in a loop block?


a) skip
b) pass
c) stop
d) exit
Answer: b) pass

12. Which loop is best for generating a pattern of stars or numbers?


a) while loop
b) if statement
c) nested loop
d) break
Answer: c) nested loop

13. How many times will the loop range(1, 6) iterate?


a) 5
b) 6
c) 4
d) Infinite
Answer: a) 5
14. Which function can be used in loops to create a series of numbers with a fixed step size?
a) loop()
b) count()
c) range()
d) series()
Answer: c) range()

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

16. What type of loop is required to calculate the factorial of a number?


a) for or while
b) if-else
c) switch-case
d) try-except
Answer: a) for or while

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

18. In a flowchart, a loop is generally represented by which shape?


a) Circle
b) Diamond
c) Rectangle
d) Hexagon
Answer: b) Diamond

19. What does the continue statement do inside a loop?


a) Ends the loop
b) Skips the next loop
c) Skips current iteration and jumps to the next
d) Restarts the program
Answer: c) Skips current iteration and jumps to the next

20. Which of the following is a correct loop structure in Python?


a) for…range()
b) repeat…until
c) do…while
d) loop…next
Answer: a) for…range()
21. Which loop is ideal for reading user input until a condition is met?
a) for loop
b) while loop
c) if-else
d) break
Answer: b) while loop

22. What is the purpose of using nested loops?


a) To break from outer loop
b) To perform repeated actions inside another loop
c) To run only one loop
d) To exit a program
Answer: b) To perform repeated actions inside another loop

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

24. Which type of loop is helpful to generate multiplication tables?


a) if statement
b) for loop
c) while loop
d) nested loop
Answer: b) for loop

25. In nested loops, which loop finishes first?


a) Outer loop
b) First loop
c) Inner loop
d) Any random loop
Answer: c) Inner loop

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

30. What is a common condition in a loop for finding factorial?


a) Number equals 0
b) Product not zero
c) Loop from 1 to number
d) Loop until sum equals number
Answer: c) Loop from 1 to number

31. What will be the output of summing values using range(1, 6)?
a) 10
b) 15
c) 6
d) 5
Answer: b) 15

32. What does the pass statement do in a loop?


a) Exits the loop
b) Skips current iteration
c) Does nothing
d) Jumps to else block
Answer: c) Does nothing

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

Python Strings – MCQs


1. What is a string in Python?
a) A numeric value
b) A sequence of characters
c) A list of integers
d) A block of code
Answer: b) A sequence of characters

2. Which operator is used for string concatenation?


a) &
b) +
c) *
d) %
Answer: b) +

3. Which operator is used to repeat a string multiple times?


a) +
b) *
c) %
d) &
Answer: b) *

4. What does the in operator check in a string?


a) Index
b) Membership
c) Length
d) Type
Answer: b) Membership

5. What does slicing allow you to do with a string?


a) Reverse it
b) Modify its case
c) Extract a part of it
d) Concatenate two strings
Answer: c) Extract a part of it

6. Which function returns the number of characters in a string?


a) count()
b) length()
c) size()
d) len()
Answer: d) len()
7. Which method converts the first character of the string to uppercase?
a) upper()
b) capitalize()
c) title()
d) start()
Answer: b) capitalize()

8. Which method capitalizes the first letter of each word in a string?


a) capitalize()
b) title()
c) upper()
d) start()
Answer: b) title()

9. Which method converts all characters in a string to lowercase?


a) lower()
b) down()
c) small()
d) mincase()
Answer: a) lower()

10. Which method converts all characters to uppercase?


a) capital()
b) title()
c) upper()
d) case()
Answer: c) upper()

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

13. Which method throws an error if the character is not found?


a) find()
b) index()
c) locate()
d) count()
Answer: b) index()
14. Which method checks if the string ends with a specific substring?
a) end()
b) endswith()
c) finish()
d) tail()
Answer: b) endswith()

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

17. Which method returns True if all characters are alphabets?


a) isalpha()
b) isalnum()
c) isdigit()
d) alpha()
Answer: a) isalpha()

18. Which method checks if all characters are digits?


a) isnumber()
b) isalnum()
c) isdigit()
d) isnumeric()
Answer: c) isdigit()

19. Which method checks if all characters are lowercase?


a) islower()
b) lower()
c) isalpha()
d) title()
Answer: a) islower()

20. Which method checks if all characters are uppercase?


a) isupper()
b) upper()
c) iscapital()
d) capital()
Answer: a) isupper()
21. Which method checks if the string contains only whitespace?
a) isempty()
b) isspace()
c) space()
d) trim()
Answer: b) isspace()

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

26. Which method joins elements of a list into a single string?


a) join()
b) add()
c) merge()
d) append()
Answer: a) join()

27. Which method splits a string into a list?


a) cut()
b) split()
c) divide()
d) break()
Answer: b) split()
28. Which method divides the string into three parts based on a separator?
a) slice()
b) cut()
c) partition()
d) trim()
Answer: c) partition()

29. Which of the following operations is NOT allowed on strings?


a) Concatenation
b) Slicing
c) Repetition
d) Item assignment
Answer: d) Item assignment

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

31. Which operation can be used to check if a character is in a string?


a) find
b) index
c) in
d) locate
Answer: c) in

32. Which loop is commonly used to traverse each character in a string?


a) while
b) for
c) do
d) if
Answer: b) for

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

34. Which method will convert "python programming" to "Python Programming"?


a) capitalize()
b) title()
c) upper()
d) replace()
Answer: b) title()
35. Which method is used to search for a substring in a string and return the position?
a) search()
b) index()
c) find()
d) check()
Answer: c) find()

36. What is the result of len() on an empty string?


a) 1
b) 0
c) -1
d) Error
Answer: b) 0

37. Which method will return True for "123"?


a) isalpha()
b) isalnum()
c) isdigit()
d) isspace()
Answer: c) isdigit()

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

39. Which method returns the position of a substring, or -1 if not found?


a) find()
b) index()
c) locate()
d) count()
Answer: a) find()

40. Which of the following is TRUE about Python strings?


a) Strings are mutable
b) Strings are immutable
c) Strings are declared using commas
d) Strings can't be sliced
Answer: b) Strings are immutable

Python Lists – MCQs


1. What is a list in Python?
a) A string of characters
b) A sequence of immutable items
c) A mutable ordered collection
d) A single value container
Answer: c) A mutable ordered collection
2. Which type of brackets is used to define a list in Python?
a) ()
b) {}
c) []
d) <>
Answer: c) []

3. What does list indexing in Python start with?


a) -1
b) 0
c) 1
d) Depends on values
Answer: b) 0

4. Which operator is used to concatenate two lists?


a) *
b) +
c) &
d) %
Answer: b) +

5. Which operator repeats elements of a list?


a) +
b) %
c) *
d) /
Answer: c) *

6. Which keyword checks if an element exists in a list?


a) has
b) with
c) in
d) of
Answer: c) in

7. Which operation is used to extract a sublist from a list?


a) slicing
b) splicing
c) splitting
d) binding
Answer: a) slicing

8. What does the len() function return for a list?


a) Total size
b) Number of elements
c) Number of types
d) Sum of values
Answer: b) Number of elements
9. Which method adds a single element to the end of a list?
a) append()
b) insert()
c) extend()
d) add()
Answer: a) append()

10. Which method adds multiple elements to the end of a list?


a) add()
b) insert()
c) append()
d) extend()
Answer: d) extend()

11. Which method inserts an element at a specific index?


a) insert()
b) append()
c) add()
d) put()
Answer: a) insert()

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

17. Which method sorts a list in ascending order in place?


a) order()
b) sort()
c) sorted()
d) arrange()
Answer: b) sort()

18. Which function returns a new sorted list without changing the original?
a) arrange()
b) sort()
c) sorted()
d) order()
Answer: c) sorted()

19. Which function returns the smallest element in a list?


a) min()
b) smallest()
c) low()
d) minval()
Answer: a) min()

20. Which function returns the largest element in a list?


a) max()
b) largest()
c) high()
d) maxval()
Answer: a) max()

21. Which function gives the sum of numeric values in a list?


a) add()
b) total()
c) sum()
d) count()
Answer: c) sum()

22. Which of the following supports indexing?


a) list
b) set
c) dictionary
d) all of the above
Answer: a) list
23. What will be the result of accessing an index greater than the list length?
a) Zero
b) None
c) IndexError
d) Negative value
Answer: c) IndexError

24. What is the type of a list created using list()?


a) int
b) str
c) list
d) tuple
Answer: c) list

25. What is a nested list?


a) A list with one element
b) A list with repeated elements
c) A list inside another list
d) A sorted list
Answer: c) A list inside another list

26. Which loop is typically used to traverse through a list?


a) if
b) while
c) for
d) switch
Answer: c) for

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

28. What is the result of sum(list) / len(list) for numeric values?


a) Maximum
b) Minimum
c) Mean
d) Median
Answer: c) Mean

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

32. Which list operation is not valid?


a) Indexing
b) Assignment
c) Item deletion
d) Direct arithmetic between two lists
Answer: d) Direct arithmetic between two lists

33. How many arguments does insert() take?


a) One
b) Two
c) Three
d) None
Answer: b) Two

34. What does pop(0) do in a list?


a) Deletes the last element
b) Deletes the first element
c) Deletes a random element
d) Returns the list length
Answer: b) Deletes the first element

35. What happens when reverse() is called on a list?


a) Returns a new reversed list
b) Sorts the list in descending order
c) Modifies the original list
d) Replaces items with reversed strings
Answer: c) Modifies the original list

36. Which method is best for sorting names alphabetically?


a) sorted()
b) max()
c) insert()
d) count()
Answer: a) sorted()
37. Which method is best to remove all occurrences of a value from a list?
a) remove() in loop
b) pop()
c) clear()
d) index()
Answer: a) remove() in loop

38. What does list() do when applied to a string?


a) Gives length
b) Creates list of characters
c) Returns an error
d) Converts to integer
Answer: b) Creates list of characters

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

Python Tuples – MCQs


1. What is a tuple in Python?
a) A mutable sequence
b) An immutable sequence
c) A dictionary
d) A function
Answer: b) An immutable sequence

2. Which brackets are used to define a tuple?


a) {}
b) []
c) ()
d) <>
Answer: c) ()

3. Which of the following is TRUE about tuples?


a) Their elements can be modified
b) They support indexing
c) They support item deletion
d) They allow duplicate keys
Answer: b) They support indexing
4. What is the result of accessing an invalid index in a tuple?
a) 0
b) None
c) IndexError
d) False
Answer: c) IndexError

5. Which operator is used to concatenate two tuples?


a) *
b) &
c) +
d) %
Answer: c) +

6. Which operator is used to repeat a tuple multiple times?


a) %
b) *
c) +
d) //
Answer: b) *

7. Which keyword is used to check membership in a tuple?


a) exist
b) in
c) has
d) find
Answer: b) in

8. Which function returns the number of elements in a tuple?


a) count()
b) index()
c) size()
d) len()
Answer: d) len()

9. Which function creates a tuple from a string or list?


a) tuple()
b) list()
c) set()
d) str()
Answer: a) tuple()

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

12. Which function returns the smallest value in a tuple?


a) min()
b) low()
c) bottom()
d) small()
Answer: a) min()

13. Which function returns the largest value in a tuple?


a) top()
b) high()
c) max()
d) maxval()
Answer: c) max()

14. Which function returns the sum of numeric values in a tuple?


a) total()
b) sum()
c) count()
d) add()
Answer: b) sum()

15. Which function sorts elements of a tuple and returns a list?


a) sort()
b) order()
c) sorted()
d) arrange()
Answer: c) sorted()

16. What happens if you try to change a value inside a tuple?


a) The value changes
b) A warning is shown
c) An error occurs
d) It becomes a list
Answer: c) An error occurs

17. How do you define a tuple with a single element?


a) (5)
b) (5,)
c) 5,
d) Both b and c
Answer: d) Both b and c
18. What is a nested tuple?
a) A tuple with repeated values
b) A tuple inside another tuple
c) A tuple of strings
d) A tuple that can be modified
Answer: b) A tuple inside another tuple

19. Which of the following is valid for slicing a tuple?


a) Using indices
b) Using keys
c) Using dot notation
d) Using parentheses
Answer: a) Using indices

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

21. What does tuple assignment allow you to do?


a) Modify tuple values
b) Assign multiple variables in one line
c) Change data type
d) Remove duplicate values
Answer: b) Assign multiple variables in one line

22. Which of the following will return a sorted version of a tuple?


a) sort()
b) sorted()
c) tuple.sort()
d) order()
Answer: b) sorted()

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

24. What data type is the result of using sorted() on a tuple?


a) tuple
b) list
c) set
d) string
Answer: b) list
25. Which function is used to convert a list into a tuple?
a) convert()
b) tuple()
c) list()
d) str()
Answer: b) tuple()

26. What is the output of len() when applied to an empty tuple?


a) 1
b) 0
c) None
d) Error
Answer: b) 0

27. Which of these is immutable?


a) List
b) Dictionary
c) Tuple
d) Set
Answer: c) Tuple

28. Which of the following is TRUE about tuple elements?


a) Cannot contain lists
b) Can only be strings
c) Can be of any data type
d) Must be unique
Answer: c) Can be of any data type

29. Which method is NOT available for tuples but available for lists?
a) count()
b) index()
c) append()
d) len()
Answer: c) append()

30. Which of the following statements is false about tuples?


a) Tuples are ordered
b) Tuples are immutable
c) Tuples allow indexing
d) Tuples support item assignment
Answer: d) Tuples support item assignment

Python Dictionaries – MCQs


1. What is a dictionary in Python?
a) Ordered collection of values
b) Unordered collection of key-value pairs
c) Mutable sequence of values
d) Immutable mapping
Answer: b) Unordered collection of key-value pairs
2. Which symbol is used to define a dictionary?
a) []
b) ()
c) {}
d) <>
Answer: c) {}

3. What is used to access values in a dictionary?


a) Index numbers
b) Keys
c) Length
d) Range
Answer: b) Keys

4. Which of the following will raise an error?


a) Accessing a key that doesn't exist
b) Using the get() method
c) Modifying an existing key
d) Adding a new key
Answer: a) Accessing a key that doesn't exist

5. Which method avoids an error when accessing a missing key?


a) access()
b) get()
c) pop()
d) find()
Answer: b) get()

6. Dictionaries are:
a) Immutable
b) Mutable
c) Static
d) Read-only
Answer: b) Mutable

7. Which method adds or updates key-value pairs in a dictionary?


a) insert()
b) append()
c) update()
d) add()
Answer: c) update()

8. Which method removes all items from a dictionary?


a) clear()
b) delete()
c) remove()
d) pop()
Answer: a) clear()
9. What does the keys() method return?
a) List of values
b) List of keys
c) Dictionary length
d) Tuple of keys
Answer: b) List of keys

10. What does the values() method return?


a) Dictionary keys
b) List of tuples
c) All values in the dictionary
d) Dictionary length
Answer: c) All values in the dictionary

11. Which method returns both keys and values together?


a) keys()
b) values()
c) items()
d) get()
Answer: c) items()

12. Which method removes a specific key and returns its value?
a) pop()
b) remove()
c) del()
d) discard()
Answer: a) pop()

13. Which statement removes a key-value pair using the key?


a) delete dict[key]
b) remove dict[key]
c) del dict[key]
d) pop dict[key]
Answer: c) del dict[key]

14. Which method removes and returns the last inserted item?
a) pop()
b) popitem()
c) clear()
d) remove()
Answer: b) popitem()

15. Which method creates a dictionary from a sequence of keys?


a) keys()
b) fromkeys()
c) dict()
d) init()
Answer: b) fromkeys()
16. Which function returns a shallow copy of a dictionary?
a) duplicate()
b) copy()
c) clone()
d) deepcopy()
Answer: b) copy()

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

18. What does the dict() constructor do?


a) Creates a list
b) Converts list to tuple
c) Creates a dictionary
d) Converts string to dict
Answer: c) Creates a dictionary

19. Which function returns the total number of key-value pairs?


a) count()
b) total()
c) len()
d) size()
Answer: c) len()

20. What will max() return when used on a dictionary?


a) Highest value
b) Highest key
c) Largest item
d) Error
Answer: b) Highest key

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)

22. Which of the following is a valid dictionary key?


a) List
b) Tuple
c) Dictionary
d) Set
Answer: b) Tuple
23. What happens when two keys in a dictionary are the same?
a) Error
b) Both stored
c) Last one overrides previous
d) First one is kept
Answer: c) Last one overrides previous

24. Which of the following is NOT allowed as a dictionary key?


a) int
b) str
c) list
d) tuple
Answer: c) list

25. Which method is used to traverse through all dictionary keys and values?
a) keys()
b) items()
c) get()
d) len()
Answer: b) items()

26. Which loop is typically used to traverse a dictionary?


a) for
b) while
c) do
d) switch
Answer: a) for

27. Which statement is used to delete an entire dictionary?


a) remove dict
b) clear dict
c) del dict
d) delete dict
Answer: c) del dict

28. Which method allows updating multiple values in a dictionary at once?


a) update()
b) set()
c) get()
d) insert()
Answer: a) update()

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

32. Which is the default value of fromkeys() method if not specified?


a) None
b) 0
c) “”
d) False
Answer: a) None

33. What type is returned by dict.keys()?


a) list
b) set
c) dict_keys object
d) tuple
Answer: c) dict_keys object

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

35. Which is faster for key lookups: list or dictionary?


a) List
b) Dictionary
c) Same speed
d) Tuple
Answer: b) Dictionary

36. What does dict.items() return?


a) Keys only
b) Values only
c) List of (key, value) pairs
d) List of keys
Answer: c) List of (key, value) pairs
37. Which function will give the lowest key in a dictionary?
a) min()
b) sort()
c) first()
d) low()
Answer: a) min()

38. Which function helps sort a dictionary by keys?


a) sorted(dict)
b) dict.sort()
c) keys().sort()
d) None of the above
Answer: a) sorted(dict)

39. Which method returns a view object of the dictionary's values?


a) items()
b) keys()
c) values()
d) get()
Answer: c) values()

40. Which of the following statements is correct about dictionary traversal?


a) You can access keys only
b) You can access both keys and values
c) You can access values only
d) You can’t traverse dictionaries
Answer: b) You can access both keys and values

Python Modules – MCQs


1. What is a Python module?
a) A file containing variables
b) A file with only comments
c) A file with Python code (functions, variables)
d) A database file
Answer: c) A file with Python code (functions, variables)

2. Which keyword is used to import a module in Python?


a) get
b) use
c) import
d) include
Answer: c) import

3. Which syntax is used to import specific items from a module?


a) import from
b) using from
c) from module import item
d) get module item
Answer: c) from module import item
4. What does import math do?
a) Runs all math operations
b) Loads the math module
c) Creates a math object
d) Converts integers
Answer: b) Loads the math module

5. Which module provides mathematical functions like square root and trigonometry?
a) random
b) statistics
c) math
d) numpy
Answer: c) math

6. Which constant in the math module represents the value of π (pi)?


a) math.pi
b) pi()
c) math.pie
d) constant.pi
Answer: a) math.pi

7. What does math.e represent?


a) Euler’s number
b) Log base 2
c) Error rate
d) Exponential sum
Answer: a) Euler’s number

8. Which function gives the square root of a number in math module?


a) sqrt()
b) root()
c) square()
d) power()
Answer: a) sqrt()

9. Which function rounds a number up to the next whole number?


a) round()
b) ceil()
c) floor()
d) top()
Answer: b) ceil()

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

12. Which function returns the absolute value as a float?


a) abs()
b) fabs()
c) floatabs()
d) absval()
Answer: b) fabs()

13. Which function returns the sine of an angle in radians?


a) sin()
b) sine()
c) rad_sin()
d) trig()
Answer: a) sin()

14. Which function returns the cosine of an angle in radians?


a) costheta()
b) trig_cos()
c) cos()
d) cosine()
Answer: c) cos()

15. Which function returns the tangent of an angle in radians?


a) tan()
b) tanx()
c) tangent()
d) trig_tan()
Answer: a) tan()

16. Which module is used to generate random numbers?


a) math
b) statistics
c) random
d) numpy
Answer: c) random

17. Which function generates a random float between 0 and 1?


a) rand()
b) random()
c) randint()
d) float()
Answer: b) random()
18. Which function returns a random integer between two values (inclusive)?
a) randrange()
b) randint()
c) random()
d) range()
Answer: b) randint()

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

20. What does the statistics module help with?


a) Calculus operations
b) Matrix algebra
c) Basic statistical operations
d) Graph plotting
Answer: c) Basic statistical operations

21. Which function returns the average of numbers in the statistics module?
a) average()
b) mean()
c) mid()
d) avg()
Answer: b) mean()

22. Which function returns the middle value of a list of numbers?


a) mean()
b) middle()
c) median()
d) midval()
Answer: c) median()

23. Which function returns the most frequent value in a dataset?


a) mode()
b) max()
c) mean()
d) median()
Answer: a) mode()

24. What is the result of importing an entire module using import?


a) Only one function is available
b) All functions and constants are available with prefix
c) Functions are copied into the script
d) Only constants are accessible
Answer: b) All functions and constants are available with prefix
25. Which is correct to import only sqrt from the math module?
a) from math import sqrt
b) import sqrt from math
c) use math.sqrt
d) import math.sqrt
Answer: a) from math import sqrt

26. How do you access pi after importing only it from math?


a) math.pi
b) pi()
c) just pi
d) from.pi
Answer: c) just pi

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

28. Which module must be imported to use random() function?


a) math
b) statistics
c) random
d) numbers
Answer: c) random

29. What does randint(1, 10) return?


a) Always 5
b) Any float
c) An integer between 1 and 10
d) An error
Answer: c) An integer between 1 and 10

30. Which of these statements is TRUE about median()?


a) Returns smallest number
b) Returns sum divided by count
c) Returns average of middle two numbers if list is even
d) Returns largest number
Answer: c) Returns average of middle two numbers if list is even

31. Which function in math returns decimal square root only?


a) pow()
b) sqrt()
c) root()
d) sqrt_int()
Answer: b) sqrt()
32. Which of the following will return True if random() returns a float?
a) type is string
b) type is integer
c) type is float
d) None of the above
Answer: c) type is float

33. Which module must be imported before using median() or mode()?


a) math
b) random
c) statistics
d) numpy
Answer: c) statistics

34. Which function is used to round up a number in the math module?


a) floor()
b) round()
c) ceil()
d) up()
Answer: c) ceil()

35. What will from math import * do?


a) Import selected functions
b) Import only math.pi
c) Import all functions/constants from math
d) Throw error
Answer: c) Import all functions/constants from math

Python Functions – MCQs


1. What is a function in Python?
a) A loop
b) A block of code that performs a specific task
c) A variable
d) A list of operations
Answer: b) A block of code that performs a specific task

2. Which of the following is a type of function in Python?


a) Built-in
b) User-defined
c) Module-defined
d) All of the above
Answer: d) All of the above

3. Which of the following is a built-in function?


a) print()
b) sum()
c) len()
d) All of the above
Answer: d) All of the above
4. Where are module-defined functions stored?
a) In the standard library
b) In user directories
c) In Python cache
d) Inside tuples
Answer: a) In the standard library

5. What is required to define a function?


a) def keyword
b) class keyword
c) import keyword
d) return keyword
Answer: a) def keyword

6. Which part of a function receives the input value?


a) Loop
b) Argument
c) Return
d) Exception
Answer: b) Argument

7. Which term refers to the names listed in the function definition?


a) Values
b) Parameters
c) Arguments
d) Outputs
Answer: b) Parameters

8. Which term refers to the actual values passed to a function?


a) Variables
b) Arguments
c) Parameters
d) Constants
Answer: b) Arguments

9. What happens when a function is called?


a) It prints the name
b) It executes the code in it
c) It stores memory
d) It raises an error
Answer: b) It executes the code in it

10. Which keyword is used to return a value from a function?


a) export
b) return
c) output
d) yield
Answer: b) return
11. How many values can a Python function return?
a) Only one
b) One or two
c) Multiple (in tuple form)
d) None
Answer: c) Multiple (in tuple form)

12. What happens if a function has no return statement?


a) Returns 0
b) Returns None
c) Raises error
d) Returns null
Answer: b) Returns None

13. Which of the following is a user-defined function name?


a) len
b) def myfunc
c) print
d) str
Answer: b) def myfunc

14. Which type of argument uses the position of the values?


a) Keyword
b) Default
c) Positional
d) Named
Answer: c) Positional

15. Which type of parameter provides a default value if not specified?


a) Local
b) Optional
c) Global
d) Default
Answer: d) Default

16. Which of these functions will return a value?


a) One that uses return
b) One that uses print only
c) One that has no code
d) All built-in functions
Answer: a) One that uses return

17. What is the flow of execution in a function?


a) Skips to the end
b) Runs from bottom to top
c) Top to bottom inside the function when called
d) Undefined order
Answer: c) Top to bottom inside the function when called
18. Which variable scope is created inside a function?
a) Global
b) Static
c) Local
d) Universal
Answer: c) Local

19. Which scope is accessible throughout the entire program?


a) Function
b) Global
c) Static
d) Local
Answer: b) Global

20. Which statement is used to declare a global variable inside a function?


a) access global
b) define global
c) global
d) return global
Answer: c) global

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

22. Can a function return more than one value?


a) No
b) Yes, using tuple
c) Yes, using loop
d) Only if values are integers
Answer: b) Yes, using tuple

23. What is the purpose of using parameters in a function?


a) To return values
b) To loop
c) To receive input
d) To close functions
Answer: c) To receive input

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

27. Which of the following functions is automatically available in Python?


a) custom_func()
b) max()
c) myfun()
d) randomfunc()
Answer: b) max()

28. What is the scope of a variable defined inside a function?


a) Global
b) Local
c) Class
d) Module
Answer: b) Local

29. Which function header is valid for two default parameters?


a) def func(a, b=2)
b) def func(a=2, b)
c) def func(b, a=2)
d) def func(a=2, b=3)
Answer: d) def func(a=2, b=3)

30. Which of these statements is true about user-defined functions?


a) They can’t return values
b) They must be placed at the end of the code
c) They start with def keyword
d) They are automatically called
Answer: c) They start with def keyword

31. Which of the following is used to call a function?


a) def
b) return
c) function_name()
d) execute()
Answer: c) function_name()
32. Which function will execute first in a program with multiple functions?
a) The last one
b) All at once
c) The one explicitly called
d) Any random
Answer: c) The one explicitly called

33. What is true about the return statement?


a) Can be used multiple times
b) Stops function execution
c) Returns values
d) All of the above
Answer: d) All of the above

34. Which function returns the length of an object?


a) len()
b) count()
c) size()
d) range()
Answer: a) len()

35. Which is a valid user-defined function definition?


a) def show()
b) function show()
c) func show()
d) define show()
Answer: a) def show()

36. Which built-in function returns the maximum value?


a) max()
b) largest()
c) big()
d) maxval()
Answer: a) max()

37. If no arguments are passed to a function with default parameters:


a) Error occurs
b) Returns 0
c) Defaults are used
d) Execution stops
Answer: c) Defaults are used

38. Which function header is invalid?


a) def fun():
b) def fun(a):
c) def fun(a, b=0):
d) def fun(a=0, b):
Answer: d) def fun(a=0, b)
39. Which scope exists only during the function’s execution?
a) Global
b) Module
c) Local
d) Static
Answer: c) Local

40. What is the default return value of a function with no return statement?
a) 0
b) ""
c) None
d) False
Answer: c) None

Python Exception Handling – MCQs


1. What is an exception in Python?
a) A syntax error
b) A warning
c) An error that occurs during program execution
d) A variable
Answer: c) An error that occurs during program execution

2. What happens if an exception is not handled in Python?


a) The program continues
b) A warning is shown
c) The program terminates
d) It fixes itself
Answer: c) The program terminates

3. Which keyword is used to handle exceptions?


a) handle
b) except
c) error
d) catch
Answer: b) except

4. Which block must always be used when handling exceptions?


a) except
b) finally
c) try
d) raise
Answer: c) try

5. Which block is executed whether an exception occurs or not?


a) except
b) try
c) catch
d) finally
Answer: d) finally
6. Which of the following is a built-in exception in Python?
a) FileNotFoundError
b) NullPointerException
c) NoSuchElement
d) IndexMissing
Answer: a) FileNotFoundError

7. Which statement is used to raise an exception manually?


a) raise
b) throw
c) except
d) break
Answer: a) raise

8. What type of error is handled by ZeroDivisionError?


a) Accessing a wrong index
b) Dividing by zero
c) File not found
d) None value
Answer: b) Dividing by zero

9. Which block is optional in exception handling?


a) try
b) except
c) finally
d) All are required
Answer: c) finally

10. What is the output if no exception occurs in a try block?


a) except block runs
b) finally block runs
c) Error occurs
d) Nothing happens
Answer: b) finally block runs

11. Which block runs only when an exception occurs?


a) try
b) except
c) finally
d) None
Answer: b) except

12. Which error occurs when a variable is not defined?


a) IndexError
b) NameError
c) ValueError
d) TypeError
Answer: b) NameError
13. Which exception is raised when a string is used in a mathematical operation?
a) TypeError
b) NameError
c) SyntaxError
d) IndexError
Answer: a) TypeError

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

15. Which block is executed first in a try-except-finally structure?


a) except
b) finally
c) try
d) raise
Answer: c) try

16. Which exception is raised when a list index is out of range?


a) KeyError
b) IndexError
c) NameError
d) ListError
Answer: b) IndexError

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

21. Which statement is TRUE about finally block?


a) It runs only on error
b) It runs only when try succeeds
c) It always runs
d) It never runs
Answer: c) It always runs

22. What is the purpose of exception handling?


a) To increase program speed
b) To avoid writing code
c) To gracefully handle runtime errors
d) To skip syntax checks
Answer: c) To gracefully handle runtime errors

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

24. What does try-except prevent?


a) Compilation
b) Runtime termination
c) Syntax error
d) Variable creation
Answer: b) Runtime termination

25. Can there be multiple except blocks with one try block?
a) No
b) Yes
c) Only two
d) Only one
Answer: b) Yes

26. What does except Exception: catch?


a) Only user-defined exceptions
b) Only system exceptions
c) All exceptions
d) Only syntax errors
Answer: c) All exceptions
27. Which exception is raised for syntax-related issues?
a) TypeError
b) SyntaxError
c) IndentationError
d) ValueError
Answer: b) SyntaxError

28. What will happen if an error occurs in except block?


a) Error is skipped
b) Program crashes
c) Goes to finally
d) Tries again
Answer: b) Program crashes

29. Which block is ideal to release resources like files or connections?


a) try
b) finally
c) except
d) raise
Answer: b) finally

30. Which of these is the correct order of exception handling?


a) try → except → finally
b) except → try → finally
c) try → finally → except
d) finally → try → except
Answer: a) try → except → finally

You might also like