Академический Документы
Профессиональный Документы
Культура Документы
Faculty of
Computing
SECTION ALL
VENUES BK1
INSTRUCTIO NS:
(Please W rite Your Lecture Name And Section In Your Answer Booklet)
Name
I/C No.
Year / Course
Section
Lecturer Name
This questions paper consists of EIGHT ( 8 ) printed pages excluding this page.
SECTION A: OBJECTIVE QUESTIONS (10 MARKS)
Part A consists o f 10 objective questions. Choose the best answer, and write your answer in
the answer booklet. Each question carries 1 mark.
1. Given the following UML notation, the relationship between class CUSTOMER and
class ORDER is
place
A. Inheritance
B. Aggregation
C. Association
D. Composition
//Program Al
class Fruit {
double k;
void display()
{System.out.println(k) ; }
}
class Rambutan extends Fruit {
double j ;
void display()
{System.out.println(j) ; }
}
class inheritance_demo {
public static void main(String args[]){
Rambutan abc = new Rambutan();
abc.k = 5.0;
a b c .j = 9.0;
abc.display();
}
}
1
A. 0.0 B. 9.0
C. 5.0 D. Compilation Error
4. In order to ensure subclasses do not override the methods in their super class,
modifier should be used when declaring the method.
A. final
B. extends
C. static
D. private
6. An abstract class___________
A. can be instantiated.
B. should be extended and implemented in the subclasses.
C. should have only one abstract method.
D. cannot be used as a data type.
//Program A2
abstract class HelloAbstractWorld {}
class AbstractDemo {
public static void main(String[] args) {
System.out.print("Hello world! ");
HelloAbstractWorld haw = new HelloAbstractWorld();
System.out.print("Hello world! ");
}
}
A. Hello world!
B. Hello world! Hello world!
C. Error: Main method not found
2
D. Error: HelloAbstractWorld is abstract; cannot be
instantiated
A. interface A { void p r i n t () { }; }
B. abstract interface A { p r i n t ( ) ; }
C. abstract interface A { abstract void p r i n t () { };}
D. interface A { void p r i n t ( ) ; }
A. Throwable
B. Exception
C. Error
D. RuntimeException
//Program A3
public class Test {
public static void main(String[] args) {
Object o = null;
System.out.println(o);
}
}
A. ArithmeticException
B. ArrayIndexOutOfBoundsException
C. NullPointerException
D. No exception
3
SECTION B: STRUCTURED QUESTIONS___________________________ (40 MARKS)
Part B consists o f 4 structured questions. Answer all questions in the answer booklet. The
marks for each part o f the question is as indicated.
(i) Identify and explain the relationships among the classes in Program B l.
(2 marks)
(ii) Draw the UML class diagram to illustrate the relationships.
(8 marks)
1. //Program Bl
2. public class University {
3■3
4. private Department department;
5.
6. public University() {
7. department = new Department();
8. department.getDeptName();
9. }
10. }
11.
12. public class Department {
13. private String deptName;
14 . private Person lecturers;
15.
16. public Department(Person lecturers) {
17. this.lecturers = lecturers;
18. }
19.
20. public String getDeptName() {
21. return deptName;
22. }
23. }
24.
25. public class Person {
26. private String name;
27.
28 . public Person(String name) {
29. this.name = name;
30. }
31. }
QUESTION 2 [10 marks]
Program B2 is an incomplete program of Vehicle class. Write the correct Java statements
based on questions (i) to (v).
(i) Complete the code, so that class Car inherits class Vehicle. (1 mark)
(ii) Write one public data member of class Car named registrationNo that holds String
data type.
(1 mark)
(iv)Create one Car object, named carl with appropriate values for all instance variables.
(2 marks)
(v) Refer to line 42, explain why object carl is allowed to invoke the method of Vehicle
class, that is printDescription () ? (1.5 marks)
5
1 //Program B2
2 class Vehicle
op - {
«
4 public int gear;
5 public int speed;
6
7 public Vehicle(int startSpeed, int startGear)
8 {
9 gear = startGear;
10 speed = startSpeed;
11
XX }
12
13 public void printDescription()
14 {
15 System.out.println("\nVehicle is " + "in gear " +
16 this.gear + " and travelling at a speed of " +
17 this.speed + ". ") ;
is
Xo }
19 }// end of class Vehicle
20
21 . class Car // (a)
( ’
o o r
i
23
24 //(b)
OK„
6
QUESTION 3 [10 marks]
(i) Program B3 consists of classes Examplel, Example2 and Demo. Class Example2
inherits class Examplel. Complete the program by filling the blanks according to the
numbered questions. (6 marks)
(ii) Program B4 consists of interface class Examplel and classes that are Example2 and
Demo. Class Example2 implements class Examplel. Complete the program by filling
the blanks according to the numbered questions. (4 marks)
1. //Program B3
2. (a) class Examplel{
3. private int num0ne=10;
4. protected int numTwo=20;
5. public int numThree=500;
6. public (b) void displayl();
7. }
8. }
9.
10. class Example2 (c) (d) {
11. public (e) (){
12. System.out.println("Num2="+numTwo);
13. System.out.println("Num3="+numThree);
14 . }
15. }
16.
17. class Demo{
18 . public static void main(String args[]){
19. (f) obj=new (g)
20. obj.displayl();
21. }
22. }
1. //Program B4
2. (a) Examplel{
3. int num0ne=10;
4. }
5. class Example2 (b) (c) {
6. public void displayl(){
7. System.out.println("Numl="+numOne);
8. }
9. }
10. class Demo{
11. public static void main(String args[]){
12 . (d) obj=new (e) ;
13. obj.displayl();
14 . }
15. }
7
QUESTION 4 [10 marks]
(i) Given Program B5 below, answer the following questions.
//Program B5
class Example {
public static void main(String args[]) {
int .i, sum;
sum = 10;
for (i = -1; i < 3 ;++i) {
sum = (sum / i);
System.out.print (i);
}
}
}
(a) Program B5 causes an error to occur. Name the type of this error/exception.
(2 marks)
(b) Modify the program and add related exception handlers to catch the errors.
(2 marks)
(c) What is the output of the modified program as in your answer in (b) ?
(2 marks)
//Program B6
class TestEx {
public static void main(String[] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2/i;
System.out.println("Welcome to Java");
}
catch (RuntimeException ex) {
System.out.println("It's RuntimeException");
}
catch (Exception ex){
System.out.println("It1s Exception");
}
finally {
System.out.println("End of the block");
}
System.out.println("End of the block");
}
}