Computer Programming II Exam Dec 2023
Computer Programming II Exam Dec 2023
DEPARTMENT: ICT
DATE:
TIME:
INSTRUCTIONS:
i. This paper consists of two (2) sections: Section A and Section B
ii. Section A is compulsory and all numbers MUST be attempted
iii. Section A has a total of 40 marks
iv. Section B contains five (5) questions each with 20 marks
v. Attempt ONLY three (3) questions from section B
SECTION A (40 MARKS)
This section is compulsory, attempt ALL questions
1. Given the following Java statement:
int x = 15 % 6 + 9;
What value is currently stored in the variable x? (2 Marks)
ANSWER: 12
2. Where in a java program can one use the “return” statement? (2 Marks)
ANSWER: In the code block of any method or function without the void keyword but
specifying a return type like int, String and others.
3. What effect does the above “return” statement have on flow control? (2 Marks)
ANSWER: The return statement confirms whether the program is running as expected
and is return the right value after execution.
Student(){
this.name = name;
this.age = age;
}
1
void details(){
System.out.println(“My name is ” + this.name + “ and am “ + this.age
+ “ years old”);
}
}
void sem_details(){
System.out.println(“I am in year:” + year + “ semester:” + semester );
}
}
3
SECTION B (60 MARKS)
Each question in this sections has a total of 20 Marks; attempt any three (3) numbers
from this section.
QUESTION 1:
(a) What is a static method in Java? (3 Marks)
(b) Given the following Java statements:
int num_1 = 5;
int num_2 = 12;
num_2 = num_1; -----------------------------------------------statement 1
Sum value_1 = new Sum();
Sum value_2 = new Sum();
value_2 = value_1; ---------------------------------------------statement 2
Explain the difference between statement 1 and statement 2. (4 Marks)
(c) Given the following piece of code:
public class ExampleMinNumber {
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("Minimum Value = " + c);
}
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
} }
What is the output of this code? (3 Marks)
(d) Write a program that finds the largest number in a set of 20 integers and displays it to the
screen. (10 Marks)
4
QUESTION 2:
(a) List three operations that can be performed on data structures? (3 Marks)
(b) Briefly explain the difference between constructors and methods (4 Marks)
(c) Explain what you understand by casting in Java? (2 Marks)
(d) What does the String method concat() do? (2 Marks)
(e) Write a program that prompts the user for three integers. (3 Marks)
(f) The program should then use the integers entered in part (e) above to compute and print
to the screen the following:
i. The Difference (3 Marks)
ii. The Product (3 Marks)
QUESTION 3:
(a) What is the difference between the private and public keywords? (4 Marks)
(b) Given the following program:
public class Test {
static int x = 5;
static int y = 4;
public static void main (Strings args) {
System.out.println (“The sum is “ + p(x, y));
}
static int p (int x, int y) {
x = 10;
int sum = x + y;
return sum;
} }
What is the output of this program? (4 Marks)
(c) Using sample code, distinguish between a do-while loop and a while loop. (6 Marks)
(d) Write a simple program using arrays which captures ten numbers with decimal points,
computes and prints the average of the numbers to the screen. (6 Marks)
QUESTION 4:
(a) What are Strings in Java? (2 Marks)
(b) With illustrations, show any two ways for creating string objects? (4 Marks)
5
(c) Strings in Java are immutable; what does this statement mean? (3 Marks)
(d) Consider the following code snippet:
public class EqualsExample {
public static void main(String args[]) {
String s1 = “hello”;
String s2 = “HELLO”;
String s3 = new String(“hello”);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
} }
What will be the output of the first and second print statements? (4 Marks)
(e) What do the String methods isEmpty() and length() do? (4 Marks)
(f) What is polymorphism in Java? (3 Marks)
QUESTION 5:
(a) If a Java statement attempts to divide a number by zero, which exception is going to be
thrown? (2 Marks)
(b) State the syntax for the “for loop” and explain how it works. (4 Marks)
(c) Given the following snippet of code:
int sum = 6, x = 7, y = 8;
sum *= 15;
sum += x – y * 4;
System.out.println(“Sum = “ + sum);
What is the output of the above code? (4 Marks)
(d) Develop a grading system for an institution which can be used to carry out grading of
students’ marks following the table below: (10 Marks)
MARKS GRADE COMMENT
80 - 100 A 5.0
70 - 79 B 4.0
60 - 69 C 3.0
50 - 59 D 2.0
0 - 49 F 0.0