Sie sind auf Seite 1von 8

Maastricht University

Faculty of Humanities and Science


Knowledge Engineering Study

EXAMINATION
Introduction to Computer Science 2
Block 1.2: Introduction to Computer Science 2
Code: 10060
Examiner: E.N. Smirnov
Date: December 19, 2011
Time: 14.00 - 17.00 uur
Place: GG90-92, room A.0 (Turnzaal), Grote Gracht 90-92
(Faculty of Arts and Social Sciences)

Exam Notes:
1. The exam is an open-book exam.
2. The only book that can be used during the exam is “Java Concepts”, by C.
Horstmann, 6rd Edition, John Wiley, 2011.
3. The lecture slides can be used during the exam.
4. The exam consists of 10 pages (including this page).
5. The exam time is 3 hours (180 minutes).
6. The number of exam questions is 19.
7. The exam questions are separated in sections according to the course topics.
8. The number of points you can maximally receive for each question is given (in
bold).
9. The total number of points if all the questions are answered correctly is 100.
10. The final exam grade is the sum of the points of the questions answered correctly
divided by 10.
11. Before answering the questions, please first read all the exam questions, and then
make a plan to spend the three hours.
12. When answer the questions please do not forget:
 to write your name and student number on each answer page;
 to number the answers; and
 to number the answer pages.

1
Lecture 1: Objects and Classes

1. (3 points) Consider the class A given below:

public class A
{
public A()
{ x = 1;
System.out.println("A1");
}

public A(int n)
{ x = n;
System.out.println("A2");
}

public A(double n)
{ x = n;
System.out.println("A3");
}

public static void main(String[] args)


{ System.out.println( new A(3.0).x);
}

public double x = 5;
}

What will be printed when the class A will be executed? Explain why.

2. (3 points) After the following statements have been executed, how many
BankAccount objects will exist, not counting garbage objects?

BankAccount account1 = new BankAccount();


BankAccount account2 = new BankAccount();
account2 = new BankAccount();
BankAccount account3 = account1;

account2 = account3;
account1 = null;

2
Lecture 2: Interfaces

3. (4 points) The following interface A contains four syntax errors. Find all four
errors and repair them. (To indicate an error, please write the line, highlight the
error, and explain the error. Next to your explanation, please write your
correction. For example:
a[i] = a[j]; error: the index j is not defined; correction: define and
initialize j before the outer loop.)
public interface A
{ public final void getA();
private void getB();
public void getC();

private double f = 1;
public double d = 2;
protected double e = 3;
}

4. (5 points) Consider the code given below. Please explain (1) how may classes
you have found in the code; (2) whether the class Y can be compiled; and (3) what
will be printed by the main method.

public interface X
{ public int getX(); }

public class Y
{ public static void main(String[] args)
{
X x = new X()
{
public int getX() { return x;}

private int x;
};

System.out.println(x.getX());
}
}

3
Lecture 3: Inheritance

5. (1 point) (True/False) In Java there is a restriction on the number of interfaces


that a class can implement. F

6. (1 point) (True/False) In Java there is no restriction on the number of classes that


a class can extend directly. F

7. (1 point) (True/False) In Java a polymorphic method can be implemented using


interfaces. T

8. (1 point) (True/False) In Java a polymorphic method can be implemented using


abstract classes. T

9. (5 points) Consider the classes G and H given below. Please provide answers to
the following questions:

(a) Which of the methods of class H override methods of class G? m1


(b) Which of the methods of class G are inherited in class H? m2 m3
(c) Which of the methods of class H are new with respect to methods in class
m4
G?
(d) Which variable in class H overshadows a variable in class G? v1
(e) Which variable in class G is directly accessible in class H? v2

public class G
{ public void m1(){}
public void m2(){}
public void m3(){}

private int v1;


protected int v2;
}

public class H extends G


{ public void m1(){}
public void m4(){}

private int v1;


protected int v3;
}

4
10. (5 points) What does the main method print? Explain your answer.

public class a
{ protected double x;

public a(double y){ x = y;}


}

public class b extends a


{ public b(double y){ super(y);}

public b(){ this(0); }

public static void main(String[] args)


{ System.out.println(new b().x); }
}

Lecture 4: Programming Graphics

11. (16 points) Write a graphical application or an applet that first asks for an
integer number n and then draws n number of circles so that the first circle
has radius 1, the second circle has radius 2, … and the last n-th circle has a radius
n.

Example for n = 2.

5
Lecture 5: Event Handling

12. (2 points) The MouseAdapter class implements all listener methods as:
(A) empty methods;
(B) action methods;
(C) class methods;
(D) private methods.

13. (10 points) Write a GUI application with one button. Each time the button is
clicked the application has to print in a console window how many times the
button was clicked so far.

Lecture 6: GUI Interfaces

14. (2 point) (True/False) We use inheritance to customize frames in order to draw on


the panes of frames. T

15. (2 points) Before showing the container with radio buttons, you should call:
(A) setSelected(true) on one radio button;
(B) setSelected(false) on all radio buttons;
(C) setOn() on one button;
(D) setEnabled(true) on one radio button;
(E) setEnabled(false) on all radio buttons.

16. (2 points) What type of layout do you have to use so that the individual
components stay at their original size? flowLayout

17. (9 points) Consider the program represented by a class MiniConcatinator.


The program draws a JFrame window with three text labels, three text fields,
and a button “Concatenate”. The first two fields are the fields for two strings that
a user can enter. The third field is the field of the result. When the user pushes the
button the program concatenates the strings entered in the first two fields and
provides the concatenated string in the third field. (see next page)

6
class MiniConcatinator
{
public static void main(String[] args)
{ JFrame frame = new JFrame("MiniConcatinator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();

final JLabel label1 = new JLabel("Enter a string");


final JLabel label2 = new JLabel("Enter a string");
final JLabel resultLabel = new JLabel("Result");
final JTextField firstString = new JTextField(20);
final JTextField secondString = new JTextField(20);
final JTextField result = new JTextField(20);
JButton addButton = new JButton("Concatinate");

(A)………………

class Concatinator implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
(B)………………
}
}

(C)………………

frame.setContentPane(pane);
frame.setVisible(true);
}
}

For the purposes of this exam the class MiniConcatinator is not complete in
points (A), (B), and (C). You have to complete the code in these points. More
precisely,

7
 In point (A) you have to add code that groups the three text labels, three text
fields, and button “Concatinate” in the way that is shown in the picture of the
window drawn by the program.
 In point (B) you have to add the code of the method actionPerformed
of the listener class Concatinator;
 In point (C) you have to create an object of the listener class
Concatinator and then to install it properly.

Exceptions and Streams

18. (7 points) What output will be produced by the following code?

int n;
try
{ n = 42;
if (n > 0)
throw new Exception();
else if (n < 0)
throw new NumberFormatException();
else
System.out.println(“Bingo”);
}
catch (NumberFormatException e)
{ System.out.println(“First catch”); }

catch (Exception e)
{ System.out.println(“Second catch”); }

finally
{ System.out.println(“Finalising”); }

System.out.println(“End of excercise”);

19. (12 points) Write a method readBankAccountsStream that reads


BankAccount objects from an object-stream file and returns an ArrayList
object of these BankAccount objects. The method has to have one String
parameter for the name of the file. The method has to use the class
ObjectInputStream. For the purpose of this exam we assume that the
method readObject() of the class ObjectInputStream returns null
when the end of the file has been reached.

Das könnte Ihnen auch gefallen