UNIT 2 PYTHON MCQ
UNIT 2 PYTHON MCQ
• a) .py
• b) .pt
• c) .pyt
• d) .pyth
Answer: a) .py
• a) Dennis Ritchie
• b) Guido van Rossum
• c) James Gosling
• d) Bjarne Stroustrup
Answer: b) Guido van Rossum
• a) echo("Hello")
• b) print("Hello")
• c) print Hello
• d) printf("Hello")
Answer: b) print("Hello")
• a) 6
• b) 9
• c) 8
• d) 5
Answer: c) 8
• a) scanf()
• b) cin
• c) input()
• d) read()
Answer: c) input()
• a) list = {1, 2, 3}
• b) list = [1, 2, 3]
• c) list = (1, 2, 3)
• d) list = <1, 2, 3>
Answer: b) list = [1, 2, 3]
• a) List
• b) Set
• c) Dictionary
• d) Tuple
Answer: d) Tuple
• a) //
• b) <!-- -->
• c) #
• d) /* */
Answer: c) #
• a) int
• b) float
• c) str
• d) bool
Answer: a) int
10. What is the output of 10 % 3?
• a) 3
• b) 1
• c) 0
• d) 10
Answer: b) 1
• a) delete()
• b) pop()
• c) remove()
• d) discard()
Answer: c) remove()
• a) while True:
• b) for i in range(0, ∞):
• c) while(1):
• d) Both a and c
Answer: d) Both a and c
• a) True
• b) False
• c) 0
• d) 1
Answer: b) False
• a) func
• b) define
• c) def
• d) function
Answer: c) def
• a) 5
• b) 6
• c) 7
• d) Error
Answer: b) 6
• a) str()
• b) float()
• c) int()
• d) bool()
Answer: c) int()
• a) //
• b) #
• c) <!-- -->
• d) %
Answer: b) #
• a) dict = []
• b) dict = ()
• c) dict = {}
• d) dict = set()
Answer: c) dict = {}
19. What is the result of 2 + 3 * 4?
• a) 20
• b) 14
• c) 24
• d) 12
Answer: b) 14
• a) python
• b) PYTHON
• c) Python
• d) Error
Answer: b) PYTHON
• a) split()
• b) separate()
• c) break()
• d) slice()
Answer: a) split()
• a) True
• b) False
• c) None
• d) Error
Answer: b) False
• a) ^
• b) **
• c) %
• d) //
Answer: b) **
• a) HelloWorld
• b) Hello World
• c) Error
• d) Hello+World
Answer: a) HelloWorld
• a) {}
• b) ()
• c) :
• d) ;
Answer: c) :
• a) 4.6
• b) 4.57
• c) 4.56
• d) 4.5
Answer: b) 4.57
• a) read()
• b) fread()
• c) open()
• d) file()
Answer: c) open()
28. What is the correct way to create a tuple?
• a) [1, 2, 3]
• b) {1, 2, 3}
• c) (1, 2, 3)
• d) <1, 2, 3>
Answer: c) (1, 2, 3)
29. Which method is used to remove whitespace from the start and end of a
string?
• a) strip()
• b) remove()
• c) trim()
• d) clean()
Answer: a) strip()
• a) True
• b) False
• c) Error
• d) None
Answer: a) True
• a) Compiler
• b) Translator
• c) Interpreter
• d) Assembler
Answer: c) Interpreter
• a) python.exe
• b) py
• c) python
• d) cmd
Answer: c) python
• a) python --version
• b) python -v
• c) py -version
• d) python version
Answer: a) python --version
35. What is the extension of bytecode files generated by the Python interpreter?
• a) .class
• b) .obj
• c) .pyc
• d) .exe
Answer: c) .pyc
• a) Assembly code
• b) Machine code
• c) Bytecode
• d) Source code
Answer: c) Bytecode
37. How do you exit the Python interpreter in interactive mode?
• a) exit()
• b) quit()
• c) Ctrl + Z
• d) All of the above
Answer: d) All of the above
40. Which command is used to run a Python script from the terminal?
• a) run file.py
• b) python file.py
• c) py file.py
• d) Both b and c
Answer: d) Both b and c
• a) Compile code
• b) Execute code
• c) Debug code
• d) Optimize code
Answer: b) Execute code
• a) help()
• b) info()
• c) guide()
• d) manual()
Answer: a) help()
• a) restart()
• b) Ctrl + C
• c) exit() and reopen
• d) refresh()
Answer: c) exit() and reopen
• a) Source code
• b) Bytecode files
• c) Log files
• d) Error logs
Answer: b) Bytecode files
45. Which file format is used by the Python interpreter to store bytecode?
• a) .class
• b) .exe
• c) .pyc
• d) .jar
Answer: c) .pyc
46. How can you run a Python script without opening the interpreter?
• a) python filename.py
• b) open filename.py
• c) run filename.py
• d) execute filename.py
Answer: a) python filename.py
• a) os
• b) sys
• c) time
• d) random
Answer: b) sys
50. Which of the following can be used to install third-party libraries in Python
interpreter?
• a) install
• b) apt-get
• c) pip
• d) pkg
Answer: c) pip
• a) 53
• b) 8
• c) 15
• d) Error
Answer: b) 8
• a) ^
• b) **
• c) *
• d) %
Answer: b) **
• a) 3
• b) 3.0
• c) 3.33
• d) 3.3333333333333335
Answer: d) 3.3333333333333335
• a) 3
• b) 3.33
• c) 3.0
• d) 4
Answer: a) 3
• a) 6
• b) 8
• c) 9
• d) 5
Answer: b) 8
• a) 1
• b) 2
• c) 3
• d) 0
Answer: b) 1
• a) 3.46
• b) 3.45
• c) 3.50
• d) 3.4
Answer: a) 3.46
• a) 10
• b) -10
• c) 0
• d) Error
Answer: a) 10
60. What is the output of max(4, 9, 2)?
• a) 4
• b) 2
• c) 9
• d) Error
Answer: c) 9
• a) 7
• b) 3
• c) -2
• d) 5
Answer: c) -2
• a) 36
• b) 16
• c) 40
• d) 20
Answer: b) 16
• a) sqrt(16)
• b) 16 ** 0.5
• c) square(16)
• d) power(16, 0.5)
Answer: b) 16 ** 0.5
• a) 27
• b) 18
• c) 9
• d) 81
Answer: a) 27
• a) 6
• b) 8
• c) 9
• d) 5
Answer: b) 8
• a) round()
• b) ceil()
• c) floor()
• d) abs()
Answer: a) round()
• a) (3, 1)
• b) (3.33, 1)
• c) (3, 0)
• d) (10, 3)
Answer: a) (3, 1)
• a) 0.1
• b) 0.01
• c) 100
• d) Error
Answer: b) 0.01
69. Which operator has the highest precedence in Python?
• a) +
• b) *
• c) **
• d) %
Answer: c) **
• a) 3
• b) 4
• c) 3.8
• d) Error
Answer: a) 3
• a) Text Editor
• b) Interactive Interpreter
• c) Compiler
• d) Debugger
Answer: b) Interactive Interpreter
• a) python
• b) python.exe
• c) py
• d) Both a and c
Answer: d) Both a and c
• a) $
• b) >
• c) >>>
• d) #
Answer: c) >>>
• a) close()
• b) exit()
• c) quit()
• d) Both b and c
Answer: d) Both b and c
75. How can you check the Python version inside the Python Shell?
• a) version()
• b) sys.version
• c) python --version
• d) shell.version()
Answer: b) sys.version
• a) Write scripts
• b) Execute code line by line
• c) Debug code
• d) All of the above
Answer: d) All of the above
• a) Using {}
• b) Using ()
• c) Using backslash \
• d) Press Enter twice
Answer: b) Using ()
78. Which module provides access to the Python Shell interpreter?
• a) shell
• b) sys
• c) os
• d) subprocess
Answer: b) sys
• a) Error
• b) Nothing
• c) 10
• d) 55
Answer: c) 10
• a) restart()
• b) Ctrl + Z
• c) exit() and reopen
• d) refresh()
Answer: c) exit() and reopen
• a) Hello
• b) "Hello"
• c) Error
• d) None
Answer: a) Hello
• a) int
• b) float
• c) str
• d) bool
Answer: a) int
84. What is the command to clear the Python Shell screen on Windows?
• a) clear
• b) cls
• c) clean
• d) reset
Answer: b) cls (in terminal, not directly in shell)
85. How can you access the last result in Python Shell?
• a) $
• b) _
• c) @
• d) #
Answer: b) _
• a) func
• b) def
• c) function
• d) define
Answer: b) def
87. How can you view all built-in functions in Python Shell?
• a) list()
• b) dir()
• c) help()
• d) methods()
Answer: b) dir()
• a) 6
• b) 9
• c) 32
• d) 12
Answer: b) 9
• a) 3.33
• b) 3
• c) 4
• d) 3.0
Answer: b) 3
• a) use module
• b) include module
• c) import module
• d) module()
Answer: c) import module
• a) Start of a comment
• b) Code block
• c) End of a line
• d) Function call
Answer: b) Code block
• a) No effect
• b) Code will execute normally
• c) IndentationError
• d) SyntaxWarning
Answer: c) IndentationError
• a) 2
• b) 3
• c) 4
• d) 5
Answer: c) 4
• a) SyntaxError
• b) TypeError
• c) IndentationError
• d) ValueError
Answer: c) IndentationError
• a) Yes
• b) No
Answer: b) No
• a) if statement
• b) for loop
• c) while loop
• d) All of the above
Answer: d) All of the above
• a) Yes
• b) No
Answer: b) No
100. What is the standard indentation size followed in Python PEP 8 guidelines?
• a) 2 spaces
• b) 4 spaces
• c) 6 spaces
• d) 8 spaces
Answer: b) 4 spaces
A) 10
B) 'Hello'
C) [1, 2, 3]
D) All of the above
A) Identifiers
B) Literals
C) Containers
D) All of the above
A) int
B) float
C) complex
D) str
Answer: B) float
A) Tuple
B) Dictionary
C) String
D) If-else statement
A) Numbers
B) Identifiers
C) Strings
D) Keywords
Answer: B) Identifiers
A) int
B) float
C) complex
D) str
Answer: C) complex
A) List
B) Dictionary
C) String
D) Set
Answer: C) String
A) int
B) bool
C) str
D) None
Answer: B) bool
110. Which function is used to get the data type of any atom in Python?
A) type()
B) atom()
C) id()
D) isinstance()
Answer: A) type()
1. Arithmetic Operators
4. Assignment Operators
5. Ternary Operator
6. Bitwise Operators
2. Output Statements
5. Miscellaneous Questions
if 0:
print("True")
else:
print("False")
a) True
b) False
c) Error
d) None
Answer: b) False
x = 5
if x > 10:
print("Greater")
elif x > 3:
print("Medium")
else:
print("Smaller")
a) Greater
b) Medium
c) Smaller
d) None
Answer: b) Medium
2. Looping Statements (for, while, nested loops)
i = 1
while i < 4:
print(i)
i += 1
a) 1 2 3 4
b) 1 2 3
c) 2 3 4
d) Infinite loop
Answer: b) 1 2 3
python
CopyEdit
for i in range(5):
if i == 3:
break
print(i)
a) 0 1 2 3 4
b) 0 1 2
c) 1 2 3
d) 0 1 2 3
Answer: b) 0 1 2
for i in range(5):
if i == 3:
continue
print(i)
a) 0 1 2 3 4
b) 0 1 2 4
c) 1 2 3 4
d) 0 1 2
Answer: b) 0 1 2 4
4. Exit Function (sys.exit())
for i in range(2):
for j in range(2):
print(i, j)
python
CopyEdit
for i in range(3):
for j in range(2):
print(i, j)
a) 3 times
b) 2 times
c) 5 times
d) 6 times
Answer: d) 6 times
23. What happens when a break statement is encountered inside a nested loop?
a) Only the inner loop exits
b) Both inner and outer loops exit
c) The program stops execution
d) The loop skips one iteration
Answer: a) Only the inner loop exits
24. What will be the output of this program?
for i in range(3):
for j in range(3):
if i == j:
break
print(i, j)
a) 1 2 3 4
b) 1 3
c) 1 2
d) 1 3 5
Answer: b) 1 3
a) 3 times
b) 4 times
c) 5 times
d) Infinite
Answer: c) 5 times
a) 1 2 3 4 5
b) 1 3 5
c) 2 4
d) None
Answer: b) 1 3 5
for i in range(3):
print(i)
else:
print("Loop completed")
a) 0 1 2
b) 0 1 2 Loop completed
c) Loop completed
d) 0 1 Loop completed
Answer: b) 0 1 2 Loop completed
for i in range(3):
pass
print("Done")
a) 0 1 2 Done
b) Done
c) Pass Done
d) Error
Answer: b) Done
i = 1
while i < 5:
if i == 3:
break
print(i)
i += 1
a) 1 2 3 4
b) 1 2
c) 1 2 4
d) 3 4
Answer: b) 1 2
for i in range(5):
if i == 3:
pass
print(i)
a) 0 1 2 4
b) 0 1 2 3 4
c) 0 1 2
d) 1 2 3 4
Answer: b) 0 1 2 3 4
i = 5
while i > 0:
print(i, end=" ")
i -= 2
a) 5 3 1
b) 5 4 3 2 1
c) 5 3
d) 5 4 3 2
Answer: a) 5 3 1
a) 1 2 3 4 5
b) 1 3 5
c) 1 3
d) 1 2
Answer: b) 1 3 5
for i in range(2):
print("Hello")
a) Hello Hello
b) Hello
c) Error
d) None
Answer: a) Hello Hello
def greet():
print("Hello, World!")
greet()
a) Hello, World!
b) None
c) greet()
d) Error
Answer: a) Hello, World!
def my_func():
return 5
print(my_func())
a) 5
b) None
c) Error
d) my_func()
Answer: a) 5
5. Which of the following is correct syntax to return multiple values from a function?
a) return a, b, c
b) return [a, b, c]
c) return (a, b, c)
d) All of the above
Answer: d) All of the above
2. Function Arguments
6. What is the default return value of a function that does not have a return statement?
a) 0
b) None
c) Error
d) Empty string
Answer: b) None
7. How many arguments can a Python function take?
a) Only 1
b) At most 5
c) Any number of arguments
d) None
Answer: c) Any number of arguments
8. What will be the output of this function?
a) 10 20 5
b) 10 3 5
c) 10 20
d) Error
Answer: a) 10 20 5
a) 6
b) 12
c) 9
d) 8
Answer: b) 12
def greet(name="Guest"):
print("Hello,", name)
a) greet()
b) greet("Alice")
c) Both a and b
d) None of the above
Answer: c) Both a and b
14. What happens if you pass all arguments to a function with default values?
a) Default values are ignored
b) Error occurs
c) Default values are used
d) None of the above
Answer: a) Default values are ignored
15. Which of the following statements is correct regarding default arguments?
a) Default arguments can only be strings
b) Default arguments can only be integers
c) Default arguments can be any data type
d) Default arguments must always be 0
Answer: c) Default arguments can be any data type
a) 5 2 10
b) 5 10 2
c) 10 2 5
d) Error
Answer: a) 5 2 10
a) 10 2 15
b) 10 15 2
c) Error
d) 15 2 10
Answer: a) 10 2 15
a) add(3, 4, 5)
b) add(3, c=8)
c) add(a=2, 3)
d) add(3)
Answer: c) add(a=2, 3)
5. Miscellaneous Questions
a) 14
b) 9
c) Error
d) 25
Answer: a) 14
def hello():
return "Hi"
print(hello())
a) Hi
b) None
c) hello()
d) Error
Answer: a) Hi
1. Errors and Exceptions (15 Questions)
print(10 / 0)
a) TypeError
b) ZeroDivisionError
c) SyntaxError
d) ValueError
Answer: b) ZeroDivisionError
3. Which exception is raised when trying to access a list index that doesn’t exist?
a) KeyError
b) IndexError
c) TypeError
d) AttributeError
Answer: b) IndexError
4. What will be the output of this program?
try:
print(5 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
a) ZeroDivisionError
b) Cannot divide by zero
c) None
d) Runtime Error
Answer: b) Cannot divide by zero
try:
x = int("hello")
except ValueError:
print("Invalid conversion")
a) Invalid conversion
b) ValueError
c) hello
d) TypeError
Answer: a) Invalid conversion
try:
x = 5 / 0
finally:
print("Finally block executed")
x = 1
while x < 5:
print(x)
x += 1
def count(n):
if n == 0:
return
print(n)
count(n-1)
count(3)
a) 3 2 1
b) 1 2 3
c) Error
d) Infinite loop
Answer: a) 3 2 1
if 3 > 2:
print("Yes")
a) Yes
b) No
c) Error
d) None
Answer: a) Yes
a = 10
b = 20
if a > b:
print("A")
else:
print("B")
a) A
b) B
c) None
d) Error
Answer: b) B
x = 10
if x > 5:
if x < 15:
print("Nested")
a) Nested
b) Error
c) No output
d) 15
Answer: a) Nested
def func():
return 5
print("Hello")
print(func())
a) Hello
b) 5 Hello
c) 5
d) Error
Answer: c) 5
print(10 / 0)
a) 0
b) Infinity
c) ZeroDivisionError
d) None
Answer: c) ZeroDivisionError
2. Iteration in Python
for i in range(3):
print(i, end=" ")
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) None
Answer: a) 0 1 2
for i in range(3):
if i == 1:
continue
print(i, end=" ")
a) 0 1 2
b) 0 2
c) 1 2
d) None
Answer: b) 0 2
x = 0
while x < 3:
print(x)
x += 1
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) Infinite loop
Answer: a) 0 1 2
3. Recursion in Python
def func(n):
if n == 0:
return 1
return n * func(n - 1)
print(func(3))
a) 3
b) 6
c) 9
d) None
Answer: b) 6
4. Conditional Execution
if 0:
print("Hello")
a) Hello
b) Nothing
c) Error
d) 0
Answer: b) Nothing
a = 5
if a < 10:
print("Less than 10")
a) Less than 10
b) Error
c) None
d) Nothing
Answer: a) Less than 10
5. Alternative Execution
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
a) Even
b) Odd
c) None
d) Error
Answer: b) Odd
6. Nested Conditionals
x = 5
if x > 0:
if x < 10:
print("Between 0 and 10")
a) Between 0 and 10
b) Nothing
c) Error
d) None
Answer: a) Between 0 and 10
try:
print(5 / 0)
except:
print("Exception Occurred")
a) Exception Occurred
b) ZeroDivisionError
c) None
d) 5 / 0
Answer: a) Exception Occurred
print("Hello)
a) SyntaxError
b) NameError
c) IndentationError
d) TypeError
Answer: a) SyntaxError
try:
x = int("hello")
except ValueError:
print("Invalid conversion")
a) Invalid conversion
b) ValueError
c) TypeError
d) hello
Answer: a) Invalid conversion
4. What does the finally block do in a try-except statement?
a) Runs only if an exception occurs
b) Always executes
c) Executes only if no error occurs
d) Skips error handling
Answer: b) Always executes
5. What will be the output of this code?
try:
print(1 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Execution completed")
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input")
else:
print("Valid input")
If the user enters "abc", what is printed?
a) Invalid input
b) Valid input
c) None
d) Error
Answer: a) Invalid input
10. Which error occurs if you try to open a file that does not exist?
a) FileNotFoundError
b) IOError
c) SyntaxError
d) TypeError
Answer: a) FileNotFoundError
x = 0
while x < 3:
print(x)
x += 1
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) Infinite loop
Answer: a) 0 1 2
13. What will happen if the base condition is not defined in a recursive function?
a) Infinite recursion
b) Syntax error
c) Logical error
d) Stops execution
Answer: a) Infinite recursion
14. What will this recursive function print?
def count(n):
if n == 0:
return
print(n)
count(n-1)
count(3)
a) 3 2 1
b) 1 2 3
c) 0
d) None
Answer: a) 3 2 1
if 5 > 3:
print("Yes")
a) Yes
b) No
c) Error
d) None
Answer: a) Yes
a) 5
b) 2, 3
c) None
d) Error
Answer: a) 5