Practice Questions Introduction to Python
Practice Questions Introduction to Python
ii. What value does the following expression evaluate to? 2.0+9*((3*12)–6)/10
a) 29 b) 29.0 c) 33.0 d) None of these
iii. Which of the following is NOT a valid variable name in Python?
iv. How many operands are there in the following arithmetic expression? 6*35+8−25
a) 4 b) 3 c) 5 d) 8
xii. What is the correct syntax for slicing a string to get the first three characters?
xix. How many binary operators are there in the following arithmetic expression? -
6+10/(23+56)
a) 2 b) 3 c) 4 d) 1
xx. Which of the following best describes the main difference between a compiler and an
interpreter?
a) A compiler translates the entire code at once, whereas an interpreter executes code line
by line.
b) A compiler processes code line by line, while an interpreter compiles the entire code at
once.
c) A compiler generates an independent executable file, while an interpreter requires
source code every time it runs.
d) Interpreted languages generally offer easier debugging compared to compiled
languages.
ii. num1 = 20
num2 = 10/4
answer = num1 % 8 // num2
print(f“The final answer is : {answer}”)
v. first_name = ‘chikondi’
print(first_name[0:5])
4. Identify the errors in the following Python code snippets and suggest how to correct them
using a table format as shown below.
NB: Do not rewrite the entire code, just fix the errors by modifying the necessary line(s).
2
5
f
a
h
r
e
n
h
e
i
t
(
c
e
l
c
i
u
s
9
/
5
)
+
A circle program
re
pie = 3.14
a
of radius = input(“Enter the Radius of the circle:”)
a
print(“The area of the circle is”, pi x radius x radius
Discount = “10”
final_price = itemPrice - discount print(“The final price
is”, final_price)
5. BMI (Body Mass Index) is a numerical value derived from a person's weight and height. It is used as a
simple screening tool to classify individuals into different weight categories, such as underweight, normal
ℎ
=
(ℎ ℎ )
Write a Python program that asks any user for their first name, last name, weight (in kg), height (in
meters) and calculate and display the user’s full name (in a proper format i.e., only first letter of first name
and last name should be displayed in upper cases regardless of how the user entered them) and their BMI
------BMI Calculator------
User : Chimwemwe Chosatha
Weight : 80 kgs
Height : 1.5 m
--------------------------
BMI : 35.55555555555556
--------------------------
1. What happens if the condition in an if statement is False?
a) The code block under if is executed.
b) The code block under else is executed.
c) The code block under if is skipped.
d) The program crashes.
2. What keyword is used to start a conditional statement in Python?
a) when b) if
c) check d) condition
3. What is the result of (5 > 3) and (2 < 1)?
a) True b) False
c) 5 d) None
4. Which statement is used to handle multiple conditions in Python?
a) if b) if-else
c) if-elif-else d) switch
5. What is the output of the following code?
x = 10
if x > 5:
print(“Greater”)
else:
print(“Smaller”)
a) Greater b) Smaller
c) 10 d) None
6. What is the output of the following code?
x=5
if x < 5:
print("Less")
elif x == 5:
print("Equal")
else:
print("Greater")
a) Less b) Equal
c) Greater d) None
7. What is a nested if statement?
a) An if statement inside another if statement.
b) An if statement with multiple elif blocks.
c) An if statement without an else block.
d) An if statement that repeats itself.
8. How is the level of nesting indicated in Python?
a) Using curly braces {}. b) Using parentheses ().
c) Using indentation. d) Using semicolons ;
10. What is the output of the following code if the user enters "hello"?
word = input('Enter your word: ')
if len(word) < 5:
print("Less than 5")
elif len(word) > 5:
print("Greater than 5")
else:
print("Equal to 5")
a) Less than 5 b) Greater than 5
c) Equal to 5 d) None
a)1 b) 0 1 2 0
c) 0 1 2 d) 2
12. What will be the output of the code:
a) x = 'abcd'
b) for i in range(len(x)):
c) print(i, end= “ ”)
a) abc b) 1 2 3 4
c) a b c d d) 0 1 2 3
13. What is the output of the following code?
a) count = 1
b) while count < 3:
c) print(count)
d) count += 1
a) 1 2 3 b) 1 2
c) 1 d) 2 3
14. Which of the following is true about the for loop in Python?
a) It is used only for numeric ranges
b) It must include a break statement
c) It is used for iterating over sequences like lists, tuples, and strings
d) It executes only once
15. What is the role of the pass statement in Python loops?
a) Terminates the loop b) Skips an iteration
c) Does nothing d) Repeats the last iteration
16. Which of the following will create an infinite loop?
a) for i in range(5): print(i) b) while True: print("Hi")
c) while i < 5: print(i); i += 1 d) for i in range(1, 5): continue
17. In a for loop like for i in range(3):, what values will i take?
a) 0, 1, 2 b) 1, 2, 3
c) 0, 1, 2, 3 d) 1, 2
18. What will be printed by the following code?
a) for i in range(4):
b) if i == 2:
c) continue
d) print(i)
a) 0 1 2 3 b) 0 1 3
c) 2 3 d) 0 1 2
a) func b) define
c) def d) function
def greet(name):
return "Hello " + name
print(greet("Sam"))
def y():
b = x()
return b // 40
a) 10 b) 100
c) 2 d) 2.5
26. Which of these will correctly pass an arbitrary number of arguments to a function?
a) 3 b) 5
c) 8 d) Error
def fruits(**kwargs):
return kwargs["apple"]
print(fruits(apple=5, orange=10))
a) multiply b) result
c) def d) 4
a) () b) {}
c) [] d) <>
a) insert() b) add()
c) append() d) extend()
33. Given numbers = [10, 20, 30, 40, 50], what does numbers[1:4] return?
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:5:2])
a) [1, 2, 3, 4] b) [1, 3, 5]
36. Which of the following can be used as a dictionary key?
a) A list of integers b) Another dictionary
c) A tuple of integers d) A set
a) pets[0][1] b)pets[1][0]
c) pets[1][1] d)pets[0][0]
38. What is the result of this dictionary operation?
a = [1, 2, 3]
b=a
b[0] = 10
print(a)
a) [1, 2, 3] b) [10, 2, 3]
c) [1, 2, 10] d) Error
1. Describe what the following code does. What input will cause it to print “Access Granted”?
username = input("Enter username: ")
password = input("Enter password: ")
i=1
while i < 5:
print(i)
i += 1
Answer:
1
2
3
4
b.
for i in range(5):
if i == 2:
continue
print(i)
Answer:
0
1
3
4
4. Given the following dictionary, what will be the output?
Answer:
def sum_list(lst):
total = 0
for num in lst:
total += num
return total
7. What will this function return, and why?
x=7
if x > 5 and not x < 10:
print("Condition met")
else:
print("Condition not met")
a) if...elseif...else b) if...elif...else
c) if...else if...else d) if...then...else
age = 20
country = "Malawi"
if age > 18 or country != "Zambia":
print("Allowed")
else:
print("Denied")
a) Allowed b) Denied
c) Error d) None
a) 3 b) 4
c) 5 d) 6
a) 0 1 1 2 b) 0 1 2 3
c) 0 1 1 2 d) 0 1 1 2
a) pop() b) remove()
c) del d) All of the above
a) [1, 2, 3, 4] b) [0, 2, 4]
c) [2, 4] d) [1, 3]
a) list b) dictionary
c) tuple d) set
a = [1, 2, 3]
b = a[:]
b[0] = 10
print(a)
a) [1, 2, 3] b) [10, 2, 3]
c) [1, 10, 3] d) Error
a) 1 b) 2
c) 3 d) 4
items = {1, 2, 3, 2, 1}
print(len(items))
a) 5 b) 4
c) 3 d) 2
x=5
if x > 3:
print("A")
elif x > 2:
print("B")
else:
print("C")
a) A b) B
c) C d) A B
a) [1, 2, 3, 4, 5, 6, 7, 8, 9] b) [1, 4, 7]
c) [1, 5, 9] d) [1, 3, 6, 9]
23. Choose the correct way to create a dictionary from two lists:
a) Go Go b) GoGo
c) Go2 d) Error
nums = [1, 2, 3, 4]
squares = [n**2 for n in nums if n % 2 == 0]
print(squares)
x=7
if x % 2 == 0:
print("Even")
else:
print("Odd")
a) 7 b) Even
c) Odd d) Error
27. Which of the following is a valid Python list comprehension to get squares of even numbers from 0 to 10?
x = [1, 2, 3]
x.append([4,5])
print(x)
a) Set b) Tuple
c) Dictionary d) List
count = 0
while count < 5:
if count == 3:
break
print(count)
count += 1
a) 0 1 2 3 4 b) 0 1 2
c) 1 2 3 d) 0 1 2 3
nums = [2, 4, 6]
print(nums[::-1])
a) [2, 4, 6] b) [6, 4, 2]
c) Error d) [2, 6, 4]
a) *** b) * ** ***
c) *\n**\n*** d) Error
39. Which of the following methods adds all elements from one list to another?
a) list1.add(list2) b) list1.append(list2)
c) list1.extend(list2) d) list1.merge(list2)
for i in range(3):
for j in range(2):
print(i + j, end=" ")
a) 0 1 1 2 2 3 b) 0 1 2 3 4 5
c) 1 2 3 4 5 6 d) 0 1 1 2 2 3
8. Write a Python function called, is_even(num), that returns True if the number is even, else returns False.
Then write a loop that prints whether each number from 1 to 10 is even or not.
10. Write a Python program that accepts a string and prints the number of vowels in the string.
1.A microsoft word document has a file name extension as .doc, .txt or .docx.
What is the file name extension for a Python file?
2.Study the arithmetic expression below and answer the questions that follow.
9*35+10−8
What is the result to the expression above?
How many operands and how many arithmetic operators does the expression have?
3. How many binary operators are there in the following arithmetic expression?
-7+57/(49+101)
(i)
class_president = Blessings’
print(class_president [0:5])
(ii)
class_president =‘Blessings’
print(class_president [0::2])
(iii.) class_president =
‘Blessings’
print(class_president [::-1])
(iv) print('learning'
'python')