AS/AK/ITEC
1630 3.0 – All Sections
OBJECT-ORIENTED
PROGRAMMING
Midterm
Test
Duration:
90 Minutes
This exam is closed textbook(s) and closed
notes. Use of any electronic
device
(e.g., for computing and/or communicating) is NOT permitted.
Do not un-staple this test book – any
detached sheets will be discarded.
Answer all questions in the space provided.
No additional sheets are
permitted.
(Note: a blank page has been provided at the back of this test
book
that you may use for rough work; do not detach it)
Work independently. The value of each
question is indicated. The total
value
of all questions is 100.
Write your section letter, your name and
student number in the space
below.
NOTE:
YOU MAY USE PEN OR PENCIL, BUT ANSWERS IN PENCIL WILL
NOT
BE CONSIDERED FOR RE-GRADING.
Section
(A/B): _____________________________________
Surname:
_________________________________________
Given
Names: _____________________________________
Student
Number: ___________________________________
A (15)
B (15) C (30) D(40) Total (100)
_____________________________________________________________________
Part A (15
points)
Consider the following (partial) API
of two classes:
class Department
Department(String
name, int budget)
Parameters:
name - name of the department
budget - budget of this department
void assign(Employee
who)
Parameter:
who - the employee to be assigned to this department
int
getHeadCount()
Returns:
the number of employees in this department
int
getBudget()
Returns:
the budget of this
department
void changeBudget(int delta)
Increase or decrease the budget of this department by the passed
amount. Delta is the increment or decrement, not
the new budget.
Parameter:
delta - change the budget by adding this amount to it (to
reduce budget,
provide
a negative amount).
class Employee
Employee(String name,int
rank)
Parameters:
name - name of the employee
rank - the rank (level) of the employee
Develop the Java application App whose
main method performs the following tasks, in the order shown:
1. Create
a department called "R&D" with budget 2,000,000.
2. Create an employee John whose
rank is 3.
3. Create an employee Debbie whose
rank is 2.
4. Assign both John and Debbie to
the R&D department
5. Determine the head count of the
R&D department by using a method, and store it in
some variable count.
6. If count is greater than 10, increase the department budget by 5%,
otherwise reduce
it by 2%.
Note
that it is OK to use the above magic numbers –no need to store them as
constants.
public class App{
public static void
main(String[] args){
Part B (15
marks)
Write the 10-lines output of the program Bird.java shown below.
interface Silly {
public void narf();
public void poit(Silly s);
}
public class Bird implements Silly
{
public static void
main(String args[]) {
System.out.println("zero");
Silly s = new SillyBird(1);
Silly s2 = new Loony();
s.poit(s2);
s2.poit(s);
System.out.println("zymurgy");
}
public Bird() {
this(0);
System.out.println("zircon");
}
public Bird(int i) {
System.out.println("
}
public void narf() {
System.out.println("zort");
}
public void poit(Silly s) {
s.narf();
}
}
class SillyBird
extends Bird {
public SillyBird() {
System.out.println("duchess");
}
public SillyBird(int i)
{
super(i);
}
public void narf() {
System.out.println("drum");
super.narf();
}
}
class Loony extends SillyBird {
public Loony() {
System.out.println("stupendous");
}
public void narf() {
System.out.println("snark");
}
}
PART C (30 marks)
Multiple-Choice
Questions.
Circle the correct answers. Each question has only one correct answer unless
specifically stated otherwise. Each
question is worth 2 marks.
1) If a subclass does not explicitly
call one of the superclass constructors, the default superclass constructor is called automatically.
a) TRUE
b) FALSE
2) Components in a GridLayout format can have different sizes.
a) TRUE
b) FALSE
3) Which 2 statements are true about
an abstract class ?
(circle both)
a). It can not be instantiated.
b). It can not be extended.
c). It can not be declared public.
d). It will have an abstract method.
e). It can not be overridden.
4) Given a JLabel
label and a JPanel panel with border layout, which of
the
following statements would you use to place
the label at the top of the panel?
a). panel.add(label,
NORTH);
b). label.add(panel, NORTH);
c). panel.add(label, BorderLayout.NORTH);
d). panel.add(label, BorderLayout.TOP);
e). panel.add(label, TOP);
5) Which is the default LayoutManager for a JFrame ?
a) GridLayout
b) FlowLayout
c) CardLayout
d) BorderLayout
e) there is
no default LayoutManager for a JFrame
6) In using the GridLayout,
adding space between components in a container can be accomplished best by:
a) including
JButtons that have no names and thus will appear
invisible.
b) adding
invisible components.
c) inserting
IconImages of blank images.
d) specifying
the spaces in pixels through the GridLayout
constructor
7) In calling the method actionPerformed (ActionEvent
event) event
is:
a) an interface.
b) a GUI
component.
c) an
object.
d) a
listener.
e) a
reference to an existing object.
8) In order to display three
components vertically in a container, you could use which 2 layout managers ? (circle both)
a) CardLayout
b) GridLayout
c) BorderLayout
d) FlowLayout
e) You could use any of the above.
For the next 3 questions, consider
the following class definition:
public class AClass
{
protected int x;
protected int y;
public AClass(int a, int
b){
x = a;
y = b;
}
public String toString( ){
return “x=” +
(++x) + “\ny=” + (y++) ;
}
}
9) Consider that you want to extend AClass to BClass. BClass will have a third instance data, int
z. Which of the following would best define BClass'
constructor?
a) public
BClass(int a, int b, int c){
super(a, b, c);
}
b) public
BClass(int a, int b, int c){
x = a;
y = b;
z = c;
}
c) public
BClass(int a, int b, int c){
z = c;
}
d) public
BClass(int a, int b, int c){
super(a, b);
z
= c;
}
e) public
BClass(int a, int b, int c){
super( );
}
10) If the driver has the lines:
BClass o = new BClass (1,2,3);
System.out.println(o);
the first line of the output is:
a) x=1
b) x=1 \n
y=2
c) x=2 \n
y=3
d) x=2
e) nothing
11) For the same driver the second
line of output is:
a) y=1
b) x=1 \n
y=2
c) y=2
d) y=3
e) nothing
For the next 4 questions, use the
following partial class definitions:
public class A{
public int x;
private int y;
protected int z;
...
}
public class B extends A {
protected int a;
private int b;
...
}
public class C extends A{
private int q;
...
}
12) Which of the following is true
with respect to classes A, B, and C ?
a) C is a subclass of B, and B is a
subclass of A
b) C and B are both subclasses of A
c) A and B are both subclasses of C
d) A is a subclass of B and C
13) Which of the following lists of
instance variables are accessible in class B ?
a) x, y, z,
a, b
b) x, y, z,
a, b, q
c) x, y, z,
b
d) x, z, a,
b
e) x, z, a,
q
14) Which of the following lists of
instance data are accessible in class C ?
a) a, q
b) x, y, z,
a, q
c) x, z, q
d) z, a, q
e) x, y, z,
q
15) If you change the declaration of
class C to be “public class C extends B” which of the following lists of
instance variables are accessible in class C ?
a) x, y, z,
a, b
b) x, y, z,
a, b, q
c) x, y, z,
b
d) x, z, a,
b
e) x, z, a,
q
PART D (40 marks)
Write the Java applet and the html
file corresponding to the GUI shown below.
The user starts by entering the
results of a large number of students. For each student the user enters the name
and the mark in the two textfields and then clicks on
the “Enter student data” button. After finishing the data entry the user clicks
on the other two buttons to produce the class average (truncated as an integer)
and the name of the student with the highest mark.