PRG510S - PROGRAMMING 1 - 1ST OPP - NOV 2025


PRG510S - PROGRAMMING 1 - 1ST OPP - NOV 2025



1 Page 1

▲back to top


nAmlBIA unlVERSITY
OF SCIEnCE AnD TECHnOLOGY
FACULTY OF COMPUTING AND INFORMATICS
DEPARTMENT OF SOFTWARE ENGINEERING
QUALIFICATION: BACHELOR OF COMPUTER SCIENCE, BACHELOR OF COMPUTER SCIENCE IN
CYBER SECURITY AND BACHELOR OF INFORMATICS
QUALIFICATION CODE: 07BCMS, 07BCCY, 07BAIT
LEVEL: 5
COURSE : PROGRAMMING 1
COURSE CODE: PRG510S
DATE: OCTOBER 2025
PAPER: THEORY
DURATION: 3 HOURS
MARKS: 75
EXAMINER(S)
FIRST OPPORTUNITY EXAMINATION QUESTION PAPER
MR MIKE KALE
MODERATOR:
MS NDINELAGO NASHANDI
INSTRUCTIONS TO STUDENTS
1. Answer ALL the questions.
2. Read all the questions carefully before answering.
3. Number the answers clearly.
THIS QUESTION PAPER CONSISTS OF 10 PAGES
(Including this front page)
Page 1 of 10

2 Page 2

▲back to top


SECTION A: Multiple choices and True and false Questions
[10 Marks]
The section consists of10 questions. Answer ALL the questions.
1. Which of the following is a valid variable declaration in Java? [1 Mark]
A. int lnumber;
B. float rate = 3.Sf;
C. char name= "John";
D. boolean = true;
2. Which keyword is used to prevent modification of a variable in Java? (1 Mark]
A. static
B. const
C. final
D. protected
3. What is the return type of a method that returns no value? [1 Mark]
A. null
B. void
C. int
D. empty
Page 2 of 10

3 Page 3

▲back to top


4. How many times will the following loop execute? [1 Mark)
inti = 2;
while (i < 5) {
i = i - 1;
System.out.print(i);
i++;
}
A. 4
B. 5
C. 6
D. Infinite
5. What will be the result of compiling the following code? [1 Mark)
int x;
System.out.println(x);
A. Prints 0
B. Compilation error
C. Runtime exception
D. Prints a random value
6. You can write an if statement in Java without using curly braces{} if it has only one
statement in the block. [1 Mark]
A. True
B. False
Page 3 of 10

4 Page 4

▲back to top


7. A method in Java can return an array. [1 Mark]
A. True
B. False
8. A variable declared inside a for loop can be accessed outside the for loop body.
[1 Mark]
A. True
B. False
9. In Java, you can declare multiple variables of different data types in a single
declaration line. [1 Mark]
A. True
B. False
10. The switch statement in Java can be used with String values. [1 Mark]
A. True
8. False
Page 4 of 10

5 Page 5

▲back to top


SECTION B: BASIC QUESTIONS
[11]
This section consists of5 questions. Answer ALL the questions.
When is answering the questions in this section, it is not necessary to include the
class and main method.
Question One [2 Marks]
Fill in the missing type to cast (type cast) the valued to an integer. Re-write the correct line
2 only.
1. doubled = 10.75;
2. inti= _ _ _ d;
3. System.out.println(i);
Question Two [2 Marks]
Declare two variables called first name and last name. Assign the value James to first name
and assign the value Smith to last name. Create a new variable called full name that stores
the full name by concatenating/ combining the first name and last name.
Question Three [2 Marks]
Declare variable called x and assign it the value 10. Also declare a variable called y, and
assign ya calculated square of x, then print the value of y to the console. Do not use any
java built-in/pre-defined methods.
Question Four [3 Marks]
What is the purpose of the continue keyword in Java? How does it affect loop execution?
Give an example that prints the numbers 1 to 5, excluding the number 2. You do not need to
create a class and the main method.
Question Five (2 Marks]
Why is a base case necessary in recursion? What happens if it is missing?
Page 5 of 10

6 Page 6

▲back to top


SECTION C: DEBUGGING / CODE TRACING QUESTIONS
[12]
This section consists of 2 questions. Answer ALL the questions.
Identify and correct any errors in the given Java code below. identify which lines (numbers
on the left) have errors, describe the error and suggest how you will fix it. Ignore the
absence of the main class or any surrounding structure. Just focus on correcting the code
that is shown.
Question One [10 Marks]
1. int numberl = 10;
2. int number2 = 20.5;
3. double result;
4.
5. if (numberl > number2)
6. System.out.println("Numberl is greater");
7. else
8. system.out.println("Number2 is greater");
9.
10. result= numberl / number2
11.
12. if (result> 0.5) {
13. System.out.println("Result is greater than 0.5"};
14. } else (result<= 0.5)
15. System.out.println("Result is 0.5 or less");
16.
17. System.out.println("The value of total is:"+ total);
18.
19. for(int i = O; i =< 5; i++) {
20. System.out.println("i = " + i)
21.}
22.
23. boolean flag= "true";
24. if(flag = false) {
25. System.out.println("Flag is true");
26.} else
27. System.out.println("Flag is false");
28.}
Page 6 of 10

7 Page 7

▲back to top


Question Two [2 Marks]
What will be the output of the following code? Ignore the absence of the main class or any
surrounding structure. Just focus on snippet of code that is shown.
int[] data = {3, 1, 4, 1, 5};
int sum= O;
for (inti= O; i < 5; i++) {
sum+= data[data.length - 1- i];
}
System.out.println(sum);
Page 7 of 10

8 Page 8

▲back to top


SECTION D: CODING
[42]
This section consists of 4 questions. Answer ALL the questions.
Question One (4 Marks]
Given the two-dimensional array below called nums,
nums =
Values to be
15
5
8
3
---13
printed
i..--
5
4
2
10
12
2
9
1
45
10
Write a Java code using nested loops to print the values 4, 2, 10 from the second row of the
array as shown above. You should access only the relevant elements in t he loop and display
them to the console, in a single line, separated by spaces.
NOTE: Do not write the full Java program, write only the nested loop code that performs the
printing.
Sample Run
4 2 10
Page 8 of 10

9 Page 9

▲back to top


Question Two [5 Marks]
You are tasked with developing a quiz system for children. The system should print the
numbers from 1 to 150. For each number in this range, you need to check certain
conditions. If the number is a multiple of 3, print "M3", if it is a multiple of 5, print "MS", if it
is a multiple of both 3 and 5, print "M3-M5", and if none of these conditions are met, simply
print the number itself. Write a complete Java program that accomplishes this behaviour.
Use loops for your solution.
NOTE: YOU MUST NOT GET ANY INPUT FROM THE USER.
Question Three [10 Marks]
Write a Java program that prompts the user to enter 10 odd numbers. If the user enters a
number that is not odd, the program should display the message "Only odd numbers must
be entered.". The numbers that are not odd are not counted toward the 10 required odd
numbers. Additionally, the program keeps track of how many invalid (non-odd) numbers the
user attempted to enter. The program continues to prompt the user until exactly 10 valid
odd numbers have been entered. The program should then display the total number of the
numbers that are not odd (non-odd numbers) that the user attempted to enter. Use loops
for your solution.
NOTE: DO NOT USE ARRAYS
Sample Run
Enter a number: 5
Enter a number: 3
Enter a number: 2
Only odd numbers must be entered.
Enter a number: 7
Enter a number: 9
Enter a number: 4
Only odd numbers must be entered.
Enter a number: 13
Enter a number: 1
Enter a number: 21
Enter a number: 15
Enter a number: 23
Enter a number: 11
Number of invalid (non-odd) inputs: 2
Page 9 of 10

10 Page 10

▲back to top


Question Four (23 Marks]
You are tasked with developing a simple system for a lecturer to enter the names and marks
of N students. N here is the number of students.
Write a complete Java program that:
1. Prompts the user to enter the number of students.
2. For each student, asks for their name and mark.
3. Store the student names in an array called names and store their corresponding
marks in an array called marks.
YOU MUST USE LOOPS FOR YOUR SOLUTION.
NOTE: The number of times the loop repeats is determined by the numbers of students.
Also create a method called calculateAverage() that accepts the names array, marks array
and the number of students as parameters.
The calculateAverage() method should do the following:
1. Calculate the average mark of all students.
2. Print the average mark to the console.
3. Print the names and corresponding marks of students who scored above the average
mark to the console.
YOU MUST USE LOOPS FOR YOUR SOLUTION.
NOTE: The logic to calculate and print the average mark and print the names and marks of
the above average students must be implemented in a separate method called
calculateAverage().
******************************* END OF EXAM **********************************
Page 10 of 10