Universiti Teknologi Mara Final Examination: Confidential CS/APR 2008/CSC175/ITC160

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

CONFIDENTIAL CS/APR 2008/CSC175/ITC160

UNIVERSITI TEKNOLOGI MARA


FINAL EXAMINATION

COURSE : OBJECT ORIENTED PROGRAMMING I


COURSE CODE : CSC175/ITC160
EXAMINATION : APRIL 2008
TIME : 3 HOURS

INSTRUCTIONS TO CANDIDATES

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

PART B (6 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

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


This examination paper consists of 11 printed pages
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 2 CS/APR 2008/CSC175/ITC160

Answer ALL Questions

PART A (30 marks)

1. The following methods will return a non-numeric value when they are called EXCEPT

A. charAt()
B. indexOf()
C. substring()
D. equalsIgnoreCase()

2. Which of the following groups of constructors are INVALID?

I. p u b l i c C a t ( i n t age){}
p u b l i c Cat(double weight){}

II. public Dog (String name, double weight) {}


public Dog(String name, double height){}

III. p u b l i c R a b b i t ( S t r i n g name, i n t a g e ) { }
public Rabbit(){}

IV. public Cat(String name, double weight){}


public Cat (double weight, String name){}

A. II only
B. I and II
C. I and IV
D. Ill and IV

3. When a method has been declared as p r i v a t e , it can be called .

A. from any class


B. from any of its subclasses
C. from within the same class only
D. from any class within the same package only

4. What is the difference between programmer-defined classes and standard classes?

A. Programmer-defined classes are always written after standard classes.


B. Programmer-defined classes can be named anything the user chooses, while
standard classes have strict formatting regulations.
C. Programmer-defined classes are written by the programmer, while standard
classes are built into Java.
D. Programmer-defined classes are written by the programmer, while standard
classes are created automatically after compilation.

© Hak Clpta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/APR 2008/CSC175/ITC160

5. The relationship between a class and an object is best described as

A. classes are instances of objects


B. objects are instances of classes
C. objects are the messages of classes
D. objects and classes are the same thing

Questions 6 and 7 are based on the following code segment:

int calcVolume(int height, double length, double depth)


{
return (int)(height * length * depth);

6. Which of the following is an INVALID call to calcVolume () ?

A. int x = c a l c V o l u m e ( 1 , 2, 3 ) ;
B. int x = calcVolume(1, 2.4, 3.70);
C. int x = calcVolume(1.1, 2.2, 3.3);
D. int x = c a l c V o l u m e d , 2 . 2 , 3) ;

7. What is the value that it will return when h e i g h t is 1, l e n g t h is 2 . 5 and d e p t h is


3.45?

A. 8.62
B. 8.6
C. 8.0
D. 8

8. The practice of writing


The wri multiple constructors to handle different sets of inputs is known as

A. inheritance
B. overloading
C. dynamic
D. abstraction
9. Given the the following code segment:

public double calcSomeValue() // line 1


{ II line 2
double x = 0, unknown; 1 / line 3
II line 4
for (int j = 0; j < someArray.length; j++) II line 5
x += someArray[j]; II line 6
unknown = x / someArray.length; II line 7
return x; II line 8
} 11 line 9

© Hak Clpta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/APR 2008/CSC175/ITC160

What do the lines 5 to 7 do?

A. Computes the sum of the doubles in the array someArray.


B. Deletes all the doubles in the array someArray.
C. Computes the average of all the doubles in the array someArray.
D. Computes the dot product of all the doubles in the array someArray.

Questions 10 and 11 are based on the following code segment:

FurnitureStore fs = new FurnitureStore(30, "LazyBoy");


int size = fs.getSize();

10. Which of the following is the definition for the constructor method used above?

A. public Furniture () {...}


B. public void Furniture () {...}
C. public Furniture (int no, String name) {...}
D. public Furniture (no, name) {...}

11. What is the type of method called g e t s i z e () ?

A. constructor
B. accessor
C. mutator
D. processor

12. Given the following method prototypes:

I. void myMethod (int number[], int size);


II. float myMethod (float number[], int size);
III. float m y M e t h o d (int n u m l , int n u m 2 , int n u m 3 ) ;
IV. void myMethod (float n u m [ ] , int s i z e ) ;

Based on the above prototypes, which of the following statements is TRUE?

A. II and IV are overloaded methods.


B. I and III are overloaded methods.
C. I, II and IV are overloaded methods.
D. I, II, III and IV are overloaded methods.

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 CS/APR 2008/CSC175/ITC160

Questions 13 and 14 are based on the following code segment:

int[] no = {3 , -5, 8, 4}; // line 1


// line 2
for(int i = 0 ; i < no .length; i++) // line 3
System out .println (i) ; // line 4

13. What is the output of the above code segment?

A. 0
1
2
3

B. 3
-5

C. 0
0
0
0

D. 1
2
3
4

14. Which of the following statements is the MOST APPROPRIATE to replace the statement
in l i n e 4 if the intention is to print the whole content of the array involved?

A. System.out.println(no[i]);
B. System.out.println(no[i] + no[i+l]);
C. System.out.println(no);
D. System.out.println("no");

© Hak Cipta Universiti Teknologi MARA C O N FIDENTIAL


CONFIDENTIAL CS/APR 2008/CSC175/ITC160

15. Given the following class declaration:

public class Student


1
private int studentID;
private String studentName;
private String gender;
public Student (int i, String n, String g){}
public int theld(){}
public String theName(){}
public String theGenderO {}
public String toStringO {}
}

Which are TRUE about the student class above?

I. The class has a normal constructor.


II. The class has a default constructor,
ill. The class has accessors.
IV. The class has a mutator.

A. I and II
B. I and III
C. II and IV
D. I, II and III

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL CS/APR 2008/CSC175/ITC160

PART B (70 marks)

QUESTION 1

Write a program that reads a sentence and prints out the sentence in reverse order with all letters
in capital form.

For example, if the input is:

Programming i s fun.

Then, the output is:

.NUF SI GNIMMARGORP
(7 marks)

QUESTION 2

a) Given the following code segment:

public static void main(String[] args) // line 1


{ // line 2
int[] no = {21,60,35,10,48}; // line 3
int x = theNumber(no); // line 4
System.out.printIn(no[x]); // line 5
} // line 6
// line 7
public static int theNumber(int[] num) // line 8
{ // line 9
int numTemp; // line 10
for(int i = 0; i < num.length; i++) // line 11
for(int j = i+1; j < num.length; j++) // line 12
if(numti] > num[j]) // line 13
{ // line 14
numTemp = num[i]; // line 15
num[i] = num[j]; // line 16
num[j] = numTemp; // line 17
} // line 18
int ans = num.length-1; // line 19
return ans; // line 20
} // line 21

i) What is the output displayed?


(2 marks)

ii) What is the purpose of the statements in lines 11 to 18?


(2 marks)

iii) What does the statement in line 20 specifically do?


(2 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 CS/APR 2008/CSC175/ITC160

b) Display the output for the following code segment:

String title = "Ingredients for preparing cheese omelette ";


String[] ingredients = {"eggs", "cheddar cheese", "margarine",
"salt", "pepper"};
String text = "Live a Life";

System.out.print(title.substring(16, 22));
System.out.print(ingredients[3].charAt(1));
System.out.print(ingredients[3].charAt(3));
System.out.print(text.charAt(1));
System.out.print(title.charAt(13)) ;
System.out.print(title.charAt(1));
System.out.printIn(title.substring(12,15));
System.out.print(text.substring(7) ) ;

(4 marks)

QUESTION 3

Write overloaded methods named listofNumbers () for each of the following tasks:

a) Calculate and return the sum of all numbers that are greater than 5 from an array of
floating point values. The array is received as a parameter.
(3 marks)

b) Compute and return the average of all numbers in an array. The array and its size are
received as parameters.
(3 marks)

c) Return the position of the largest number stored in an array of integers. The array is
received as a parameter.
(3 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 9 CS/APR 2008/CSC175/ITC160

QUESTION 4

Listed below are two classes named RectanguiarBox and RoundBaii to represent a
rectangular box and a round ball respectively.

// RectanguiarBox class
public class RectanguiarBox

private double height; // in cm


private double width; // in cm
private double length; // in cm

// RoundBaii class
public class RoundBaii
{
public static final double PI = 3.142 r
private double radius; II in cm
public RoundBaii(double rad){ .-}
public double getRadius (){...}

a) Write a default constructor for the RectanguiarBox class which initializes the values for
all its data members to 0.
(1.5 marks)

b) Write a mutator method for the RectanguiarBox class which sets the values for all its
data members.
(2 marks)

c) Write the accesor methods for all data members of the RectanguiarBox class.
(3 marks)

d) Write a method in the RoundBaii class to calculate the diameter of a ball.


(1.5 marks)

e) Write a method named check () in the RectanguiarBox class. This method is used to
check whether a RoundBaii object can fit into the RectanguiarBox object or not.
Assume that the box can be closed with the ball inside. The RoundBaii object is
passed through the parameter. An appropriate message should be displayed for both
cases.
(4 marks)

f) Write the boxArealnlnches () method for RectanguiarBox class which calculates


the box's area and then converts the value to inches. (NOTE: 1 inch = 2.54 cm)
(4 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 10 CS/APR 2008/CSC175/ITC160

QUESTION 5

Selera Marvellous Restaurant is a contemporary restaurant in town. The management would like
to have an electronic menu panel that will be placed beside each table in the restaurant. The
electronic menu will enable the customers to order the food or beverages from their seats without
having to wait for menu from the waitresses. They can order at their own time. The menu lists
food and beverages that this restaurant offers. The food can be categorized as Western,
Chinese or Malay food, while the beverages can be hot or cold drinks. Besides the categories of
food and beverages, the price list is also included in the menu. The customers only need to
touch on the screen pointing to what kind of food or beverage they want to order, and the number
of sets for each ordered food or beverage. Then the menu panel will display the table number
and the total price that the customers have to pay.

a) Name the object involved.


(2 marks)

b) List all possible attributes for the object(s).


(6 marks)

QUESTION 6

The following class is used to handle operation on a meal card used by the students at a
college's cafeteria:

public class MealCard


{
private String ownerName;
private int cardNo; // 4-digit number: e.g. 1123, 3451
private int point;

//constructor
public MealCard (){...}

//mutator
public void setData (String name, int no, int pt) {...}

//retrievers
public String getName (){...}
public int getNo (){...}
public int getPoint (){...}

© Hak Cipta Unlversiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 11 CS/APR 2008/CSC175/ITC160

Perform the following tasks:

a) Write a method named c a l c P o i n t o which calculates the number of points used


based on the price of the food or drink bought using the meal card. The price of the food
or drink is passed as a parameter to the method. These points will then be deducted
from the current balance points where each RM0.10 is equivalent to 1 point.
(3 marks)

b) Write a method named addPointO that will be used to add points to the current
balance points. The points are entered by the user.
(2 marks)

c) Write the main method to perform as follows:

i) create an array of FIFTY (50) Meaicard objects.


(1 mark)

ii) get all the values for the variables and store the data. For each card, initialize the
p o i n t to 100.
(5 marks)

iii) all students use their cards to have lunch at the cafeteria.
(3 marks)

iv) the students with the card number that starts with " 1 1 " add points to their cards.
(3 marks)

v) display all information of all Meaicard objects.


(3 marks)

END OF QUESTION PAPER

© Hak Cipta Universal Teknologi MARA CONFIDENTIAL

You might also like