0% found this document useful (0 votes)
151 views7 pages

Csi141 2022 Final Special

The document is an examination paper for the Programming Principles course at the University of Botswana, with a total duration of 3 hours and a maximum score of 100 points. It includes multiple choice questions, coding exercises, and programming tasks related to Java. The paper is structured into sections that test various programming concepts and skills.

Uploaded by

HAMO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views7 pages

Csi141 2022 Final Special

The document is an examination paper for the Programming Principles course at the University of Botswana, with a total duration of 3 hours and a maximum score of 100 points. It includes multiple choice questions, coding exercises, and programming tasks related to Java. The paper is structured into sections that test various programming concepts and skills.

Uploaded by

HAMO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

University of Botswana

Department of Computer Science


Special Final Examination

Paper code: CSI141 Duration: 3 Hrs Date: January 2023


Title of Paper: Programming Principles

Subject: Computer Science Title of Examination: BSc I

Instructions:
 No aids of any kind allowed.
 Answer ALL questions.
 Answers MUST be written in the answer booklets provided.
 Total marks: 100
 READ QUESTIONS CAREFULLY.
 NUMBER OF PAGES IN THIS PAPER IS 7, INCLUDING THE COVER PAGE.

DO NOT OPEN THIS EXAM PAPER UNTIL YOU ARE TOLD TO DO SO BY


THE INVIGILATOR.

This is Page 1 of 7
Section A [Multiple choice - 20 points]
1. A student compiled a Java class in a file 2. A syntax error is [2]
called FinalClass.java and got the following a) An error in the source code that causes
results. the compiler not to understand the code.
λ javac FinalClass.java b) An error in the bytecode that causes the
FinalClass.java:1: error: class FClass is compiler not to understand the code.
public, should be declared in a file named
FClass.java
c) An input to a program that is not valid.
public class FClass { d) Division by zero error.
^ e) Incorrect program logic.
1 error
How should they fix the error?[2]
a) Compile with the command: javac
FClass.java
b) Compile with the command: java
FClass.java
c) Change the file name to FClass.class
d) Change the class name to FClass.java
e) Change the class name to
FinalClass.java
3. What is the output of the Java code below? 4. What data type should be used to store the
[2] number of visitors for each day of the
public class Special month of December?[2]
{ a) int
public static void main(String[] args) {
String band = “Heavy”;
b) double
String metal; c) int[]
metal = band + metal; d) byte[]
System.out.printf(“%s\n”, metal); e) Scanner
}
a) Heavy
b) Heavy Metal
c) bandmetal
d) Heavynull
e) The code does not compile
6. Which of the following is a valid Java
5. Given the following Java code. identifier? [2]
for (int i = 0; i < 5; i++) { a) int
for (int j = 0; j < 5; j++) { b) byte
System.out.println("*");
} c) short
} d) string
How many stars (*) are printed when it is e) double
executed? [2]
a) 16
b) 25
c) 4
d) 5
e) 20

This is Page 2 of 7
7. Given the following Java code. 8. Given the following Java code.
if (score >= 90) grade = "A"; int a = 10;
if (score >= 80) grade = "B"; int b = -10;
if (score >= 70) grade = "C"; int d = ++b * a++;
if (score >= 60) grade = "D"; System.out.printf(“%d %d %d\n”,a,b, d);
else grade = "E"; What is printed when the code is executed?
What is assigned to grade when code is [2]
executed with score = 80? [2] a) 10 -9 -90
a) A b) 10 -11 -110
b) B c) 11 -9 -90
d) 11 -9 -99
c) C e) 11 -11 -121
d) D
e) E

9. Given the following Java code, how many 10. What is the output of the following Java
times is line 8 executed when the code is code if the input is 5 4 3 2 1 -1.[2]
executed?[2] import java.util.Scanner;
1. int N = 100; public class A {
public static void main(String[] args) {
2. int sum = 0; Scanner in = new Scanner(System.in);
3. int i = N; final int SENTINEL = -1;
4. while (i > 0) int sum = 0;
5. { int val = in.nextInt();
6. if (i % 2 == 0) while (val != SENTINEL) {
sum = sum + val;
7. { }
8. sum = sum + i; System.out.printf(“sum = %d\n”, sum);
9. } }
10. i = i / 3; }
11.} a) Nothing, the while loop never terminates
a) 100 b) sum = 15
b) 33 c) sum = 12
c) 11 d) sum = 14
d) 1 e) sum = 9
e) 5

This is Page 3 of 7
Section B [Selection - 30 points]
1. The following Java code is to read N (input to the program) integers from the keyboard and sum
them, with the sum displayed on the screen. Line numbers are not part of the code.

1. import java.util.Scanner;
2. public class SumN {
3. public static void main(String[] args) {
4. Scanner in = new Scanner(System.in);
5. System.out.printf("Enter an integer: ");
6. int N = in.nextInt();
7. int sum = 0.0;
8. int i = 1;
9. while ( i < N) {
10. int x = in.nextInt();
11. sum = sum + x;
12. i++;
13. }
14. System.out.printf("Your sum is: %d\n", sum);
15. }
16. }
The code has several bugs.

a) A bug prevents the program from compiling successfully. Identify the line number where the
bug appears and give a correct version of this line of code. [2]

b) After fixing the first bug, another bug causes the program to read one (1) number less than
required. Identify the line number where the bug appears and give a correct version of this
line of code. [2]

2. Copy and complete the following table. Give the type and value of each of the following Java
expressions. To express your answer, write a Java literal of the appropriate type, such as 0, 0.0,
false, or "0". If an expression results in a compile-time or run-time error, write ERROR for its
value. Assume that the variables x, y, and z have been initialized. [5]
Java expression Type Value
“100” + 99 String “10099”
1 + 1/2 + 2/4
!!!true
3 <= x <= 10
Math.pi * 2/4
12 + “00” + 1 + 2

This is Page 4 of 7
3. Write the following mathematical equation as a Java assignment statement. [3]
−𝑏 + √𝑏 2 − 4𝑎𝑐
𝑥=
2𝑎
Assume all the variables have been properly declared an initialized.

4. Draw memory diagrams to show the values of all variables when the following Java statements
are executed: [8]
int x;
int j = 1;
String cup = "Coupe du monde";
String m = cup.charAt(0) + “” + cup.substring(2,4);
Scanner in;
int l = cup.length();
double d = j / 2;
int p = cup.indexOf(‘U’);

5. Given the following Java code snippet.


String str = “Special”;
byte b = 4;;
int x1 = 1;
char ch;
double d = 100 * b;

i) How many variables have been declared? [1]


ii) How many variables have been declared and initialized? [1]
iii) How much memory is used by the variables of primitive data types? [2]

6. Explain the difference between [3]


A. B.
int sum = 0; int sum = 0;
if (x > 0) { sum++; } if (x > 0) { sum++; }
if (y > 0) { sum++; } else if (y > 0) { sum++; }

7. Convert the following while loop into an equivalent for loop. [3]
int sum = 0;
int temp = m;
while (temp > 1) {
sum = sum + (temp % 10);
temp = temp / 10;
}

This is Page 5 of 7
Section C [50 points --- 25 points each]
1. University of Wakanda computes a course’s grade point using the table below.
Marks (%) Grade point
90 - 100 5.0
85 - 89 4.9
80 - 84 4.7
75 - 79 4.5
70 - 74 4.0
65 - 69 3.5
60 - 64 3.0
0 - 59 0.0

You are given a file containing the final mark of CSI141 students from the year 2099. Each line in the
file contains, first, the student number, followed by student’s last name, which is followed by the
student’s first name, and lastly, the student’s final mark in the course. A snippet of the file is given
below.

209900123 Silver Fox 79


209901345 Bugs Bunny 86
209912345 Speedy Gonzalez 66
209904321 Wyle Coyote 54
209905432 Charlie Brown 69
209934521 Yogi Bear 92

You are to write a Java program called MarkToGrade.java, that reads the input and output filenames
from the keyboard. Your program should read each line of the input file, computes the student’s grade
point, and writes the student’s information to the output file, including the grade point. For the input
given above, the output file should look like this. [25 points]

209900123 Silver Fox 79 4.5


209901345 Bugs Bunny 86 4.9
209912345 Speedy Gonzalez 66 3.5
209904321 Wyle Coyote 54 0.0
209905432 Charlie Brown 69 3.5
209934521 Yogi Bear 92 5.0

This is Page 6 of 7
2. Each line of a file contains three different integers. You are to write a program called
Ordered.java that reads the three integers on each line, and decides whether the numbers
appear in ascending order, descending order or neither. The input file name should be read from
the keyboard. The results should be written to a file called Results.txt. The figure below
illustrates this. [25 points]

Sample input file contents Sample output file contents


⋮ ⋮
32 76 198 32 76 198 :=> ascending
76 56 -12 76 56 -12 :=> descending
12 13 11 12 13 11 :=> neither
⋮ ⋮

This is Page 7 of 7

You might also like