Anda di halaman 1dari 20

Name:

______________________
Please fill in your Student Number and Name.
______________________
Student Number : __________________________________
Stude nt Number:

______________________

University of Cape Town ~ Department of Computer Science


Computer Science 1016S ~ 2017

January (Supp/DE) Examination

Question Max Internal External

1 12

2 16

3 7

4 10

5 10

6 12

7 8

8 25

TOTAL 100

Marks : 100
Time : 10 minutes reading + 120 minutes
Instructions:
a) Answer all questions.
b) Write your answers in PEN in the spaces provided.
c) You may use a calculator, BUT show all calculations where required.
Question 1 [12]
For each question below, write down ONE letter corresponding to the correct answer.
(a) An attribute that is shared by all objects of
the class is coded using ________. (d) Analyse the following code and select the
best answer.
a. an instance variable
public class Test {
b. a static variable
public static void
c. an instance method main(String[] args) {
d. a static method
System.out.println(xMethod(
______________________ 5, 500));
(b) Analyse the following code and select the }
best answer:
public static int
public class Test {
xMethod(int n, long l) {
private int t;
public static void System.out.println("int,
main(String[] args) { long");
Test test = new Test();
return n;
System.out.println(test.t);
} }
} public static long
xMethod(long n, long l) {
a. The variable t is not initialized and
therefore causes errors.
System.out.println("long,
b. The variable t is private and long");
therefore cannot be accessed in the
main method. return n;

c. Since t is an instance variable, it }


cannot appear in the static main }
method.
a. The program does not compile
d. The program compiles and runs fine. because the compiler cannot
______________________ distinguish which xmethod to
invoke.
(c) If a class named Student has no
b. The program runs fine but displays
constructors defined explicitly, the following
things other than 5.
constructor is implicitly provided.
c. The program displays long, long
a. public Student()
followed by 5.
b. protected Student()
d. The program displays int, long
c. private Student() followed by 5.
d. Student() ______________________
______________________

2
(e) What modifier should you use on a variable
so that it can only be referenced inside its
(h) The following lines of code will:
defining class?
int[] a = new int[3];
a. Public
a = new int[8];
b. private
c. protected a. Compile without errors
b. Throw an
d. Use the default modifier. ArrayIndexOutOfBoundsException
error
______________________
c. Not compile since it is attempting to
(f) When invoking a method with an object assign a larger array to an already
argument, ___________ is passed. smaller array
d. Throw an Array Dimension error
a. a copy of the object
b. the object is copied, then the ___________________________________
reference of the copied object
c. the reference of the object (i) When an array is returned by a method, it is
returned as a ______
d. the contents of the object
a. Clone of the array
b. Reference to the array
______________________ c. Length of the array
d. String of the array items
(g) Analyse the following code, and then select
the best answer option. ___________________________________
class Circle {
private double radius; (j) When creating a generic linked list, a type
parameter T is passed into the class
public Circle(double definition through < >. T is used because
radius) { a. It will not compile with a variable
radius = radius; other than T
}
b. It is an interface that needs to be
}
implemented
a. The program has a compile error c. It's a convention to use T for type
because you cannot assign radius to parameters
radius. d. All of the above
b. The program will compile, but you
cannot create an object of Circle ___________________________________
with a specified radius. The object
will always have radius 0. (k) Which one of the following is a Last In First
c. The program does not compile Out data structure?
because Circle does not have a a. Array
default constructor. b. Stack
d. The program has a compilation error c. Linked List
because it does not have a main d. Queue
method.
______________________ _________________________________

3
(l) When overriding the paint function for a
JFrame, the correct function name to
override is
a. draw()
b. paintComponent()
c. setBackground()
d. paint()

___________________________________

4
Question 2 [16]
(a) Explain what method overloading is and how Java knows which method to use when called. [3]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

(b) Study the class definitions below: [8]


public abstract class Plant {
public void smell() {
System.out.println(“No smell”);
}
}

public class Flower extends Plant {


public void smell() {
System.out.println(“I smell good!”);
}
}

public class Fruit extends Plant {


public void smell() {
System.out.println(“I smell yummy!”);
}
}

Give the output of each of the following code snippets. If there is no output due to error/s,
state whether the code:
a) fails to compile,
OR
b) compiles, but result in an error at runtime.

i. Flower rose = new Flower();


rose.smell();
_________________________________________________________________________
_________________________________________________________________________

5
ii. Fruit orange = new Fruit();
orange.smell();
_________________________________________________________________________
_________________________________________________________________________
iii. Plant oakTree = new Plant();
oakTree.smell();
_________________________________________________________________________
_________________________________________________________________________
iv. Plant violet = new Flower();
Flower myViolet = violet;
myViolet.smell();
_________________________________________________________________________
_________________________________________________________________________

(c) What is wrong in the following program? Show how one would correct it. [2]
1 public class Test {
2 public static void main(String[] args) {
3 nPrintln("Welcome to Java!", 5);
4 }
5
6 public static void nPrintln(String message, int n) {
7 int n = 1;
8 for (int i = 0; i < n; i++)
9 System.out.println(message);
10 }
11}

_________________________________________________________________________
_________________________________________________________________________

(d) Explain, using appropriate examples if necessary, what encapsulation means. [3]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

6
Question 3 [7]

Consider the following scenario.


A private dental practice wishes to computerize its patient records system. A patient must register
with the practice and the system needs to store their name, address and mobile telephone number.
Each patient is given a unique seven digit patient number.
The system will keep a count of how many patients the practice currently has. Patients can book an
appointment with a particular dentist; the system needs to store the date of the appointment and if
the patient attended. A text message will be automatically sent out two working days before the
appointment. After the appointment the dentist updates the system with the cost of the treatment
undertaken.
The practice employs two types of staff: Receptionists and Dentists. The system needs to record
their details; which for all staff includes a four digit employee number, their name, address, gender,
contact telephone number and next of kin. Dentists must be qualified; the system will store their
highest dental qualification, date awarded and their General Dental Council registration number.
A list of appointment statistics is required at the end of each week. This will be a summary of how
many patients turned up and how many were no-shows. If a patient repeatedly misses an
appointment they will be charged a fixed amount of money.
All receptionists must go on a first aid course every year. The system must record the date of when
they last attended the course and the name of the course provider.

Draw a UML class diagram showing the classes with their attributes you would use, the
relationships between them, and the appropriate multiplicity constraints. Do not show any
methods. [7]

7
Question 4 [10]

(a) Consider the following UML class diagram, showing part of a program to manage the
membership information for a professional society:

i. Write a Java version of the UML class ManagementCttee assuming it has this constructor:
public ManagementCttee()
The management committee can have a maximum of 6 members. [4]

_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

8
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

ii. Explain the relationship between the class Member and the classes SeniorMember and
StandardMember. [2]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

iii. Write a Java version of class Member assuming it has this constructor:
public Member(String name, String address)
and that the method getFee() is abstract. [4]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

9
Question 5 [10]

(a) Consider the interface Packable below. Assume Item is some pre-defined class.
public interface Packable {
public boolean addItem(Item x);
public Item removeItem(Item x);
}

The following class is supposed to be a subclass of some pre-defined class Bag, and implements the
Packable interface. The … in line 17 is not an error, it just indicates some irrelevant code goes
there that is not shown.

1. public class Backpack inherits Bag implements Packable


2. {
3. int maxItems;
4. int num;
5.
6. public Backpack(int capacity)
7. {
8. this.maxItems = capacity;
9. this.num = 0;
10. }
11.
12. public boolean addItem(Item x)
13. {
14. if (this.num < this.capacity)
15. {
16. // Add the item somehow
17. ...
18. this.num++;
19. return true;
20. }
21. return false;
22. }
23. }

There are three errors in the Backpack class. Identify them and say how they can be fixed.
[6]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

10
________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

(b) Discuss what an inner class is and some of its uses. [4]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

11
Question 6 – Linear Data Structures [12]
Consider the code listed in Appendix A and answer the questions that follow.

(a) Explain what the purpose of the variable labelled //1 is and the data that is stored in it. [1]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

(b) The method isEmpty() is part of the linked list class and returns a boolean indicating if there
are nodes in the list. Write code that will complete this method. [1]
_________________________________________________________________________

(c) The addHere() method in the iterator class is supposed to add a new data item at the current
iterator position. Write the code that would correctly implement this functionality, considering
all cases that need to be checked when making the insertion. [6]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

12
(d) Explain how a linked list can be made into a stack data structure; i.e.: what constraints must be
placed on a linked list such that it will only behave like a stack? [1]
_________________________________________________________________________
_________________________________________________________________________

(e) Explain how a node is deleted from a linked list making specific reference to the way in which
Java manages memory. [3]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

13
Question 7 – Graphical User Interfaces [8]
Consider the code listed in Appendix B and answer the questions that follow.

(a) Draw the GUI that is displayed when this program runs. [3]

(b) For the line labelled //1 explain the relationship it has with the line labelled //2 as well as the
effect of the line labelled //2, i.e. what happens when the closed button is clicked on the
window. [2]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

14
(c) When clicking on either of the buttons, they both perform the same action.

i. Explain why this is happening. [1]


_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
ii. Suggest two ways this can be avoided without having to create a separate action listener for
each button. [2]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

15
Question 8 [25] – Social Issues and Professional Practice

(a) What does Big Brother refer to in the context of computing? [2]
a. It refers to digital assistants, such as the Alexa device, that help the user to carry out
tasks, such as scheduling appointments.
b. It is a concept that refers to the abuse of power in the sphere of civil liberties and mass
surveillance, greatly facilitated by ICT.
c. It is the informal name of the algorithm that re-ranks search engine results based on
one's prior behaviour on the Web so as to personalise search results.
_________________________________________________________________________

(b) What can one do with free software, such as those softwares licensed under GNU GPL? [2]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

(c) What are the three innate conditions for moral agency? [1]
_________________________________________________________________________
_________________________________________________________________________

(d) Is computer ethics somehow different from all the other ethics that came before? Justify your
answer briefly. [2]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

(e) South Africa uses several general information protection principles that formed the basis for the
POPIA act on the protection of personal information. Consider the following list. Which one is,
or which ones are, part of those general principles? [3]
a. Apply reasonable security measures to protect information.
b. No subject (e.g., you) has the so-called “right to be forgotten”.
c. Collect only information needed for a specific purpose.
d. Information about a subject (e.g., you) may be stored indefinitely.
e. If information is used for direct marketing, the data subject (e.g., you) has to give their
consent or be a customer.
f. Information collected by any organisation in South Africa may be transferred freely
across its borders.

16
_________________________________________________________________________

(f) Consider the following argument:


Premise 1: Most computer science professors assert that object-oriented programming
languages are better than all other classes of programming languages.
Premise 2: Java is an object-oriented programming language.
Conclusion: Java is better than the functional programming language Haskell.

Assuming the premises are true, analyse the validity and strength of the argument. [7]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

(g) Khadisha is convinced that hacking is wrong. She claims it is wrong by using the analogy of the
unlawfulness of burglaries of locked homes where thieves take the owners' TV sets and such:
hacking is like stealing private, secured, protected, data, where the data is deemed property of
the organisation.
Do you agree with this argument? If so, justify why this is a good argument. If not, describe
what you deem is wrong with the argument and why. In answering this question, you may want
to consider one or more moral theories and the type of argument that is being made. [8]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

17
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

18
Appendix A

public class LinkedList {


Node head; //1

public boolean isEmty() {


//To be completed
}

public void addToStart(char itemData) {


head = new Node(itemData, head);
}

private class Node {


char item;
Node link;
public Node(char item, Node link) {
this.item = item;
this.link = link;
}
}

private class ListIterator {


private Node position;
private Node previous;
public ListIterator() {
position = head;
previous = null;
}

public char next() {


if (!hasNext()) {
throw new NoSuchElementException();
}
char toReturn = position.item;
previous = position;
position = position.link;
return toReturn;
}
public boolean hasNext() {
return (position != null);
}
public void addHere(char newData) {
//To be completed
}
}
}

19
Appendix B

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Exam extends JFrame implements ActionListener {


private JTextField text;
private JLabel label;
public Exam() {
super("Exam Supp");
setSize(500, 200);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//1
addWindowListener(new ExamWindowListener()); //2
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 0));
text = new JTextField("Enter text here");
add(text, BorderLayout.CENTER);
JButton b1 = new JButton("Do Something");
b1.addActionListener(this);
JButton b2 = new JButton("Do Something");
b2.addActionListener(this);
panel.add(b1);
panel.add(b2);
add(panel, BorderLayout.SOUTH);
label = new JLabel("Label Display");
add(label, BorderLayout.NORTH);
}
private class ExamWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();
if (a.equals("Do Something")) {
label.setText(text.getText());
} else {
text.setText("");
}
}
public static void main(String[] args) {
Exam e = new Exam();
e.setVisible(true);
}
}

20

Anda mungkin juga menyukai