0% found this document useful (0 votes)
16 views

COSC 223

The document is an examination paper for COSC 223: Object Oriented Programming at Chuka University, intended for second-year Computer Science students. It includes various questions on Java programming concepts such as interfaces, classes, constructors, methods, exception handling, and object-oriented principles like encapsulation and inheritance. Students are required to answer Question One and any two additional questions within a 2-hour timeframe.

Uploaded by

kimutaiallan16
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)
16 views

COSC 223

The document is an examination paper for COSC 223: Object Oriented Programming at Chuka University, intended for second-year Computer Science students. It includes various questions on Java programming concepts such as interfaces, classes, constructors, methods, exception handling, and object-oriented principles like encapsulation and inheritance. Students are required to answer Question One and any two additional questions within a 2-hour timeframe.

Uploaded by

kimutaiallan16
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/ 3

COSC 223

CHUKA UNIVERSITY

UNIVERSITY EXAMINATIONS
SECOND YEAR EXAMINATION FOR THE AWARD OF DEGREE OF
BACHELOR OF SCIENCE IN COMPUTER SCIENCE

COSC 223: OBJECT ORIENTED PROGRAMMING

STREAMS: BSC (COMP SCI) TIME: 2 HOURS

DAY/DATE: TUESDAY 14/04/2020 11.30 AM – 1.30 PM


INSTRUCTIONS:

Answer Question One and any other two questions

Question One (30 marks)

(a) Explain the difference between implementing an interface and extending a class in Java.
(2 marks)

(b) Using Java code samples, explain the difference between a class and an object. (3 marks)

(c) Identify three differences between a constructor and a method. (3 marks)

(d) Discuss the following object-oriented concepts. (6 marks)

(i) Encapsulation
(ii) Inheritance
(iii) Polymorphism

(e) Declare and initialize an array of six String values to capture the following names: Joy, Jolly,
Fred, John, James and Carlos. (4 marks)

(f) Using a Java program code sample, explain the following terms.
(4 marks)
(i) local variable
(ii) instance variable

Page 1 of 3
COSC 223

(g) Write a Java method to find if an element is in array. The program should return true if
element is in the list and false if element not in the array. Use enhanced for loop. The method
takes two arguments: an array and the item to look for. (4 marks)

(h) Using an appropriate illustration, show the use of the throws and throw key words in
exception handling. (4 marks)

Question Two (20 marks)

(a) Using an example, explain and show the difference between method overloading and method
overriding in Java. (5 marks)

(b) Define a java class Bank Account with private data members: balance and account number;
and skeleton public methods: deposit, withdraw, and calculate interest. All the methods
should return the new balance after a transaction is completed. (5 marks)

(c) In the program (b) above, implement java program method withdraw that throws an Illegal
Argument Exception with the message “Withdrawal amount entered is above the account
limit. Limit is Ksh. 40, 000 per day.” If the amount entered is above ksh. 40, 000.00 or
above the available balance in the account. (5 marks)

(d) In the main method, write java code that calls the withdraw method defined in (c) above and
catches the possible exception. (5 marks)

Question Three (20 marks)

(a) Discuss the three access levels for attributes and operations in a class. Why are they
important? (4 marks)

(b) Write a java program to demonstrate the use of interface in Java. Create an interface Animal
with the methods walk and sleep. Then create two classes Dog and Cow. The two classes
use the interface Animal. Dog has extra operation bark () and Cow has extra operation milk
( ). (6 marks)

(c) Write a Java method for finding the average of an array of scores passed to the method. The
method returns the average. If the array has no scores or the access to the scores goes
beyond the array size, throw a new exception in the method. (10 marks)

Question Four (20 marks)

(a) Define a class Person with the private data members: first name, last name, and date of birth;
public member functions: get name and get date of birth and a constructor to initialize the
data members. (10 marks)

(b) Define another class Student that inherits from class Person and has with the following
properties: student registration number, address, and programmme and the public operations:

Page 2 of 3
COSC 223

get grade and a constructor to initialize student details. Ensure that the Person constructor is
invoked in the Student constructor. (10 marks)

Question Five (20 marks)

(a) What is an exception in Java? (2 marks)

(b) Write the syntax for handling exceptions in Java (4 marks)

(c) Write a Java method that returns the largest value in an array of integer values. (4 marks)

(d) Write a Java program to find the grade of a score entered. The program should report an
exception “Invalid score entry.” If the score entered is not in the range 0 to 100. The program
should have an exception propagator and an exception catcher. (10 marks)
------------------------------------------------------------------------------------------------------------

Page 3 of 3

You might also like