Beruflich Dokumente
Kultur Dokumente
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.
...2/-
2 TCC 101
Part A
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?
Answer: D
Answer: A
3. The classes Reader and Writer are derived from the class ____.
A. Streams
B. Inputs
C. Outputs
D. Object
Answer: D
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
Answer: B
7. Which of the following options above is a correct Web page for the applet
WelcomeHomeApplet?
A. <HTML>
<BODY>
<OBJECT "WelcomeHomeApplet.class">
</OBJECT>
</BODY>
</HTML>
B <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
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
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
A. 0
B. 2
C. 10
D. 15
Answer: D
A. Vector.length
B. Vector.size()
C. Vector.numElements()
D. Vector.contains
Answer: B
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
...8/-
15. Consider the following list.
list = {20, 10, 17, 2, 18, 35, 30, 90, 48, 47};
A. 3
B. 4
C. 5
D. None of these
Answer: A
7 TCC 101
A. Only (i)
B. Only (ii)
C. Both (i) and (ii)
D. None of these
Answer: A
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
A. 2 4 6 8 10
B. 10 8 6 4 2
C. 43210
D. Invalid code
Answer: B
hit[0] = 3;
hit[1] = 5;
hit[2] = 2;
hit[3] = 6;
hit[4] = 1;
System.out.println(hit[1 + 3]);
A. 1
B. 3
C. 5
D. 6
Answer: A
public Rectangle() {
length = 0;
width = 0;
}
A. Only (i)
B. Only (ii)
C. Both (i) and (ii)
D. None of these
Answer: A
10 TCC 101
Part B
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
25. To search through a list you need to know the length of the list. T
30. For interfaces such as WindowListener that contain more than one T
method, Java provides the class WindowAdapter.
11 TCC 101
Part C
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
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.
9. The modifier static in the heading specifies that the method can be invoked
by using the name of the class.
...10/-
12 TCC 101
Part D
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.
[ 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.
[ 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.
[ 2 marks ]
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) {}
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.
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
alpha[0] = 2;
for (j = 1; j < 5; j++)
alpha[j] = alpha[j – 1] + 3;
[2 marks ]
Answer: 14
if (j % 2 == 1)
alpha[j - 1] = alpha[j] + j;
}
[5 marks ]
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
[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 ]
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.
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
[4 marks]
(c) What will happen to the Exception object after exception handling?
[2 marks]
(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.
If (somecondition()) { //line 2
} //line 3
} //line 4
[4 marks]
Answer: