Python
✅ Topic: Python Interpreter and Interactive Mode
🧠 Python Interpreter and Interactive Mode – 50 MCQs
1. What is the default extension of a Python script file?
Answer: .py
2. Which command is used to start the Python interactive interpreter from the
terminal?
Answer: python or python3
3. What symbol is used in the Python interactive shell to indicate the prompt?
Answer: >>>
4. What is the function of the Python interactive mode?
Answer: Execute Python statements line-by-line immediately.
5. Which built-in function can be used to display the version of Python interpreter?
Answer: python --version
6. What keyword is used to exit the Python interactive shell?
Answer: exit()
7. Which function is used to evaluate Python expressions in the interactive shell?
Answer: eval()
8. What will the Python interpreter return if an expression is entered without using the
print function?
Answer: The result of the expression
9. Which mode of Python is best suited for testing and debugging short code snippets?
Answer: Interactive Mode
10. What is the output of running python without any script arguments?
Answer: Launches interactive mode
11. How can we exit from interactive mode using keyboard?
Answer: Ctrl + Z (Windows) or Ctrl + D (Linux/Mac)
12. Which command-line flag runs a Python script and then starts interactive mode?
Answer: -i
13. Which prompt symbol appears for continuation lines in Python interactive mode?
Answer: ...
14. What does dir() function do in the Python interpreter?
Answer: Lists the names in the current scope
1
Python
15. What does help() function do in the interactive interpreter?
Answer: Opens an interactive help utility
16. Can the Python interactive shell be used as a calculator?
Answer: Yes
17. What type of error is displayed if we enter invalid Python syntax in the interpreter?
Answer: SyntaxError
18. What is REPL in Python?
Answer: Read-Eval-Print Loop
19. Which command prints the documentation string of a module or function?
Answer: help(module_name)
20. How do you run a Python script from interactive mode?
Answer: Use exec(open('[Link]').read())
21. What is the output of type(3.5) in the interpreter?
Answer: <class 'float'>
22. How are comments written in Python scripts?
Answer: Using #
23. Which method can be used to clear the screen in interactive mode (indirectly)?
Answer: import os; [Link]('cls') or [Link]('clear')
24. Can functions be defined in Python interactive mode?
Answer: Yes
25. How can you view all keywords in the Python interpreter?
Answer: import keyword; print([Link])
26. Which function displays the object’s documentation?
Answer: help()
27. Which built-in function shows all available attributes and methods of an object?
Answer: dir()
28. Can the interactive interpreter execute multi-line code blocks?
Answer: Yes
29. What does the underscore _ return in interactive mode?
Answer: The result of the last evaluated expression
30. What does the quit() function do in interactive mode?
Answer: Exits the interpreter
31. How can we run interactive mode from the Python IDLE?
Answer: Open Python shell
2
Python
32. What happens when an error occurs in interactive mode?
Answer: Displays a traceback message
33. Can you import modules in the interactive shell?
Answer: Yes
34. What is the main benefit of the interactive shell for beginners?
Answer: Immediate feedback on code execution
35. What’s the command to start Python interactive mode in Linux terminal?
Answer: python3
36. What does input() do in interactive mode?
Answer: Reads user input as string
37. What is the difference between running a script and using interactive mode?
Answer: Scripts run whole file; interactive runs line-by-line
38. How is the standard output displayed in the interactive shell?
Answer: Automatically for expressions
39. Which Python prompt follows after a for or if line in interactive shell?
Answer: ...
40. Which command runs the Python interactive mode and executes a script before
starting the shell?
Answer: python -i [Link]
41. Can the interactive interpreter be used with third-party libraries?
Answer: Yes
42. What is the .pyc file created by Python?
Answer: Compiled Python bytecode
43. Which prompt appears if the interpreter expects more input?
Answer: ...
44. How do you continue a long expression in the next line in the interactive shell?
Answer: Use \
45. What happens if print("Hello) is entered in interactive mode?
Answer: SyntaxError due to missing quote
46. Which function is used to execute strings as Python code?
Answer: exec()
47. What will be the output of _ + 10 if _ was previously 5?
Answer: 15
3
Python
48. Can variables be defined and reused across multiple lines in interactive mode?
Answer: Yes
49. What is a common use of the Python interactive mode in development?
Answer: Testing and debugging snippets
50. What happens when a blank line is entered in interactive mode?
Answer: Interpreter waits for next command
✅ Topic: Data Types in Python
1. What is the data type of 42 in Python?
Answer: int
2. What is the data type of 3.14?
Answer: float
3. What is the type of True in Python?
Answer: bool
4. What is the data type of "Hello, World!"?
Answer: str
5. Which function is used to check the data type of a variable?
Answer: type()
6. What is the result of type([1, 2, 3])?
Answer: list
7. What is the result of type((1, 2, 3))?
Answer: tuple
8. What is the result of type({'a': 1, 'b': 2})?
Answer: dict
9. What is the result of type({1, 2, 3})?
Answer: set
10. What data type is used to store multiple values in an ordered, changeable, and
indexed manner?
Answer: list
11. Which data type is immutable: list or tuple?
Answer: tuple
4
Python
12. What data type is returned by input() function?
Answer: str
13. What is the result of type(None)?
Answer: NoneType
14. What is the output of type(2 + 3.0)?
Answer: float
15. Which data type can hold a key-value pair?
Answer: dict
16. What is the type of an empty list []?
Answer: list
17. What is the type of an empty tuple ()?
Answer: tuple
18. What is the type of an empty dictionary {}?
Answer: dict
19. What is the type of an empty set?
Answer: set
20. What keyword is used to define a boolean literal for true?
Answer: True
21. What keyword is used to define a boolean literal for false?
Answer: False
22. Can a list contain mixed data types in Python?
Answer: Yes
23. What is the type of "123"?
Answer: str
24. What is the type of b"hello"?
Answer: bytes
25. What function converts a string to an integer?
Answer: int()
26. What function converts an integer to a string?
Answer: str()
27. Which data type is used to represent complex numbers?
Answer: complex
28. What is the result of type(3 + 4j)?
Answer: complex
5
Python
29. What is the data type of range(5)?
Answer: range
30. Can a dictionary key be of any data type?
Answer: No (must be immutable)
31. Which data type is unindexed and unordered?
Answer: set
32. What is the type of the result of 5 // 2?
Answer: int
33. What is the type of the result of 5 / 2?
Answer: float
34. Which data type supports indexing, slicing, and mutability?
Answer: list
35. Which data type supports indexing and slicing but is immutable?
Answer: tuple
36. Which function is used to convert a list to a tuple?
Answer: tuple()
37. What function is used to create a dictionary from two lists?
Answer: dict(zip(list1, list2))
38. What data type is created by set()?
Answer: set
39. Can a set contain duplicate elements?
Answer: No
40. What function is used to convert a float to an integer?
Answer: int()
41. What type is the result of isinstance(3, int)?
Answer: bool
42. What will isinstance("abc", str) return?
Answer: True
43. What will isinstance([1, 2, 3], tuple) return?
Answer: False
44. Which function is used to find the length of a list or string?
Answer: len()
45. What will len({}) return?
Answer: 0
6
Python
46. Can a tuple contain a list?
Answer: Yes
47. Can a list contain another list?
Answer: Yes
48. What will type(()) return?
Answer: tuple
49. What will type([]) return?
Answer: list
50. What will type({}) return?
Answer: dict
✅ Topic: Statements in Python – 50 MCQs with Answers
1. What is a statement in Python?
Answer: A line of code that performs an action.
2. What is the output of a print("Hello") statement?
Answer: Hello
3. Which keyword is used for conditional statements?
Answer: if
4. What keyword is used to start a loop in Python?
Answer: for or while
5. What statement is used to exit a loop prematurely?
Answer: break
6. What statement is used to skip the current iteration of a loop?
Answer: continue
7. Which statement is used to create a function?
Answer: def
8. Which statement is used to return a value from a function?
Answer: return
9. What statement is used to include code only if a condition is true?
Answer: if
10. Which keyword is used for multi-branch decision making?
Answer: elif
7
Python
11. What statement does nothing and is used as a placeholder?
Answer: pass
12. Which statement is used to import modules in Python?
Answer: import
13. What will a = 10 be classified as?
Answer: Assignment statement
14. What kind of statement is print("Python")?
Answer: Output statement
15. Which keyword begins a try block for exception handling?
Answer: try
16. What is the keyword used to handle exceptions?
Answer: except
17. What keyword is used to ensure a block of code always executes?
Answer: finally
18. What kind of statement is used to define a class?
Answer: class statement
19. What statement will terminate the program immediately?
Answer: exit()
20. What kind of statement is while True:?
Answer: Looping statement
21. Can statements be combined on one line in Python?
Answer: Yes, using ;
22. What kind of statement is x, y = y, x?
Answer: Multiple assignment
23. What is the use of the global statement?
Answer: Declare a variable as global inside a function
24. What is the nonlocal statement used for?
Answer: Refer to variables in the nearest enclosing scope
25. What is an example of a compound statement in Python?
Answer: if-else
26. What is the default indentation level in Python?
Answer: 4 spaces
27. Which control flow statement tests a condition and runs the block repeatedly?
Answer: while
8
Python
28. What kind of statement is used in context managers?
Answer: with statement
29. What happens if indentation is not properly followed in a statement block?
Answer: IndentationError
30. What is the statement to read user input in Python 3?
Answer: input()
31. Which statement converts a string to an integer?
Answer: int()
32. Can a single line contain multiple statements?
Answer: Yes, separated by ;
33. What is the difference between break and continue?
Answer: break exits the loop; continue skips to next iteration
34. What is the use of assert statement?
Answer: Test if a condition is true
35. What kind of statement is del x?
Answer: Deletion statement
36. Can a try statement exist without an except or finally?
Answer: No
37. Which statement is used to open files?
Answer: with open()
38. What kind of statement is lambda x: x+1?
Answer: Anonymous function statement
39. What does the yield statement do?
Answer: Returns a value from a generator
40. What is the difference between return and yield?
Answer: return ends the function; yield pauses and resumes
41. What is the type of print()?
Answer: Function call statement
42. What does raise do in a statement?
Answer: Raises an exception
43. What is the keyword for an alternative to try-except block?
Answer: None (try-except is required)
44. Which statement is used for iteration over sequences?
Answer: for
9
Python
45. What is else in a for-loop used for?
Answer: Executes if loop completes without break
46. What kind of statement is if a > b:?
Answer: Conditional statement
47. Can if statements be nested?
Answer: Yes
48. What kind of statement is print("A") if x > 0 else print("B")?
Answer: Ternary/Conditional expression
49. What does is check in a conditional statement?
Answer: Identity of two objects
50. What statement would you use to check if a value exists in a list?
Answer: if x in list
Great! Let's move on.
✅ Topic: Expressions in Python – 50 MCQs with Answers
1. What is an expression in Python?
Answer: A combination of values, variables, operators, and function calls that can be
evaluated.
2. What will be the result of the expression 5 + 3 * 2?
Answer: 11
3. What type of expression is a + b?
Answer: Arithmetic expression
4. What is the output of 4 // 2?
Answer: 2
5. What is the result of 3 ** 2?
Answer: 9
6. Which operator is used for exponentiation?
Answer: **
7. What will 7 % 3 return?
Answer: 1
10
Python
8. What does == operator do in expressions?
Answer: Checks for equality
9. What will the expression 10 != 5 return?
Answer: True
10. What type of result is produced by a comparison expression?
Answer: Boolean
11. What is the output of 5 > 2 and 3 < 4?
Answer: True
12. What is the output of not (5 > 2)?
Answer: False
13. Which operator has the highest precedence: *, +, -, //?
Answer: *
14. What will the expression "Hello" + "World" result in?
Answer: HelloWorld
15. What will "Python" * 3 evaluate to?
Answer: PythonPythonPython
16. What will the expression len("abc") return?
Answer: 3
17. What is the result of "a" in "apple"?
Answer: True
18. What does the expression x is y evaluate?
Answer: Identity comparison
19. What does x += 1 do?
Answer: Increments x by 1 (same as x = x + 1)
20. What is the result of int("5") + float("6.7")?
Answer: 11.7
21. What is the value of True + False?
Answer: 1
22. What is the result of None == None?
Answer: True
23. What is the output of "A" > "B"?
Answer: False
24. Which expression converts a float to int?
Answer: int(4.7)
11
Python
25. What will str(100) return?
Answer: "100"
26. What does bool("") return?
Answer: False
27. What is the value of bool("False")?
Answer: True
28. What does the expression abs(-10) return?
Answer: 10
29. What is the result of round(3.6)?
Answer: 4
30. What will min(3, 5, 2) return?
Answer: 2
31. What is the result of max([1, 2, 3, 0])?
Answer: 3
32. What does sum([1, 2, 3]) return?
Answer: 6
33. What is the type of result from 4 + 5.0?
Answer: float
34. What does type(3 * 1.0) return?
Answer: float
35. Which expression checks if a list is empty?
Answer: if not my_list
36. What is the result of "2" * 3?
Answer: "222"
37. Which operator is used for floor division?
Answer: //
38. What is the result of 9 / 2 in Python 3?
Answer: 4.5
39. What is the output of eval("2 + 3")?
Answer: 5
40. What does 3 > 2 > 1 return?
Answer: True
41. What will 3 < 2 or 4 > 1 return?
Answer: True
12
Python
42. What is the result of 3 < 2 and 4 > 1?
Answer: False
43. What will 5 & 3 return?
Answer: 1 (bitwise AND)
44. What will 5 | 3 return?
Answer: 7 (bitwise OR)
45. What is the result of 5 ^ 3?
Answer: 6 (bitwise XOR)
46. What does ~5 return?
Answer: -6 (bitwise NOT)
47. What is the result of 5 << 1?
Answer: 10
48. What is the result of 5 >> 1?
Answer: 2
49. What type of error occurs with 1/0?
Answer: ZeroDivisionError
50. What is returned by "apple" == "Apple"?
Answer: False
✅ Topic: Expressions in Python – 50 MCQs with Answers & Explanation
1. What is an expression in Python?
Answer: A combination of values, variables, and operators that evaluates to a value.
Explanation: Expressions compute a value, like 2 + 3.
2. What is the result of 3 + 4 * 2?
Answer: 11
Explanation: Multiplication has higher precedence than addition.
3. What does 10 // 3 return?
Answer: 3
Explanation: // is floor division operator.
4. What is the result of 2 ** 3?
Answer: 8
Explanation: ** is the exponentiation operator.
13
Python
5. Which of these is a valid expression? 5 + 3, if x:, def fun():
Answer: 5 + 3
Explanation: Only 5 + 3 is an expression; others are statements.
6. What is the type of the result of 4 + 5.0?
Answer: float
Explanation: int and float give float result.
7. What does x = 5 represent?
Answer: Assignment statement
Explanation: x = 5 assigns a value, not an expression result.
8. Which expression checks equality?
Answer: a == b
Explanation: == compares two values.
9. What does a != b evaluate?
Answer: True if a is not equal to b
Explanation: != is not equal operator.
10. What is the result of "a" + "b"?
Answer: "ab"
Explanation: + concatenates strings.
11. What does 5 > 2 and 3 < 4 return?
Answer: True
Explanation: Both conditions are true.
12. What is the result of not True?
Answer: False
Explanation: not negates the boolean value.
13. Which of these is a logical operator?
Answer: and
Explanation: and, or, not are logical operators.
14. What is the result of "5" + str(6)?
Answer: "56"
Explanation: str(6) converts int to string.
15. What is the result of int("10") + 5?
Answer: 15
Explanation: Converts string to int then adds.
16. What does a = b = 10 do?
Answer: Assigns 10 to both a and b
Explanation: Chain assignment.
14
Python
17. What is the result of "abc" * 3?
Answer: "abcabcabc"
Explanation: String is repeated 3 times.
18. What is the value of 5 + 4 * 2 - 1?
Answer: 12
Explanation: 4*2=8, then 5+8=13, 13-1=12.
19. What does a += 2 mean?
Answer: a = a + 2
Explanation: It's a shorthand for addition assignment.
20. Which operator has highest precedence?
Answer: **
Explanation: Exponentiation ** has highest precedence.
21. What is the result of True + True?
Answer: 2
Explanation: In Python, True is 1.
22. What is the result of 3 > 2 > 1?
Answer: True
Explanation: Chained comparison.
23. What does 3 == 3.0 return?
Answer: True
Explanation: int and float are equal in value.
24. What is the output of bool("False")?
Answer: True
Explanation: Non-empty strings are truthy.
25. What does None == 0 return?
Answer: False
Explanation: None is not equal to 0.
26. What does "a" < "b" return?
Answer: True
Explanation: Lexicographical comparison.
27. What is the value of 'a' * 0?
Answer: ""
Explanation: Anything multiplied by 0 is empty.
28. What does True and False return?
Answer: False
Explanation: and returns True only if both are True.
15
Python
29. What does True or False return?
Answer: True
Explanation: or returns True if any operand is True.
30. What is 5 % 2?
Answer: 1
Explanation: Modulus operator returns remainder.
31. What is the result of 2 + 3 * 4 ** 2?
Answer: 50
Explanation: 4**2=16, 3*16=48, 2+48=50.
32. What is the result of bool(0)?
Answer: False
Explanation: 0 is falsy.
33. What does "abc"[1] return?
Answer: b
Explanation: Indexing starts at 0.
34. What is the result of len("abc")?
Answer: 3
Explanation: Returns number of characters.
35. What does "abc"[::-1] return?
Answer: cba
Explanation: Reverses the string.
36. What is True * 3?
Answer: 3
Explanation: True is 1; 13=3.*
37. What is False + 5?
Answer: 5
Explanation: False is 0; 0+5=5.
38. What is the result of type(5/2)?
Answer: float
Explanation: / always gives float.
39. What is the result of type(5//2)?
Answer: int
Explanation: // returns integer part.
40. What does 5 != 5 return?
Answer: False
Explanation: They are equal.
16
Python
41. What is the result of not (5 > 2)?
Answer: False
Explanation: 5 > 2 is True, not True is False.
42. What is 10 - 3 * 2?
Answer: 4
Explanation: 3*2=6, then 10-6=4.
43. What is 4 + 3 % 2?
Answer: 5
Explanation: 3%2=1, then 4+1=5.
44. What is 2 ** 0?
Answer: 1
Explanation: Anything power 0 is 1.
45. What does abs(-7) return?
Answer: 7
Explanation: abs gives absolute value.
46. What does max(3, 7, 2) return?
Answer: 7
Explanation: Maximum value.
47. What does min(3, 7, 2) return?
Answer: 2
Explanation: Minimum value.
48. What is round(4.5) in Python 3?
Answer: 4
Explanation: Bankers’ rounding (even number rule).
49. What is round(3.5)?
Answer: 4
Explanation: 3.5 rounds to nearest even: 4.
50. What is pow(2, 3)?
Answer: 8
Explanation: Same as 2**3.
✅ Topic: Boolean Values and Operators – 50 MCQs with Answers & Explanation
1. What is the type of True in Python?
Answer: bool
Explanation: True and False are of type bool.
17
Python
2. What is the result of bool(0)?
Answer: False
Explanation: Zero is considered False.
3. What is the result of bool(1)?
Answer: True
Explanation: Non-zero integers are True.
4. What does bool("") return?
Answer: False
Explanation: Empty string is falsy.
5. What is bool("False")?
Answer: True
Explanation: Non-empty strings are truthy.
6. Which of these values evaluates to False?
Answer: None
Explanation: None, 0, "", [], {} are falsy.
7. What does not False return?
Answer: True
Explanation: not negates a boolean.
8. What is the result of True and False?
Answer: False
Explanation: and returns True only if both operands are True.
9. What is the result of False or True?
Answer: True
Explanation: or returns True if any operand is True.
10. What is the value of not (True or False)?
Answer: False
Explanation: True or False = True, not True = False.
11. What does not 0 evaluate to?
Answer: True
Explanation: 0 is False, not False = True.
12. What does True or False and False return?
Answer: True
Explanation: and has higher precedence: False and False = False, then True or False =
True.
18
Python
13. What is False and True or True?
Answer: True
Explanation: False and True = False, False or True = True.
14. What is True and not False?
Answer: True
Explanation: not False = True, then True and True = True.
15. What is not (True and False)?
Answer: True
Explanation: True and False = False, not False = True.
16. What is the result of 1 and 0?
Answer: 0
Explanation: and returns the first falsy value.
17. What is the result of 0 or 5?
Answer: 5
Explanation: or returns the first truthy value.
18. What is the output of True + True?
Answer: 2
Explanation: True is 1 in arithmetic context.
19. What is False + False?
Answer: 0
Explanation: False is 0.
20. What is the result of bool([])?
Answer: False
Explanation: Empty list is falsy.
21. What is the output of bool([1, 2])?
Answer: True
Explanation: Non-empty lists are truthy.
22. What does None or True return?
Answer: True
Explanation: None is falsy; returns True.
23. What does [] or {} return?
Answer: {}
Explanation: Both are falsy, but or returns the second value.
24. What does "a" and "b" return?
Answer: "b"
Explanation: Both truthy; and returns last operand.
19
Python
25. What does "a" or "b" return?
Answer: "a"
Explanation: or returns first truthy value.
26. What is the result of not not 10?
Answer: True
Explanation: not 10 = False, not False = True.
27. What is not []?
Answer: True
Explanation: [] is falsy; not makes it True.
28. What is the result of True and False or True?
Answer: True
Explanation: True and False = False, then False or True = True.
29. What is the output of False or False or False?
Answer: False
Explanation: All operands are False.
30. What is the result of True and True and False?
Answer: False
Explanation: All need to be True for and to be True.
31. What does not (5 > 3) return?
Answer: False
Explanation: 5 > 3 = True, not True = False.
32. What is the result of (3 < 4) and (5 > 1)?
Answer: True
Explanation: Both expressions are True.
33. What is 3 < 2 or 2 < 1?
Answer: False
Explanation: Both expressions are False.
34. What is the output of not (1 and 0)?
Answer: True
Explanation: 1 and 0 = 0, not 0 = True.
35. What is the output of bool(None)?
Answer: False
Explanation: None is falsy.
36. What is not None?
Answer: True
Explanation: None is falsy, not gives True.
20
Python
37. What is the result of 5 and 10?
Answer: 10
Explanation: Both truthy, and returns second operand.
38. What is the result of 0 and 10?
Answer: 0
Explanation: and returns the first falsy operand.
39. What is the result of 10 or 0?
Answer: 10
Explanation: or returns first truthy operand.
40. What is the value of not (False or 0)?
Answer: True
Explanation: False or 0 = 0, not 0 = True.
41. What is the type of bool(5)?
Answer: bool
Explanation: bool() always returns a boolean value.
42. What is True * 10?
Answer: 10
Explanation: True is 1 in numeric operations.
43. What is False * 5?
Answer: 0
Explanation: False is 0 in arithmetic.
44. What is the output of not (2 < 1)?
Answer: True
Explanation: 2 < 1 is False, not False = True.
45. What is the output of bool(3.14)?
Answer: True
Explanation: Non-zero numbers are truthy.
46. What is the result of 0.0 or False?
Answer: False
Explanation: Both are falsy, or returns the second.
47. What is the result of bool(())?
Answer: False
Explanation: Empty tuple is falsy.
48. What does not ("" or []) return?
Answer: True
Explanation: Both "" and [] are falsy, or returns falsy, not makes it True.
21
Python
49. What is not "text"?
Answer: False
Explanation: Non-empty strings are truthy, not makes it False.
50. What is the result of bool(set())?
Answer: False
Explanation: Empty set is falsy.
✅ Topic: Strings – 50 MCQs with Answers & Explanation
1. What is the output of 'Python'[0]?
Answer: 'P'
Explanation: Indexing starts at 0.
2. What does 'Python'[-1] return?
Answer: 'n'
Explanation: Negative indexing starts from the end.
3. Which method converts string to uppercase?
Answer: upper()
Explanation: [Link]() returns a copy of the string in all uppercase.
4. What is the result of 'abc' + 'def'?
Answer: 'abcdef'
Explanation: String concatenation.
5. What does 'Hello' * 3 return?
Answer: 'HelloHelloHello'
Explanation: String repetition using multiplication.
6. What does 'Python'.lower() return?
Answer: 'python'
Explanation: Converts all characters to lowercase.
7. What is the output of ' hello '.strip()?
Answer: 'hello'
Explanation: Removes leading and trailing whitespace.
8. Which method finds the index of a substring?
Answer: find()
Explanation: [Link](sub) returns the first index of sub, or -1.
9. What is the output of 'abcde'.replace('a', 'A')?
Answer: 'Abcde'
Explanation: Replaces all occurrences of 'a' with 'A'.
22
Python
10. What does 'apple'.startswith('a') return?
Answer: True
Explanation: Checks if string starts with 'a'.
11. What does 'test123'.isalnum() return?
Answer: True
Explanation: Checks if all characters are alphanumeric.
12. What does 'hello!'.isalpha() return?
Answer: False
Explanation: ! is not alphabetic.
13. What is '123'.isdigit()?
Answer: True
Explanation: Checks if all characters are digits.
14. 'Python' in 'I love Python' evaluates to?
Answer: True
Explanation: Checks for substring.
15. What does 'pyTHon'.capitalize() return?
Answer: 'Python'
Explanation: First letter capitalized, rest lowercase.
16. What does 'abc'.center(7, '*') return?
Answer: 'abc'
Explanation: Centers the string with padding.
17. ' space '.lstrip() returns?
Answer: 'space '
Explanation: Removes leading spaces.
18. ' space '.rstrip() returns?
Answer: ' space'
Explanation: Removes trailing spaces.
19. 'abc\nxyz'.splitlines() returns?
Answer: ['abc', 'xyz']
Explanation: Splits the string at line boundaries.
20. 'a-b-c'.split('-') returns?
Answer: ['a', 'b', 'c']
Explanation: Splits the string at each '-'
21. '12345'.zfill(8) returns?
Answer: '00012345'
Explanation: Pads the string on the left with zeros.
23
Python
22. 'PYTHON'.isupper() returns?
Answer: True
Explanation: All letters are uppercase.
23. 'abc123'.islower() returns?
Answer: True
Explanation: All letters (if any) are lowercase, digits are ignored.
24. 'Hello'.endswith('o') returns?
Answer: True
Explanation: Checks if the string ends with 'o'.
25. What does len("Python") return?
Answer: 6
Explanation: Returns the number of characters.
26. ''.join(['a', 'b', 'c']) returns?
Answer: 'abc'
Explanation: Joins list items into a string.
27. '1,2,3'.split(',') returns?
Answer: ['1', '2', '3']
Explanation: Splits the string by commas.
28. 'Hello World'.title() returns?
Answer: 'Hello World'
Explanation: Each word capitalized.
29. 'abc'.isidentifier() returns?
Answer: True
Explanation: Valid Python identifier.
30. '123abc'.isidentifier() returns?
Answer: False
Explanation: Identifiers can't start with digits.
31. '\\n'.isspace() returns?
Answer: False
Explanation: \\n is a backslash and 'n', not a whitespace.
32. r'\\n' is?
Answer: '\n'
Explanation: Raw string, backslash is not escaped.
33. 'abc'.swapcase() returns?
Answer: 'ABC'
Explanation: Swaps lowercase to uppercase and vice versa.
24
Python
34. '123abc'.isalnum() returns?
Answer: True
Explanation: All characters are alphanumeric.
35. 'Python Programming'.count('P') returns?
Answer: 2
Explanation: Counts uppercase 'P'.
36. 'xyx'.find('y') returns?
Answer: 1
Explanation: First index of 'y'.
37. 'abcabc'.rfind('a') returns?
Answer: 3
Explanation: Finds last occurrence of 'a'.
38. 'ABCD'.lower().isupper() returns?
Answer: False
Explanation: Lowercase is not uppercase.
39. 'hello'.upper().isupper() returns?
Answer: True
Explanation: Uppercased and checked.
40. ''.isspace() returns?
Answer: False
Explanation: Empty string is not space.
41. chr(97) returns?
Answer: 'a'
Explanation: ASCII to character.
42. ord('A') returns?
Answer: 65
Explanation: Character to ASCII.
43. "Python".replace('P', '') returns?
Answer: 'ython'
Explanation: Replaces 'P' with empty string.
44. "Python".index('y') returns?
Answer: 1
Explanation: Returns the first index.
45. 'abc'.ljust(6, '-') returns?
Answer: 'abc---'
Explanation: Left-justified string.
25
Python
46. 'abc'.rjust(6, '-') returns?
Answer: '---abc'
Explanation: Right-justified string.
47. 'abc\nxyz'.count('\n') returns?
Answer: 1
Explanation: Counts newline characters.
48. 'a\tb'.expandtabs(4) returns?
Answer: 'a b'
Explanation: Tabs are expanded to 4 spaces.
49. 'abc'.startswith(('a', 'b')) returns?
Answer: True
Explanation: Starts with 'a'.
50. 'python'.capitalize().startswith('P') returns?
Answer: True
Explanation: Capitalized string starts with 'P'.
✅ Topic: Arrays of Numbers – 50 MCQs with Answers & Explanation
(In Python, arrays are generally handled using either the built-in list or the array module, or
more commonly with NumPy for numerical arrays.)
1. Which module is used for creating arrays in Python?
Answer: array
Explanation: array is a built-in module for numeric arrays.
2. How do you import the array module?
Answer: import array
Explanation: Standard import for using the array module.
3. How do you create an integer array using the array module?
Answer: [Link]('i', [1, 2, 3])
Explanation: 'i' is the type code for integers.
26
Python
4. What will [Link]('i', [1, 2, '3']) raise?
Answer: TypeError
Explanation: '3' is a string, not an integer.
5. What does arr = [Link]('i', [10, 20, 30]); arr[1] return?
Answer: 20
Explanation: Accessing the second element.
6. Which of these is a valid array type code for floats?
Answer: 'f'
Explanation: 'f' stands for 4-byte float.
7. What does len([Link]('i', [1,2,3,4])) return?
Answer: 4
Explanation: Returns number of elements.
8. What does [Link](5) do?
Answer: Adds 5 to the end
Explanation: append() adds a new item.
9. What is the output of [Link]()?
Answer: Last item
Explanation: Removes and returns the last element.
10. Which method inserts a value at a given position in an array?
Answer: insert()
Explanation: insert(index, value) adds value at position.
11. What does [Link](3) do?
Answer: Removes the first occurrence of 3
Explanation: remove() deletes the first match.
12. What will [Link]('i', [1, 2]) + [Link]('i', [3]) return?
Answer: array('i', [1, 2, 3])
Explanation: Concatenation of same-type arrays.
13. What will [Link]() do?
Answer: Reverses the array in-place
Explanation: Changes the order of elements.
14. Which module is better for numerical arrays with vectorized operations?
Answer: numpy
Explanation: NumPy is optimized for numerical computation.
15. What does [Link]() return?
Answer: A list copy of the array
Explanation: Converts array to a Python list.
27
Python
16. What happens when you access arr[10] and the array has only 5 elements?
Answer: IndexError
Explanation: Index out of bounds.
17. Type code 'd' stands for?
Answer: Double (float64)
Explanation: 'd' is for double precision floats.
18. What is the result of [Link](30) if 30 is in the array?
Answer: Index of 30
Explanation: Returns index of first occurrence.
19. What will [Link](20) return?
Answer: Number of times 20 occurs
Explanation: Counts matching values.
20. What method clears all elements in an array?
Answer: [Link]()
Explanation: Empties the array.
21. Which function is used to get the buffer info of an array?
Answer: arr.buffer_info()
Explanation: Returns (address, length) tuple.
22. What is the default endianness in NumPy arrays?
Answer: Native to the platform
Explanation: NumPy uses native byte order.
23. What is the use of [Link]?
Answer: Size of one array element in bytes
Explanation: Useful for memory calculations.
24. Can [Link]('i', []) be empty?
Answer: Yes
Explanation: Arrays can be empty.
25. What does arr[::2] return?
Answer: Every second element
Explanation: Slicing with step size 2.
26. Which operation resizes an array?
Answer: No direct method
Explanation: Unlike lists, arrays have fixed types, not lengths.
27. Can [Link] store mixed types?
Answer: No
Explanation: All elements must be of the same type.
28
Python
28. Which attribute shows type code of an array?
Answer: [Link]
Explanation: Returns the type code string.
29. What is the purpose of [Link]([4,5])?
Answer: Appends list values to array
Explanation: Extends array with list elements.
30. Which method copies elements to another array?
Answer: [Link]()
Explanation: Appends another array or iterable.
31. Can you convert a NumPy array to [Link]?
Answer: Yes, with [Link](typecode, np_array.tolist())
Explanation: Convert NumPy to list first.
32. Which method returns the number of occurrences of a value?
Answer: count()
Explanation: Built-in method of array.
33. [Link]('u', 'hello') is used for?
Answer: Unicode characters
Explanation: 'u' type code is for Unicode (Python 2 only).
34. What does arr[1:3] return?
Answer: Slice from index 1 to 2
Explanation: Stop index is exclusive.
35. Which is not a valid type code? 'i', 'f', 'z', 'd'
Answer: 'z'
Explanation: No type code z exists.
36. What does [Link] == 'i' check?
Answer: If array holds integers
Explanation: Compares type code string.
37. arr[1] = 100 does what?
Answer: Replaces element at index 1
Explanation: In-place modification.
38. Can [Link] be resized like lists?
Answer: Partially, using methods like append, insert, etc.
Explanation: You can grow/shrink but all values must be of the same type.
39. What will sum(arr) return for an array of ints?
Answer: Sum of all elements
Explanation: sum() works on iterable.
29
Python
40. [Link] is more efficient than list for?
Answer: Storing homogeneous numeric data
Explanation: Compact, fixed-type memory layout.
41. [Link] vs list difference?
Answer: Array has fixed types, list can store mixed types
Explanation: Main difference in use.
42. What does arr[:0] return?
Answer: Empty array
Explanation: Slice up to 0.
43. What happens when assigning a float to an 'i' array?
Answer: TypeError
Explanation: Incompatible type assignment.
44. Does slicing return a copy or a view in [Link]?
Answer: Copy
Explanation: Always a new array.
45. Which function creates an array from bytes?
Answer: frombytes()
Explanation: Converts bytes to array.
46. Which function returns array bytes?
Answer: tobytes()
Explanation: Converts array to bytes.
47. What will arr * 2 do?
Answer: Repeat elements twice
Explanation: Like list repetition.
48. Can [Link] be sorted?
Answer: Yes, using [Link]()
Explanation: In-place sort method.
49. What does max(arr) return?
Answer: Largest element
Explanation: Built-in max() function.
50. Which error is raised on type mismatch during insert?
Answer: TypeError
Explanation: Enforces strict typing.
✅ Topic: Lists – 50 MCQs with Answers & Explanation
30
Python
1. What is the output of len([10, 20, 30])?
Answer: 3
Explanation: Returns the number of elements in the list.
2. How do you add an item to the end of a list?
Answer: append()
Explanation: append() adds a single item to the end.
3. Which method adds multiple items to a list?
Answer: extend()
Explanation: extend() takes an iterable and adds each element.
4. What does list1 + list2 do?
Answer: Concatenates the lists
Explanation: Combines both lists into one.
5. What is the result of [1, 2] * 2?
Answer: [1, 2, 1, 2]
Explanation: List repetition operator.
6. What does my_list[1:3] return?
Answer: Elements at index 1 and 2
Explanation: Slicing up to but not including index 3.
7. What is the output of my_list[::-1]?
Answer: Reversed list
Explanation: Slice with step -1.
8. How do you remove an element by value?
Answer: remove()
Explanation: Deletes the first occurrence.
9. How do you delete an element by index?
Answer: del or pop(index)
Explanation: Both can remove by position.
10. What does pop() return?
Answer: The last element
Explanation: Removes and returns last item.
11. What is list(range(3))?
Answer: [0, 1, 2]
Explanation: range(3) generates numbers from 0 to 2.
31
Python
12. What is the output of min([3, 1, 4])?
Answer: 1
Explanation: Returns smallest element.
13. Which method sorts a list in-place?
Answer: sort()
Explanation: Modifies the list directly.
14. What is returned by sorted([3, 1, 2])?
Answer: [1, 2, 3]
Explanation: Returns a new sorted list.
15. Which method returns index of first occurrence of a value?
Answer: index()
Explanation: Finds position of a value.
16. What is my_list.count(5) used for?
Answer: Counts occurrences of 5
Explanation: Returns how many times 5 appears.
17. How to reverse a list in-place?
Answer: reverse()
Explanation: Modifies list to reverse order.
18. What will list('abc') return?
Answer: ['a', 'b', 'c']
Explanation: Converts string to list of characters.
19. What happens if my_list[10] is accessed but list has only 5 items?
Answer: IndexError
Explanation: Out of bounds.
20. Which is faster for membership test: list or set?
Answer: set
Explanation: Set has O(1) lookup, list is O(n).
21. What is the output of list([])?
Answer: []
Explanation: Creates an empty list.
22. Can lists contain different data types?
Answer: Yes
Explanation: Python lists are heterogeneous.
23. What does my_list.clear() do?
Answer: Empties the list
Explanation: Removes all items.
32
Python
24. How to copy a list?
Answer: copy() or [:]
Explanation: Both give shallow copy.
25. What does list1 is list2 check?
Answer: Object identity
Explanation: Whether both refer to same object.
26. What does list1 == list2 check?
Answer: Value equality
Explanation: Whether both lists have the same contents.
27. What is list comprehension?
Answer: A compact way to create lists
Explanation: [x for x in iterable] syntax.
28. What is the output of [x**2 for x in range(3)]?
Answer: [0, 1, 4]
Explanation: Squares of 0, 1, 2.
29. Which method inserts an item at a specific index?
Answer: insert()
Explanation: insert(index, value).
30. What will list(('a', 'b')) return?
Answer: ['a', 'b']
Explanation: Converts tuple to list.
31. What happens when my_list.append([4, 5])?
Answer: Appends list as a single element
Explanation: Result is nested list.
32. How to flatten a nested list?
Answer: Use a loop or list comprehension
Explanation: Example: [item for sublist in nested for item in sublist].
33. What does all([True, True, False]) return?
Answer: False
Explanation: all() needs all elements to be True.
34. What does any([False, False, True]) return?
Answer: True
Explanation: At least one True is enough.
35. How do you remove duplicates from a list?
Answer: list(set(my_list))
Explanation: Set removes duplicates.
33
Python
36. What is the result of my_list *= 2?
Answer: List repeated twice
Explanation: Extends the list in-place.
37. Can list elements be modified?
Answer: Yes
Explanation: Lists are mutable.
38. What is the time complexity of append()?
Answer: O(1)
Explanation: Constant time operation.
39. What is the time complexity of insert(0, x)?
Answer: O(n)
Explanation: Needs to shift elements.
40. Can you sort a list of strings?
Answer: Yes
Explanation: Alphabetical order.
41. What is the result of list("12345")[::-1]?
Answer: ['5', '4', '3', '2', '1']
Explanation: Reversed string as list.
42. Can list be used as a stack?
Answer: Yes
Explanation: Use append() and pop().
43. Can list be used as a queue?
Answer: Yes, but inefficient
Explanation: Use [Link] instead.
44. What does reversed(my_list) return?
Answer: An iterator
Explanation: Use list(reversed(...)) to convert.
45. What will [None]*5 create?
Answer: [None, None, None, None, None]
Explanation: List of 5 Nones.
46. Is [] a falsey value in Python?
Answer: Yes
Explanation: Empty list evaluates to False.
47. Can a list contain itself?
Answer: Yes
Explanation: Circular references are allowed.
34
Python
48. What does my_list[1:] = [] do?
Answer: Keeps only the first element
Explanation: Deletes from index 1 onward.
49. What is returned by enumerate(my_list)?
Answer: An iterator of (index, item) pairs
Explanation: Useful for looping with index.
50. Which built-in function zips multiple lists?
Answer: zip()
Explanation: Aggregates elements by position.
✅ Topic: Tuples – 50 MCQs with Answers & Explanation
1. What is a tuple in Python?
Answer: An immutable sequence
Explanation: Tuples cannot be changed once created.
2. What is the syntax for creating a tuple with one element?
Answer: (10,)
Explanation: The comma is required to differentiate from just parentheses.
3. What is the result of type((1))?
Answer: <class 'int'>
Explanation: Without a comma, it's treated as an integer.
4. What is the result of type((1,))?
Answer: <class 'tuple'>
Explanation: The comma defines it as a tuple.
5. Are tuples ordered collections?
Answer: Yes
Explanation: Elements maintain insertion order.
6. Can tuples contain different data types?
Answer: Yes
Explanation: Tuples are heterogeneous like lists.
7. What is the output of len((1, 2, 3))?
Answer: 3
Explanation: len() returns number of items.
35
Python
8. Can a tuple be nested?
Answer: Yes
Explanation: Tuples can contain other tuples.
9. What will (1, 2) + (3, 4) return?
Answer: (1, 2, 3, 4)
Explanation: Tuple concatenation.
10. What is the result of (1, 2) * 2?
Answer: (1, 2, 1, 2)
Explanation: Tuple repetition.
11. Can a tuple be sliced?
Answer: Yes
Explanation: Just like lists, slicing is allowed.
12. Can tuples be used as dictionary keys?
Answer: Yes
Explanation: They are immutable and hashable.
13. What will tuple("abc") return?
Answer: ('a', 'b', 'c')
Explanation: Converts string to tuple of characters.
14. What does max((3, 5, 1)) return?
Answer: 5
Explanation: Maximum value in the tuple.
15. What does min((3, 5, 1)) return?
Answer: 1
Explanation: Minimum value in the tuple.
16. What does (3, 4, 5).count(4) return?
Answer: 1
Explanation: Occurrences of value 4.
17. What does (3, 4, 4, 5).index(4) return?
Answer: 1
Explanation: Index of the first occurrence.
18. Can you assign a new value to an index in a tuple?
Answer: No
Explanation: Tuples are immutable.
19. How to convert a list to a tuple?
Answer: tuple(my_list)
Explanation: Built-in tuple() function.
36
Python
20. What is the result of tuple()?
Answer: ()
Explanation: Creates an empty tuple.
21. Is () a valid empty tuple?
Answer: Yes
Explanation: Empty parentheses create an empty tuple.
22. What is ('a', 'b', 'c')[1]?
Answer: 'b'
Explanation: Indexing is zero-based.
23. Is (10, 20) equal to (10, 20)?
Answer: Yes
Explanation: Value equality.
24. What is the output of sum((1, 2, 3))?
Answer: 6
Explanation: Sums all tuple elements.
25. Can tuples be elements of a set?
Answer: Yes
Explanation: Tuples are hashable.
26. Can sets be elements of a tuple?
Answer: Yes
Explanation: Tuples can contain any objects.
27. Which function returns the index of an element in a tuple?
Answer: index()
Explanation: Standard tuple method.
28. Which function counts how many times an element appears?
Answer: count()
Explanation: Built-in method.
29. Are tuples more memory efficient than lists?
Answer: Yes
Explanation: Because they are immutable.
30. Is (1, [2, 3]) a valid tuple?
Answer: Yes
Explanation: The list inside is mutable, but the tuple structure is valid.
31. Can tuple elements be accessed by index?
Answer: Yes
Explanation: Indexing works like lists.
37
Python
32. Can a tuple contain duplicate elements?
Answer: Yes
Explanation: Duplicates are allowed.
33. Is (1, 2) == (1, 2)?
Answer: Yes
Explanation: Tuple comparison is element-wise.
34. What is the output of 'a', 'b'?
Answer: ('a', 'b')
Explanation: Without parentheses, still a tuple with comma.
35. Can tuples be used in loops?
Answer: Yes
Explanation: You can iterate through them.
36. Is tuple comprehension allowed?
Answer: No
Explanation: Only list comprehension is available. Use generator expression instead.
37. What is a generator expression inside tuple()?
Answer: A way to generate tuples from comprehensions
Explanation: tuple(x for x in range(3)) returns (0, 1, 2).
38. What is the output of tuple([1, 2]) == (1, 2)?
Answer: True
Explanation: List converted to identical tuple.
39. Can tuples be unpacked?
Answer: Yes
Explanation: Assign each element to a variable.
40. What does a, b = (1, 2) assign to a?
Answer: 1
Explanation: Tuple unpacking.
41. What does a, *b = (1, 2, 3, 4) assign to b?
Answer: [2, 3, 4]
Explanation: Extended unpacking.
42. Which of these is not a tuple method?
Answer: append()
Explanation: Tuples don’t support append().
43. How are tuples stored in memory compared to lists?
Answer: More compact
Explanation: No extra space for dynamic changes.
38
Python
44. Which is faster for iteration: tuple or list?
Answer: Tuple
Explanation: Slightly faster due to immutability.
45. Can tuple be subclassed?
Answer: Yes
Explanation: You can create a subclass of tuple.
46. Can tuple elements be objects?
Answer: Yes
Explanation: Any object can be an element.
47. What happens if you try to delete an element from a tuple?
Answer: TypeError
Explanation: Tuples are immutable.
48. Can a tuple contain functions?
Answer: Yes
Explanation: Any object can be stored.
49. What is the result of tuple(range(3))?
Answer: (0, 1, 2)
Explanation: Range converted to tuple.
50. What is tuple(map(str, [1, 2]))?
Answer: ('1', '2')
Explanation: Converts integers to strings and creates a tuple.
✅ Topic: Dictionaries – 50 MCQs with Answers & Explanation
1. What is a dictionary in Python?
Answer: A collection of key-value pairs
Explanation: Each key maps to a value.
2. Are Python dictionaries ordered as of Python 3.7+?
Answer: Yes
Explanation: Insertion order is preserved.
3. How do you define an empty dictionary?
Answer: {}
Explanation: Curly braces with no content.
39
Python
4. Which method returns all keys from a dictionary?
Answer: keys()
Explanation: Returns a view of all keys.
5. Which method returns all values?
Answer: values()
Explanation: Returns a view of all values.
6. Which method returns key-value pairs?
Answer: items()
Explanation: Returns tuples of key-value pairs.
7. How do you access the value of a key k in d?
Answer: d[k]
Explanation: Direct access using square brackets.
8. What happens if a key is not found using d[k]?
Answer: KeyError
Explanation: Raises an exception.
9. Which method safely accesses a key and returns None if not found?
Answer: get()
Explanation: Doesn’t raise an error.
10. How do you add a new key-value pair?
Answer: d[key] = value
Explanation: Direct assignment.
11. How do you delete a key from a dictionary?
Answer: del d[key]
Explanation: Deletes the key and value.
12. Which method removes and returns a value by key?
Answer: pop(key)
Explanation: Returns value and removes the key.
13. Which method removes the last inserted item?
Answer: popitem()
Explanation: Removes and returns last key-value pair.
14. What is the result of len({1: 'a', 2: 'b'})?
Answer: 2
Explanation: Number of key-value pairs.
15. Can dictionary keys be of different data types?
Answer: Yes
Explanation: As long as they’re hashable.
40
Python
16. Can dictionary values be duplicated?
Answer: Yes
Explanation: Values can repeat.
17. Can dictionary keys be duplicated?
Answer: No
Explanation: Keys must be unique.
18. What is the result of dict([(1, 'a'), (2, 'b')])?
Answer: {1: 'a', 2: 'b'}
Explanation: Creates a dictionary from pairs.
19. What is the output of {x: x**2 for x in range(3)}?
Answer: {0: 0, 1: 1, 2: 4}
Explanation: Dictionary comprehension.
20. Is dict() a valid way to create a dictionary?
Answer: Yes
Explanation: Returns an empty dictionary.
21. What is type({})?
Answer: <class 'dict'>
Explanation: Empty dictionary type.
22. Which method clears all entries in a dictionary?
Answer: clear()
Explanation: Removes all key-value pairs.
23. Which method sets default value if key doesn't exist?
Answer: setdefault()
Explanation: Sets and returns value if key absent.
24. How do you merge two dictionaries in Python 3.9+?
Answer: d1 | d2
Explanation: Pipe operator merges dictionaries.
25. Can a list be used as a key in a dictionary?
Answer: No
Explanation: Lists are unhashable.
26. Can a tuple be used as a key?
Answer: Yes
Explanation: Tuples are immutable and hashable.
27. Can a dictionary be nested?
Answer: Yes
Explanation: Dictionaries can contain dictionaries.
41
Python
28. What is all({1: True, 0: False})?
Answer: False
Explanation: 0 is treated as False.
29. What is any({0: False, 1: False})?
Answer: True
Explanation: At least one key (1) is True.
30. What is sum({1: 10, 2: 20})?
Answer: 3
Explanation: sum() on dictionary sums keys.
31. How do you check if a key exists?
Answer: key in d
Explanation: Boolean check.
32. How do you copy a dictionary?
Answer: copy()
Explanation: Shallow copy of dictionary.
33. What happens when you assign d2 = d1?
Answer: Both refer to same object
Explanation: It's a reference, not a copy.
34. Which method creates a new dictionary from keys?
Answer: fromkeys()
Explanation: Creates dictionary with specified keys.
35. What is the default return of get() if not specified?
Answer: None
Explanation: If no second argument is passed.
36. How do you update a dictionary with another?
Answer: update()
Explanation: Merges another dictionary in place.
37. Are dictionaries mutable?
Answer: Yes
Explanation: Values and keys can be changed.
38. Can a dictionary hold another dictionary as a value?
Answer: Yes
Explanation: Supports nesting.
39. What does reversed(d) return for a dictionary d?
Answer: Reverse of keys
Explanation: Reverse order of keys.
42
Python
40. What is the time complexity of accessing a key?
Answer: O(1)
Explanation: Dictionary uses hash tables.
41. Can you sort a dictionary by keys?
Answer: Yes
Explanation: Use sorted([Link]()).
42. Can values be of any data type?
Answer: Yes
Explanation: No restriction on values.
43. What is a common use case for dictionaries?
Answer: Lookup tables
Explanation: Efficient key-based access.
44. Is {} same as dict()?
Answer: Yes
Explanation: Both return an empty dictionary.
45. Can dictionary keys be None?
Answer: Yes
Explanation: Any hashable object is allowed.
46. Can dictionary values be None?
Answer: Yes
Explanation: Values have no restriction.
47. Can dictionary values be functions?
Answer: Yes
Explanation: Functions are objects.
48. Is dictionary key search case sensitive?
Answer: Yes
Explanation: 'A' and 'a' are different keys.
49. What is d = {1: "a", 2: "b"}; d[3] = [Link](1)?
Answer: d becomes {2: "b", 3: "a"}
Explanation: Removes key 1 and assigns to key 3.
50. What is the output of {x: x for x in "abc"}?
Answer: {'a': 'a', 'b': 'b', 'c': 'c'}
Explanation: Simple dictionary comprehension.
✅ Topic: Functions in Python – 50 MCQs with Answers & Explanation
43
Python
1. How do you define a function in Python?
Answer: Using def keyword
Explanation: Functions are defined using def.
2. What is the default return value of a function that does not explicitly return?
Answer: None
Explanation: All functions return None if no return is specified.
3. Can a function return multiple values?
Answer: Yes
Explanation: It returns a tuple of values.
4. What keyword is used to exit a function and return a value?
Answer: return
Explanation: Exits the function and optionally sends a value.
5. What is the scope of a variable declared inside a function?
Answer: Local
Explanation: It’s accessible only within that function.
6. Can default argument values be specified in Python functions?
Answer: Yes
Explanation: You can assign default values to parameters.
7. What is the syntax to call a function named foo?
Answer: foo()
Explanation: Function call with parentheses.
8. Which symbol is used for defining default arguments?
Answer: =
Explanation: Assigns default values to parameters.
9. What is a keyword argument?
Answer: Argument passed with parameter name
Explanation: e.g., foo(x=5).
10. What is the result of calling a function without required arguments?
Answer: TypeError
Explanation: Missing arguments raise an error.
11. Can functions be assigned to variables?
Answer: Yes
Explanation: Functions are first-class objects.
44
Python
12. What is a lambda function?
Answer: Anonymous function
Explanation: Defined using lambda keyword.
13. What is the syntax for a lambda function?
Answer: lambda args: expression
Explanation: One-line anonymous function.
14. Can a lambda function have multiple expressions?
Answer: No
Explanation: Lambda supports only a single expression.
15. What is the return type of type(lambda x: x+1)?
Answer: <class 'function'>
Explanation: Lambda is still a function.
16. What is recursion in Python?
Answer: Function calling itself
Explanation: Used for problems like factorial.
17. Is there a recursion limit in Python?
Answer: Yes
Explanation: Set by [Link]().
18. What is the use of *args?
Answer: Variable number of positional arguments
Explanation: Captures multiple arguments as a tuple.
19. What is the use of **kwargs?
Answer: Variable number of keyword arguments
Explanation: Captures keyword args as a dictionary.
20. Can *args and **kwargs be used together?
Answer: Yes
Explanation: *args first, then **kwargs.
21. Can functions be nested in Python?
Answer: Yes
Explanation: You can define a function inside another.
22. What is a closure?
Answer: A function remembering its enclosing scope
Explanation: Inner function uses outer variable.
23. What is a decorator in Python?
Answer: Function modifying another function
Explanation: Used with @ symbol.
45
Python
24. What does @staticmethod do?
Answer: Defines a static method in class
Explanation: Doesn’t access class or instance.
25. What does @classmethod do?
Answer: Accesses class as first parameter
Explanation: First parameter is cls.
26. What does globals() return?
Answer: Dictionary of global variables
Explanation: Used to inspect global scope.
27. What does locals() return?
Answer: Dictionary of local variables
Explanation: Useful for debugging inside functions.
28. What is __name__ == "__main__" used for?
Answer: Script entry point
Explanation: Runs only when file is executed directly.
29. Can a function call itself?
Answer: Yes
Explanation: This is recursion.
30. Can function parameters be mutable?
Answer: Yes
Explanation: Lists or dicts can be modified inside.
31. What is a docstring?
Answer: String describing function
Explanation: Placed after function definition.
32. How do you define a docstring?
Answer: Triple quotes after def
Explanation: """docstring""".
33. What function retrieves a docstring?
Answer: help() or .__doc__
Explanation: Both access docstring.
34. Can a function return another function?
Answer: Yes
Explanation: Functions are first-class.
35. Can a function take another function as a parameter?
Answer: Yes
Explanation: Common in callbacks and decorators.
46
Python
36. How are functions stored in Python?
Answer: As objects
Explanation: Stored like any other object.
37. What is the default value of unspecified *args?
Answer: Empty tuple
Explanation: If not passed, it’s ().
38. What is the default value of unspecified **kwargs?
Answer: Empty dict
Explanation: If not passed, it’s {}.
39. Which keyword is used to define generator functions?
Answer: yield
Explanation: yield returns one value at a time.
40. How do you define a function that does nothing?
Answer: Use pass
Explanation: Placeholder for body.
41. Can function names be reused?
Answer: Yes
Explanation: New definition replaces the old one.
42. Can you return multiple values without parentheses?
Answer: Yes
Explanation: Python packs them into a tuple.
43. Which keyword is used to indicate an argument should be keyword-only?
Answer: *
Explanation: def f(*, x) makes x keyword-only.
44. Are arguments passed by value or reference?
Answer: By object reference
Explanation: Depends if the object is mutable.
45. What will print(f()) output if f has no return statement?
Answer: None
Explanation: Implicit return of None.
46. What is the scope of a parameter variable?
Answer: Local to function
Explanation: Not accessible outside.
47. What happens if return is followed by multiple values?
Answer: A tuple is returned
Explanation: Values are packed into a tuple.
47
Python
48. Can function arguments be assigned default values?
Answer: Yes
Explanation: Common for optional parameters.
49. What is the effect of mutable default arguments?
Answer: Persists across calls
Explanation: Can lead to bugs.
50. What will def f(a, b=2): return a + b return for f(3)?
Answer: 5
Explanation: Uses default value for b.
✅ Topic: File Reading and Writing in Python – 50 MCQs with Answers & Explanation
1. Which function is used to open a file in Python?
Answer: open()
Explanation: open() is used to read/write files.
2. What is the default mode of open()?
Answer: 'r'
Explanation: Read mode is the default.
3. What does the 'w' mode do?
Answer: Overwrites the file
Explanation: It creates or truncates the file.
4. What does 'a' mode do?
Answer: Appends to the end
Explanation: It doesn’t remove existing content.
5. What is 'rb' mode used for?
Answer: Read binary
Explanation: Used for non-text files.
6. What is the correct way to read an entire file at once?
Answer: [Link]()
Explanation: Reads full content as a string.
7. Which method reads file line by line into a list?
Answer: readlines()
Explanation: Returns list of lines.
48
Python
8. Which method reads a single line?
Answer: readline()
Explanation: Reads the next line.
9. How do you close a file in Python?
Answer: [Link]()
Explanation: Frees up system resources.
10. What is the use of with open()?
Answer: Automatic file closing
Explanation: It ensures the file is closed properly.
11. What does [Link]("hello") do in 'w' mode?
Answer: Overwrites content
Explanation: 'w' truncates the file.
12. Which method writes multiple lines?
Answer: writelines()
Explanation: Accepts a list of strings.
13. What type does [Link]() return?
Answer: str
Explanation: File data is returned as a string.
14. What happens if you try to read a file that doesn't exist?
Answer: Raises FileNotFoundError
Explanation: File must exist in 'r' mode.
15. Can you read and write at the same time?
Answer: Yes, using 'r+' or 'w+'
Explanation: These modes allow both operations.
16. Which mode is used to create a new file but fail if it exists?
Answer: 'x'
Explanation: Exclusive creation mode.
17. What does [Link](0) do?
Answer: Moves the cursor to the start
Explanation: Allows re-reading or overwriting.
18. What does [Link]() return?
Answer: Current cursor position
Explanation: Returns the file pointer location.
19. What does encoding='utf-8' do in open()?
Answer: Sets file character encoding
Explanation: Important for non-ASCII text.
49
Python
20. Is file an object in Python?
Answer: Yes
Explanation: Files are objects with methods.
21. How do you append without overwriting content?
Answer: Use 'a' mode
Explanation: Adds to the end.
22. What does flush() do?
Answer: Writes buffer to disk
Explanation: Ensures data is saved.
23. What happens if you open a file in 'w' mode that exists?
Answer: Contents are erased
Explanation: File is truncated.
24. What type of file is read using 'rb'?
Answer: Binary file
Explanation: e.g., images, PDFs.
25. Which function checks if a file exists?
Answer: [Link]()
Explanation: From os module.
26. How do you read a file in chunks?
Answer: Use read(n)
Explanation: Reads n bytes or characters.
27. What does [Link]() return if the file is empty?
Answer: Empty list
Explanation: No lines to read.
28. What is the result of writing a string to a file in binary mode?
Answer: Raises TypeError
Explanation: Binary mode needs bytes, not string.
29. Which method is used to write text data in binary mode?
Answer: write(b"data")
Explanation: Prefix string with b.
30. Can with be used with multiple files?
Answer: Yes
Explanation: Use commas.
31. What happens when [Link]() is called after [Link]()?
Answer: Returns ''
Explanation: Cursor is at the end.
50
Python
32. What is the type of value returned by [Link]()?
Answer: list
Explanation: List of strings.
33. What is the correct way to iterate through a file line by line?
Answer: for line in file:
Explanation: Memory-efficient line reading.
34. How do you write a newline character?
Answer: \n
Explanation: Adds a line break.
35. Which module supports file operations like rename or remove?
Answer: os
Explanation: Used for file system tasks.
36. What happens if you forget to close a file?
Answer: Data may not be saved
Explanation: Buffered data could be lost.
37. What does [Link]() do?
Answer: Cuts the file at current cursor
Explanation: Removes remaining content.
38. What happens if you call write() after closing the file?
Answer: Raises ValueError
Explanation: Cannot write to closed file.
39. Can you reopen a closed file?
Answer: No
Explanation: You must call open() again.
40. Can with open() be nested?
Answer: Yes
Explanation: Multiple context managers supported.
41. What is the difference between text and binary mode?
Answer: Binary uses bytes, text uses strings
Explanation: Text is human-readable.
42. What mode to use to read and write a binary file?
Answer: 'rb+'
Explanation: Read and write binary.
43. How to read all lines as a single string with line breaks?
Answer: [Link]()
Explanation: Keeps \n characters.
51
Python
44. What will open("[Link]", "r+") do?
Answer: Read/write without truncation
Explanation: Doesn’t erase content.
45. How do you delete a file in Python?
Answer: [Link]("[Link]")
Explanation: Deletes the file from disk.
46. How to copy contents from one file to another?
Answer: Read from one, write to another
Explanation: Use two file handles.
47. Can you write a list directly using write()?
Answer: No
Explanation: Must join or loop through list.
48. Can Python handle large files?
Answer: Yes
Explanation: Process line-by-line or chunks.
49. What type of error is raised for permission issues?
Answer: PermissionError
Explanation: If you lack rights to access file.
50. What does [Link] return?
Answer: File access mode
Explanation: Returns 'r', 'w', etc.
52