CSC1016S 2022 Exam
CSC1016S 2022 Exam
in the top right corner. Then fold over the top right corner. __________________
_______________________ _________________
November Examination
1 3
2 4
3 10
4 3
5 8
6 6
7 9
8 9
9 12
10 6
TOTAL 70
MARKS: 70
INSTRUCTIONS:
a) No output
b) Income is greater than 3000
c) Income is greater than 3000 followed by Income is greater than 4000
d) Income is greater than 4000
e) Income is greater than 4000 followed by Income is greater than 3000
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Question 2 [4]
1. What is the value of sum after the following loop terminates? (1)
int sum = 0;
int item = 0;
do {
item++;
sum += item;
if (sum > 4)
break;
}
while (item < 5);
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Question 3 [10]
1. What is the difference between the public and private modifiers? (2)
public modifiers make class, variables and methods accessible outside the class and package and anywhere
_______________________________________________________________________________
in the application
_______________________________________________________________________________
private modifiers restricts access to variables, class and methods to within the class only
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
2. Consider the following code and answer the questions that follow:
1 class Test {
2 public static void main (String[] args) {
3 A a;
4 a= new A(“Hello”);
5 }
6 }
7
8 public class A {
9 private String s;
10
11 public A (String newS) {
12 s = newS;
13 }
14
15 public void setS() {
16 s = “Hello”;
17 }
18 }
A a;
instantiate it by s = newS;
_______________________________________________________________________________
private String s;
_______________________________________________________________________________
the variable assigns it a reference in memory
_______________________________________________________________________________
a= new A(“Hello”);
_______________________________________________________________________________
c) Does the class A have a default constructor? Explain. (2)
_______________________________________________________________________________
d) A class can have more than one method with the same name, but different signatures. What
do we call this? Give an example of this (write a new method) using the method setS(). (2)
Compile- time/ static Polymorphism, the method executed depends at compile time depends on the method
_______________________________________________________________________________
signature that matches the arguments in the method call.
_______________________________________________________________________________
_______________________________________________________________________________
Other type includes Dynamic/ run-time polymorphism that, resolved at runtime where a subclass overrides
a method defined/explicitly defined in super class
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
3. What are the naming conventions for mutator and accessor methods? (2)
_______________________________________________________________________________
getVariableName()
setVariableName()
_______________________________________________________________________________
_______________________________________________________________________________
Question 4 [3]
1. Given the following code snippet, assume that x and y are public instance variables of the Circle
public class SQ_Q1 {
object.
int x = 10; int y = 10; public static Toy_Object[]
copy(Toy_Object[] original) {
Circle c = new Circle(x, y); Toy_Object[] copy = new
Toy_Object[original.length];
Circle d = c; for (int i = 0; i <
original.length; i++) {
d.x = 20; d.y = 20; copy[i] = new
Toy_Object(original[i]); // Using
Which of the following statements is most accurate? the copy constructor (1)
}
a) The above code will not compile. return copy;
b) Both c and d contain a reference to a Circle object, with c at (10,10) and d at (20,20)
c) c and d contain references to the same Circle object, located at (20,20)
d) c and d contain two separate Circle objects, located at (10,10) and (20,20) respectively.
Any changes made to the Circle object through c will also be reflected when accessing the object through
d, since both variables point to the same memory location.
_______________________________________________________________________________
copy[i] = new Toy_Object(original[i].getNum()); // Create a new instance to prevent shallow copying
Circle d = new Circle( c.getx(), c.gety());
2. clone() is an example of polymorphic method. How does polymorphism add functionality to an
object?
a) Through composition
b) By overriding methods
c) By overloading methods
d) Through inheritance (1)
_______________________________________________________________________________
3. A library contains books, each book has a unique identifier, even if the library has multiple
copies of that book title. What is the best relationship between Library and Book?
a) Inheritance
b) Parent - Child
c) Aggregation
d) Composition (1)
_______________________________________________________________________________
Question 5 [8]
1. Constructors are different from other methods in a class. Give 3 differences between
constructors and other Java methods. Assume that both are public. (2)
_______________________________________________________________________________
constructor initializes a object in a class
method defines behavior of the object
_______________________________________________________________________________
constructor must be same name as class
method can have any name
_______________________________________________________________________________
constructor has no return type not even void
method has a return type even void
_______________________________________________________________________________
_______________________________________________________________________________
2. Private methods are different from protected, package and public methods. One difference is
that private methods can only be called from inside their own class. Name two other differences
between private methods and other class methods. (2)
_______________________________________________________________________________
Private methods are not inherited by subclass
_______________________________________________________________________________
Protected and public method can be inherited
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
3. Consider the following code snippet and answer the questions that follow:
0 class Parent { public String toString() { return "Parent"; }}
1 class Child extends Parent {
2 public String toString() { return super.toString()+ "+Child";}
3 }
4 Parent p = new Child();
5 System.out.println(p.toString());
a) In line 4 a new Child is assigned to variable p, which is of type Parent. Is this allowed? Explain
why or why not. What kind of operation is this? (3)
{upcasting} - Polymorphism
_______________________________________________________________________________
Employee employee = worker
_______________________________________________________________________________
_______________________________________________________________________________
worker = ( FullTimeWorker) employee { downcasting}
_______________________________________________________________________________
Here the Child is treated as a Parent type and has access to Parent class methods and the toString() method
will call the overridden method and return Parent+Child
b) What will be the output of line 5? (1)
Parent + Child
_______________________________________________________________________________
Any methods unique to the child class ca not be accessed since p is reference to
type Parent and to access those method would have to downcast
You are asked to design a computer game called space invaders. The game object contains all other
objects. One of those is a grid with 40x40 tiles. The grid has methods to track which items are where
and to move stuff around. The player and up to 120 aliens are represented by different CHARs in the
grid.
The game has two kinds of items: spaceships and bullets. Bullets are just "^" or “V” characters that
go straight up or straight down.
The game has two kinds of spaceships: The player's spaceship and alien spaceships. The player has
x,y coordinates + health (all 3 are integers) and unlimited bullets that fire automatically. The aliens
have x,y coordinates and health (all 3 are integer) and have unlimited bullets which drop down at
random intervals. If a bullet hits a spaceship its health drops.
The game has a loop that runs continuously. In every loop the following steps happen, each in a
separate method:
On the back of the previous page (i.e. opposite here), draw a UML class diagram to represent the
main classes suggested by this description. Include the instance variables and operations in each
class, and the relationships between classes. Do not include getters and setters, but DO assume all
attributes have getters and setters and adjust visibility of instance variables accordingly. Partial
marks given for partly correct answers.
Question 7 [9]
_______________________________________________________________________________
3. Given an ArrayList of Strings named alist that already contains the following 3 elements: A, B, C
Fill in column 2 of the table below to show what alist will contain after running each of the
statements below in the order shown below: (4)
4. Given a correctly implemented queue class Queue, what would the following Java code print?
_______________________________________________________________________________
_______________________________________________________________________________
1. Given the class definition in APPENDIX A at the end of this paper, answer the following
questions:
a) Draw the GUI displayed by the program (details are not important, only the layout). (3)
exam gui
BUTTON1 BUTTON2
c) What is the name of the method that must be implemented by the ActionListener
interface? (1)
actionPerformed(ActionEvent e)
_______________________________________________________________________________
2. Given the class definition in APPENDIX B at the end of this paper, answer the following
questions:
a) What does the method unnamed do? (2)
it iterates the list startiing from head, looking through each node if item matches item value is returns its index j and
_______________________________________________________________________________
-1 if it is not in the list
_______________________________________________________________________________
_______________________________________________________________________________
b) Write down the missing method body (1 line only) of addToStart, which adds a new
Node to the start of the linked list (1)
_______________________________________________________________________________
head = new Node( newItem, head);
c) Write down the missing method body (1 line only) of method clear, that empties the
linked list so that it contains no items. (1)
head = null;
_______________________________________________________________________________
Question 9 [12]
3. Briefly explain EACH of the following FIVE terms: “digital divide”, “open source”, “virtue ethics”,
“copyleft”, “net neutrality” (5)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
5. Explain why the following statement is an example of a non-sequitur argument: “Bob lives in a
large building, therefore his apartment is large” (1)
_______________________________________________________________________________
_______________________________________________________________________________
6. Explain why the following statement is an example of an argument that is begging the question:
“The voting age should be lowered to 16 because 16 year olds are mature enough to vote” (1)
_______________________________________________________________________________
_______________________________________________________________________________
7. In professional life we will encounter special professional relationships, such as the one we have
with our employees. Name 4 other types of professional relationships we can encounter: (2)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Question 10 [6]
1. Engineering and Accounting are examples of what are called professions. Do you think
computing is, or is not, a profession? Give reasons for your answer, drawing on what it means to
be called a profession. (3)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
2. A foreign computer company wants to donate laptops to Soweto schools, on condition that the
classrooms have cameras and the laptops have tracking devices, so as to avoid theft. Explain why
a deontologist (favouring duty-based ethics) and a consequentialist (e.g. a utilitarian) would
have differing views on whether this plan should go ahead or not. (3)
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
APPENDIX A
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ExamGUI extends JFrame implements ActionListener
{
public ExamGUI()
{
setSize(200,200);
setTitle("Exam GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,2));
JButton b1 = new JButton("BUTTON 1");
b1.addActionListener(this);
b1.setActionCommand("0");
add(b1);
JButton b2 = new JButton("BUTTON 2");
b2.addActionListener(this);
b2.setActionCommand("1");
add(b2);
}
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();
if (a.equals("0")) {
System.out.println("Hello user!");
} else if (a.equals("1")) {
System.out.println("Goodbye user!");
}
}
APPENDIX B