PRG510S 1ST OPP EXAMINATIONS JUNE 2018.


PRG510S 1ST OPP EXAMINATIONS JUNE 2018.



1 Page 1

▲back to top


ca}
ye
Ba}
:t
rt
eh
tea &
NAMIBIA UNIVERSITY
OF SCIENCE AND TECHNOLOGY
FACULTY OF COMPUTING AND INFORMATICS
DEPARTMENT OF COMPUTER SCIENCE
QUALIFICATION: Bachelor of Computer Science , Bachelor of Computer Science in Cyber Security,
Bachelor of Informatics
QUALIFICATION CODE: 07BACS, 07BCCS, 07BAIF
LEVEL: 5
COURSE: Programming 1
DATE: June 2018
COURSE CODE: PRG510S
PAPER: THEORY
DURATION: 2 Hours
MARKS: 100
EXAMINER
MODERATOR:
FIRST OPPORTUNITY EXAMINATION
MR HERMAN KANDJIMI
MR SIMON H. MUCHINENYIKA
MS NDINELAGO NASHANDI
MR LAMECK AMUGONGO
MR JEREMIAH LUMBASI
MR EDMORE CHIKOHORA
MR MUNYARADZI MARAVANYIKA
MR COLIN STANLEY
THIS EXAMINATION PAPER CONSISTS OF 6 PAGES
(INCLUDING THIS FRONT PAGE)
Answer all questions.
INSTRUCTIONS
Total marks per question aregiven in [].
Read and understand the question carefully before attempting to answer
When writing take the following into account: The style should inform than impress, it should
be formal, in third person, paragraphs set out according to ideas or issues and the paragraphs
flowing in a logical order. Information provided should be brief and accurate.
Please, ensure that your writing is legible, neat and presentable.
PERMISSIBLE MATERIALS
Calculator.

2 Page 2

▲back to top


1.
Fill-out the Table below.
Data type
short
char
Value
25
[10]
Declarations and assignment
short smallNumber = 25;
true
“My name is Bill”
7789.8
float num = 36.2f;
Indicate whether the following statements are TRUE or FALSE
[10]
A) Strings, char and double areall examples of primitive data types in Java.
B) In Java, the identifiers student, Student, and sTudentareall different.
C) Every source file must be named the same as the class declaredin the file
D) After an Array is declared the size can easily be changed.
E) The == operator can be used to compare twoString objects.
F) One array in the Java programming language has the ability to store many different types
of values.
G) For the expression (y >= || a == b) to be true, at least one of (y >= z) or (a == b) must be
true.
H) Consider the statement examAdmission = (score >= 50) ? "Allowed" : "Denied"; then
the value of examAdmission is Denied, if score = 49
1) The statementif(!allowed) will evaluate to false when allowed = true
J) The Java + operator is used for both string concatenation and addition.
What is the difference between the following:
A) Compiler and assembler
[3]
B) Assembly language and Machine language
[3]
) Primitive and Non-Primitive data types
[3]
Wh at is the output of the following program and showall your workings?
[6]
public class Exam_Q4
a{
public static void main(String[] args)
Ej
{
System.out.println (mystery (5));
}
public static int mystery(int n)
{
if (n <= 1)
return _;
else
return ( mystery(n - _) +n);
}
2|Page

3 Page 3

▲back to top


A) Rewrite the following piece of code using a do-while loop.
[5]
@public static void Display (String[] studentNames) {
for(int i = O;1i < studentNames.length;i++) {
System.out.printin(studentNames[i]);
Li
B) Rewrite the following code snippet using a switch statement.
[5]
if(rating == 'E') //Excellent
System.out.printin("You must see this movie!”);
else if(rating == 'A’) //Average
System.out.printin("This movie is OK, but not great.");
else if(rating == 'B') //Bad
System.out.printin("Skip it! ");
else
System.out.printlin ("Something is wrong.");
Create a method that takes in three arrays, one for student names, one for test marks and a
last one for assignment mark. The method should then calculate the students qualifying mark
(a student needs 50 or moreto qualify) using the following weights: 40% of the test and 60%
of the assignment,finally print out whether the person qualified or not.
[10]
Use the below format for you print out:
Name
Test
Assignment Final Examination
Lovisa
59
85
75 Allowed
John
52
45
48 Denied
3|Page

4 Page 4

▲back to top


Examine the code snippet below and answer the questionsthat follow:
4 public static void main(String[] args) {
5
int numl,num2,sum = 0; //variable declartion
6
Scanner kbdInput = new Scanner(System.in);
a
8
System.out.printlin("Input two whole numbers: ");
9
numl = kbdInput.nextiInt();
0
num2 = kbdInput.nextInt();
2
int start = (numl < num2) ? numl:num2;
3
int end = (numl > num2) ? numl:num2;
4
gee
while (start <= end ){
6
if(start != end)
7
System.out.print (start+" + ");
8
else
9
System.out.print(startt+" = ");
Q
//calculating the sum
1
sum += start;
2
startt+t;
3
}
4
System.out.println (sum) ;
56
//Add code for average below
7 1)
A) What doesline 8 do, and howisthis called in Programming?
[2]
B) Explain what happensline 9?
[2]
C) Whatdoesline 12 do, and howis this knownin Java?
[2]
D) By making use of 9 and 5 asuser input, briefly explain what is the purpose of this whole
code snippet?
[3]
E) Line 25 provides a commentthat requires you to add a new pieceof code for calculating
the average, provide this requested code.[Hint: the quantity of the numbers is the
difference between the num1 and num2, using Math.abs() ]
[3]
—4|Page

5 Page 5

▲back to top


8.
Examine the code snippet below,identify line with errors and correct them. Finally indicate
the output of the code snippet.
5
char symbol = 'B';
6
//array to store wages for 3 employees
7
double wages[] = new double[ ];
8
String employeeNames[] = {"Anna","“George","Van Wyk");
g
String employeeName = ‘Trump’;
10
employeeNames[!] = employeeName;
il
String workPlace = "Welcome to Wakanda : Software Engineers";
12
workPlace = workPlace.replaceFirst(":", "-");
13
workPlace = workPlace.substring(._);
14
int hours[] = {45,25,1°}
15
double rate Per Hour = 105;
1g
186
19
20
ea
223+
System. out.println(workPlace.toUpperCase ()) ;
for(int 1 =);1 < wages. length; it+) {
wages[i] = (++hours[i]) * ratePerHour;
System. out.printin(employeeNames[i]+” Salary Ns "
+ String.format("%.2f", wages[i]) );
}
A) Line with errors and Solutions:
[4]
B) Code output:
[4]
5|Page

6 Page 6

▲back to top


Given an employee’ssalary from user input, if the salary is even then increment thesalary by
5% otherwise increase by it by 4%. Next the salary has to be deducted a tax, according to the
following table:
Salary Range
Percentof Tax
Salary >= 12000
12.5%
8000 <= Salary < 12000
10%
5000 <= Salary < 8000
8%
2000 <= Salary < 5000
5%
Salary < 2000
1%
The system should then print out the current employee’s Salary and the percentof tax paid.
A) Write a Pseudocode to solve the above problem
[5]
B) Create a flowchart for the above pseudocode.
[8]
C) With the help of both your Pseudocode and Flowchart, create a Java program that solves
the program as per the given problem description
[12]
[END]
6|Page