Programming With Python (CT108-3-1-PYP) MOCK AND CLASSWORK MCQS
Programming With Python (CT108-3-1-PYP) MOCK AND CLASSWORK MCQS
Which of the following correctly defines a function named greet that takes a name as input and
prints a greeting message?
a)
greet(name):
print("Hello, " + name + "!")
b)
def greet(name):
print("Hello, " + name + "!")
c)
function greet name:
print(f"Hello, {name}!")
d)
greet = function(name):
return "Hello, " + name + "!"
6. What happens when you call a function in Python that hasn't been defined yet(2 Points)
● The code will throw an error
● Python will ignore it and continue to run the code
● The function call is ignored.
● The function call is automatically defined with default behavior.
========
7.
def square(x):
return x * x
result = square(5)
print(result)
12. which of the following components are optional when defining a function?
(2 Points)
● default argument
● return
● function name
● arguments
● def
● body
13.
def greet(name):
result = "Hello, " + name + "!"
result = greet("Bob")
print(result)
2. There are just a couple of rules to follow when naming your variables. What are those?
(1 Point)
● Variable names can contain letters, numbers, and the underscore.
● Variable names cannot contain spaces.
● Variable name cannot start with underscore
● Variable names cannot start with a number.
● Case matters—for instance, temp and Temp are different.
3. You can define a variable in python with _____ operator without declaring their type
(1 Point)
● define
● var
● =
● variable
9. You can determine the datatype of a variable in python using ____ function(2 Points)
● var()
● class()
● type()
● =
11.Which of the following Python code prompt the operator for an age?(1 Point)
● age = input("Enter age:")
● age = int(input("Enter age:"))
● var age = input("Enter age:")
● age : int = input("Enter age")
12. Convert the following design into python code
CALCULATE
Price = cost + profit(1 Point)
● Price <- cost + profit
● Price = cost + profit
● cost + profit = Price
● All of the above
13. How to print the following output in the console, given total = 12.5.
The total amount is 12.5(2 Points)
● print "The total amount is 12.5"
● print("The total amount is 12.5")
● print(The total amount is 12.5)
● print("The total amount is {total}")
● True
● False
● Error
● None
17. x = 5
y=3
z=x+y
print(z)
What is the final output of the Python code snippet above? (1 Point)
● 8
● 5
● 3
● 53
=================================================================
1. What happens when you break out of a for loop in Python?(2 Points)
● The loop continues execution.
● The loop immediately terminates.
● The current iteration finishes, then the loop stops.
● An error occurs.
2. What is the correct way to write a while loop that prints even numbers from 2 to 10
(inclusive)?
(a)
i=2
while i <= 10:
print(i)
i += 2
(b)
i=1
while i % 2 == 0 and i <= 10:
print(i)
i += 1
(c)
for i in range(2, 11):
if i % 2 == 0:
print(i)
(d) None of the above
4. Which of the following while loops will run infinitely, given x is positive?
a)
while x > 0:
x -= 1
b)
while x != 0:
x += 2
c)
while x < 10:
x *= 2
d)
while True:
x = x**2
5. Which of the following Python code snippets will print a 3x3 grid of asterisks (*), where each
row is printed on a new line?
***
***
***
a)
for i in range(3):
print("*")
b)
for i in range(3):
for j in range(3):
print("*", end="")
print()
c)
for i in range(1, 4):
for j in range(1, 4):
print("*", end=" ")
print()
d)
x=0
while x < 3:
print("*")
x += 1
for i in range(2):
for j in range(i):
print(j, end=" ")
● 01
● 012
● 0
● Errors.
7.
How many times will the following code snippet print "Hello"?
count = 0
for i in range(3):
while count < 2:
print("Hello")
count += 1
● 0
● 1
● 2
● 3
8. Which of the following statements is NOT true about nested loops in Python?(2 Points)
● The inner loop can modify variables used in the outer loop.
● The number of times the inner loop executes depends on the outer loop's current iteration.
● Nested loops can lead to infinite loops if not designed carefully.
● Nested loops are always more efficient than single loops for the same task.
for i in range(5):
if i % 2 == 0:
_______
print(i)
10. What keyword should be inserted to print only the odd numbers (1 and 3) from the range 0 to
4?
● break
● pass
● skip
● continue
11. Given the following code, how many times the program will loop?
● 1
● 3
● 5
● 6
12.
for num in range(1,10):
if num % 3 == 0:
break
print(num)
● substring = message[2:5]
● substring = message.slice(2, 5)
● substring = message.getSubstring(2, 5)
● substring = message.extract("llo")
● welcome to APU!
● Welcome To APU!
● WELCOME TO APU!
● WELCOME TO apu!
3. How can you check if a string message starts with an "apple" word?
● if "apple" in message:
● if message.startswith("apple"):
● if message == "apple":
● if message.isStartingWith("apple"):
5. How can you join two strings (msg1 and msg2) together?
● message = msg1 + msg2
● message = msg1.concat(msg2)
● message = msg1 & msg2
● message = msg1.join(msg2)
6. Which of the following methods will add an element to the end of a list called 'col'?
● col.insert(0, element)
● col.append(element)
● col.insert(end,element)
● col.add(element)
● [1, 2, 3, 4, 5]
● [1, 4, 9, 16, 25]
● 120
● An error occurs
● list.removeDuplicates()
● list = tuple(list)
● list = set(list)
● There's no built-in way to remove duplicates from lists
● element in list
● list.contains(element)
● find(element, list)
● There's no built-in way to check for element existence
10. Which of the following code snippets able to reverse the order of elements in a list given
following collection.(select more than 1)
numbers = [1,2,3,4,5]
● numbers.reverse()
● reverse = numbers[::-1]
● numbers.backwards()
● numbers = [num[n] for n in range(len(num)-1,-1,-1)]
● numbers.sort(False)
11. What is the key difference between tuples and lists?(2 Points)
● Tuples can hold different data types, while lists can only hold one type.
● Tuples are mutable (changeable), while lists are immutable (unchangeable).
● Tuples are ordered collections, while lists are unordered.
● Tuples are immutable (unchangeable), while lists are mutable (changeable).
● Both tuple and list are ordered collection
15. Which of the following code snippets will correctly print the second element ("banana") from
the fruits tuple?
● print(fruits(1))
● print(fruits[1])
● print(fruits.get(1))
● print(fruits.index(2))
16. How can you add a new key-value pair to the my_dict dictionary?
● Given my_dict = {'a':1, 'b':2, 'c':3,'d':4, 'e':5}(2 Points)
● my_dict.insert("key", value)
● my_dict["key"] = value
● my_dict.append({"key", value})
● my_dict.add("key", value)
=====================================================================
The program will saves data into a file named "abc.txt".
data = "This is the data to be written to the file."
# Open the file
with __a)__("abc.txt", "__b)_") as file:
# Write the data to the file
file.write(___c)_)
print("Data saved successfully to abc.txt")
Given the following python code, Trace the program execution and show the output of the
program.
def calculate_average(numbers):
total = 0
for number in numbers:
if number > 0:
total += number
if len(numbers) > 0:
average = total / len(numbers)
else:
average = 0
return average
numbers = [1, 2, 3, 4, 5]
result = calculate_average(numbers)
print(result)