UNIVERSITY EXAMINATIONS: 2016/2017
EXAMINATION FOR THE DEGREE OF BACHELOR OF SCIENCE IN
INFORMATION TECHNOLOGY/BACHELOR OF BUSINESS
INFORMATION TECHNOLOGY/BACHELOR OF INFORMATION
COMMUNICATION TECHNOLOGY/BACHELOR OF SCIENCE APPLIED
COMPUTING
BIT 2204A JAVA PROGRAMMING
FULLTIME/PARTIME/DISTANCE LEARNING
DATE: AUGUST, 2017 TIME: 2 HOURS
INSTRUCTIONS: Answer Question One & ANY OTHER TWO questions.
QUESTION ONE
a) Briefly explain the following terms in the context Java programming. Write a sample java code
statement for each case
i) variable (2 Marks)
ii) assignment operator (2 Marks)
iii) array (2 Marks)
iv) Syntax error (2Marks)
(b) Briefly explain the following three versions of Java platform (3 Marks).
(c) State and explain four features of java programming language (4 Marks)
(d). Write a java class named employee that request the user to enter the cost of an item and computes
discount to be awarded. The discount is set 5% of all purchased items (5 Marks).
(e) Briefly explain the purpose of using interfaces in java programming (2 Marks)
(a). Write a java class that calculates the area of a triangle and print results (Area=1/2*base*height)
(4 Marks)
(b). Write a java class that reads the elements of the following array known as students and prints
results (4 Marks)
Students
Otieno Ann Job James
QUESTION TWO
a) What is wrong with the following interface? (2 Marks)
public interface SomethingIsWrong {
void aMethod(int aValue) {
System.out.println(“Hi Mom”);
}
}
b) Fix the interface in Question 2a. (3 Marks)
c) Is the following interface valid? (2 Marks)
public interface Marker {
}
d) Which should you use, abstract classes or interfaces? discuss (6 Marks)
e) Write a Java program to reverse an array of integer values (7 Marks)
QUESTION THREE
(a). Explain the meaning of the following keywords. Write a sample java code that illustrates how
each keyword is implemented (4 Marks)
i. ‘new’ operator
ii.’ import’
(b) Write a java class that computes the area of a square and displays results. The program should
have at least two methods (4 Marks)
(c) Write java code statements that uses for -Loop to print the following five characters (4 Marks)
DJWLS
(d). Write a program called Product1ToN to compute the product of integers 1 to 10 (i.e.,
1×2×3×…×10). The program should declare an integer variable called product (to accumulate the
product) and initialize to 1. (4 Marks)
(e) Write the difference between do-while loop and while. Write the syntax for each loop
(4 Marks)
QUESTION FOUR
a) Suppose that you have written a time server, which periodically notifies its clients of the current
date and time. Write an interface that the server could use to enforce a particular protocol on its
clients. (6 Marks)
b) Consider the following two classes:
public class ClassA {
public void methodOne(int i) {
}
public void methodTwo(int i) {
}
public static void methodThree(int i) {
}
public static void methodFour(int i) {
}
}
public class ClassB extends ClassA {
public static void methodOne(int i) {
}
public void methodTwo(int i) {
}
public void methodThree(int i) {
}
public static void methodFour(int i) {
}
}
i) Which method overrides a method in the superclass? (2 Marks)
ii) Which method hides a method in the superclass? (2 Marks)
iii) What do the other methods do? (2 Marks)
c) Assume that you have written some classes. Belatedly, you decide that they should be split into
three packages, as listed in the table below. Furthermore, assume that the classes are currently in
the default package (they have no package statements).
i) What line of code will you need to add to each source file to put each class in the right
package? (3 Marks)
ii) To adhere to the directory structure, you will need to create some subdirectories in your
development directory, and put source files in the correct subdirectories. What
subdirectories must you create? Which subdirectory does each source file go in?
(3 Marks)
iii) Do you think you’ll need to make any other changes to the source files to make them
compile correctly? If so, what? (2 Marks)
QUESTION FIVE
a) Write a program called SquareBoard that displays the following n×n (n=5) pattern using two
nested for-loops. (4 Marks)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
b) Write a program called Fibonacci to display the first 20 Fibonacci numbers F(n), where
F(n)=F(n–1)+F(n–2) and F(1)=F(2)=1. Also compute their average.
(8 Marks)
The output shall look like:
The first 20 Fibonacci numbers are:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
The average is 885.5
c) Write a program called HarmonicSum to compute the sum of a harmonic series, as shown
below, where n=50000. The program shall compute the sum from left-to-right as well as from
the right-to-left. Obtain the difference between these two sums and explain the difference.
Which sum is more accurate?
(8 Marks)