Documente Academic
Documente Profesional
Documente Cultură
Student information
Last name ______________________
ii) Eating, drinking and smoking are not permitted in examination rooms. Personal
recording/playback devices are not permitted in the examination room.
iii) If it is necessary for a candidate to leave the room he or she may do so and return if
accompanied by the Presiding Examiner or an invigilator.
iv) A candidate must not write on any paper, other than that in the answer book, and must
keep all papers on the desk.
Part II
/ 20
Part III
/60
Page 1 of 23
Total
/100
Question #
Answer
10
Question #
Answer
10
Page 2 of 23
Part I
True/False
20 marks
For this part, clearly indicate if each assertion is TRUE or FALSE. You must
write TRUE or FALSE, NOT T or F.
1.
An interface reference can refer to any object of any class that implements
the interface.
T
2.
A try block must have at least one catch block, but could have many catch
blocks, and may or may not have a finally block.
F
3.
4.
5.
If a GUI container is resized by the user, all components of the GUI are
automatically increased or decreased by the same fraction to fit in the
newly sized container.
F
6.
7.
8.
An inner class is allowed to access directly all the instance variables and
methods of its enclosing class.
T
9.
10.
The font of the text of a button can be changed using the setFont() method.
T
Page 3 of 23
Part II
20 marks
For this part, please note that each question below has only one answer.
Choose the best answer.
1. What is the output of the following program?
public class Inherit
{
class Figure
{
void display( )
{
System.out.println("Figure");
}
}
class Rectangle extends Figure
{
void display( )
{
System.out.println("Rectangle");
}
}
class Box extends Rectangle
{
void display( )
{
System.out.println("Box");
}
}
Inherit( )
{
Figure f = new Figure( );
Rectangle r = new Rectangle( );
Box b = new Box( );
f.display( );
f = r;
((Figure) f).display( );
f = (Figure) b;
f.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
Page 4 of 23
A
2.
A. Figure
Rectangle
Box
B. Figure
Figure
Figure
D. Rectangle
Figure
C. Figure
Rectangle
Figure
An abstract class
A.
is a class which cannot be instantiated
B.
is a class which has no methods
C.
is a class which has only abstract methods
D.
is a class which has only overridden methods
A
For questions 3 5, use the following skeletal code.
public class ExceptionThrowerCode{
}
public void m2( ) {
try{
m3( );
}
catch(ArithmeticException ae) {}
catch(NullPointerException npe) {}
}
public void m3( )
{
try{
}
catch(ArithmeticException ae) {}
}
public static void main(String[ ] args){
try {
ExceptionThrowerCode etc = new ExceptionThrowerCode( );
etc.m1( );
etc.m2( );
}
catch (ArithmeticException ae) { }
}
}
Page 5 of 23
3.
Page 6 of 23
Page 7 of 23
9. What happens when you attempt to compile and run these two files in the
same directory?
//File P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
A.
B.
C.
D.
D
10. If you want to draw a red circle inside of a green square in a JPanel where
the paintComponent method is passed a Graphics object called page, which
of the following sets of commands might you use?
A.
B.
page.setColor(Color.green);
page.setColor(Color.red);
page.fillRect(50, 50, 100, 100);
page.fillOval(60, 60, 80, 80);
page.setColor(Color.red);
page.setColor(Color.green);
page.fillOval(60, 60, 80, 80);
page.fillRect(50, 50, 100, 100);
C.
page.setColor(Color.green);
page.fillRect(60, 60, 80, 80);
page.setColor(Color.red);
page.fillOval(50, 50, 100, 100);
E.
any of the above would accomplish this.
A
Page 8 of 23
D.
page.setColor(Color.red);
page.fillOval(50, 50, 100, 100);
page.setColor(Color.green);
page.fillRect(60, 60, 80, 80);
Part III
Programming Questions
60 marks
Page 9 of 23
Page 10 of 23
Page 11 of 23
The above program compiles and runs. Answer each of the following questions.
a)
If you are using a command line terminal window on SOL to develop your
java programs, write down the command to compile the above program. (1 mark)
Your answer:
b)
Draw diagrams to show how the program works by following the sequence
of commands/actions issued by user. (6 marks, 2 marks each)
(i) The user first types java Phone on the command line.
Your answer:
(ii) After the command in (i), the user clicks the phone no. 2533000, then
clicks send.
Your answer:
(iii) Following the actions in (ii), the user clicks the Clr button four times.
Your answer:
Page 12 of 23
c)
The program in this question contains a logical error. When user dials a
number, say, 2533000 and clicks send, the phone is supposed to call the
number 2533000 and this number is shown in the phone display. However, at
this point, if user continues to click another digit, say 9, the phone display
shows the number 25330009. This means that during a phone call to 2533000,
the phone user can still change the number. This is a logical error.
Modify this program by changing ONE single existing statement in the event
handler for the Send button so that this logical error can be eliminated. Show
your change and explain why. Remember that you are allowed to change only
one SINGLE existing statement. (6 marks)
Your answer:
Change the statement
lcdOutput = " Calling...\n\n " + lcdOutput;
to
lcdOutput = "Calling...\n\n " + lcdOutput;
Page 13 of 23
Question 2: 15 marks
Consider the classes MyClass, MyExtendedClass and MyFinalClass.
public class MyClass{
public int v1;
public int v2;
public MyClass(){
v1 = 1;
v2 = 2;
}
public void method1(){
System.out.println("method1 in MyClass "+v1);
}
public void method2(){
System.out.println("method2 in MyClass "+v2);
}
public void method3(){
System.out.println("method3 in MyClass "+v2);
this.method2();
}
}
public class MyExtendedClass extends MyClass{
public int v2;
public MyExtendedClass(){
v1=11;
v2=22;
}
public void method2(){
System.out.println("method2 in MyExtendedClass "+v2);
this.method1();
}
}
public final class MyFinalClass extends MyExtendedClass{
public int v1;
public MyFinalClass(){
v1 = 111;
}
public void method1(){
System.out.println("method1 in MyFinalClass "+v1);
super.method1();
}
}
Page 14 of 23
These classes, as well as the class Test given below, do not contain errors.
What is the output produced after running the application Test?
Page 15 of 23
Your answer:
FIRST CASE
method1 in MyClass 1 ->0.5
method2 in MyClass 2 -> 0.5
method3 in MyClass 2 ->0.5
method2 in MyClass 2 ->0.5
SECOND CASE
method1 in MyClass 11 -> 0.5 mark
method2 in MyExtendedClass 22 -> 1 marks
method1 in MyClass 11 -> 1 mark
method3 in MyClass 2 -> 0.5 mark
method2 in MyExtendedClass 22 -> 0.5 mark
method1 in MyClass 11 -> 0.5 mark
THIRD CASE
method1 in MyFinalClass 111 -> 1 marks
method1 in MyClass 11 -> 1 marks
method2 in MyExtendedClass 22 -> 0.5 mark
method1 in MyFinalClass 111 -> 0.5 mark
method1 in MyClass 11 -> 0.5 mark
method3 in MyClass 2 -> 0.5 mark
method2 in MyExtendedClass 22 -> 0.5 mark
method1 in MyFinalClass 111 -> 0.5 mark
method1 in MyClass 11 -> 1 mark
FOURTH CASE
method2 in MyExtendedClass 22 -> 1 mark
method1 in MyFinalClass 111 -> 1 mark
method1 in MyClass 11 -> 1 marks
Page 16 of 23
Question 3: 12 marks
File ParseInts.java below contains a program template that should do the
following:
z
Prompts for and reads in a line of input
z
Takes the input line one token at a time and parses an integer from
each token as it is extracted.
z
Sums the integers.
z
Prints the sum.
For example, if you give it the input
10 20 30 40
it should print
The sum of the integers on the line is 100.
However, if you try a line that contains both integers and other words, e.g.,
We have 2 dogs and 1 cat.
You will probably get a NumberFormatException when it tries to call
Integer.parseInt on "We" if your program does not handle exceptions.
Use exception handling, complete this program so that it always produces correct
answer. For example, for the line of input We have 2 dogs and 1 cat., your
program should print
The sum of the integers on the line is 3.
//what do you want to import
public class ParseInts {
public static void main(String[] args) {
// Prompts for and reads in a line of input
// Takes the input line one token at a time and parses an integer from
// each token as it is extracted.
// Sums the integers.
// Prints the sum.
System.out.println("The sum of the integers on this
line is " + sum);
}
}
Page 17 of 23
Your answer:
//
//
//
//
//
//
****************************************************************
ParseInts.java
Reads a line of text and prints the integers in the line.
****************************************************************
import java.util.Scanner;
public class ParseInts
{
public static void main(String[] args)
{
int val, sum=0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a line of text");
Scanner scanLine = new Scanner(scan.nextLine());
while (scanLine.hasNext())
{
try
{
val = Integer.parseInt(scanLine.next());
sum += val;
}
catch (NumberFormatException exception)
{
}
}
System.out.println("The sum of the integers on this line is " +
sum);
}
}
Page 18 of 23
Question 4: 10 marks
File IntList.java contains definitions for a linked list of integers. The class
contains an inner class IntNode, which holds information for a single node in
the list.
public class IntList
{
private IntNode front;
//first node in list
public IntList(){
front = null;
}
public void addToFront(int val){
front = new IntNode(val,front);
}
public void addToEnd(int val){
IntNode newnode = new IntNode(val,null);
if (front == null)
front = newnode;
else {
IntNode temp = front;
while (temp.next != null)
temp = temp.next;
temp.next = newnode;
}
}
public void removeFirst(){
if (front != null)
front = front.next;
}
public void print(){
System.out.println("---------------------");
System.out.print("List elements: ");
IntNode temp = front;
while (temp != null){
System.out.print(temp.val + " ");
temp = temp.next;
}
System.out.println("\n---------------------\n");
}
Page 19 of 23
Page 20 of 23
}
}
Page 21 of 23
Question 5: 10 marks
Write a Java program that can display the windows shown below. The size of the
frame is 200 by 150.
When user presses the button "Register", your program should display a dialog
window showing the contents of the name and email JTextFields. When user
presses the button "Clear", your program should clear the two JTextFields for
name and email.
You only need to handle the events for buttons.
For the button "Register", you should use inner class to handle the event. For the
button "Clear", you should use anonymous class to handle the event.
Sample Answers:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Register extends JFrame
implements ActionListener
{
JLabel name, email;
JTextField nameArea, emailArea;
JButton okButton, clearButton;
public Register()
{
super("register me!"); // 0.5 mark for calling super.
Container c = getContentPane(); //optional statement if you are using J2SE 5.0
c.setLayout(new FlowLayout()); // 0.5 mark for setting up layout manager
name = new JLabel( "Your Name:" );
c.add( name ); // 0.5 mark for setting up label 1 // add(name) if J2SE 5.0
Page 22 of 23
Page 23 of 23