PRG510S - PROGRAMMING 1 - 1ST OPP - NOV 2023


PRG510S - PROGRAMMING 1 - 1ST OPP - NOV 2023



1 Page 1

▲back to top


n Arn I BI A u n IVER s I TY
OF SCIEnCE Ano TECHnOLOGY
FACULTY OF COMPUTING AND INFORMATICS
DEPARTMENTOF SOFTWAREENGINEERING
QUALIFICATION: BACHELOROF COMPUTERSCIENCE,BACHELOROF COMPUTERSCIENCE
IN CYBERSECURITYAND BACHELOROF INFORMATICS
QUALIFICATIONCODE:07BCMS, 07BCCY,07BAIT
LEVEL: 5
COURSE:PROGRAMMING 1
DATE: NOVEMBER 2023
DURATION: 2 HOURS
COURSECODE:PRGSlOS
PAPER:THEORY
MARKS: 100
FIRSTOPPORTUNITYEXAMINATION QUESTIONPAPER
EXAMINER
DR AMBROSEAZETA
MODERATOR:
MR STEVENTJIRASO
INSTRUCTIONS
1. Answer ALLthe questions.
2. Read all the questions carefully before answering.
3. Number the answers clearly.
THIS QUESTION PAPERCONSISTSOF 5 PAGES
(Including this front page)
1

2 Page 2

▲back to top


SECTIONA: TRUEOR 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. Declarations must appear at the start of the body of a Java method.
[2 Marks]
2. The statement d = a/-b; is a valid java statement.
[2 Maries]
3. Built-in functions are the same as user-defined functions.
[2 Maries]
4. This is a valid statement in Java: int ++a= 100;
(2 Marks]
5. Arrays cannot be created and initialize as in this statement:
int x[] = new int[]{l0,20,30};
[2 Marks]
6. The following are NOT valid identifier names:
9FirstName, %School.
[2 Marks]
7. The Java statement: int= Numl + Num2 *Sum3 is valid.
(2 Marks]
8. The code segment int x = 8; System.out.println(x--); prints 8 on the screen.
(2 Marks]
9. In Java, a Try statement must end without Catch statement.
(2 Maries]
10. In programming a method, is recursive if it contains a call to itself.
(2 Maries]
11. Windows and GUI programming are similar.
(2 Maries]
12. Fabonaci and factorial are NOT good examples to demonstrate recursion.
(2 Maries]
13. Division by O or index out of array range throws an exception handling.
[2 Maries]
14. Heap technique is a search algorithm.
(2 Marks]
15. In Java, the identifiers shank, sBank and SBANI<are all the same.
[2 Maries]
16. In Java, a for loop inside another for loop is regarded as structure loop.
(2 Maries]
17. In Java, double quote such as "\\n Hello World") is a char.
[2 Marks]
18. This statement var+= a; is equivalent to var= var+ a.
(2 Marks]
19. For the expression (y>=z &&a==b) to be true, both of (y>=z) and (a==b) must be true.
(2 Maries]
20. This statement double amount= 2.5, interestRate = 89.5; implies: declaration and
assignment.
[2 Maries]
SECTIONB: FILLTHE 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 average weight of students in a class is 47:
(2 Maries]
22. The name of your family is Johnson:
[2 Marks]
23. Whether or not an aircraft is moving:
[2 Marks]
24. The amount of money in your bank is 3050.00:
(2 Marks]
25. The grade you obtained in a certain subject is A:
[2 Marks]
26. The number of your house in your street:
[2 Marks]
2

3 Page 3

▲back to top


SECTIONC: CODESNIPPET
This section consist of 3 questions. Answer ALLthe questions
Each correct answer is allocated 8 Marks
Question One
[8 Maries]
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
Status
75-100
Distinction
A
70-74
Merit
B
60-69
Credit
C
50-59
Pass
D
0-49
Fail
F
//Grade.java
1. import java.util.Scanner;
2. public class Mark {
3. public static void main(String[] args) {
4. string mark;
5. string status;
6. Scanner console= new Scanner(System.in)
7. Syste.println("Enter your numeric exam mark: );
8. mark= console.nextlnt();
9. if (mark>= 75) {status= 'A';}
10. else if (mark>= 70) {status= 'B';}
11. else if (mark>= 60) {status= 'C';}
12. else if (mark>= SO){status= 'D';}
13. else { status = "F"; }
14. System.out.println("Your Status is : " + status); }
15. }
In the Java program above (Grade.java), identify which lines (numbers on the left)
have errors and describe the error.
Question Two
[8 Marks]
(A) Giving the program (example1.java) below, identify the error(s) in the Java code snippet. [6 Marks]
1. //examplel.java
2. public class Main{
3. public static void main(String []args){
4.
int x = 5;
Stringy= 10;
5.
if(z<y)
6.
System.out.println("x is less than y");
7.
else if (x > y)
8.
System.out.println("x is greater than y");
9.
Else
10.
System.out.println("x is equal toy");
11. }
12.}
3

4 Page 4

▲back to top


(B) Identify the error(s) in the code snippet below:
1.
//example2.java
2.
import java.util.Scanner;
3.
public class Lab0l_A {
4.
public static void main(String[] args) {
5.
Scanner sc = new Scanner(System.in);
6.
System.out.println("Enter First Number:");
7.
Nl = sc.nextlnt();
8.
System.out.println("Enter Second Number:");
9.
N2 = sc.nextlnt();
10.
Sl=Nl+N2;
11.
System.out.println(Sl);
12.
}
13. }
[2 Maries]
Question Three
Rewrite the following piece of code using a for loop:
[8 Maries]
Public static void Display (String [ ] Lnames, int [] Stud_marks) {
int J = 0;
while (J < Lnames.length ) {
System.out.println(Lnames[J]+"\\tStudent's Score: "+Stud_marks[J]);
J += 1;
}
SECTION D: CODES/THEORY
Answer all questions
Each correct answer is allocated 8 Marks
Question One
[8 Maries]
(A) Write a Java program to find the average of 3 real numbers. The program should
accept 10 different set of numbers using a loop.
[4 Marks]
(B) Write a Java program to calculate Body Mass Index (BMI), the BMI value is calculated based on
the inputs given by the user in metric units. BMI is used to measure the body mass based on the
height and weight of the individual. The range of BMI classifies individuals as Underweight, Normal,
Overweight or Obese. We have categorized our BMI range as follows:
[4 Marks]
Category
Underweight
Normal
Overweight
Obese
Table 1: Body Mass Index categories
BMI Range
< 18.5
18.5 - 25
> 25 and< 30
30 and above
4

5 Page 5

▲back to top


The formula for calculating BMI in Metric Units is:
BMI = Weight in l<g/(Height in Meters* Height in Meters)
For example, let us consider height= 1.50m and weight= 90.0kg
BMI = 90.0 / (1.50 * 1.50) = 40.0 (You are Obese).
Here is a sample output of the BMI Calculator program:
Enter your weight in kg: 90.0
Enter your height in cm: 1.50
Your BMI value: 40.0
Status:
You are Obese
Question Two
[8 Marks]
Write a Java program that will accept two numbers as input from the keyboard and find the
largest of the two Numbers
Question Three
Write a Java program to sum even numbers between startNo and endNo.
You may either use afar statement or any other looping statement in Java.
The following should be programmed:
[8 Marks]
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. (2 Marks).
(b) If end No is less than 0 then the program should print "endNo cannot be less than 0" and
the program will end. (2 Maries).
(c) If end No is greater than 9 then the program should print "end No cannot be greater than
9" and the program will end. (2 Marks).
(d) But, if the right numbers (startNo and end No) are entered ie any two numbers within 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, then the sum of even numbers between startNo and EndNo will be calculated
and the program will printout "Sum of Even Numbers=" sum Even. (2 Marks).
Sample output 1:
Input: Enter 2 Numbers separated with a space (startNo and endNo): -3 7
Output: startNo cannot be less than 0
Sample output 2:
Input: Enter 2 Numbers separated with a space (startNo and endNo): 2 -9
Output: end No cannot be less than 0
Sample output 3:
Input: Enter 2 Numbers separated with a space (startNo and endNo): 2 20
Output: end No cannot be greater than 9
Sample output 4:
Input: Enter 2 Numbers separated with a space (startNo and endNo): 2 8
Output: Sum of Even Numbers= 20
[Hint: Even numbers from 2 to 8 are: 2,4,6,8, therefore Sum = 2+4+6+8=20]
---
---
THE END ---
---
---
5