OOP - Final Sem2 2016 2017 EDITED
OOP - Final Sem2 2016 2017 EDITED
Faculty of
Computing
(Please Write Your Lecture Name And Section In Your Answer Booklet)
Name
I/C No.
Year / Course
Section
Lecturer Name
This questions paper consists of ELEVEN ( 11 ) printed pages excluding this page.
0
SECTION A: OBJECTIVE QUESTIONS (10 MARKS)
Part A consists of 10 objective questions. Choose the best answer, and write your answer in
the answer booklet. Each question carries 1 mark.
class ParentClass {
int id = 1;
void print() {
System.out.println(id);
}
}
A. 0 B. 1
C. 2 D. Nothing
1
4. Which of the following statements is appropriate to be filled in the blank space in
Program A1 below?
//Program A1
public class TestBook1{
public static void main (String[] args){
LabBook book = new LabBook ("OOP",75.00);
}
}
class Book{
private String title;
public Book (String t){
title=t;
System.out.println("Title : "+title);
}
}
class LabBook extends Book {
private double price;
public LabBook (String t,double p){
__________________________________
__________________________________
System.out.println("Price : RM"+price);
}
}
A. super(t); B. super(p);
title=t; price=p;
C. super(t); D. super(p);
price=p; title=t;
i. A a = new A();
ii. A a = new B();
iii. B b = new A();
iv. B b = new B();
A. RuntimeException C. Error
B. Exception D. Throwable
3
9. What will happen when Program A2 is compiled and run?
//Program A2
class Test {
public static void main(String[] args) {
try {
String s = "10.5";
Integer.parseInt(s);
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
catch (Exception ex) {
System.out.println(ex);
}
}
}
//Program A3
class Test {
public static void main (String[] args) {
try {
System.out.println("Welcome to Java");
}
}
}
C. A method call that does not declare exceptions cannot be placed inside a try block.
D. Nothing is wrong.
4
SECTION B: STRUCTURED QUESTIONS (40 MARKS)
Part B consists of 4 structured questions. Answer all questions in the answer booklet. The
marks for each part of the question is as indicated.
1 //Program B1
2 class ClassA{
3 public ClassA(){}
4 public void method1()
5 { System.out.println("UTM"); }
6 public void method1(String a)
7 { System.out.println("UTM" +a); }
8 public void method1(int a)
9 { System.out.println("UTM" +a); }
10 }
11 class ClassB extends ClassA{
12 public ClassB(){}
13 public void method1()
14 { System.out.println("FC UTM"); }
15 public void method1(String a)
16 { System.out.println("FC UTM" +a); }
17 public void method2(String a, int b)
18 { System.out.println("Studied at "+a+" in "+b); }
19 }
20 class ClassC extends ClassB{
21 public ClassC(){}
22 public void method1()
23 { System.out.println("SE@FC UTM"); }
24 public void method1(int a)
25 { System.out.println(" SE@FC UTM" +a); }
26 }
27
28 public class TestFC {
29 public static void main(String []args)
30 {
31 ________________________________________
32 ________________________________________
33
34 }
If the following statements are inserted at line 31 and 32, determine whether the program is
correct or has an error during compilation. If the program is correct, state the output. If the
program has an error, give the reason. Write your answer as in Table 1.
5
c) ClassA ob = new ClassB();
ob.method2("FC UTM",2017);
Table 1
Statement Correct / Error Output/Reason
No.
a)
b)
c)
d)
e)
Figure B1 shows relationship of the classes in Program B2. Write the missing Java statements
in Program B2 as guided in the comment parts in order to implement the class hierarchy as in
Figure B1.
<<abstract>>
Time
+ getMinutes(): int
Days HoursMinutes
Figure B1
6
1. // Program B2
2. _____________(i)________________ // Declaration of abstract class Time
3. { // [1 marks]
4. _____________(ii)_______________ // with an abstract method
5. } // getMinutes() [1 marks]
6.
7. _______________(iii)____________ // Signature of class Days that
8. { // inherits class Time [1 marks]
9. private int days;
10. ________(iv)____________ // Parameterized constructor
11. _________(v)____________ // of class Days [2 marks]
12.
13.
14. public int getMinutes() {
15. return days * 24 * 60;
16. }
17. }
18.
19. _________________(vi)___________ // Signature of class HoursMinutes that
20.
{ // inherits class Time [1 marks]
21.
private int hours;
22.
private int minutes;
23.
24.
___________(viii)_________ // Parameterized constructor
25.
___________(ix)___________ // of class HoursMinutes [2 marks]
26.
____________(x)___________
27.
28.
public int getMinutes() {
29.
return hours * 60 + minutes;
30.
}
31.
}
32.
33.
public class Demo {
34.
public static void main(String args[]) {
35.
// Create an object of class Time that refer to class Days
36.
// named t1 with argument 2
37.
_____________(xi)_________________ // [1 marks]
38.
// Create an object of class Time that refer to class HoursMinutes
39.
// named t2 with arguments 4 and 10
40.
41. __________(xii)_______________ // [1 marks]
42. System.out.println(t1.getMinutes());
43. System.out.println(t2.getMinutes());
44. }
45. }
7
Question 3 [10 marks]
Given the UML class diagram in Figure B2, Program B3, and output in Figure B3, answer
the following questions (a) to (c).
<<interface>> <<interface>>
GST Discount
Book BookApplication
price : double
8
1 //Program B3
2
3 _______(a)_________{
4 _________________________
5
_________________________
6
7 _________________________
8
}
9
10
__________________ {
11
12 _________________________
13
_________________________
14
15 _________________________
16
}
17
18
______________(b)____________________{
19
20 _____________________________________
21
_____________________________________}
22
23
24 public String toString(){
25 return "\nInitial Price: "+price+"\nPrice after 5%
26 discount: "+ (price-calcDiscount())+ "\nPrice after discount and
27 GST: "+(price-calcDiscount()+getGSTCharges());
28 }
29 public double getGSTCharges() {return price*RATE;}
30 public double calcGST() {return price+getGSTCharges();}
31 public double getDiscount() { return rate;}
32 public double calcDiscount() { return price*getDiscount();}
33 }
34
35 public class BookApplication {
36 public static void main(String[] args) {
37 ________________(c)_________________________
38
________________(d)_________________________
39
40 }
41 }
a) Write Java code that defines GST (line 3-7) and Discount (line 11-15) interface
classes [5 marks]
b) Write Java code that defines class Book (line 18-22) that implements the
interfaces defined in (a). [3 marks]
c) Write Java code to create a Book object with price is initialized with 10. [ 1 mark]
d) Display the price of book after discounts and tax levied GST by invoking
toString() method. [1 mark]
9
Question 4 [10 marks]
a. Answer question (i) to (v) as in Program B4 below with suitable codes so that it can
throw the exception. [ 5 marks]
//Program B4
public class FinalExamException {
public static void main (String args[]) {
int arr []={30,40};
Scanner in= new Scanner (System.in);
____(i)____ {
int b = in.nextInt();
int x = arr[2]/ (b – arr[1]);
}
______(v)_________ {
int y = arr[1]/ arr[0];
System.out.println("y = ” +y);
}
}
}
10
b. Given Program B5 below, answer the following question.
i. Explain why error will occur when Program B5 is compiled? [ 2 marks]
ii. Rewrite the program so that the program will compile and run properly. [ 3 marks]
//Program B5
class Test {
public static void main(String[] args) {
try {
String s = "5.6";
Integer.parseInt(s);
int i = 0;
int y = 2 / i;
}
catch (Exception ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
}
11