Sie sind auf Seite 1von 17
Specimen Exam Paper TCC 102 Computing 2 Time: 3 Hours Instructions to candidates: 1. Please

Specimen Exam Paper

TCC 102 Computing 2

Time: 3 Hours

Instructions to candidates:

1. Please check that this examination paper consists of fourteen (14) pages of printed material before you begin the examination.

2. This paper consists of FOUR parts: Part A (20 questions), Part B (10 questions), Part C (10 questions) and Part D (4 questions). Answer ALL questions of Part A and B in the MCQ Answer Sheet provided. Answer ALL questions in Part C and TWO out of FOUR questions in Part D in the Answer booklet provided.

3. Write in black or blue and answer all questions in the answer booklet provided.

4. No books, dictionaries, notes or any other written materials are allowed in this examination.

5. Non-programmable electronic calculator may be used.

Copyright © 2007 WOU

2

TCC 101

Part A

Multiple Choice Questions

(40 marks)

For Question 1 to 20, shade your answers in the MCQ Answer Sheet provided. Every question carries 2 marks.

1. What is the correct syntax for defining a new class Parakeet based on the superclass Bird?

A. class Parakeet isa Bird{ }

B. class Bird defines Parakeet{ }

C. class Bird hasa Parakeet{ }

D. class Parakeet extends Bird{ }

Answer: D

2. A subclass can directly access

A. public members of a superclass

B. private members of a superclass

C. all members of a superclass

D. none of the members of a superclass

Answer: A

3. The classes Reader and Writer are derived from the class

A. Streams

B. Inputs

C. Outputs

D. Object

Answer: D

4. Which of the following is a valid statement?

A. String name("Doe");

B. String ("Doe");

C. String name = "Doe";

D. name = new String;

Answer: A

3

TCC 101

5. What String instance method would return true when invoked like this:

a.method(b);

where a = "GROUNDhog" and b = "groundHOG"?

A. equals()

B. toLowerCase()

C. toUpperCase()

D. equalsIgnoreCase()

Answer: D

6. Consider the following definition of a recursive method.

public static int func1(int m, int n) { if (m == n || n == 1) return 1;

else

return func1(m - 1, n - 1) + n * func1(m - 1, n);

}

What precondition must exist in order to prevent the code above from infinite recursion?

A. m > = 0 and n >= 0

B. m >= 0 and n >= 1

C. m >= 1 and n >= 0

D. m >= 1 and n >= 1

Answer: B

7. Which of the following options above is a correct Web page for the applet WelcomeHomeApplet?

A.

B

<HTML> <BODY> <OBJECT "WelcomeHomeApplet.class"> </OBJECT> </BODY> </HTML>

<HTML> <BODY> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </OBJECT> </HTML>

4

TCC 101

C <HTML> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </HTML>

D <HTML> <BODY> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </OBJECT> </BODY> </HTML>

Answer: A

</OBJECT> </BODY> </HTML> Answer: A 8. Which of the following is NOT included in the Java

8. Which of the following is NOT included in the Java applet above?

A. Check box

B. Radio button

C. Combo box

D. list

Answer: D

9. In which of the following layout managers do all rows (columns) have the same number of components and all components have the same size?

A. GridLayout

B. FlowLayout

C. BorderLayout

D. EvenLayout

Answer: A

5

TCC 101

10.Consider the following class definitions.

public class BClass{ private int x; private double y;

public void print() { }

}

public class DClass extends BClass{ private int a; private int b; public void print() { }

}

Suppose that you have the following statement.

DClass dObject = new DClass();

How many instance variables DObject has?

A. 0

B. 2

C. 4

D. None of these

Answer: A

11.Suppose that sales is a two dimensional array of 10 rows and 7 columns wherein each component is of the type int , and sum and j are int variables. Which of the following correctly finds the sum of the elements of the fifth row of sales?

A. sum = 0; for(j = 0; j < 7; j++) sum = sum + sales[5][j];

B. sum = 0; for(j = 0; j < 7; j++) sum = sum + sales[4][j];

C. sum = 0; for(j = 0; j < 10; j++) sum = sum + sales[5][j];

D. sum = 0; for(j = 0; j < 10; j++) sum = sum + sales[4][j];

Answer: B

6

TCC 101

char[][] array1 = new char[15][10];

12.What is the value of array1.length?

A. 0

B. 2

C. 10

D. 15

Answer: D

13.Which of the following returns the number of elements in a Vector?

A. Vector.length

B. Vector.size()

C. Vector.numElements()

D. Vector.contains

Answer: B

14.If a negative value is used for an array index,

A. a NumberFormatException is thrown

B. the program terminates immediately

C. the last index of the array is automatically accessed instead

D. an IndexOutOfBoundsException is thrown

Answer: A

15.Consider the following list.

8/-

list = {20, 10, 17, 2, 18, 35, 30, 90, 48, 47};

Suppose that sequential search as discussed is used to determine whether 2 is in list. Exactly how many key comparisons are executed by the sequential search algorithm?

A. 3

B. 4

C. 5

D. None of these

Answer: A

7

TCC 101

16.Suppose alpha is an array of 50 components. Which of the following about alpha is true? (i) The base address of alpha is the address of alpha[0]. (ii) The base address of alpha is the address of alpha[1].

A. Only (i)

B. Only (ii)

C. Both (i) and (ii)

D. None of these

Answer: A

17. Consider the following code segment

public class Illustrate { private int x; private static int y; public static int count; public static int z;

public Illustrate()

x = 1;

}

{

public Illustrate(int a)

x = a;

}

public void print()

{

{

System.out.println("x = " + x + ", y = " + y + ", count = " + count);

}

public static void incrementY()

}

}

y++;

{

Based on the class definition above, which of the following statements is illegal?

A. Illustrate.incrementY();

B. Illustrate.count++;

C. Illustrate.z++;

D. Illustrate.x++;

Answer: D

8

TCC 101

18.What is the output of the following Java code?

int[] alpha = {2, 4, 6, 8, 10}; int j;

for (j = 4; j >= 0; j--) System.out.print(alpha[j] + " "); System.out.println();

A. 2 4 6 8 10

B. 10 8 6 4 2

C. 4 3 2 1 0

D. Invalid code

Answer: B

19. Consider the following code segment

int[] hit = new hit[5];

hit[0] = 3; hit[1] = 5; hit[2] = 2; hit[3] = 6; hit[4] = 1;

System.out.println(hit[1 + 3]);

What is the output of the code fragment above?

A. 1

B. 3

C. 5

D. 6

Answer: A

20.Consider the following class definition.

public class Rectangle{ private double length; private double width; private double area; private double perimeter;

9

TCC 101

public Rectangle() length = 0; width = 0;

}

{

public Rectangle(double l, double w) length = l; width = w;

}

public void set(double l, double w) length = l; width = w;

}

public void print()

{

{

{

System.out.println(length + " " + width);

}

public double area()

{

return length * width;

}

public double perimeter()

{

return 2 * length + 2 * width;

}

}

Suppose that you have the following declaration.

Rectangle bigRect = new Rectangle(10, 4);

Which of the following set of statements are valid in Java?

(i)

 

bigRect.area();

bigRect.perimeter();

bigRect.print();

(ii)

 

bigRect.area = bigRect.area(); bigRect.perimeter = bigRect.perimeter(); bigRect.print();

A.

Only (i)

B.

Only (ii)

C.

Both (i) and (ii)

D.

None of these

Answer: A

10

TCC 101

Part B

True and False Questions

(10 marks)

Use the MCQ Answer Sheet provided for Question 21 to 30. Shade on ‘=A=’ if TRUE and ‘=B=’ if FALSE. Every question carries 1 mark.

Questions

T/F

21.To determine whether two reference variables of the Integer type

F

point to the same Integer object, we can use either the method equal

of the class Integer or the operator ==.

22.If a member of a class is public you cannot access it outside the

F

class.

23.The methods of a class must be public.

F

24.An ArrayOutOfBoundsException is thrown if an index is greater than

F

or equal to the size of the array minus 1

25.To search through a list you need to know the length of the list.

T

26.Key comparisons are also called item comparisons.

T

27.Vectors can be used to represent lists

T

28.Redefining a method of a superclass is also known as overloading a

F

method.

29.An unchecked exception is any exception that causes a program to

F

terminate.

30.For interfaces such as WindowListener that contain more than one

T

method, Java provides the class WindowAdapter.

11

TCC 101

Part C

Fill in the Blank

(10 marks)

Please fill in the blank with the following phrases given. Every question carries 1 mark.

JButton

Container

init

Exception

WindowListener

Object

polymorphism

static

interface

garbage collection

1. When you click a JButton an event is created, known as an action event.

2. The class Container is the superclass of all the classes designed to provide a

GUI.

3. Applets use the init method in place of constructors to initialize various GUI

components and data members.

4. The class Exception and its subclasses are designed to catch exceptions

that should be caught and processed during program execution.

5. To handle window events you first create a class that implements the

interface WindowListener and then you create and register objects of that

class.

6. Using the mechanism of inheritance, every public member of the class Object

can be overridden and/or invoked by every object of any class type.

7. In Java, polymorphism is implemented using late binding.

8. An interface is a class that contains only abstract methods and/or named

constants.

9. The modifier static in the heading specifies that the method can be invoked

by using the name of the class.

10.Java provides automatic garbage collection.

12

TCC 101

Part D

Short and Long Questions

(40 marks)

Answer TWO out of FOUR questions in the answer booklet provided. Every question carries twenty marks.

1. (a) What does the super keyword allow in Java?

[ 2 marks ]

Answer: The super keyword allows the child object to access methods of the parent class. This even includes access to the parent constructor.

(b) When is the garbage collector called in Java?

[ 3 marks ]

Answer: The garbage collector in Java is a daemon thread that counts the references on allocated object, allocating their memory space back to the heap when the object goes out of scope. It is run automatically by the VM when the memory is low, or the CPU is idle. The user can also request that the garbage collector should run.

(c) What does the term over-riding mean?

[ 2 marks ]

Answer: Over-riding means re-writing the same method in a child class, with the exact same set of parameters. Over-riding is a form of polymorphism that allows behaviour to be replaced in a child class.

(d) What is meant by the term encapsulation?

[ 2 marks ]

Answer: Encapsulation may simply be defined as data hiding. It allows us to distinguish internal methods from the interface. We can use the public, private and protected modifiers to set the level of encapsulation.

(e) Polymorphism is a feature of most OOP languages. Describe polymorphism

with reference to over-loading and over-riding. Address the following issues:

i. Over-loading versus over-riding.

ii. Over-loading issues that arise when a child over-rides one method of many that are inherited from a parent class.

iii. How do access modifiers affect over-riding?

[11 marks]

13

TCC 101

Answer: Overloading – Re-using the same method name with different arguments and possibly a different return type is known as overloading. In unrelated class there is no issues when re-using names but in the same class, we can overload methods, provided that those methods have different argument types. A method may not be distinguished by return type alone. Overriding – Re-using the same name with the exact same arguments is known as overriding. This is most commonly used in a derived class, where the method has been defined in the parent, but a version with different functionality is required. This allows the facility to add specialised behaviour to a derived class. e.g. public void aMethod(String s) {} public void aMethod(int s) {}

is an example of an overloaded method.

We can call an overridden method using the super keyword. For example to call the aMethod from a derived class we can use super.aMethod(). Difficulties occur when we override a method in a derived class, as the other overloaded methods in the base class are no longer available to the derived class. Access modifiers affect the use of overriding. If for example a method is declared as private then the overriding method may not reduce the level of access to the method.

2. (a) Consider the code:

int x[] = new int[25];

Explain why each of the following statements are either true of false:

i. x[24] is 0

ii. x[24] is undefined

iii. x[25] is 0

iv. x[0] is null

v. x.length is 25

 

[10 marks ]

Answer: (a)

i. is true as all elements are initialised to 0.

[2 marks]

ii. is therefore false.

[1 marks]

iii. is false as there are 25 elements 0 to 24, so 25 is out of bounds.

[3 marks]

iv. is false as x[0] = 0.

[2 marks]

v.

is true as there are 25 elements so 25 would be returned.

[2 marks]

14

TCC 101

(b) Consider the following program code

int[] alpha = new int[5]; int j; i. What is the value of alpha[4] after the following code executes?

alpha[0] = 2; for (j = 1; j < 5; j++) alpha[j] = alpha[j – 1] + 3;

 

[2 marks ]

Answer: 14

ii. What is stored in alpha after the following code executes?

for (j = 0; j < 5; j++) { alpha[j] = 2 * j;

if (j % 2 == 1) alpha[j - 1] = alpha[j] + j;

}

 

[5 marks ]

Answer: alpha = {3, 2, 9, 6, 8}

Iii. What is the value of alpha[3] after the following code executes?

alpha[0] = 5; for (j = 1; j < 5; j++){ if (j % 2 == 0) alpha[j] = alpha[j – 1] + 2;

else

alpha[j] = alpha[j – 1] + 3;

}

 

[3 marks ]

Answer: 13

15

TCC 101

3. (a) Explain the function of a LayoutManager in Java? Give THREE examples of standard layout manager class.

[6 marks ]

Answer: A LayoutManager implements some policy for laying out all the visual components that have been added to a container. That is, it sets the sizes and positions of the components. Different types of layout managers have different rules about how components are to be arranged Some standard layout manager classes are FlowLayout, BorderLayout and GridLayout [3 marks for the function given and 3 marks for three examples given]

(b) What layout manager should we use for the following needs?

need to display a component in as much space as it can get

need to put the space-hungry component in the center

need to display a few components of the same size in rows and columns.

[6 marks ]

Answer: In the case where you need to display a component in as much space as it can get, if it is the only component in its container, use GridLayout or BorderLayout. Otherwise, BorderLayout might be a good match.

If you use BorderLayout, you will need to put the space-hungry component in the center. In another case, you may need to display a few components in a compact row at their natural size. Then, consider using FlowLayout manager.

Finally GridLayout is perfect when you need to display a few components of the same size in rows and columns. [2 marks for the each needs, maximum 6 marks for three needs]

(c) Compare and contrast applets to applications. When would one choose to use applets over applications? What are the limitations of applets? Describe the lifecycle of applets and applications. Can an application be an applet?

[8 marks ]

Answer: An applet is a specific type of Java application that is designed to be portable and downloadable over the Internet. For this reason an applet may only contain a subset of the total functionality of an application. For example, an applet cannot have access to local files on a machine; otherwise it is theoretically possible for a hostile applet to upload (to some destination) the contents of a local machine. This type of hostile applet also includes applets that may try to use all CPU available, or contact remote sites, without your permission. These too are not allowed. Applications on the other hand are assumed to be trusted, as they reside on the local hard disk. These applications have full access to the local hard-drive and to all network addresses.

16

TCC 101

An applet would be chosen when an application must be distributed to a number of clients who trust, or don’t trust the content. They allow a local version to reside on the server, so only one copy exists, allowing complete version control.

An application on the other hand does not contain the inherent restrictions and so must be completely trusted by the client. An independent version is distributed to each client so there is not the same degree of version control as that associated with applets. There is no overhead in downloading the application before running it. You also do not have to be connected to the Internet to run an application.

The order of execution of an applet is main() and then whatever functionality that is involved in main()

In an applet the order is init() -> start() -> paint() and stop() on the destruction. These calls may happen asynchronously.

An application may be an applet and an application. This is created by extending the Applet class (java.awt.Applet) and then adding a main() method to the Applet class by calling the init()->start()->paint() methods in the order that the appletviewer would.

4. (a) What are Exceptions in Java? Why and when are they used? Give an

application example of how you might use your own exceptions?

[10 marks] Answer: Exceptions are generated during program execution if something occurs that is not quite normal from the task in question. For example the network may fail, a file may be corrupt or there may be a bug in the application the causes it to address invalid memory. In Java we may recover from this type of run time error, allowing the user to choose a different file, or check the network before continuing. The syntax we can use is in the form of try{} and catch{}

try{ // Some statement that may generate an exception

}

catch (Exception e){ // perform some operation to recover

}

We may write our own type of exception for our application. For example if we wrote a method that required a non-negative number and a negative number was passed, we could generate an InvalidNumberArgument exception that was to be handled by the caller, allowing the user to re-enter the number or for that number to be skipped.

[3 marks for definition; 3 marks for why and when they used; 4 marks for an valid application]

17

TCC 101

(b) What is the ‘finally’ block?

[4 marks]

Answer: Finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also execute.

(c) What will happen to the Exception object after exception handling?

[2 marks]

Answer: It will go for Garbage Collector. And frees the memory.

(d) In the following program, if somecondition() is true, then only line number 3

must throw a custom exception MyException. MyException is not a subclass of

runtime exceptions. What are the changes to be made to the given method.

Public void method() {

//line 1

If (somecondition()) {

//line 2

}

//line 3

}

//line 4

Answer:

[4 marks]

- Add throw new MyException(); in line number 3

- Modify the method declaration such that an object of type Exception is to be thrown

End of question paper