Beruflich Dokumente
Kultur Dokumente
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
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 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?
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
9. (5 points) Consider the classes G and H given below. Please provide answers to
the following questions:
public class G
{ public void m1(){}
public void m2(){}
public void m3(){}
4
10. (5 points) What does the main method print? Explain your answer.
public class a
{ protected double x;
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.
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
6
class MiniConcatinator
{
public static void main(String[] args)
{ JFrame frame = new JFrame("MiniConcatinator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
(A)………………
(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.
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”);