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

Computer Programming II Exam Dec 2023

Programming past paper

Uploaded by

vicitug
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
25 views7 pages

Computer Programming II Exam Dec 2023

Programming past paper

Uploaded by

vicitug
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

UGANDA INSTITUTE OF INFORMATION AND COMMUNICATIONS TECHNOLOGY

END OF SEMESTER ONE EXAMINATIONS

ACADEMIC YEAR 2022/2023

DEPARTMENT: ICT

YEAR OF STUDY: TWO

PROGRAMMES: DIPLOMA IN COMPUTER SCIENCE (DCS)

COURSE: OBJECT ORIENTED PROGRAMMING (JAVA)

COURSE CODE: CSC 211

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.

4. What is the significance of the semi-colon (;) in Java? (2 Marks)


ANSWER: The semi-colon (;) is used to terminate a statement / line of code acting as a
full stop in grammar.
5. Explain what you understand by the following terms with reference to Java and with
examples for each, demonstrate how each would be implemented:
i. Inheritance (4 Marks)
ANSWER: This is an object-oriented principle the enable the creation of new class
called sub-classes that copy behaviors and properties from already existing classes
called super-classes. This concept also allows creating addition behaviors and
properties for the sub-classes as shown below.

//Super Class called Student


class Student{
String name;
String age;

Student(){
this.name = name;
this.age = age;
}

1
void details(){
System.out.println(“My name is ” + this.name + “ and am “ + this.age
+ “ years old”);
}
}

//Sub Class call Y1_Student


class Y1_Student extends Student{
String year = 1;
String semester = 1;

void sem_details(){
System.out.println(“I am in year:” + year + “ semester:” + semester );
}
}

ii. Encapsulation (4 Marks)


ANSWER: Encapsulation is an object-oriented programing concept that involves
prevents direct access to class property but rather access them using getter and setter
methods. This is done by using private properties.
iii. Method Overloading (4 Marks)
ANSWER: This is the creating methods with similar names but different argument
expected and different functionality
6. What is the difference between an Array and an ArrayList and under what
circumstances would each be used? (4 Marks)
7. Assuming that n is an integer variable, write a simple program to print to the screen
“positive” if n is a positive number, “negative” is n is a negative number or “zero” if n is
zero. (6 Marks)
8. Given the following Java code fragment:
int a, v = 9;
a = v++;
if(a < 10) {
System.out.println(“Hello World”);
} else {
2
System.out.println(“Hello Universe”); }
What is the output of this code fragment? (3 Marks)
9. Assuming that the Random class has already been imported into a program and a
random object named “rn” has also been created, write the full statement to create a
random integer in the range 1 to 100 inclusive and store this randomly selected number
is a variable x. (3 Marks)
10. What is the difference between the final and finally keywords in Java? (4 Marks)

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

You might also like