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

CSC186 FinalDec2019

The document is an examination paper for an Object Oriented Programming course. It consists of instructions for candidates taking the exam as well as 20 multiple choice questions that assess knowledge of OOP concepts like classes, objects, inheritance, polymorphism, and arrays. Candidates are to answer Part A questions on an objective answer sheet and Part B questions in an answer booklet.

Uploaded by

aufa wada'ah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
612 views

CSC186 FinalDec2019

The document is an examination paper for an Object Oriented Programming course. It consists of instructions for candidates taking the exam as well as 20 multiple choice questions that assess knowledge of OOP concepts like classes, objects, inheritance, polymorphism, and arrays. Candidates are to answer Part A questions on an objective answer sheet and Part B questions in an answer booklet.

Uploaded by

aufa wada'ah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CONFIDENTIAL CS/DEC 2019/CSC186

UNIVERSITI TEKNOLOGI MARA


FINAL EXAMINATION

COURSE OBJECT ORIENTED PROGRAMMING


COURSE CODE CSC186
EXAMINATION DECEMBER 2019
TIME 3 HOURS

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of two (2) parts : PART A (20 Questions)
PART B (4 Questions)

2. Answer ALL questions from all two (2) parts :

i) Answer PART A in the Objective Answer Sheet.


ii) Answer PART B in the Answer Booklet. Start each answer on a new page.

3. Do not bring any material into the examination room unless permission is given by the
invigilator.

4. Please check to make sure that this examination pack consists of :

i) the Question Paper


ii) an Answer Booklet — provided by the Faculty
iii) an Objective Answer Sheet — provided by the Faculty

5. Answer ALL questions in English.

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 13 printed pages

O• Hak Cipta Universiti Teknologi CONFIDENTIAL


MARA
CONFIDENTIAL
CS/DEC 2019/CSC186

PART A

1. Which of the following is FALSE about a Java object?

a) The object is a combination of attributes and behaviors.


b) The object has a state.
c) The object is created from classes.
d) The object’s states is stored in methods.

2. A constructor is

a) used to create objects for a class


b) executed when an array is created
c) used to store variables for the object
d) defined with a return type

3. Methods of a class with the access modifier publ1c

a) define the behavior of the class


b) cannot be accessed from any other class
c) can be accessed from any other class
d) can only be accessed within the class itself

4. The values of a pr1vate attribute is made accessible to outside client programs with
the use of

a) mutator method
b) accessor method
c) processor method
d) tostring method

Question 5 and 6 are based on the following statements:

Car carl = new Car("Aventador”, "Coupe”,


5000000.00); double price = carl.ca1cPrice(0.05);

5. Which of the following is the method signature of ca1 cPr1ce ( ) ?

a) public void calcPrice(double a)


b) public double calcPrice(int a)
C) public double calcPrice(double a)
d) public void calcPrice()

O Hak Cipta Universiti Teknologi CONFIDENTIAL


MARA
CONFIDENTIAL 3 CS/DEC 2019/CSC186

6. Which of the following is the CORRECT parameter type and sequence for the Ca:C
constructor?

a) double, String, String


b) String, String, String
C) String, String, int
d) String, String, double

7. Composite object is

a) an object contains values


b) an object contains many attributes
c) an object contains another object
d) an object contains many methods

8. Which of the following declares an array of 20 Person objects?

a).String person = new Person(20);


b) Person person = new Person[20];
c) Person[] person = new Person[20];
d) Person[20] person = new Person[],

9. Which of the following statements is FALSE about overloaded methods?

a) The number of parameters is different.


b) The number of parameters is the same but the data types of the parameters are
different.
c) Both the number of parameters and the data types of the parameters are different.
d) Both the number of parameters and the data types of the parameters are the
same.

10.Which of the following statements is TRUE?

a) At least one method in the class must be overloaded.


b) Some methods in the class may have composite object.
c) Array of object can store many objects of similar class.
d) At least one method in the class must return object.

O Hak Cipta Universiti Teknologi CONFIDENTIAL


MARA
CONFIDENTIAL 4
CS/DEC 2019/CSC186

11. What will be displayed when the following codes are


executed?

biskid.cost[0] = 19.90;
biskid.cost[1] = 20.00,
biskid.cost[2] = 10.50;
biskid.cost[3] = 9.20;
for (int i=l; i<3; i+
+)
sum = sum + biskid.cost[i];
System.out.print("Total cost: " +
sum);
a) Total cost: 19.90
b) Total cost: 30.50
C) Total cost: 39.70
d) Total cost: 59.60

12.Given the following Fract1on class

public class Fraction {

public Fraction add(Fraction


f){ Fraction sum;
sum = this +
f; return sum;

Which is the CORRECT statement to calculate the sum of three (3) Fraction instances
f1, f2 and f3?

a) Fraction sum = f1 + f2 + f3,


b) Fraction sum = add( fl + f2 + f3);
C) Fraction sum = f1.add(f2 + f3);
d) Fraction sum = f3.add(f2.add(f1));

O• Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL
CS/DEC 2019/CSC186

13.The following code segment is an example of a(n)

public static int min(int a, int b, int c){. }

public static int min(int a, int b, int c, int d)( )

a) polymorphism
b) overloaded method
c) overriding method
d) interfaces

14.Which of the following constructors has composite objects?

a) public person (int id, String name, int dd, int mm, int yy)
{this.id = id; this.name = name; this.dd = dd; this.mm =
mm, this.yy = yy;)
b) public person (int id, String name, int dd, int mm, int yy)
{super(dd,mm,yy); this.id = id, this.name = name;}
C) public person (int id, String name, int dd, int mm, int
yy){student = new Student(id, name); date = new Date(dd, mm,

d) public person (int id, String name, int dd, int mm, int
yy){super(id, name); Date(dd, mm, yy);l

15. Which of the following statements is TRUE about protected method?

a) It cannot be accessed by name inside its own class definitions.


b) It cannot be accessed by name inside any class derived from it.
c) It can be accessed by name in the definition of any class in the same package.
d) None of the above.

16. The following is a method defined in a parent class:

public int Summation(int arr);

Which of the following methods is required in a child class to show overriding?

a) public int Summation(int arr)


b) public int Summation(int arr, floar
ar) C)public double Summation(int arr)
d)public int Summation(long arr)

O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 6
CS/DEC 2019/CSC186

17. What does the instanceof operator do?

a) Creates an object of a certain class.


b) Casts the object to a certain class.
c) Checks to see if the object is of a certain
class.
d) None of the above.

18. What is the type of reference variable for cat?

cat;

cat = new Siamese("Thailand”, 15),


cat.display();
cat = new Persian("Iran”, 17);
cat.display();
cat = new Himalayan("US”, "Boboy”);
cat.display(),

a) Siamese
b) Persian
c) Himalayan
d) Cat

19. The following diagram is the inheritance structure for pub11cTEansport , ART and
T classes:

PublicTransport

MRT LRT

Which of the following code will print the information of z*

PublicTransport[] pubT = new MRT[10],

a) for(int i = 0, i < MRT.length; i++)


{System.out.println(MRT[i].tostring()); l
b) for(int i = 0; i < PublicTransport.1ength; i++)
{ if (PublicTransport[i] instanceof MRT)
System.out.println(MRT[i].tostring()); }
c) for(int i = 0; i < pubT.length; i++)
{System.out.print1n(pubT[i].tostring()); )
d) for(int i = 0; i < PublicTransport.length; i++)
(System.out.println(PublicTransport[i].tostring());)

O• Hak Cipta Universiti Teknologi CONFIDENTIAL


MARA
CONFIDENTIAL 7 CS/DEC 2019ICSC186

20. What is the output for the following


program?

class Flower {
public Flower()
{
System.out.print1n("The default constructor of Flower
is invoked”)}

class Orchid extends Flower


{ public Orchid()(
System.out.print1n(”The default constructor of Orchid
is invoked”))

public class FlowerApp {


public static void main(String[] args){
Orchid orc = new Orchid();}

a) The default constructor of Orchid is invoked


b) The default constructor of Orchid is
invoked The default constructor of Flower
is invoked
c) The default constructor of Flower is
invoked The default constructor of Orchid
is invoked
d) No output

(40 MARKS)

Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 CS/DEC 2019/CSC186

PART B

QUESTION 1

Consider the following class Book .

public class Book {


private String bookCode; /egBKC1147
private String title;
private double price;
private String
author;

//constructor
//mutator
//accessor
//toString()

Based on the above information, write the statement for the following tasks:

a) In class definition, write default constructor and copy constructor.

public Book(){
bookCode = null;
title = null;
price = 0;
author = null;
}

Public Book(Book b){


this.bookCode = b.bookCode;
this.title = b.title;
this.price = b.price;
this.author = b.author;
(2 marks)

b) In class application, write the statements to display publisher’s name through the
fourth character of the bookCode (e.g BKC1147 is published by Course Pubs).

Character Publisher’s Name


1 Course Pubs
2 Thomson
3 Pocket Book
O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
(3 marks)
Book b = new Book();

If (b.getBookCode().charAt(3) == ‘1’){
author = “Course Pubs”;}

else If (b.getBookCode().charAt(3) == ‘2’){


author = “Thomson”;}

else If (b.getBookCode().charAt(3) == ‘3’){


author = “Pocket Book”;}

SOP(b.getBookCode() + “is published by” + author);

(60 MARKS)

O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL
9 CSIDEC 2019/CSC186

QUESTION 2

Sedap Bak Hang Catering Services offers attractive packages for the coming school
holidays as stated in a table below. In order to provide efficient service to customer,
the company decided to develop an ordering system that consists of 2 classes;
Order and Date .

Package Type Price per ax


A 25.00
B 35.00
C 45.00

public class Order {


private String name;
private String phoneNo,
private Date date;
(composite object)
private char package;
private int numCustomer,

public String toString(){return ("Name : " + name + "\


nPhone No : " + phoneNo + "\nDate : " + date.toString() + "\nPackage :
+
package + "\nPax : " + numCustomer);

public class Date {


private int day;
private int
month, private
int year,

public String tostring()


(return (day + "/" + month + "/" + year);)
a) Write the definition of Order class for the following tasks:

i) Method ca1cPr1ce ( ) that will calculate and return the total price based on
the selected packages and number of customers. If the number of customers is
more than 1000, a 10% discount is deducted from the total price.

O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


Public double calcPrice(){
If(package ==’A’)
Price = 25 * numCustomer;
If(package ==’B’)
Price = 35 * numCustomer;
If(package ==’C’)
Price = 45 * numCustomer;
If(numCustomer>1000)
totPrice = price *0.9;
return totPrice;
}

(3 marks)

ii) Method ca1cPr1ce double disc) that will calculate and return the total price
based on the selected packages, number of customers and discount given
during promotion period. Discount given depends on management decision.

Public calcPrice(double disc){

disc = calcPrice() * disc;

return disc;

}
(2 marks)

O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 10 CS/DEC 2019/CSC186

iii) Method iisExpensive (Order) that receives second object of Order as a


parameter and returns true or false. This method compares the total price of two
Order objects during non-promotion period. This method returns true if first
object is more expensive than second object otherwise returns false.

Public Boolean isExpensive(Order order2){


totalPrice1= calcPrice();
totalPrice2 = order2.calcPrice();

return totalPrice1>totalPrice2; }

(2 marks)

b) Write a complete Java main program to perform the following tasks:

Declare an array to store TEN (10) Order


objects.

Order [] order = new Order[10];

Read and store all data into the objects.

For(int i=0; i<order.length; i++)

SOP(“……………”);
……………………..

Order[i] = new Order (…………………….)


Date date = new Date(day, month, …..)

Calculate and display all information and the total prices charged, for each
objects during promotion period. A 15% discount given for all sales.

double disc = 0.85;


double netPrice = 0;

For(int i=0; i<order.length; i++){

netPrice = order[i].calcPrice(disc);

SOP(“Order:” + order[i].toString());
SOP(“total sale:”) + netprice; }

(5 marks)
QUESTION 3

Sewa4U Online provides rental services for terrace houses and condominium units. The
company needs a system to handle its monthly rental collections from tenants. The
following class is used to represent a house for rent.

public class House (


private String tenant; //tenant’s name
private String ICNo; //tenant’s IC
number private String address; //tenant’s
address

public House(.:..)( )

public String getTenant() { return


tenant;} public String getICNo() ( return
ICNo;) public String getAdd() { return
address;}
a) Define a class Terrace to represent a terrace house by using the concept of
inheritance. The data members for this class are type (single-storey or double-storey)
and corner lot status (Yes/No)

public class Terrace extends House{


private String type;
private Boolean cornerlotStat;

public Terrace(String tenant, String ICNo, String address, String type, boolean
cornerlotStat){
super(tenant, ICNo, address);
this.type = type;
this.cornerlotStat = cornerlotStat;

public String getType() {return type;}


public Boolean getCornerLotStat() {return cornerlotStat;}

public doubleCalcRent(int month){


double rent;
if(type.equalsIgnoreCase(“Single-storey”) {
if(cornerlotStat){
rent = 2000;
else
rent = 1500;}
}
if(type.equalsIgnoreCase(“Double-storey”) {
if(cornerlotStat){
rent = 3000;
else
rent = 2500;}

Rent = rent * month;


return rent; }

The class should also have a method to calculate and return the rent based on the
following table:

Type Corner Lot Rent per month


Single-storey Yes 2000.00
No 1500.00
Double-storey Yes 3000.00
No 2500.00

(6 marks)

O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 11
CS/DEC 2019/CSC186

b) Write a complete Java application to do the following


tasks:

i) Declare an array of TEN (10) Terrace objects.

Terrace [] t = terrace [10];

ii) Read and store all data into the objects.

For(int i=0; t.length; i++){


SOP(“……….”);
Boolean cornerlotStat = scanner.nextBoolean():

T[i] = new Terrace(“……………………”)

iii) Count and display the information of tenants for double-storey corner lots.

For(int i=0;i<t.length; i++){

If(type.equalsIgnoreCase(“Double-storey”) && cornerlotStat){

Count++;}

SOP(t[i].toString());}
SOP(“total double-storey corner lots:” + count);

iv) Display the total rent the company should collect for the month.

For(int i=0;i<t.length; i++){

Total = total + t[i].calcRent(month);}


SOP(“……………” + total);

v) Display the type of house rented by a tenant named “Ali bin Abu".

For(int i=0;i<t.length; i++){


If(t[i].getTenant().equalsIgnoreCase(“Ali bin Abu”){
SOP(t[i].getType())}
(15 marks)

O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


QUESTION 4

Cinta Sayang Tailoring offers custom-made men’s and women’s fashion such as baju
melayu, baju kurung, coats, blazer, jackets and many more. As for the first phase, the
ordering system focuses on baju melayu and baju kurung only. The following are
classes and inheritance hierarchy for its ordering system.

TailorOrder

BajuKurun BajuMelayu
g
Given is the definition for TailorOrder, BajuKurung and BajuMelayu
classes:

abstract class Tailororder


( private String
custName; private
String orderNum;
private double deposit;
private boolean
delivery;

public Tailororder (.... ) (.... .) / /sets all data members

public abstract double payment ();

public String getName() {.:.1


//returns customer name
public String getorderNum() {.
//returns receipt number
„.} public double getDeposit()
//returns the deposit
(.:.} public boolean
//returns the delivery
getDelivery() {.:} public double
//returns the balance
getBalance() {.:.}

O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 12 CS/DEC 2019/CSC186

public class BajuKurung extends Tailororder {


private int type; //type ofbaju kurung
private boolean embroidery; //add-on embroider(yes/no)

public BajuKurung(..) {.:.}


//sets all data members
public double addEmbroidery()
{.:)
/*If customer adds embroidery, RM25.00 is charged. Returns
the additional charge.*/

public double payment(){.:.}


/*The rate is as follows:
Type Description Price (RM)

1 Normal Baju Kurung 30.00


2 Baju Kurung Lining 60.00
3 Normal Baju Kebaya 55.00
4 Baju Kebaya Lining 110.00

The total payment is calculated after adding


embroidery charge.*/

public class BajuMelayu extends Tailororder


( private int type; /type ofbaju
melayu

public BajuMelayu(..) (.:.) //sets all data members


public double payment()(.:.)
/*The rate is as follows:
Type Description Price (RM)

1 Baju Melayu Cekak Musang 75.00


2 Baju Melayu Teluk Belanga 95.00

a) Write the definitions for method addEmbroidery () and method payment () in


BajuKurung and BajuMelayu classes.

public class BajuKurung extends Tailororder


Public double addEmbroidery(){
If(embroidery){
O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
addCharge = 25;
else
addCharge = 0;
return addCharge; }

public double payment(){

Type Description Price (RM)

1 Normal Baju Kurung 30.00


2 Baju Kurung Lining 60.00
3 Normal Baju Kebaya 55.00
4 Baju Kebaya Lining 110.00

totPrice = price + addEmbroidery();


return totPrice;
}

public class BajuMelayu extends Tailororder{

public double payment(){


Type Description Price (RM)

1 Baju Melayu Cekak


Musang 75.00
2 Baju Melayu Teluk
Belanga 95.00

Return totPrice; }
(9 marks)

O Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 13
CS/DEC 2019/CSC186

b) Write Java program segments, to do the following tasks:

i) Declare an array of objects based on customer order. The number of customer


orders is determined by user.

SOP(“The number of customer orders:”);

Int custOrder = scanner.nextInt();

Tailororder order[] = Tailororder[custOrder];

(2 marks)

ii) Calculate and display the number of customer orders for Baju Kurung with
embroidery.

For(int=0; i<order.length; i++){

If(order[i] instanceOf BajuKurung){

BajuKurung bk = (BajuKurung) order[i];

If(bk.getEmbroidery(){

Count++; }

SOP(“number of customer orders for Baju Kurung with embroidery:” + count);

(4.5 marks)

0 Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


iii) Calculate and display the payment for each order. If the customer requests
delivery, additional charge of RM5.00 has to be included in the payment.
Calculate and display the total payment for all orders. Display the output in the
following format:

Order Number Price(RM)

Total :RM :..


(6.5 marks)

For(int=0; i<order.length; i++){

Payment = payment + order[i].payment();

If(order[i].getDelivery()){

addCharge = 5 + payment;

SOP(order[i].getOrderNum());

SOP(“total payment: ” + addCharge);

END OF QUESTION PAPER

0 Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like