______________________
Please fill in your Student Number and Name.
______________________
Student Number : __________________________________
Stude nt Number:
______________________
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;
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]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
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.
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]
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.
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.
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
_________________________________________________________________________
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
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;
20