Programming With Python (CT108-3-1-PYP) ALL 12 CHAPTERS MCQ
Programming With Python (CT108-3-1-PYP) ALL 12 CHAPTERS MCQ
In the context of problem-solving, which component is NOT part of the IPO chart?
● A. Input
● B. Output
● C. Processing
● D. Feedback
● A. PRINT
● B. COMPUTE
● C. GET
● D. SET
● A. Rectangle
● B. Diamond
● C. Oval
● D. Arrow
● A. Input
● B. Arithmetic operations
● C. User interface design
● D. Repetition
Which pseudocode keyword is used to start a loop that continues while a condition is true?
● A. IF
● B. WHILE
● C. PRINT
● D. SET
● A. PRINT total_price
● B. SET total_price to 0
● C. DISPLAY total_price
● D. INPUT total_price
● A. Process symbol
● B. Terminal symbol
● C. Decision symbol
● D. Connector symbol
● A. THEN
● B. IF
● C. ELSE
● D. ENDIF
● A. Rectangle
● B. Parallelogram
● C. Oval
● D. Arrow
Which of the following best describes the term "processing" in the context of an algorithm?
● A. ENDIF
● B. ELSE
● C. END
● D. STOP
● A. Rectangle
● B. Diamond
● C. Oval
● D. Parallelogram
Which of the following actions does NOT typically occur during the "code the algorithm" step?
In a pseudocode algorithm, which keyword would you use to indicate an output operation?
● A. GET
● B. PRINT
● C. SET
● D. INPUT
Which of the following describes the role of a "defining diagram" or IPO chart in
problem-solving?
====================================================================
CHAPTER 2
In the following pseudocode, what will be the value of service_charge if account_balance is 400?
7.
○ A. 5.00
○ B. 2.00
○ C. 0.00
○ D. 7.00
8. Which of the following is a correct representation of a relational expression?
○ A. (x > y) and (y > z)
○ B. (x => y) or (z <= y)
○ C. (x <== z) and (y == z)
○ D. (x =< z) or (y != z)
9. In a for loop, the initialization of the loop variable occurs:
○ A. At the end of the loop
○ B. Before the loop starts
○ C. In the loop body
○ D. After each iteration
10. Which pseudocode keyword indicates the end of a conditional block?
○ A. THEN
○ B. ELSE
○ C. ENDIF
○ D. IF
11. A _________ structure allows you to choose between two or more alternatives.
○ Answer: Selection
12. In a flowchart, the _________ symbol is used to represent the start and end of a process.
○ Answer: Oval
13. The logical operator used to negate a condition is _________.
○ Answer: NOT
14. In a nested selection structure, an _________ statement appears within another IF
statement.
○ Answer: IF
15. The keyword _________ is used in pseudocode to represent an output operation.
○ Answer: PRINT
16. True or False: In Python, the logical operator for OR is represented by ||.
○ Answer: False
17. True or False: A while loop checks its condition before executing the loop body.
○ Answer: True
18. True or False: In a flowchart, a rectangle is used to represent a decision point.
○ Answer: False
19. True or False: The ELSE statement in pseudocode is used to specify actions to be taken
when the IF condition is true.
○ Answer: False
20. True or False: The AND logical operator in Python requires both conditions to be true for
the combined expression to be true.
○ Answer: True
21. Which keyword in pseudocode indicates the beginning of a conditional block?
○ A. IF
○ B. THEN
○ C. ELSE
○ D. ENDIF
22. The relational operator != in Python means:
○ A. Less than
○ B. Equal to
○ C. Greater than
○ D. Not equal to
23. Which of the following is an example of a post-test loop structure?
○ A. while
○ B. for
○ C. do-whiletea
○ D. repeat-until
24. In pseudocode, which keyword is used to signify an output operation to the screen?
○ A. GET
○ B. INPUT
○ C. DISPLAY
○ D. READ
25. The control structure that repeats a block of code as long as a condition is true is called:
○ A. Selection
○ B. Sequence
○ C. Iteration
○ D. Condition
○
26. The _________ logical operator is used to combine multiple conditions where all
conditions must be true.
○ Answer: AND
27. In a flowchart, the _________ symbol is used to indicate a process or operation.
○ Answer: Rectangle
28. A _________ loop tests the condition after executing the loop body.
○ Answer: Post-test
29. The _________ operator is used to compare two values for equality in Python.
○ Answer: ==
30. A _________ structure in programming is used to execute a set of statements
sequentially.
○ Answer: Sequence
● A. Iteration
● B. Sequence
● C. Selection
● D. Repetition
● A. Selection
● B. Iteration
● C. Sequence
● D. Condition
In pseudocode, the keyword used to represent a loop that continues while a condition is true is:
● A. FOR
● B. WHILE
● C. REPEAT
● D. UNTIL
Which logical operator would you use to combine conditions where at least one must be true?
● A. AND
● B. OR
● C. NOT
● D. XOR
IF x > y THEN
DISPLAY "X is greater"
ELSE
DISPLAY "Y is greater"
ENDIF
● A. X is greater
● B. Y is greater
● C. X and Y are equal
● D. No output
● A. Ambiguous steps
● B. Simplicity
● C. Precision
● D. Correctness
The keyword used in pseudocode to signify a comparison is:
● A. COMPARE
● B. IF
● C. CHECK
● D. TEST
Which pseudocode keyword represents an action that should only occur if a specified condition
is false?
● A. THEN
● B. ELSE
● C. ENDIF
● D. IF
Which of the following control structures allows multiple selections based on different
conditions?
● A. Simple IF
● B. Nested IF
● C. Switch-case
● D. For loop
● A. ENDLOOP
● B. ENDIF
● C. ENDWHILE
● D. STOP
What is the primary purpose of using flowcharts in programming?
● A. PROCESS
● B. OPERATION
● C. ACTION
● D. COMPUTE
===================================================================
CHAPTER 3
● A. Strongly typed
● B. Object-oriented
● C. Static typing
● D. Free and open-source
● A. 1985
● B. 1989
● C. 1991
● D. 2000
● A. echo
● B. printf
● C. print
● D. output
● A. Integer
● B. Float
● C. Character
● D. String
● A. Initialization
● B. Declaration
● C. Assignment
● D. Naming
● A. Python 2.7
● B. Python 3.x
● C. Python 1.6
● D. Python 2.6
x = 10
y=5
print(x + y)
● A. 10
● B. 5
● C. 15
● D. Error
● A. 2.5
● B. 2
● C. 3
● D. 1
● A. Single quotes
● B. Double quotes
● C. Triple quotes
● D. All of the above
● A. +
● B. &
● C. *
● D. %
What is the result of the following code snippet?
a = "Python"
b = "Programming"
print(a + " " + b)
● A. PythonProgramming
● B. Python Programming
● C. "Python" "Programming"
● D. Error
● A. 2nd_value
● B. value_2
● C. value-2
● D. value.2
● A. //
● B. /*
● C. #
● D. --
● A. def myFunction {}
● B. function myFunction()
● C. def myFunction():
● D. function: myFunction()
● A. try
● B. except
● C. finally
● D. All of the above
● A. 5 + 5
● B. "5" + "5"
● C. 5 + "5"
● D. "5" * 2
for i in range(3):
print(i)
● A. 1 2 3
● B. 0 1 2
● C. 1 2 3 4
● D. 0 1 2 3
● A. String
● B. List
● C. Tuple
● D. Integer
● A. append()
● B. add()
● C. insert()
● D. extend()
Which statement will execute a block of code only if a specified condition is true?
● A. for
● B. while
● C. if
● D. def
In Python, which keyword is used to start a loop that will continue as long as a specified
condition is true?
● A. for
● B. while
● C. loop
● D. until
Which of the following is the correct way to open a file named data.txt for reading in Python?
● A. open("data.txt", "r")
● B. open("data.txt", "w")
● C. open("data.txt", "rb")
● D. open("data.txt")
x = [1, 2, 3, 4, 5]
print(x[1:3])
● A. [1, 2]
● B. [2, 3]
● C. [1, 2, 3]
● D. [2, 3, 4]
Which of the following functions is used to find the length of a list in Python?
● A. length()
● B. len()
● C. count()
● D. size()
x = 10
y=5
x, y = y, x
print(x, y)
● A. 10 5
● B. 5 10
● C. 0 0
● D. Error
● A. {1, 2, 3}
● B. [1, 2, 3]
● C. {1: "one", 2: "two", 3: "three"}
● D. (1, 2, 3)
Which method is used to remove an item from a list by its value in Python?
● A. delete()
● B. remove()
● C. pop()
● D. discard()
● A. []
● B. {}
● C. ()
● D. set()
a = [1, 2, 3]
b=a
b.append(4)
print(a)
● A. [1, 2, 3]
● B. [1, 2, 3, 4]
● C. [4, 2, 3, 4]
● D. Error
● A. str()
● B. int()
● C. float()
● D. chr()
What is the purpose of the pass statement in Python?
● A. global
● B. nonlocal
● C. public
● D. lambda
def func():
x = 10
print(x)
x = 20
func()
print(x)
● A. 10 20
● B. 20 10
● C. 10 10
● D. 20 20
● A. upper()
● B. lower()
● C. split()
● D. replace()
Which of the following methods is used to remove whitespace from the beginning and end of a
string in Python?
● A. strip()
● B. trim()
● C. remove()
● D. clear()
● A. def
● B. lambda
● C. func
● D. anon
x = "Hello"
y = "World"
print(x * 2 + y)
● A. Hello World
● B. HelloHello World
● C. HelloHelloWorld
● D. Error
===============================================================
CHAPTER 4
● A. 2nd_variable
● B. variable_2
● C. variable-2
● D. variable.2
● A. ==
● B. :=
● C. =
● D. =>
a, b, c = 3, 4.5, "Python"
print(a, b, c)
● A. 3 4 Python
● B. 3 4.5 Python
● C. 3.0 4.5 Python
● D. Error
number = 10
number = 2.5
print(number)
● A. 10
● B. 2.5
● C. Error
● D. None
● A. global
● B. Global
● C. GLOBAL
● D. public
def add():
a = 20
b = 10
c=a+b
print("The sum is", c)
add()
● A. The sum is 20
● B. The sum is 10
● C. The sum is 30
● D. Error
Which of the following statements correctly declares and assigns a value to a constant in Python?
● A. PI = 3.14
● B. const PI = 3.14
● C. constant PI = 3.14
● D. define PI = 3.14
Which of the following functions is used to check the data type of a variable in Python?
● A. checktype()
● B. datatype()
● C. typeof()
● D. type()
5 // 2
● A. 2.5
● B. 2
● C. 3
● D. 2.0
● A. +
● B. **
● C. *
● D. /
What will be the result of the following expression?
2 ** 3 ** 2
● A. 64
● B. 512
● C. 8
● D. 256
x = 10
y=5
result = x + y * x / y - x
print(result)
10 + 50/5 - 10
10 + 10 - 10
20 - 10
10
● A. 10.0
● B. 5.0
● C. 15.0
● D. 20.0
Which of the following statements is true about the type() function in Python?
x = True
y = False
result = x and not y
print(result)
● A. True
● B. False
● C. None
● D. Error
● A. 5 + 2 * 3
● B. 5 + * 2
● C. 5 / + 2
● D. 5 + 2 *
a = 10
b=a*5
c = "Your result is: "
print(c + str(b))
● A. int("123")
● B. str(123)
● C. float("123.45")
● D. string("123")
a = 10
b=5
result = a > b and a < 20
print(result)
● A. True
● B. False
● C. None
● D. Error
● A. None
● B. False
● C. True
● D. value
a = "5"
b = int(a)
c=b+5
print(c)
● A. 55
● B. 10
● C. Error
● D. None
● A. //
● B. /*
● C. #
● D. ---
Which of the following is NOT a valid way to declare multiple variables in a single line in
Python?
● A. a, b, c = 1, 2, 3
● B. a = b = c = 1
● C. a = 1; b = 2; c = 3
● D. a b c = 1 2 3
7%3
● A. 2
● B. 1
● C. 3
● D. 0
Which of the following is the correct way to use the global keyword inside a function?
● A. global x = 10
● B. def global x
● C. global x; x = 10
● D. x = global 10
def test():
x = 10
y=5
print(x + y)
test()
print(x)
● A. 15 and 10
● B. 15 and Error
● C. 10 and 15
● D. Error and 10
● A. exit
● B. stop
● C. break
● D. return
● A. int("123")
● B. float("123.45")
● C. int("abc")
● D. str(123)
x=3
y = x ** 2
print(y)
● A. 6
● B. 9
● C. 3
● D. 8
● A. ^
● B. **
● C. %
● D. $
x = 10
y=5
print(x * y)
● A. 15
● B. 50
● C. 5
● D. 2
● A. length()
● B. len()
● C. size()
● D. count()
=================================================================
CHAPTER 5
x = 10
if x < 10:
print("Less than 10")
elif x == 10:
print("Equal to 10")
else:
print("Greater than 10")
● A. Less than 10
● B. Equal to 10
● C. Greater than 10
● D. Error
● A. ==
● B. !=
● C. <>
● D. <=
x=5
if x > 10:
print("Greater than 10")
else:
print("10 or less")
● A. Greater than 10
● B. 10 or less
● C. Error
● D. None
B.
if x < 10:
print("Less than 10")
elif x == 10:
print("Equal to 10")
else:
print("Greater than 10")
C.
if x < 10:
print("Less than 10")
elif x == 10:
print("Greater than 10")
else:
print("Equal to 10")
D.
if x < 10:
print("Less than 10")
elif x == 10
print("Equal to 10")
else:
print("Greater than 10")
x = 10
y=5
if x > y:
if x == 10:
print("x is 10")
else:
print("x is greater than 10")
else:
print("x is less than or equal to y")
● A. x is 10
● B. x is greater than 10
● C. x is less than or equal to y
● D. Error
Which of the following best describes the purpose of the elif keyword in Python?
● A. To end an if block
● B. To provide an alternative condition if the initial if statement is false
● C. To indicate the end of a loop
● D. To execute a block of code when the initial if statement is true
A.
if x > 10:
print("Greater than 10")
B.
if x > 10:
print("Greater than 10")
C.
if x > 10:
print("Greater than 10")
else:
print("10 or less")
D.
if x > 10:
print("Greater than 10")
else if x == 10:
print("Equal to 10")
What will be the output of the following code?
x = 15
if x < 10:
print("Less than 10")
elif x < 20:
print("Less than 20")
else:
print("20 or more")
● A. Less than 10
● B. Less than 20
● C. 20 or more
● D. Error
x=5
if x == 5:
print("Equal to 5")
elif x == 10:
print("Equal to 10")
else:
print("Not equal to 5 or 10")
● A. Equal to 5
● B. Equal to 10
● C. Not equal to 5 or 10
● D. Error
B.
if x > 10:
print("Greater than 10")
else:
print("10 or less")
C.
if x > 10:
print("Greater than 10")
else if x == 10:
print("Equal to 10")
D.
if x > 10:
print("Greater than 10")
elif x == 10:
print("Equal to 10")
else:
print("10 or less")
x = 20
if x < 10:
print("Less than 10")
elif x < 30:
print("Less than 30")
elif x < 40:
print("Less than 40")
else:
print("40 or more")
● A. Less than 10
● B. Less than 30
● C. Less than 40
● D. 40 or more
B.
if x > 10:
if x < 20:
print("Between 10 and 20")
else:
print("Greater than 20")
else:
print("10 or less")
C.
if x > 10:
if x < 20:
print("Between 10 and 20")
elif x == 20:
print("Equal to 20")
else:
print("Greater than 20")
else:
print("10 or less")
A.
if x > 10:
print("Greater than 10")
B.
if x > 10:
print("Greater than 10")
else:
print("10 or less")
C.
if x > 10:
print("Greater than 10")
elif x == 10:
print("Equal to 10")
else:
print("10 or less")
x = 25
if x < 10:
print("Less than 10")
elif x < 20:
print("Less than 20")
else:
print("20 or more")
● A. Less than 10
● B. Less than 20
● C. 20 or more
● D. Error
x = 30
if x < 20:
print("Less than 20")
elif x < 40:
print("Less than 40")
else:
print("40 or more")
● A. Less than 20
● B. Less than 40
● C. 40 or more
● D. Error
● A. Curly braces {}
● B. Parentheses ()
● C. Indentation
● D. Semicolons ;
A.
if "string":
print("True")
B.
if 5 > 3:
print("True")
C. if [1, 2, 3]:
print("True")
x = 10
if x > 5:
pass
else:
print("x is less than or equal to 5")
A.
if not True:
print("False")
B.
if not (5 > 10):
print("True")
C.
if not x:
print("Not x")
A.
if x = 10:
print("x is 10")
B.
if x == 10:
print("x is 10")
C.
if x => 10:
print("x is greater than or equal to 10")
D.
if x <> 10:
print("x is not equal to 10")
x = 10
y = 20
if x < y and y < 30:
print("x is less than y and y is less than 30")
else:
print("Condition not met")
x = 50
if x < 20:
print("Less than 20")
elif x < 40:
print("Less than 40")
elif x < 60:
print("Less than 60")
else:
print("60 or more")
● A. Less than 20
● B. Less than 40
● C. Less than 60
● D. 60 or more
Which of the following correctly describes the use of Boolean operators in Python?
x=5
if x > 10:
print("Greater than 10")
elif x > 5:
print("Greater than 5")
else:
print("5 or less")
● A. Greater than 10
● B. Greater than 5
● C. 5 or less
● D. Error
A.
if x > 10:
print("Greater than 10")
B.
if 5 > 10:
print("True")
else:
print("False")
C.
if "string":
print("True")
A.
if x:
print("x is True")
B.
if x > 10:
print("Greater than 10")
elif x == 10:
print("Equal to 10")
else:
print("10 or less")
C.
if x = 10:
print("x is 10")
D.
if not x:
print("Not x")
x = 10
if x >= 10:
print("Greater than or equal to 10")
if x > 5:
print("Greater than 5")
else:
print("5 or less")
=====================================================================
CHAPTER 6
Which keyword is used to create a loop that repeats as long as a condition is true?
● A. for
● B. while
● C. loop
● D. repeat
i=0
while i < 3:
print(i)
i += 1
● A. 0 1 2
● B. 1 2 3
● C. 0 1 2 3
● D. 1 2
● A. 0 1 2 3 4
● B. 1 2 3 4 5
● C. 1 2 3 4
● D. 2 3 4 5
● A. while
● B. for
● C. infinite
● D. sentinel
● A. 5 4 3 2 1
● B. 5 4 3 2
● C. 4 3 2 1
● D. 5 4 3
● A. exit
● B. stop
● C. break
● D. terminate
i=0
while i < 5:
i += 1
if i == 3:
continue
print(i)
● A. 1 2 3 4 5
● B. 1 2 4 5
● C. 0 1 2 4 5
● D. 1 2 3 4
A.
while x < 10:
print(x)
x += 1
B.
for x in range(10):
print(x)
C.
while True:
if x == 10:
break
print(x)
x += 1
D.
for x in [1, 2, 3, 4, 5]:
print(x)
What is the output of the following code snippet?
● A. 1 3 5 7 9
● B. 2 4 6 8 10
● C. 1 3 5 7 9 11
● D. 0 2 4 6 8
x = 10
while x > 0:
print(x)
x -= 2
● A. 10 8 6 4 2 0
● B. 8 6 4 2 0
● C. 10 8 6 4 2
● D. 10 9 8 7 6 5 4 3 2 1
total = 0
num = 0
while num >= 0:
num = int(input("Enter a non-negative integer (negative to end series): "))
if num >= 0:
total += num
print("Sum total of the series:", total)
● A. It will prompt the user until a negative number is entered and print the sum of all
non-negative numbers entered.
● B. It will print the sum of all numbers entered including the negative number.
● C. It will run indefinitely.
● D. It will print an error because the loop never ends.
i=0
while i < 3:
print(i)
i += 1
else:
print("Done")
● A. 0 1 2
● B. 0 1 2 Done
● C. Done
● D. 1 2 3 Done
In which scenario would you use a for loop instead of a while loop?
for i in range(3):
for j in range(2):
print(i, j)
● A. 0 0 0 1 1 0 1 1 2 0 2 1
● B. 0 1 1 0 2 1 2 0 3 1
● C. 0 0 0 1 1 0 1 1 2 0 2 1 3 0 3 1
● D. 0 0 1 0 1 1 2 0 2 1
A.
i=0
while i < 5:
print(i)
i += 1
B.
for i in range(10):
print(i)
C.
i=0
while i < 5:
print(i)
D.
for i in [1, 2, 3]:
print(i)
What is the output of the following code snippet?
for i in range(2):
for j in range(2):
if i == j:
break
print(i, j)
● A. 0 0 1 1
● B. 0 1 1 0
● C. 1 0
● D. 0 1
Which statement will skip the rest of the code inside the loop for the current iteration and
continue with the next iteration?
● A. break
● B. exit
● C. continue
● D. pass
count = 0
while count < 5:
count += 1
if count == 3:
break
print(count)
● A. 1 2
● B. 1 2 3
● C. 1 2 4 5
● D. 1 2 3 4 5
● A. 2 4 6 8 10
● B. 2 4 6 8
● C. 0 2 4 6 8 10
● D. 2 3 4 5 6 7 8 9
● A. The loop body is executed at least once before the condition is tested.
● B. The loop condition is tested before the loop body is executed.
● C. The loop executes an infinite number of times.
● D. The loop condition is tested after the loop body is executed.
i = 10
while i > 0:
print(i, end=' ')
i -= 3
● A. 10 7 4 1 -2
● B. 10 8 6 4 2
● C. 10 7 4 1
● D. 10 7 4 1 -2
Which of the following loops will execute at least once, even if the condition is initially False?
● A. Pre-test loop
● B. Post-test loop
● C. Infinite loop
● D. Sentinel loop
total = 0
for i in range(1, 5):
total += i
print(total)
● A. 5
● B. 10
● C. 15
● D. 20
n=5
while n > 0:
n -= 1
print(n)
● A. 5 4 3 2 1 0
● B. 4 3 2 1 0
● C. 4 3 2 1
● D. 5 4 3 2 1
Which of the following best describes the use of the range() function in a for loop?
A.
for i in range('a', 'z'):
print(i)
B.
for i in range(10, 1, -1):
print(i)
C.
for i in 10:
print(i)
D.
for i in [10, 20, 30]:
print(i)
i=1
while i <= 3:
print(i)
i=i+1
● A. 1 2 3
● B. 0 1 2 3
● C. 2 3 4
● D. 1 2 3 4
A.
for i in range(3):
print(i)
B.
while i < 5:
print(i)
i += 1
C.
for i in range(3):
for j in range(2):
print(i, j)
D.
while i < 5:
print(i)
i=i+1
for i in range(3):
for j in range(2):
if j == 1:
break
print(i, j)
● A. 0 0 1 0 2 0
● B. 0 1 1 1 2 1
● C. 0 0 0 1 1 0 1 1 2 0 2 1
● D. 0 0 1 0 2 0
=====================================================================
CHAPTER 7
● A. func
● B. def
● C. function
● D. define
def greet():
print("Hello, World!")
greet()
● A. Hello, World!
● B. Error
● C. greet()
● D. None
● A. def myFunction():
● B. def myFunction:
● C. function myFunction()
● D. def myFunction()
result = add(3, 4)
print(result)
● A. 34
● B. 7
● C. Error
● D. 12
print(multiply(5))
● A. 10
● B. 25
● C. Error
● D. 5
● A. def
● B. return
● C. print
● D. func
def say_hello(name):
print("Hello, " + name)
say_hello("Alice")
● A. Hello,
● B. Hello, Alice
● C. Error
● D. name
func(2, c=8)
● A. 2 4 5
● B. 2 8 5
● C. 2 4 8
● D. 2 8 8
Which of the following best describes a function that does not return a value?
● A. Fruitful function
● B. Void function
● C. Built-in function
● D. User-defined function
● A. type()
● B. float()
● C. int()
● D. add()
What will be the output of the following code?
result = func(5, 3)
print(result)
● A. 53
● B. 8
● C. Error
● D. 15
def func():
return 3 + 4
print(func())
● A. 3 + 4
● B. 34
● C. 7
● D. Error
print(func(4))
● A. 4
● B. 24
● C. 9
● D. 8
result = func(2, 3)
print(result)
● A. 6
● B. 23
● C. Error
● D. 5
result = func(5, 2)
print(result)
● A. 7
● B. 5
● C. 8
● D. Error
● A. 20
● B. 14
● C. 10
● D. 24
Which of the following best describes the use of *args in a function definition?
def func(*args):
return sum(args)
print(func(1, 2, 3))
● A. 6
● B. 123
● C. Error
● D. None
Which of the following is true about the **kwargs parameter in Python functions?
def func(**kwargs):
for key, value in kwargs.items():
print(key, value)
● A. a 1 b 2 c 3
● B. 1 2 3
● C. a b c
● D. Error
Which of the following best describes the use of default parameter values in functions?
print(func(3))
● A. 12
● B. 7
● C. Error
● D. 3
Which of the following best describes the concept of "passing by reference" in Python?
● A. The actual object is passed to the function, and changes affect the original object
● B. A copy of the object is passed to the function, and changes do not affect the original
object
● C. The object's memory address is passed to the function
● D. The object cannot be changed within the function
result = func(3, 4)
print(result)
● A. 3 4
● B. 4 6
● C. 4 5
● D. 5 6
print(func(3, 4))
● A. 5
● B. 7
● C. Error
● D. None
def greet(name="World"):
return "Hello, " + name
print(greet("Alice"))
● A. Hello,
● B. Hello, World
● C. Hello, Alice
● D. Error
● A. 7
● B. 21
● C. 12
● D. 14
=====================================================================
CHAPTER 8
● A. str = 'Hello'
● B. str = "Hello"
● C. str = """Hello"""
● D. All of the above
str1 = "Hello"
str2 = 'World'
result = str1 + str2
print(result)
● A. Hello World
● B. HelloWorld
● C. Error
● D. Hello+World
Which of the following will raise a TypeError?
● A. H
● B. e
● C. o
● D. Error
● A. str.find()
● B. str.index()
● C. str.count()
● D. str.search()
str = "banana"
print(str.find('na'))
● A. 1
● B. 2
● C. 3
● D. 4
Which of the following will result in a TypeError?
● A. int('123')
● B. float('123.45')
● C. str(123)
● D. int('abc')
● A. str[-1]
● B. str[len(str)]
● C. str[len(str)-1]
● D. Both A and C
str = "Python"
print(str[:3])
● A. Pyt
● B. hon
● C. tho
● D. Error
● A. str.size()
● B. str.length()
● C. len(str)
● D. size(str)
● A. +
● B. *
● C. %
● D. //
str = "Hello"
print(str * 2)
● A. HelloHello
● B. Hello Hello
● C. Hello2
● D. Error
● A. str.upper()
● B. str.capitalize()
● C. str.toUpperCase()
● D. str.uppercase()
● A. python programming
● B. Python programming
● C. PYTHON PROGRAMMING
● D. Error
Which of the following will check if a string str contains only digits?
● A. str.isdigit()
● B. str.isnumeric()
● C. str.isdecimal()
● D. All of the above
● A. str.startswith()
● B. str.startswith(prefix)
● C. str.startsWith(prefix)
● D. str.beginWith(prefix)
str = "Hello"
str[0] = 'h'
print(str)
● A. hello
● B. hHello
● C. Error
● D. Hello
● A. str.replace()
● B. str.replace(old, new)
● C. str.replace(old, new, count)
● D. Both B and C
● A. str.swapcase()
● B. str.replace()
● C. str.join()
● D. str.translate()
● A. list(str)
● B. str.split()
● C. str.toList()
● D. listOf(str)
str = "Python"
print(str[::-1])
● A. nohtyP
● B. Python
● C. nohtyP Python
● D. Error
● A. str.isalpha()
● B. str.isalnum()
● C. str.isdigit()
● D. str.isnumeric()
str = "Python"
print(str[1:4])
● A. Pyt
● B. yth
● C. ytho
● D. ytho
Which of the following will raise an error?
● A. str = "Python"[3]
● B. str = "Python"[3:]
● C. str = "Python"[:3]
● D. str = "Python"[3:100]
str = "Python"
print(str[2:])
● A. Py
● B. Python
● C. thon
● D. Pyt
Which method would you use to check if a string contains only whitespace characters?
● A. str.isempty()
● B. str.iswhitespace()
● C. str.isspace()
● D. str.isblank()
str = "hello"
print(str.capitalize())
● A. hello
● B. Hello
● C. HELLO
● D. Error
Which method splits a string into a list where each word is a list item?
● A. str.split()
● B. str.join()
● C. str.partition()
● D. str.separate()
What will be the output of the following code?
Which method returns the lowest index in the string where substring sub is found?
● A. str.index(sub)
● B. str.find(sub)
● C. str.rindex(sub)
● D. Both A and B
str = "Hello"
print(str * 2)
● A. HelloHello
● B. Hello Hello
● C. Hello2
● D. Error
● A. str.lower()
● B. str.upper()
● C. str.title()
● D. str.capitalize()
● A. Hello World
● B. Hello World
● C. Hello World
● D. World
Which of the following methods can be used to check if a string ends with a specified suffix?
● A. str.endswith(suffix)
● B. str.end(suffix)
● C. str.endsWith(suffix)
● D. str.finishWith(suffix)
str = "Python"
print(str[3:6])
● A. Pyt
● B. hon
● C. tho
● D. yth
Which method returns a string where all occurrences of a specified value are replaced with
another specified value?
● A. str.swapcase()
● B. str.replace()
● C. str.join()
● D. str.translate()
=====================================================================
CHAPTER 9
● A. list = ()
● B. list = []
● C. list = {}
● D. list = list()
list = [1, 2, 3, 4, 5]
print(list[2])
● A. 1
● B. 2
● C. 3
● D. 4
list = [1, 2, 3, 4, 5]
list.append(6)
print(list)
● A. [1, 2, 3, 4, 5, 6]
● B. [1, 2, 3, 4, 5]
● C. [6, 1, 2, 3, 4, 5]
● D. Error
● A. list = range(1, 5)
● B. list = [1, 2, 3, 4, 5]
● C. list = list(1, 2, 3, 4, 5)
● D. list = (1, 2, 3, 4, 5)
list = [1, 2, 3, 4, 5]
list.insert(2, 'a')
print(list)
● A. [1, 2, 'a', 3, 4, 5]
● B. [1, 'a', 2, 3, 4, 5]
● C. [1, 2, 3, 4, 5, 'a']
● D. Error
list = [1, 2, 3, 4, 5]
del list[1:3]
print(list)
● A. [1, 2, 3, 4, 5]
● B. [1, 4, 5]
● C. [1, 3, 4, 5]
● D. [1, 4, 3, 5]
list = [1, 2, 3, 4, 5]
print(list[-1])
● A. 1
● B. 2
● C. 4
● D. 5
Which method would you use to add multiple elements to the end of a list?
● A. list.append([6, 7])
● B. list.insert(6, 7)
● C. list.extend([6, 7])
● D. list.add([6, 7])
list = [1, 2, 3, 4, 5]
list.extend([6, 7, 8])
print(list)
list = [1, 2, 3, 4, 5]
list.remove(3)
print(list)
● A. [1, 2, 4, 5]
● B. [1, 2, 3, 4, 5]
● C. [1, 4, 5]
● D. Error
Which method would you use to find the number of occurrences of an element in a list?
● A. list.index()
● B. list.count()
● C. list.find()
● D. list.search()
list = [1, 2, 3, 4, 5]
print(list.index(3))
● A. 2
● B. 3
● C. 4
● D. Error
list = [1, 2, 3, 4, 5]
print(len(list))
● A. 4
● B. 5
● C. 6
● D. Error
● A. list.reverse()
● B. list[::-1]
● C. list.reversed()
● D. list.sort(reverse=True)
list = [1, 2, 3, 4, 5]
list.reverse()
print(list)
● A. [5, 4, 3, 2, 1]
● B. [1, 2, 3, 4, 5]
● C. [1, 2, 3]
● D. Error
list = [3, 1, 4, 1, 5, 9]
list.sort()
print(list)
● A. [9, 5, 4, 3, 1, 1]
● B. [1, 1, 3, 4, 5, 9]
● C. [3, 1, 4, 1, 5, 9]
● D. Error
list = [1, 2, 3]
list[1:2] = [4, 5]
print(list)
● A. [1, 4, 5, 2, 3]
● B. [1, 4, 5, 3]
● C. [1, 2, 4, 5, 3]
● D. [1, 4, 5]
Which of the following methods would you use to clear all elements from a list?
● A. list.clear()
● B. list.remove()
● C. list.delete()
● D. list.empty()
list = [1, 2, 3, 4, 5]
list.clear()
print(list)
● A. [1, 2, 3, 4, 5]
● B. []
● C. [None]
● D. Error
● A. list.copy()
● B. list[:]
● C. list.copy(list)
● D. Both A and B
list = [1, 2, 3, 4, 5]
list_copy = list[:]
list_copy[0] = 99
print(list)
● A. [1, 2, 3, 4, 5]
● B. [99, 2, 3, 4, 5]
● C. [99, 2, 3, 4, 5, 1, 2, 3, 4, 5]
● D. Error
● A. list.add(index, element)
● B. list.insert(index, element)
● C. list.append(index, element)
● D. list.push(index, element)
list = [1, 2, 3, 4, 5]
list.insert(3, 'a')
print(list)
● A. [1, 2, 3, 'a', 4, 5]
● B. [1, 2, 'a', 3, 4, 5]
● C. [1, 'a', 2, 3, 4, 5]
● D. Error
list = [1, 2, 3, 4, 5]
print(list[1:4])
● A. [1, 2, 3]
● B. [2, 3, 4]
● C. [2, 3]
● D. [3, 4, 5]
● A. list1 + list2
● B. list1.append(list2)
● C. list1.insert(list2)
● D. list1.add(list2)
list1 = [1, 2, 3]
list2 = [4, 5, 6]
print(list1 + list2)
● A. [1, 2, 3, 4, 5, 6]
● B. [1, 2, 3, [4, 5, 6]]
● C. [[1, 2, 3], [4, 5, 6]]
● D. Error
● A. list.copy()
● B. list.shallow()
● C. list.clone()
● D. list.duplicate()
list = [1, 2, 3, 4, 5]
new_list = list.copy()
new_list[0] = 10
print(list)
● A. [1, 2, 3, 4, 5]
● B. [10, 2, 3, 4, 5]
● C. [10, 1, 2, 3, 4, 5]
● D. Error
Which method can be used to combine the elements of two lists into one?
● A. list.append()
● B. list.extend()
● C. list.add()
● D. list.join()
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)
● A. [1, 2, 3, 4, 5, 6]
● B. [1, 2, 3, [4, 5, 6]]
● C. [[1, 2, 3], [4, 5, 6]]
● D. Error
list = [1, 2, 3, 4, 5]
new_list = [x*2 for x in list]
print(new_list)
● A. [2, 4, 6, 8, 10]
● B. [1, 4, 9, 16, 25]
● C. [1, 2, 3, 4, 5]
● D. Error
Which of the following will correctly iterate over a list and print each element?
list = [1, 2, 3, 4, 5]
for i in range(len(list)):
list[i] += 1
print(list)
● A. [1, 2, 3, 4, 5]
● B. [2, 3, 4, 5, 6]
● C. [0, 1, 2, 3, 4]
● D. Error
=====================================================================
CHAPTER 10
● A. tuple = [1, 2, 3]
● B. tuple = {1, 2, 3}
● C. tuple = (1, 2, 3)
● D. tuple = <1, 2, 3>
What will be the output of the following code?
● A. Apple
● B. Banana
● C. Cherry
● D. Error
● A. tuple[1] = 4
● B. tuple[1:3]
● C. len(tuple)
● D. tuple.index(2)
● A. set = {}
● B. set = ()
● C. set = set()
● D. set = []
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 & set2)
● A. {1, 2, 3, 4, 5}
● B. {3}
● C. {1, 2, 4, 5}
● D. {}
set1 = {1, 2, 3}
set1.add(4)
print(set1)
● A. {1, 2, 3}
● B. {1, 2, 3, 4}
● C. {4, 1, 2, 3}
● D. Error
Which method is used to remove an element from a set without raising an error if the element is
not found?
● A. set.remove()
● B. set.discard()
● C. set.delete()
● D. set.pop()
● A. 1
● B. 2
● C. 3
● D. Error
● A. dict = []
● B. dict = {}
● C. dict = ()
● D. dict = set()
Which method would you use to get a list of all the keys in a dictionary?
● A. dict.keys()
● B. dict.values()
● C. dict.items()
● D. dict.get_keys()
● A. dict.clear()
● B. dict.delete_all()
● C. dict.remove()
● D. dict.discard()
● A. 1
● B. 2
● C. Not Found
● D. Error
● A. ('Apple', 'Banana')
● B. ('Banana', 'Cherry')
● C. ('Apple', 'Cherry')
● D. ('Banana')
● A. tuple.index()
● B. tuple.count()
● C. tuple.find()
● D. tuple.search()
● A. 0
● B. 1
● C. 2
● D. Error
Which of the following is a valid way to create a set?
● A. set = {1, 2, 3}
● B. set = [1, 2, 3]
● C. set = (1, 2, 3)
● D. set = <1, 2, 3>
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2)
● A. {1, 2, 3, 4, 5}
● B. {3}
● C. {1, 2, 4, 5}
● D. {}
● A. dict.copy()
● B. dict.shallow()
● C. dict.clone()
● D. dict.duplicate()
Which method is used to update a dictionary with key-value pairs from another dictionary?
● A. dict.update()
● B. dict.merge()
● C. dict.add()
● D. dict.extend()
● A. dict.keys()
● B. dict.values()
● C. dict.items()
● D. dict.get_values()
● A. 2
● B. 3
● C. 4
● D. 5
● A. set.add()
● B. set.update()
● C. set.append()
● D. set.insert()
set1 = {1, 2, 3}
set1.update([4, 5, 6])
print(set1)
● A. set.remove()
● B. set.discard()
● C. set.pop()
● D. set.delete()
set1 = {1, 2, 3}
set1.pop()
print(set1)
● A. {2, 3}
● B. {1, 3}
● C. {1, 2}
● D. Error
● A. dict.sort_keys()
● B. dict.sorted_keys()
● C. sorted(dict)
● D. dict.sort()
● A. 1
● B. 2
● C. 3
● D. Error
Which of the following methods returns a new view of the dictionary's items (key, value pairs)?
● A. dict.keys()
● B. dict.values()
● C. dict.items()
● D. dict.get_items()
● A. set.clear()
● B. set.remove_all()
● C. set.delete()
● D. set.discard()
=====================================================================
CHAPTER 11
Which mode is used to open a file for reading and writing, where the file is overwritten if it
already exists?
● A. r+
● B. w+
● C. a+
● D. x+
● A. Hello, World!
● B. Error
● C. None
● D. Hello,
Which of the following is NOT a valid file mode in Python?
● A. rb
● B. rw
● C. wb
● D. ab
● A. file.read()
● B. file.readlines()
● C. file.readline()
● D. file.read_all()
What is the purpose of the with statement when working with files in Python?
● A. False
● B. True
● C. None
● D. Error
Which of the following exceptions is raised when trying to open a non-existent file in read
mode?
● A. FileNotFoundError
● B. IOError
● C. EOFError
● D. OSError
try:
file = open("nonexistent.txt", "r")
except FileNotFoundError:
print("File not found.")
● A. Error
● B. File not found.
● C. None
● D. File cannot be opened.
● A. strip()
● B. lstrip()
● C. rstrip()
● D. remove()
● A. b'\x00\xFF\x00\xFF'
● B. \x00\xFF\x00\xFF
● C. 0000FFFF
● D. \x00FF\x00FF
Which file mode is used for both reading and writing without truncating the file?
● A. r
● B. w
● C. a
● D. r+
try:
file = open("data.txt", "r")
data = file.read()
finally:
file.close()
print(data)
● A. Contents of data.txt
● B. None
● C. Error
● D. File closed
Which method is used to write a list of strings to a file?
● A. file.write_lines()
● B. file.writelines()
● C. file.write_list()
● D. file.write()
● A. file.read()
● B. file.readline()
● C. file.readlines()
● D. file.read_line_by_line()
● A. FileNotFoundError
● B. IOError
● C. OSError
● D. EOFError
try:
file = open("example.txt", "r")
print(file.read())
except FileNotFoundError:
print("File not found.")
finally:
print("End of program.")
import logging
try:
print(10 / 0)
except ZeroDivisionError:
logging.error("Attempted to divide by zero.")
● A. Attempted to divide by zero.
● B. ZeroDivisionError
● C. Error
● D. No output
● A. ZeroDivisionError
● B. FileNotFoundError
● C. MemoryError
● D. InputError
try:
file = open("data.txt", "r")
data = file.read()
except FileNotFoundError:
data = "No data found."
finally:
print(data)
● A. No data found.
● B. Contents of data.txt
● C. Error
● D. None
Which method is used to append data to an existing file without truncating it?
● A. file.write()
● B. file.append()
● C. file.writelines()
● D. file.write_append()
● A. 123\n456\n789\n101112
● B. 123456789101112
● C. 123 456 789 101112
● D. Error
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input.")
else:
print("You entered:", num)
● A. Invalid input.
● B. You entered: <number>
● C. Error
● D. Both A and B
Which method is used to read the entire contents of a file into a string?
● A. file.read()
● B. file.readline()
● C. file.readlines()
● D. file.read_all()
Which statement is used to skip the current iteration of a loop and continue with the next
iteration?
● A. break
● B. pass
● C. continue
● D. skip
try:
with open("output.txt", "w") as file:
file.write("Output data")
except Exception as e:
print("An error occurred:", e)
=====================================================================