COSC 223
COSC 223
CHUKA UNIVERSITY
UNIVERSITY EXAMINATIONS
SECOND YEAR EXAMINATION FOR THE AWARD OF DEGREE OF
BACHELOR OF SCIENCE IN COMPUTER SCIENCE
(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)
(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)
(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)
(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)
(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)
(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