Você está na página 1de 12

# CONFIDENTIAL

CS/APR 2011/CSC238/435

## OBJECT ORIENTED PROGRAMMING CSC238/435 APRIL 2011 3 HOURS

INSTRUCTIONS TO CANDIDATES 1. This question paper consists of two (2) parts : PART A (15 Questions) PART B (5 Questions)

2.

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) ii) iii) the Question Paper an Answer Booklet - provided by the Faculty an Objective Answer Sheet - provided by the Faculty

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

This examination paper consists of 12 printed pages
Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

## CONFIDENTIAL Answer ALL Questions PART A (30 marks)

CS/APR 2011/CSC238/435

1. Consider the following fragment of code: double costPerltem = 2 2 . 0 ; int numOfltem = 10; System.out.print("Total cost:" + (costPerltem * numOfItem)); What will be the output of this program? A. B. C. D. Total Total Total "Total cost: 22.0 * 10; cost: +220.0 cost: 220.0 cost:" 22 0.0

2. Based on the following declarations: double length, width; String name; Which of the following statement is TRUE? A. B. C. D. name and l e n g t h are primitive variables, and w i d t h is an object variable. l e n g t h and w i d t h are primitive variables, and name is an object variable. All are primitive variables. All are object variables.

3. Which of the following statement is FALSE regarding arguments and parameters? A. B. C. D. The number of arguments and the parameters must be the same. The name of arguments and parameters must be the same. The type of arguments and parameters must be the same. Arguments and parameters are paired left to right.

CONFIDENTIAL

## CONFIDENTIAL 4. Consider the following code snippet.

CS/APR 2011/CSC238/435

class Sample { private int number; ^ public void doSomething() { int number; . 4 number = 18;

A B

} }
Which of the following information is TRUE about A and B? A. B. C. D. A A A A is local variable, B is instance variable. is instance variable, B is local variable. is class variable, B is instance variable. is instance variable, B is class variable. .

5. Method that does NOT return a value is called a(n) A. B. C. D. instance method private method void method class method

6. To restrict access of a data member or a method to the class itself: A. B. C. D. Use the Use the Use the Use the static modifier. private modifier. restrict modifier. protected modifier

7. In object-oriented concept, if a class Animal has a subclass called Mammal, which of the following statements must be TRUE? A. B. C. D. Mammal cannot have subclasses. Mammal can have no siblings. Animal can have only one subclass. Mammal can have no other parent other than Animal.

8. A class computer and its subclass Desktop both have a method s t a r t ( ) . If AcerMD3 0i2 refers to an object of type Desktop, what will the following code do? AcerMD3 0 1 2 . s t a r t ( ) ; A. B. C. D. The s t a r t () method defined in Desktop will be called. The compiler will complain that s t a r t () has been defined twice. Overloading will be used to pick which s t a r t () is called. The s t a r t () method defined in Computer will be called. CONFIDENTIAL

## Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CS/APR 2011/CSC238/435

9. Which of the following statements about the reference super written in a child class is TRUE? A. B. C. D. It must be used every time method from the superclass is called. It must be the last statement of the constructor. It must be the first statement of the constructor. It can only be used once in a program.

10. Which of the following method can be an example of a method in a subclass that has the same signature as a method in the superclass? A. B. C. D. Overloading Overridings Composition Constructor

11. What is the name of the operator that can be used to determine whether a reference variable is a reference of an object derived from a particular class?

A. B. C. D.

## isinstance instanceof isclass extends

12. In Java, declaring a class abstract is useful to prevent programmers from further extending the class, when it doesn't make sense to have objects of that class, when default implementations of some methods are desirable, to force programmers to extend the class to use its capabilities. A. B. C. D. i, II ii iii ii iii, iv i, ii, iii, iv

13. The two types of file access operations are A. B. C. D. read access and write access protected access and private access public access and private access public access and protected access

## Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/APR 2011/CSC238/435

14. Which type of exception occurs when creating a F i l e i n p u t s t r e a m for nonexistent file? A. B. C. D. FileNotExist FileNotExistException FileNotFoundException FileNotFound

15. At the end of any file access operation, input or output, what action should be taken to ensure that all data was saved to the file? A. B. C. D. Open the file and check the contents. Close the file streams. Output the file contents. None of the above.

## Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/APR 2011/CSC238/435

## QUESTION 1 public static void main(String[] args)

{
int arr [50] ; int size, a,; Scanner input = new Scanner(System.in); System.out.println("Size of list: " ) ; size = input.nextInt(); for (int i = 0; i < size; i++)

{
System.out.println("Enter values: " ) ; arr[i] = input.nextInt() ;

}
for (int i = 0; i < size - 1; i++)

{
for (int j = i + 1; j < size; j++)

{
if (arrti] > arr[j]

{
a = arr [i] ; arr [ i] = arr [ j] ; arr[j] = a;

} } }
System.out.println ("Display of XXX numbers: " ) ; for ( int i = 0; i < size; i++)

{
System.out.println ( " " + arr[i]);

a) What is the output for the above program segment if the input data are as follows: 6, 13, 16,37,4,9,49 (4 marks) b) What is the purpose of the above program segment? (3 marks) c) What is the appropriate word to replace xxx? (3 marks)

## Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL QUESTION 2 a)

CS/APR 2011/CSC238/435

Compare and contrast among public, private and protected access modifier for variable definition. (6 marks)

b)

Given the A i r l i n e s class that consists of the following data members (attributes): F l i g h t number ( e g : FY110, AK238)

Destination (eg: Phuket) Passenger ID (eg: WDW102) Departure time (in 24 hours format, eg: 142 5) Discount rate (eg: io%)

Define the A i r l i n e s class and the following methods: i. ii. Normal constructor that sets all data with values given through the parameter. Retriever methods for each attribute.

iii. A Mutator method. iv. A method to identify and return the flight operator. The flight operator can be determined through the first two characters of the flight number as shown below:

## Two Characters of Flight Number FY AK GD PL MN

Flight Operator FireFly Airlines AirAsia Airlines Garuda Airlines Pelangi Airlines Manila Airlines (14 marks)

## Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL QUESTION 3

CS/APR 2011/CSC238/435

Given the following inheritance hierarchy and the definition of the Gymnasium class: Gymnasium

S i1ve rMembe r

GoldMember

Superclass : class Gymnasium Data member(s): String name; String icNo; String gender; Methods: Gymnasium (); Gymnasium (String name, String icNo, String gender); String getName(); // return the patient's name String getlc(); // return the ic number String getGender(); // return the gender String toStringO; // return the details of objects // person's name // person's identification card number // person's gender

a) Write the definition for silverMember class that inherits from the Gymnasium c l a s s . Attributes: String dayActivity;

## Methods: Constructor with arguments String getDayEntry() ;

// return 'class' or // 'equipment' // calculate the charges // return the details of // objects

## Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/APR 2011/CSC238/435

Given the details of charges for silverMember class in Table 3.1 below: Facilities Attend classes Use equipments Charges (RM) /entry 10.00 5.00

Table 3.1 Details of silverMember charges (5 marks) b) Write the definition for GoldMember c l a s s that inherits from the Gymnasium c l a s s . Attributes: double monthlyFee; String memberType; Methods: Constructor with arguments double getmonthlyFee() double calGoldCharges( String toStringO;

## / / r e t u r n t h e monthly fee / / c a l c u l a t e the charges / / r e t u r n t h e d e t a i l s of / / objects

Given the details of charges for GoldMember class in Table 3.2 below Type Student Non-Student Charges (RM) /month 60.00 80.00

## Table 3.2 Details of GoldMember charges (5 marks)

QUESTION 4 Given the following c y c i i n g G l a s s and swimmingGlass subclasses are inherited from
s p o r t V i s i o n S h o p p e superclass. Superclass: s p o r t V i s i o n S h o p p e

Attributes: String custOrderNo String custOrderDate float depositPymt Abstract Method: public abstract double calChargesO

CONFIDENTIAL

## CONFIDENTIAL Subclass : c y c l i n g G l a s s Attributes: char type_glass

10

CS/APR 2011/CSC238/435

// T' - Transition Glass,'?' - Polarized // glass, * I' - Iridium glass //with UV protection extra RM 30 will be //charged to the total price // method to calculate the charges // method to display the data members

boolean

uvProtection;

Method:
public double calChargesO public String toStringO

Details of charges are shown in the table 4.1 below: Glass Type Transition Polarized Iridium Table 4. Subclass ; swimmingGlass Attributes: Price RM 300.00 RM 350.50 RM 400.00

char

Type;

## public double calChargesO public String toString0

// method to calculate the total charges // method to display the data members

Details of charges are shown in table 4.2 below: Stvle and Discount Warrior Modern 30% 20% 10% 7%

to calculate the

## Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

11

CS/APR 2011/CSC238/435

b) Assume below coding being defined : / / c o n s i s t s of b o t h s u b c l a s s e s o b j e c t s sportVisionShoppe [ ] S = new s p o r t V i s i o n S h o p p e [ 1 0 ] ; Write a program fragment to determine and display the number of customer who buy cycling and swimming glass from the sportVisionShoppe. (3 marks) c) Write a program fragment to calculate the total chargers for all customers who bought "Retro" goggle at the s p o r t V i s i o n S h o p p e outlet. (3 marks) d) Write a program fragment to list the deposit paid by the customer who bought transition cycling glass from this shop. (3 marks)

QUESTION 5 XYZ Bookstore is in the process of updating the record of books information that is currently available for the year 2011. You are given a file named B o o k s t o r e . t x t that contains the following details:
-

book's title category (example academic, science fiction, programming and etc) price per unit total books in month of June 2010 total number of book sold from June until the month of December 2010
^ ^ ^ ^ ^ ' ' . , , . 'mi i i ^ ^ ^ ^ ^ ^ ^ ^ I ^ ^ ^ ^

## ' 2 Bookstore - Notepad File Edrt Format View Help

'

t a | ta,|M&#)

:++ How to Program;Programming Book;67.50;500;300 jWanita;Female;6.50;1000;900 jOmbak Rindu;Novel;12.90;500;345 Java for Beginners;Programming Book;55.90;700;500 Bahang Selatan;Novel;4.50;1200;1000 Sujud llahi;Novel;2.50;3000;2600 Wotivasi Aliran Pelajar;Motivational Book;9.90;400;160

## Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

12

CS/APR2011/CSC238/435

Write a complete program to perform the following: a) Read all records from the file named B o o k s t o r e . t x t . b) Calculate the number of books and the total price for the books that are currently available for each record. c) Write the following information into a file named Programming. t x t : Information for programming book including the number of programming books available and the total prices for each record, ii) Summarize the total number of programming books that are currently available in the bookstore. d) Write the following to the file named N o v e l . t x t : i) Information for novel including the number of novels available and the total prices for each record, ii) Summarize the total number of novels that are currently available in the bookstore. e) Use exception handling operations to deal with input-output errors. The expected output of the program is shown below:
1

i)

## " " 'S^t

UsJxSUiKSH

RECORD FOR BOOKS A V A I L A B L E Original Title Price price 500 lombak R i n d u 12.9 1200 1Bahang S e l a t a n 4.5 3000 1Sujud l l a h i 2.5 f * * T h e t o t a l number o f n o v e l s t h a t a r e 1

- unit Unit sold Book available 155 200 400 345 1000 2600 v a i l a b l e i s 755

## Total 1999.5 900.0 1000.0

Sample of N o v e l . t x t
I^IIIUHJUIJIILIUII ' i ^

f%

Etffc- Format

Went

Hefer

ftECORD FOR BOOK AVAILABLE Title Price Original unit Unit sold Book available C++ How to program 67.5 500 300 200 Java for Beginners 55.9 700 500 200 **The total number of programming books that are vailable is 400

CONFIDENTIAL