PRG620S - PROGRAMMING 2 - 2ND OPP SUPL - JAN 2023


PRG620S - PROGRAMMING 2 - 2ND OPP SUPL - JAN 2023



1 Page 1

▲back to top


nAm I BI A un,vERS ITY
OF SCIEn CE Ano TECHn OLOGY
FACULTY OF COMPUTING AND INFORMATICS
DEPARTMENTOF COMPUTERSCIENCE
QUALIFICATION: BACHELOROF COMPUTERSCIENCE,BACHELOROF COMPUTERSCIENCE
IN CYBERSECURITYAND BACHELOROF INFORMATICS
QUALIFICATION CODE: 07BACS,07BCCS,07BAIF
LEVEL: 5
COURSE: PROGRAMMING 1
COURSE CODE: PRG510S
DATE: JANUARY2023
PAPER: THEORY
DURATION: 2 HOURS
MARKS: 100
SECOND OPPORTUNITY/SUPPLEMENTARY 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-PROGRAMMABLECALCULATOR
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.
I. A number that has a decimal point such as 8. IO is normally declared as Int.
2. Every Java program requires at least one class
3. In VPL elearning, Package statement is Compulsory
4. Once an Array is declared, the size cannot be changed.
5. An Array can store only values of the same type
6. Java program processing always starts main() method
7. In Java, the identifiers main, Main, and mAin are the same
8. All computers only understand one language known as machine language
9. These are valid identifier names: First_name, mySchool, package
I 0. The statement: K = Li/NJ * N2 is the same ask= Ll/nl *n2
I I. The output of running: int a= I 0, b=8; b--; System.out.println(a*b ); is 80
12. Assembler translates a program written in Assembly language to machine language.
I 3. The output of (8*2)/4-1 using precedence operator rule is: 3
I4. In java the statement count-- is the same as count=count+ 1
15. Every Java source file must bear the same name with the package and the
same name with the public class
16. Division by Oor out ofrange array is an example of exception handling
I 7. The result of running this Java code snippet: int a= 5; a +=2; is 3
I 8. < - > is an operator in Java
I 9. In Java, the identifiers student, Student and sTudent are all the same.
20. Sumsq is a Java keyword
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
[2 Marks]
SECTION B: COPY AND COMPLETE THE TABLE BELOW
Question
Number
1
2
3
4
5
Data Type
6
Value
7789.80
2022
True
25
"This class
is dope!!"
0.02
Declarations and
assignment
Marks
f2 Marksl
f2 Marksl
f2 Marksl
f2 Marksl
[2 Marks]
f2 Marksl
2

4 Page 4

▲back to top


5 Page 5

▲back to top


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 (Simplelnt.java) calculates simple interest using the formula:
Sinterest=(p*r*t)/100, where sinterest is Simple Interest, p is Principal, r is Rate and t is Time.
p, r, t are read as input from the keyboard.
//Sim plelnt.java
I. import java.util.Scanner;
2. public class Lab03 _ D
3. {
4. public static void main(String args[])
5. {
6. int p, r, t, sinterest;
7. Scanner scan= new Scanner(System.in);
8. System.out.print("Enter the Principal : ")
9. p = scan.nextFloat();
10. System.out.print("Enter the Rate of interest : ");
11. r = scan.nextFloat();
12. System.print("Enter the Time period:);
13. t = scan.nextFloat();
14. scan.close();
15. sinterest = (p * r * t) / 100;
16. System.out.print("Simple Interest is: " + sinterest);
17. }
18. }
In the Java program above (Simplelnt.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]
//examplel.java
public class examplel {
private static Scanner sc;
public static void main(String[] args)
{
int number, i;
sc = new Scanner(System.in);
for(i = 1; i <= 11; i = i + 2)
System.out.print(i +"\\t");
}
}
}
3

6 Page 6

▲back to top


7 Page 7

▲back to top


Question Three
Rewrite the following peace of code using a for loop.
Public static void display (int [] ages) {
int index= O;
do {
System.out. println( ages[index ]);
index++;
} While (index< ages.lenght);
}
[8 Marks]
SECTION D: CODING/THEORY
Answer ALL questions
Each correct answer is allocated 8 Marks
Question o
One
[8 Marks]
Write a Java program that computes the areas of 3 circles, giving that:
Area= PR2 where P = 3.14, R for Radius.
For each circle, the program should accept a number as input from the keyboard as Radius.
The program should display this as Sample Output:
Please Enter Radius: 5
Area= 78.5
[Hint: Area= 3.14*5*5=78.5]
Please Enter Radius: 9
Area= 254.34
[Hint: Area= 3.14*9*9=254.34]
Please Enter Radius: 4
Area= 50.24
[Hint: Area= 3. 14*4*4=50.24]
Note that the Hint is not part of the output.
Question Two
[8 Marks]
Write a Java program that will ask for two integer numbers as input and print out the Addition,
Multiplication, Subtraction 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 square of numbers between two numbers startNo and endNo.
You may either use a for statement or any other looping statement in Java. startNo and endNo
should be accepted as input from the keyboard.
(a) If startNo is less than Othen the program should print "startNo cannot be less than O"and the
program will end.
(b) If startNo is greater than endNo then the program should print "startNo must NOT be greater
than endNo" and the program will end.
(c) But, if the right numbers (startNo and endNo) are entered ie any two numbers within 0, I, 2, 3, 4, 5,
6, 7, 8, 9, then the sum of squares of numbers between startNo and endNo will be calculated and the
program will printout "Sum of squares of Numbers=" sumSquares.
[Hint: For instance, if you enter 2, 4, then all numbers from 2 to 4 are: 2,3,4 therefore Sumsq =
(2*2)+(3 *3)+( 4*4)=29]
---
THE END ---
---
---
4

8 Page 8

▲back to top