THE UNIVERSITY OF AUCKLAND

VERSION 00000001 COM PSCI 230 VERSION 00000001 THE UNIVERSITY OF AUCKLAND -2- COM PSCI 230 S ECTION A MULTIPLE CHOIC E QUES TIONS S ECOND S EME...
Author: Darrell Sparks
26 downloads 2 Views 95KB Size
VERSION 00000001

COM PSCI 230

VERSION 00000001

THE UNIVERSITY OF AUCKLAND

-2-

COM PSCI 230

S ECTION A MULTIPLE CHOIC E QUES TIONS

S ECOND S EMES TER, 2010 Campus: City Question 1 [1.5 marks] If a subclass constructor does not explicitly call a superclass constructor, Computer S cience

(a) it must include the code necessary to initialize the superclass fields. (b) Java will automatically call the superclass's default or no-arg constructor immediately after the code in the subclass's constructor executes. (c) the superclass fields will be set to the default values for their data types. (d) Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes.

TEST S oftware Design and Construction (Time Allowed: 50 MINUTES ) Note:  The use of calculators is NOT permitted.  Compare the exam version number on the Teleform sheet supplied with the version number above. If they do not match, ask the exam supervisor for a new sheet.  Enter your name and student ID on the Teleform sheet. Your name should be entered left aligned. If you name is longer than the number of boxes provided, truncate it.  Answer Section A of Multiple-choice questions on the Teleform answer sheet provided. Answer Section B in the space provided in this booklet. Attempt all questions.  Use a dark pencil to mark your answers in the multiple choice answer boxes on the Teleform sheet. Check that the question number on the sheet corresponds to the question number in this question/answer book. If you spoil your sheet, ask the supervisor for a replacement.  Write your answers in the space provided in the short answer section. Write as clearly as possible. The space provided will generally be sufficient but is not necessarily an indication of the expected length. Extra space is provided at the end of this exam book.  An appendix with a simplified API is included on the last page. You may detach this appendix.

Question 2 [1 mark] The method that must be implemented in an Integer class using the Comparable interface is: (a) (b) (c) (d)

Question 3 [1 mark] A class that has at least one abstract method is called a(n): (a) (b) (c) (d)

abstract class. private class. encapsulated class. concrete class.

Question 4 [1 mark] A class with no abstract methods is called a(n) (a) (b) (c) (d)

Surname: First Name(s): Student ID: Login Name(UPI):

public Boolean compareTo(Object other) public int compareTo(Object other) public Object compareTo(Object other) public int compareTo(Integer other)

encapsulated class. private class. abstract class. concrete class.

Question 5 [1 mark] A class that implements an interface but only gives definitions for some of the method headings given in the interface is called a(n): (a) (b) (c) (d)

MARKERS ONLY Section

Marks

Of

A

38

B

12

Total

50

CONTINUED

abstract class. discrete class. friend class. concrete class.

CONTINUED

VERSION 00000001

-3-

COM PSCI 230

the method must be overridden in subclasses. you cannot create an instance of the class. the method will have only a header, but not a body, and end with a semicolon. All of the above.

Question 7 [1 mark] Which of the following is not true of anonymous inner classes? (a) (b) (c) (d)

They are declared without a name. They typically appear inside a method declaration. They can access their top-level class’s members. They are declared with the anonymous keyword.

class Base { protected int x; protected static int y=0; public Base() { this.x = ++y; } public Base(int x) { this.x = ++y + x; } public String toString() { return "x=" + x + ", y=" + y; } }

public Derived() {}

showM essageDialog inputDialog getInput showInputDialog

public Derived(int x, int z) { super(x); this.z = this.z + z; } public static void main(String[] args) { Base b1 = new Base(); Derived d1 = new Derived(); Derived d2 = new Derived(10, 20); System.out.println(b1); System.out.println(d1.x); System.out.println(d1.z); System.out.println(d2.x); System.out.println(d2.z);

Question 9 [1 mark] What will be the results of executing the following code, if the user simply clicks OK? JPanel panel = new JPanel(); Color selectedColor; selectedColor = JColorChooser.showDialog(null,"Select color", Color.blue); panel.setForeground(selectedColor);

(a) (b) (c) (d)

//at //at //at //at //at

line line line line line

30 31 32 33 34

} }

Question 11 [1.5 marks] What will be printed at line 30 after executing the above main method?

The foreground color will be set to blue. The foreground color will be set to black. The foreground color will be set to white. The foreground color will remain unchanged.

Question 10 [1 mark] Which of the following methods can be used to set the layout manager of a container? (a) (b) (c) (d)

COM PSCI 230

public class Derived extends Base { protected int z = 10;

Question 8 [1 mark] Which of the following is the method used to display a dialog box to gather input? (a) (b) (c) (d)

-4-

The following FIVE questions relates to the classes given below:

Question 6 [1 mark] If a class contains an abstract method, (a) (b) (c) (d)

VERSION 00000001

setLayoutM anager setM anager setLayout setGUILayout

(a) (b) (c) (d)

Question 12 [1.5 marks] What will be printed at line 31 after executing the above main method? (a) (b) (c) (d)

CONTINUED

x=0, y=3 x=1, y=1 x=0, y=1 x=1, y=3

0 2 10 1

CONTINUED

VERSION 00000001

-5-

COM PSCI 230

Question 13 [1.5 marks] What will be printed at line 32 after executing the above main method? (a) (b) (c) (d)

3 13 12 10

COM PSCI 230

Question 16 [1.5 marks] What is the result of attempting to compile and run the following code fragment?

(a) (b) (c) (d)

x=0 x=10, z=0 x=10 x=10, z=20

Question 17 [1.5 marks] What is the result of attempting to compile and run the following code fragment? Base1 b2 = new Derived1(100, 50); System.out.println(b2);

Question 15 [1.5 marks] What will be printed at line 34 after executing the above main method? (a) (b) (c) (d)

-6-

Base1 b1 = new Derived1(); System.out.println(b1);

0 1 2 10

Question 14 [1.5 marks] What will be printed at line 33 after executing the above main method? (a) (b) (c) (d)

VERSION 00000001

30 1 10 20

(a) (b) (c) (d)

x=100, z=50 x=100, z=200 x=100, z=250 x=100

The following SEVEN questions relates to the classes given below: interface I {} interface J {} interface K {} class A implements I, J { public void m() { System.out.println("A"); } }

The following TWO questions relates to the classes given below: class Base1 { int x; public Base1() { x = 10; } public Base1(int x) { this.x =x; }

class B extends A { public void m() { }

System.out.println("B"); }

public String toString() { return "x=" + x ; }

class C extends B { public void m() { }

System.out.println("C"); }

}

class D extends A implements K { public void m() { System.out.println("D"); } }

class Derived1 extends Base1 { int z = x * 2; public Derived1() {}

public class TestABC { public static void main(String[] args) { A a1 = new A(); B b1 = new C(); A a2 = new C(); } }

public Derived1(int x, int z) { super(x); this.z = this.z + z; } public String toString() { return "x=" + x + ", z=" + z; } }

CONTINUED

CONTINUED

VERSION 00000001

-7-

COM PSCI 230

Question 18 [1 mark] What is the result of attempting to compile and run the following statement? System.out.println("a1 instanceof A=" + (a1 instanceof I)); (a) (b) (c) (d)

Question 20 [1.5 marks] What is the result of attempting to compile and run the following statement? System.out.println("a2 instanceof K=" + (a2 instanceof K)); (a) (b) (c) (d)

a2 instanceof K=false Run-time Error a2 instanceof K=true Compile-time Error

Question 21 [1.5 marks] What is the result of attempting to compile and run the following statement? System.out.println("a2 instanceof I=" + (a2 instanceof I)); (a) (b) (c) (d)

a2 instanceof I=true a2 instanceof I=false Run-time Error Compile-time Error

Question 22 [1.5 marks] What is the result of attempting to compile and run the following statement? ((D) a2).m();

(a) (b) (c) (d)

D Run-time Error C Compile-time Error

A C Compile-time Error Run-time Error

Question 24 [1.5 marks] What is the result of attempting to compile and run the following statement? ((B) a1).m();

Compile-time Error A Run-time Error B

The following THREE questions relates to the classes given below: class A2 { public void m1(A2 a) { System.out.println("A2"); } } class B2 extends A2 { public void m1(B2 b) { System.out.println("B2"); } } class D2 { public static void main(String[] args) { A2 x = new A2(); A2 y = new B2(); B2 z = new B2(); } }

Question 25 [1.5 marks] What is the result of attempting to compile and run the following statement? y.m1(x); (a) (b) (c) (d)

CONTINUED

COM PSCI 230

((C) a2).m();

(a) (b) (c) (d)

b1 instanceof D=true Compile-time Error Run-time Error b1 instanceof D=false

-8-

Question 23 [1.5 marks] What is the result of attempting to compile and run the following statement? (a) (b) (c) (d)

Compile-time Error a1 instanceof A=true Run-time Error a1 instanceof A=false

Question 19 [1 mark] What is the result of attempting to compile and run the following statement? System.out.println("b1 instanceof D=" + (b1 instanceof D)); (a) (b) (c) (d)

VERSION 00000001

A2 Run-time Error B2 Compile-time Error

CONTINUED

VERSION 00000001

-9-

COM PSCI 230

VERSION 00000001

- 10 -

COM PSCI 230

Question 26 [1.5 marks] What is the result of attempting to compile and run the following statement?

Question 28 [1.5 marks] What is the result of attempting to compile and run the following statement?

z.m1(y);

c.m();

(a) (b) (c) (d)

A2 Compile-time Error B2 Run-time Error

(a) (b) (c) (d)

Run-time Error A1 C1 B1

Question 27 [1.5 marks] What is the result of attempting to compile and run the following statement?

Question 29 [1.5 marks] What is the result of attempting to compile and run the following statement?

z.m1(z);

a.m();

(a) (b) (c) (d)

Run-time Error Compile-time Error A2 B2

(a) (b) (c) (d)

B1 Run-time Error A1 C1

QuestionNumber  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29 

The following TWO questions relates to the classes given below: class A1 { public static void m() { System.out.println("A1"); } public void i() { System.out.println("A1.i"); } } class B1 extends A1 { public static void m() { System.out.println("B1"); } } class C1 extends B1 { public static void m() { System.out.println("C1"); } public void i() { System.out.println("C1.i"); } } class D1 { public static void main(String[] args) { C1 c = new C1(); A1 a = c; // ... } }

CONTINUED

Answer  D  B  A  D  A  D  D  D  A  C  D  B  D  B  A  D  C  B  B  A  A  B  B  C  A  A  D  C  C 

Mark  1.5  1  1  1  1  1  1  1  1  1  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1  1  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5 

CONTINUED

VERSION 00000001 Question/Answer Sheet

- 11 -

COM PSCI 230 ID ……….…………

VERSION 00000001 Question/Answer Sheet

Answer all questions in this section in the space provided. If you run out of space then please use the Overflow Sheet and indicate in the allotted space that you have used the Overflow Sheet.

Given the following classes:

- 12 -

COM PSCI 230 ID ……….…………

S ECTION B

Question 30: Assignment 1

[12 marks]

In assignment 1, you were asked to add several changes to the program which displays moving shapes "bouncing" off the edg es of the window. Complete the MovingFace class which draws a smiley face on the window. The mouth of the face is a semi-circle and located in the bottom part of the bounded rectangle. The two eyes are two circl es/ellipses and located in the upper part of the bounded rectangl e. They are placed at the centre of the two regions in the upper part.

public MovingShape() { this(0, 0, 20, 20, 500, 500, Color.blue, Color.black, 0); }

Some examples are given below:

width/8

import java.awt.*; public abstract class MovingShape { public int marginWidth, marginHeight; // the margin of the animation protected Point p; // the top left corner of the shapes protected int width, height; // the width and height of the shapes protected Color fillColor; // the fill colour of the shapes protected MovingPath path; // the moving path of the shapes protected boolean selected = false; // draw handles if selected protected Color borderColor; // the border colour of the shapes

5*width/8

Point p (x,y) – top left corner

Eye (dimension) height/4 * width/4

height

height/2 width

fillColor: blue

public MovingShape(int x, int y, int w, int h, int mw, int mh, Color fc, Color bc,int pathType) { p = new Point(x,y); marginWidth = mw; marginHeight = mh; fillColor = fc; borderColor = bc; width = w; height = h; setPath (pathType); } public abstract boolean contains(Point p); public abstract void draw(Graphics g); public int getX() { return p.x; } public int getY() { return p.y;} public boolean isSelected() { return selected; } public void setSelected(boolean s) { selected = s; } public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } public void drawHandles(Graphics g) { // if the shape is selected, then draw the handles if (isSelected()) { g.setColor(Color.black); g.fillRect(p.x -2, p.y-2, 4, 4); g.fillRect(p.x + width -2, p.y + height -2, 4, 4); g.fillRect(p.x -2, p.y + height -2, 4, 4); g.fillRect(p.x + width -2, p.y-2, 4, 4); } } public void setFillColor(Color c) { fillColor = c; } public void setBorderColor(Color c) { borderColor =c; } public void move() { path.move(); } ... }

width/2

CONTINUED

CONTINUED

VERSION 00000001 Question/Answer Sheet

- 13 -

COM PSCI 230 ID ……….…………

import java.awt.*; public class MovingRectangle extends MovingShape { public MovingRectangle() { super(); } public MovingRectangle(int x, int y, int w, int h, int mw, int mh, Color fc, Color bc, int typeOfPath) { super(x,y,w,h,mw,mh,fc,bc, typeOfPath); }

- 14 -

COM PSCI 230 ID ……….…………

Complete the class MovingFace below. Do not duplicate code unnecessarily.

import java.awt.geom.Arc2D; import java.awt.*; public class MovingFace extends MovingShape { // 1 mark public MovingFace() { super(); } public MovingFace(int x, int y, int w, int h, int mw, int mh, Color fc, Color bc, int typeOfPath) { super(x,y,w,h,mw,mh,fc,bc, typeOfPath); }

public void draw(Graphics g) { g.setColor(fillColor); g.fillRect(p.x, p.y, width, height); g.setColor(borderColor); g.drawRect(p.x, p.y, width, height); drawHandles(g); } public boolean contains(Point mousePt) { return (p.x

Suggest Documents