NCOM31203 - Introduction To Computer Programming - Mid Semester Examinations
NCOM31203 - Introduction To Computer Programming - Mid Semester Examinations
NCOM31203 - Introduction To Computer Programming - Mid Semester Examinations
2024-NRC-2-MID-SEMESTER EXAMINATIONS
INSTRUCTIONS:
Page 2 of 7
then executes the machine code. Python has to be interpreted every time before it is executed. For
this reason, Python fits in the definition of an interpreted language.
Question 2:
Questions 3:
a. Question. Using if … elif else statement, write a program using Python programming language
that accepts 2 numbers from a user. If the first number is greater than the second number, the
program should print: “The first number is greater than the second number”. If the second
number is greater than the first number, the program should print: “The second number is
greater than the first number”. If both numbers are equal, the program should print: “The two
numbers are equal” (20
marks)
Answer:
Page 3 of 7
first_number = int(input("Enter first number: "))
# Compare the numbers and print a message indicating the largest number
or if the numbers are equal
else:
Question 4:
During a programming class, a lecturer asked students to use a while loop to write a program that
accepts 10 integer numbers and prints the sum of the numbers. One of the students wrote a
program as shown below but it is not providing the expected result.
sum = 0
count = 0
a. Question. Explain the reason the program is not giving a correct result (10 marks)
Answer:
Page 4 of 7
i. The code will loop infinitely because the value of count remains 0 (count = 0). This will make
the condition “while count <= 10” to always be true. Count needs to be incremented for the
loop to exit.
ii. count should be initialized to 1 (count = 1) if the conditional expression “while count <= 10”
is used or the conditional expression should be changed to “while count < 10” if count
remain initialized to 0 (count = 0). With count = 1 and the conditional expression as “while
count <= 10”, the program will accept 11 numbers.
b. Question. Rewrite the program below to give a correct result. (10 marks)
Version 1:
# Initialize sum to 0
sum = 0
# Initialize count to 0
count = 0
sum += number
count += 1
Version 2:
# Initialize sum to 0
sum = 0
# Initialize count to 1
count = 1
Page 5 of 7
# The loop condition should be count <= 10 since count = 1
sum += number
count += 1
Question 5:
a. Question. Using a for loop statement, write a program that prints a multiplication table of 2 and
produces the output below. (10 marks)
1x2=2
2x2=4
3x2=6
4x2=8
5 x 2 = 10
6 x 2 = 12
7 x 2 = 14
8 x 2 = 16
9 x 2 = 18
10 x 2 = 20
11 x 2 = 22
12 x 2 = 24
Answer:
# Since we are starting from 1 x 2 = 2, our range start should be 1
# Since we are ending with 12 x 2 = 24, our range end/stop should be 13
for i in range(1, 13):
print(f"{i} x 2 = {i * 2}")
b. Question. Rewrite the program below using a while loop (10 marks)
Page 6 of 7
for num in range(10):
print("Loop number {}".format(num))
Answer:
# Initialize num to 0
num = 0
Page 7 of 7