0% found this document useful (0 votes)
21 views7 pages

NCOM31203 - Introduction To Computer Programming - Mid Semester Examinations

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Knowledge Innovation Excellence

NATURAL RESOURCES COLLEGE

2024-NRC-2-MID-SEMESTER EXAMINATIONS

FACULTY OF LIFE SCIENCES AND NATURAL RESOURCES

DEPARTMENT OF LAND AND WATER RESOURCES

NCOM31203: INTRODUCTION TO COMPUTER PROGRAMMING

Date: Tuesday, 12th March, 2023 Time: 15:30 – 17:30 hrs.

INSTRUCTIONS:

1. Answer All Questions


2. Total Marks: 100
3. This paper has 3 pages and 5 questions
4. No electronic gadget other than a non-programmable scientific calculator is allowed in the
examination
Question 1:
a. Question. What is Computer programming (2 marks)
Answer: It is the process of writing sets of instructions, popularly known as code, that directs
computers to perform specific tasks or solve problems.
b. Question. Describe three types of programming languages. (6 marks)
Answer:
Machine Language: is a low-level computer language that is designed to be directly
understandable by a computer and it is the language into which all programs must be converted
before they can be run. It is entirely comprised of binary, that is 0’s and 1’s. In machine language,
all instructions, memory locations, numbers and characters are represented in 0’s and 1’s.
Assembly Language: Is a human-readable notation for the machine language. It replaces the
instructions represented by patterns of 0’s and 1’s with alphanumeric symbols also called as
mnemonics in order to make it easier to remember and work with them including reducing the
chances of making errors. Since the computer cannot understand assembly language, it uses
another program called assembler to convert the alphanumeric symbols written in assembly
language to machine language and this machine language can be directly executed on the
computer.
High Level Language: They are more like human language. Enable programmers to just focus on
the problem being solved. They are platform independent which means that the programs written
in a high-level language can be executed on different types of machines. For a computer to
understand and execute a source program written in high-level language, it must be translated into
machine language. This translation is done using either compiler or interpreter.
c. Question. What is the difference between a compiler and an interpreter (4 marks)
Answer: A compiler or an interpreter is a system software program that transforms high-level
source code written by a software developer in a high-level programming language into a low-level
machine language. Compilers translate source code all at once and the computer then executes
the machine language that the compiler produced. An interpreter is a program that reads source
code one statement at a time, translates the statement into machine language, executes the
machine language statement, then continues with the next statement.
d. Question. Explain why Python programming language is an interpreted language (8 marks)
Answer: Python requires an interpreter to be converted to machine language in order to be
executed. The interpreter converts Python code to machine language, one statement at a time and

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:

a. Question. What is a variable? (2 marks)


Answer: A variable is a named placeholder to hold any type of data which the program can use to
assign and modify during the course of execution.
b. Question. Describe the rules for creating a variable in Python. (8 marks)
Answer:
 Variable names can consist of any number of letters, underscores and digits but cannot
start with a digit. No other characters are allowed apart from these.
 Python Keywords are not allowed as variable names.
 Variable names are case-sensitive. For example, computer and Computer are different
variables.
 A variable cannot contain blank spaces
c. Question. Rewrite the following statements using compound assignment operators (10marks)
a = a + 3 -> a += 3
x = x * 9 -> a *= 9
p = p / 5 -> p /= 5
p = p ** q -> p **= q
i = i % 3 -> I %= 3

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:

# Get first number

Page 3 of 7
first_number = int(input("Enter first number: "))

# Get second number

second_number = int(input("Enter second number: "))

# Compare the numbers and print a message indicating the largest number
or if the numbers are equal

if first_number > second_number:

print("The first number is greater than the second number")

elif second_number > first_number:

print("The second number is greater than the first number")

else:

print("The two numbers are equal")

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

while count <= 10:


number = int(input("Enter a number: "))
sum += number

print(f"The sum of the numbers of {sum}")

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

# The loop condition should be count < 10 since count = 0

while count < 10:

number = int(input("Enter a number: "))

sum += number

# Increment the value of count

count += 1

print(f"The sum of the numbers is {sum}")

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

while count <= 10:

number = int(input("Enter a number: "))

sum += number

# Increment the value of count

count += 1

print(f"The sum of the numbers is {sum}")

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

# Loop through numbers 0 to 10


while num < 10:
print("Loop number {}".format(num))
# Increment num
num += 1

END OF QUESTION PAPER

Page 7 of 7

You might also like