PRG620S - PROGRAMMING 2 - 1ST OPP - NOV 2022


PRG620S - PROGRAMMING 2 - 1ST OPP - NOV 2022



1 Page 1

▲back to top


nAmlBIA UnlVERSITY
OF SCIEn CE Ano TECHn OLOGY
FACULTY OF COMPUTING AND INFORMATICS
DEPARTMENT OF COMPUTER SCIENCE
QUALIFICATION: BACHELOR OF COMPUTER SCIENCE, BACHELOR OF COMPUTER SCIENCE
IN CYBERSECURITY AND BACHELOR OF INFORMATICS
QUALIFICATION CODE: 07BACS, 07BCCS, 07BAIF
LEVEL: 5
COURSE: PROGRAMMING 1
COURSE CODE: PRG510S
DATE: NOVEMBER 2022
PAPER: THEORY
DURATION: 2 HOURS
MARKS: 100
FIRST OPPORTUNITY EXAMINATION QUESTION PAPER
EXAMINER
MODERATOR:
DR AMBROSE AZETA
MR HERMAN KANDJIMI
MS NDINELAGO NASHANDI
MR ELIAZER MBAEVA
MR SIMON MUCHINENYIKA
INSTRUCTIONS
1. Answer ALL the questions.
2. Read all the questions carefully before answering.
3. Number the answers clearly.
THIS QUESTION PAPER CONSISTS OF 4 PAGES
(Excluding this front page)
PERMISSIBLE MATERIALS
1. NON-PRGRAMMABLE CALCULATOR
1

2 Page 2

▲back to top


3 Page 3

▲back to top


SECTION A: TRUE OR FALSE
This section consist of 20 questions. Answer all the questions
Each correct answer is allocated 2 Marks
Write True or False for Questions 1 to 20.
1. All while loops repeat the associated body less than once.
[2 Marks]
2. Package statement is not allowed on VPL elearning platform
[2 Marks]
3. A sequence of statements to a problem when written in a language that can easily be
understood by people and not by computers is called a program
[2 Marks]
4. All computers only understand one language known as machine language
[2 Marks]
5. Line I:// This is a sample program
Line 2: /* This is a plagiarism comments that that requires you
to insert them on top of VPL */
Line 2 above is an inline comments and Line 1 above is a multiline comment. [2 Marks]
6. The following are all valid identifier names: First_name, mySchool, package
7. The Java statement: Cdev = NI + N2 IK is not the same as cdev = nl + n2/k
[2 Marks]
[2 Marks]
8. The output of running: int C=4, D=7; D--; System.out.println(C*D); is 24
[2 Marks]
9. The output of 5+7*8 using precedence operator rule is equal to: 96
[2 Marks]
10. The output of (8+3)-27/3 using precedence operator rule is equal to: 2
[2 Marks]
11. Injava the statement sum++ is the same as sum=sum-1
[2 Marks]
12. Exception is an indication ofreading and writing to a file
[2 Marks]
13. Division by Oor index out of array range throws an exception handling
[2 Marks]
14. Sorting is the process of arranging a group of items into a defined order based on a particular
criteria
[2 Marks]
15. int, char and double are all examples of primitive data types in Java
[2 Marks]
16. In Java, the identifiers student, Student and sTudent are all the same.
[2 Marks]
17. Every source file must be named the same as the class declared in the file
[2 Marks]
18. Once an array is declared, the size cannot be changed.
[2 Marks]
19. An Array in the Java programming language has the ability to store only the same types of
values
[2 Marks]
20. For the expression (y>=z && a=b) to be true, both of (y>=z) and (a==b) must be true
[2 Marks]
SECTION B: FILL THE GAP
Examine the following cases, then for each case decide the correct data type to be used
and create a valid declaration and assignment.
21. The number of pages in a book:
[2 Marks]
22. Your name:
[2 Marks]
23. Whether or not a car is moving:
[2 Marks]
24. The amount of money in your bank:
[2 Marks]
25. The symbol you obtained in a certain subject ie 'A', or 'B', etc: [2 Marks]
26. The day you were born:
[2 Marks]
2

4 Page 4

▲back to top


5 Page 5

▲back to top


-I
SECTION C: CODE SNIPPET
This section consist of 3 questions. Answer ALL the questions
Each correct answer is allocated 8 Marks
Question One
[8 Marks]
The Java program below (Grade.java) use IF Statement to determine the grade based
on mark obtained by student. Score is read from the keyboard and grade is calculated as follows:
Mark
Grade
75-100
Distinction
70-74
Merit
60-69
Credit
50-59
Pass
0-49
Fail
//Grade.java
1. import java.util.Scanner;
2. public class Lab04_A {
3. public static void main(String[] args) {
4. int mark;
5. char grade;
6. Scanner console= new Scanner(System.in);
7. System.out.print("Enter your numeric exam mark: ");
8. mark= console.nextlnt()
9. if (mark>= 75) { grade= "Distinction"; }
10. else if (mark >= 70) { grade = "Merit"; }
11. else if (mark>= 60) { grade= "Cedit"; }
12. else if (mark>= 50) {grade= "Pass";}
13. else { grade = "Fail"; }
14. Syste.println("Your grade is +grade);}
15. }
In the Java program above (Grade.java), identify which lines (numbers on the left)
have errors and describe the error.
Question Two
Giving the program (examplel.java) below,
1. Explain what the Java program is doing.
2. Give the output of running the program.
[8 Marks]
//exampleC.java
public class example 1 {
private static Scanner sc;
public static void main(String[] args)
{
int number, i;
sc = new Scanner(System.in);
for(i = 0; i <= 20; i = i + 2) {
System.out.print(i +"\\t");
}
}
}
3

6 Page 6

▲back to top


7 Page 7

▲back to top


Question Three
[8 Marks]
Rewrite the following peace of code using a for loop
Public static void Display (String [] names, int [] marks) {
int index= 0;
while (index< names.length ) {
System.out. println(names[index ]+"\\tMark: "+marks[index ]);
Index+= l;
}
}
SECTION D: CODES/fHEORY
Answer all questions
Each correct answer is allocated 8 Marks
Question One
[8 Marks]
Using arrays write ajava program that uses a semester mark to determine if a student
qualifies to write examination or not.
Giving that: semester mark= test] + test2
If semester mark is less than 50% the program should say 'No, Not Qualified'
If semester mark equal or greater than 50% the program should say 'Yes, Qualified'
Get the input for test! and test2 from the keyboard. Use arrays in your solution.
Name of
test!
test2
Semester
student
Mark
John
25
33
58
Harry
19
14
33
Kane
20
22
42
Assume there are three students in the class-John, Hany and Kane.
Also assume there are two Assessments: test] and test2.
The program should display this Output:
Student
Semester mark
Qualities for Exam?
John
Harry
Kane
58
Yes, Qualified
33
No, Not Qualified
42
No, Not Qualified
Question Two
[8 Marks]
Write a Java program that will ask for two integer numbers as input and print out the Addition,
Subtraction, Multiplication and Division separated by space.
[Hint: Addition= A+B, Subtraction= A-B, Multiplication= A* B, Division= A/B]
Question Three
[8 Marks]
Write a Java program to sum the Cubes of numbers between two numbers startNo and
endNo. You may use either for statement or any other looping statement.
Two numbers (startNo and endNo) should be accepted as input from the keyboard.
(a) If startNo is less than 0 then the program should print "startNo cannot be less than 0" and
the program will end.
(b) If endNo is greater than 9 then the program should print "endNo cannot be greater than 9"
and the program will end.
(c) But, if the right numbers (startNo and endNo) are entered ie any two numbers within 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, then the sum of Cube of numbers between startNo and endNo will be calculated and
the program will printout "Sum of Cube of Numbers=" sumCube.
[Hint: All numbers from 1 to 3 are: 1,2,3 therefore SumCube =
(1 *1 *1)+(2*2*2)+(3*3*3)=36]
THE END --- --- ---
4

8 Page 8

▲back to top


,.~
~:
f-·:-,.,.,,,·
'TIIBIR
~• ; .·J\\:RSITY
'; :. ."~.!iCI!AnO
:q_OGY
y•it•.:.•P~:'.'
V:'·.;.,,, ..
:f1&INFOR'~i\\i
·1. -,;,.J,ffR SCIENCl..