17
EXAMINATION AND EVALUATION DIVISION DEPARTMENT OF POLYTECHNIC EDUCATION (MINISTRY OF HIGHER EDUCATION) INFORMATION & COMMUNICATION TECHNOLOGY (ICT) DEPARTMENT FINAL EXAMINATION JUNE 2012 SESSION FP301: OBJECT ORIENTED PROGRAMMING DATE: 21 NOVEMBER 2012 (WEDNESDAY) DURATION: 2 HOURS (2.30PM-4.30PM) This paper consists of SEVENTEEN (17) pages including the front page. Section A: Objective (40 questions answer ALL) Section B: Structure (2 questions answer ALL). CONFIDENTIAL DO NOT OPEN THIS QUESTION PAPER UNTIL INSTRUCTED BY THE CHIEF INVIGILATOR (The CLO stated is for reference only)

FP 301 OOP FINAL PAPER JUNE 2013

Embed Size (px)

Citation preview

Page 1: FP 301 OOP FINAL PAPER JUNE 2013

EXAMINATION AND EVALUATION DIVISION

DEPARTMENT OF POLYTECHNIC EDUCATION

(MINISTRY OF HIGHER EDUCATION)

INFORMATION & COMMUNICATION TECHNOLOGY (ICT)

DEPARTMENT

FINAL EXAMINATION

JUNE 2012 SESSION

FP301: OBJECT ORIENTED PROGRAMMING

DATE: 21 NOVEMBER 2012 (WEDNESDAY)

DURATION: 2 HOURS (2.30PM-4.30PM)

This paper consists of SEVENTEEN (17) pages including the front page.

Section A: Objective (40 questions – answer ALL)

Section B: Structure (2 questions – answer ALL).

CONFIDENTIAL

DO NOT OPEN THIS QUESTION PAPER UNTIL INSTRUCTED BY THE

CHIEF INVIGILATOR

(The CLO stated is for reference only)

Page 2: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 2 of 17

SECTION A

OBJECTIVE QUESTIONS (50 marks)

INSTRUCTION:

This section consists of FORTY (40) objective questions. Answer ALL questions in the

answer booklet.

1. UML is the acronym of _________________________ [CLO1]

A. Unification Multi Language

B. Unify Multimedia Language

C. United Modeling Language

D. Unified Modeling Language

2. Which of the following terminologies is used to describe the data component in

UML class diagram. [CLO1]

A. Attribute

B. Method

C. Object

D. Class

3. Which of the following define the best statement of object oriented analysis?

[CLO1]

A. The process of defining the problem in terms of real-world objects with which

the system must interact

B. The process of defining the components, interfaces, objects, classes, attributes,

and operations that will satisfy the requirements

C. The process of defining the problem, scenario, and operations that will satisfy

the requirements

D. The process of defining the components, interfaces, objects, classes, attributes,

and operations which the system must interact

4. Which of the following statements is correct? [CLO1]

A. Every class must end with a semicolon.

B. Every comment line must end with a semicolon.

C. Every line in a program must end with a semicolon.

D. Every statement in a program must end with a semicolon

Page 3: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 3 of 17

5. What is byte code in the context of Java? [CLO1]

A. It is the code written within the instance methods of a class.

B. The type of code generated by a Java Virtual Machine.

C. The type of code generated by a Java compiler.

D. It is another name for a Java source file.

6. Select the best description about data type.

[CLO1]

A. The part of the CPU that does arithmetic.

B. A part of main memory used to store data.

C. The collection of variables that a program uses.

D. A particular scheme for representing values with bit patterns.

7. Why is main() method special in a Java program? [CLO1]

A. It is where the Java interpreter starts the whole program running.

B. Only the main() method may create objects.

C. Every class must have a main() method.

D. The main() method must be the only static method in a program.

8. The act of creating an object of given class is called ___________ [CLO1]

A. Declaration

B. Referencing

C. Instantiation

D. Implementation

9. Which one of the following illustrates proper naming convention in Java

programming style? [CLO1]

A. int StudentName;

B. final double MAXVALUE = 3.547;

C. public class compute_Area(){}

D. public static int ReadDouble(){}

Page 4: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 4 of 17

10. What value will be the result if you attempt to add an int, a byte, a long and a

double? [CLO1]

A. byte

B. int

C. long

D. Double

11. Which of the following statements correctly creates an input stream by user input?

[CLO1]

A. BufferedReader kb = new InputStreamReader(System.in);

B. BufferedReader kb = new BufferedReader( );

C. BufferedReader kb = new BufferedReader(InputStreamReader(System.in));

D. BufferedReader kb = new BufferedReader (new InputStreamReader

(System.in));

12. This operator performs an arithmetic or signed right shift. Which of the following

is the symbol of the operator? [CLO1]

A. >>

B. >>>

C. <<

D. <<<

13. Which of the following statements is correct to display Welcome to Java?[CLO2]

A. System.out.println('Welcome to Java');

B. System.out.println("Welcome to Java");

C. System.println('Welcome to Java');

D. System.print('Welcome to Java');

14. Given a java class as follows:

In order to compile this program, the source code should be stored in a file _____

[CLO2]

A. Test.class

B. Test.java

C. Test.doc

D. Test.txt

public class Test {}

Page 5: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 5 of 17

15. Consider the following code snippet:

What would you write in order to instantiate MyClass? [CLO2]

A. MyClass mc = new MyClass();

B. MyClass mc = new MyClass;

C. MyClass mc = MyClass();

D. MyClass mc = MyClass;

16. Which will legally declare, construct, and initialize an array? [CLO2]

A. int [] myList = {"1", "2", "3"};

B. int [] myList = (5, 8, 2);

C. int myList [] [] = {4,9,7,0};

D. int myList [] = {4, 3, 7};

17. What are the three parts of a counting loop that must be coordinated in order for

the loop to work properly? [CLO2]

A. Initializing the condition, changing the condition, terminating the loop.

B. Initializing the counter, testing the counter, changing the counter.

C. The while statement, the if statement, and sequential execution.

D. The while, the assignment, and the loop body.

public class MyClass{

public MyClass()

{ /*code*/ }

}

Page 6: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 6 of 17

18. Analyze the following code:

Select the best statement that describes the program [CLO3]

A. The program has compile errors because the variable radius is not initialized.

B. The program has a compile error because a constant PI is defined inside a

method.

C. The program has no compile errors but will get a runtime error because radius

is not initialized.

D. The program compiles and runs fine.

19. Examine the following code:

What is the output? [CLO3]

A. 1 2 3 4 5 6

B. 0 2 4 6 8

C. 0 2 4 6

D. 0 2 4

20. What is the difference between ‘Exception’ and ‘error’ in Java? [CLO1]

A. Exception class is used for exceptional conditions that user program should

catch.

B. Error defines exceptions that are not excepted to be caught by the program.

C. Exception and Error are the subclasses of the Throwable class.

D. All of the above.

public class Test {

public static void main(String[] args) {

double radius;

final double PI= 3.15169;

double area = radius * radius * PI;

System.out.println("Area is " + area);

}

}

int count = 0;

while ( count <= 6 )

{

System.out.print( count + " " );

count = count + 2;

}

System.out.println( );

Page 7: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 7 of 17

21. The following are keywords in exception handling except [CLO1]

A. try

B. finally

C. caught

D. throw

22. Choose the common exception type [CLO1]

i. NullPointerException

ii. NumberFormatException

iii. SecurityException

A. i, ii

B. i, iii

C. ii, iii

D. i, ii, iii

23. Which is TRUE about assertion? [CLO1]

A. Assertion is a mechanism used by many programming languages to describe

what to do when something unexpected happen.

B. Assertion is a way to test some assumption about the logic of a program.

C. Assertion is a part of the program and cannot be removed independently from

the program.

D. All of the above.

24. What does exception type in the following program throw? [CLO2]

A. ArithmeticException

B. ArrayIndexOutOfBoundsException

C. StringIndexOutOfBoundsException

D. ClassCastException

public class Test {

public static void main(String[] args) {

int[] list = new int[5];

System.out.println(list[5]);

}

}

Page 8: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 8 of 17

25. Given the following code:

Which could be used to create an appropriate catch block? [CLO2]

A. ClassCastException

B. IllegalStateException

C. NumberFormatException

D. IllegalArgumentException

26. What will be the output of the following program? [CLO3]

A. finally

exception

finished

B. exception

finished

C. finally

D. Compilation fails

try { int x = Integer.parseInt(“two”); }

public class Test {

public static void aMethod() throws Exception {

try {

throw new Exception();

} finally {

System.out.println(“finally”);

}

public static void main(String args[]) {

try {

aMethod();

} catch (Exception e) {

System.out.println(“exception”);

}

System.out.println(“finished”)

}

}

Page 9: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 9 of 17

27. What will be the result of compiling and running the program below? [CLO3]

A. Run time error

B. Compile time error

C. Program compiles correctly and print “A” when executed

D. Program compiles correctly and prints “A” and “C” when executed

28. Which methods can access a private attribute? [CLO1]

A. Only classes in the same package.

B. Only static methods in the same class.

C. Only those defined in the same class.

D. Only instance methods in the same class.

29. Which of the following is the general scheme for a class definition? [CLO1]

A. Class ClassName {

// Description of the instance variables.

// Description of the constructors.

// Description of the methods.}

B. class ClassName {

// Description of the instance variables.

// Description of the constructors.

// Description of the methods.}

C. ClassName {

// Description of the instance variables.

// Description of the constructors.

// Description of the methods.}

D. class ClassName { };

public class Exception {

public static void main(String[] args){

System.out.println(“A”);

try{}

catch(java.io.IOException t) {

System.out.println(“B”);

}

}

}

Page 10: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 10 of 17

30. average = calculate (no1, no2, no3)

What is the type of parameter passing based on the above coding. [CLO1]

A. Call by value

B. Call by object

C. Call by parameter

D. Call waiting

31. If a method assigns a new value to a member of an object which can be accessed

through an object reference parameter, will this have any effect on its caller?

[CLO1]

A. No, because it only has a copy of the object.

B. No, because it does not allow to do this.

C. Yes, this will change part of the object that both it and the caller are referring

to.

D. Yes, the caller will now get a new object.

32. Which one of Java packages below is used for basic language functionality and

fundamentals types? [CLO1]

A. java.lang

B. java.util

C. java.math

D. java.io

33. A constructor ________ [CLO1]

A. must have the same name as the class it is declared within

B. is used to create objects

C. maybe declared private

D. all of the above

Page 11: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 11 of 17

34. A String class _________________________________________ [CLO1]

i. is final

ii. is public

iii. is serializable

iv. has a constructor which takes a StringBuffer objects as an arguments

A. i only

B. i, ii

C. i, ii, iii

D. All of the above

35. Which of the following is a correct syntax for defining a new class Cupboard

based on the superclass Furniture?

[CLO 2]

A. class Cupboard isa Furniture { //additional definitions go here }

B. class Cupboard implements Furniture { //additional definitions go here }

C. class Cupboard defines Furniture { //additional definitions go here }

D. class Cupboard extends Furniture { //additional definitions go here }

36. Which three lines of codes are equivalent to line 3? [CLO 2]

i. final int k = 4;

ii. public int k = 4;

iii. static int k = 4;

iv. abstract int k = 4;

A. i, ii, iii

B. ii, iii, iv

C. i, iii,iv

D. All of the above

public interface Foo

{

int k = 4; /* Line 3 */

}

Page 12: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 12 of 17

37. What is the output of the following segment program?

[CLO 2]

A. fa fa

B. fa la

C. la la

D. Compilation fails

38. Consider the following coding

[CLO 2]

What is the output?

A. 6

B. river

C. 8

D. Columbia

39. Which of the following defines a legal abstract class?

[CLO 3]

A. abstract class Vehicle {

abstract void display(); }

B. class Vehicle {

abstract void display(); }

C. abstract Vehicle {

abstract void display(); }

D. class abstract Vehicle {

abstract void display(); }

public class Tenor extends Singer {

public static String sing(){ return “fa”;}

public static void main(String[] args {

Tenor t = new Tenor();

Singer s = new Tenor();

System.out.println(t.sing() + “ “ + s.sing());

}

}

String river = new String(“Columbia”);

System.out.println(river.length());

Page 13: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 13 of 17

40. Which of the statement, is NOT correct to declare an interface?

[CLO 3]

A.

B.

C.

D.

public interface Marker {

}

public interface SomethingIsWrong {

voidaMethod(intaValue);

}

public interface SomethingIsWrong {

voidaMethod(intaValue) {

System.out.println("Hi Mom");}

}

publicRectanglePlus(Point p, int w, int h) {

origin = p;

width = w;

height = h;

}

Page 14: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 14 of 17

SECTION B

STRUCTURED QUESTIONS (50 marks)

INSTRUCTION:

This section consists of TWO (2) structured questions. Answer ALL questions.

QUESTION 1

a. Refer to Figure 1, define source program, Java compiler and Java bytecodes.

[CLO1]

Figure 1 : Program Flow

(6 marks)

b. Car is a real world object and it has its own characteristics like colour of the car,

model of the car and engine capacity. We can drive the car and stop it. Draw an

UML class diagram to show the attributes and the behaviors of a car. [CLO2]

(7 marks)

c. Based on your answer in 1(b), write a class definition for class Car by using Java.

[CLO3]

(7 marks)

d. Write a program to accept a number from the user. Use the assert statement to

determine the entered number is within the valid range between 0 and 20.

[CLO3]

(5 marks)

Page 15: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 15 of 17

QUESTION 2

a. Explain briefly the operators below: [CLO1]

i. Conditional operator (2 marks)

ii. Logical operator (2 marks)

iii. Left-shift operator (2 marks)

b. Write a program to get 3 values of array elements from command line with those

values in the main() method. Add try and catch block to handle

ArrayIndexOutOfBoundsException. [CLO2]

(5 marks)

c. Write a command to enable assertion when you run Java program. [CLO2]

(2 marks)

Page 16: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 16 of 17

d. The class Birthday is the parent for the two new classes, YouthBirthday and

AdultBirthday. Each of the new classes would ordinarily inherit the greeting()

method from Birthday.

YouthBirthday birthday card for young people. This card will add the line "How

you have grown!" to the usual birthday greeting. An AdultBirthday birthday card

is for old people. This card will add the line "You haven't changed at all!" to the

usual birthday greeting.

class Birthday {

int age;

public Birthday ( String r, int years ) {

recipient = r;

age = years;

}

public void greeting() {

System.out.println("Dear " + recipient + ",\n");

System.out.println("Happy " + age + "th Birthday\n\n");

}

}

Based on the above coding,

Page 17: FP 301 OOP FINAL PAPER JUNE 2013

CONFIDENTIAL FP301 OBJECT ORIENTED PROGRAMMING

Page 17 of 17

i. complete the following definition for class YouthBirthday. [CLO3]

class ______(a)______ extends ________(b)_______

{

public ________(c)_______ ( String r, int years )

{

____________(d)_______ ( r, years )

}

public void greeting()

{

__________(e)___________();

System.out.println("How you have grown!!\n");

}

}

(5 marks)

ii. write a new class definition for AdultBirthday with an overriding method

greeting() in it. [CLO3]

(7 marks)