0% found this document useful (0 votes)
26 views14 pages

CSC1016S 2022 Exam

Uploaded by

lilithaprecious8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views14 pages

CSC1016S 2022 Exam

Uploaded by

lilithaprecious8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Fill in your Peoplesoft 7-digit student number below, NAME (neatly thanks):

and your Name and Student Number (e.g. BNDJAM007) __________________

in the top right corner. Then fold over the top right corner. __________________

Peoplesoft Student Number: STUDENT NUMBER:

_______________________ _________________

UCT Department of Computer Science

Computer Science 1016S ~ 2022

November Examination

Question Max Internal External

1 3
2 4
3 10
4 3
5 8
6 6
7 9
8 9
9 12
10 6

TOTAL 70

MARKS: 70

TIME: 120 minutes + 10 minutes reading time

INSTRUCTIONS:

a) Answer ALL questions


b) Write your answers in PEN in the spaces provided.
c) If you run out of space, you can use the back of the page as well.
Question 1 [3]

1. Suppose income is 4100, what is the output of the following code?


if (income > 3000)
System.out.println(“Income is greater than 3000”);
else if (income > 4000)
System.out.print(“Income is greater than 4000”);

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

_______________________________________________________________________________

2. What would happen after the following block of code is executed?


int[5] list = new int[];
list = new int[6];

a) The code has compile errors.


b) The code has runtime errors because the variable list cannot be changed once it is assigned.
c) The code can compile and run fine. The second line assigns a new array to list.
d) The code has compile errors because you cannot assign a different size to list.
e) None of the above are correct.

_______________________________________________________________________________

3. What is invoked to create an object?


a) The main method
b) A method with a return type
c) A constructor
d) A method with the void return type
e) Memory

_______________________________________________________________________________
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);

_______________________________________________________________________________

2. Explain the error in each of the following statements? (3)


1. System.out.printf(“%5d %d.1%n”, 1, 2);
2. System.out.printf(“%5d %d\n”, 1);
3. System.out.printf(“%5d %f%n”, 1, 2);

lncorrect format specifier for d.1.


_______________________________________________________________________________
%5d is valid and specifies an integer formatted in a field width of 5.
_______________________________________________________________________________
Missing specifier argument for 2.
_______________________________________________________________________________
Incorrect format specifier for a integer argument, %.2f expects a double/float
_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________
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) Which statements are examples of declaring an object’s reference variable? (2)

A a;
instantiate it by s = newS;
_______________________________________________________________________________
private String s;
_______________________________________________________________________________
the variable assigns it a reference in memory

b) Give an example of a statement that is creating an object? (1)

_______________________________________________________________________________
a= new A(“Hello”);

_______________________________________________________________________________
c) Does the class A have a default constructor? Explain. (2)

No, class A has a defined constructor with parameter (String)


_______________________________________________________________________________

_______________________________________________________________________________

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

Child child = new Child();


Parent p = child; {upcasting}
child = (Child) p { downcasting}
Question 6 [6]

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:

- read input from the keyboard (move or fire bullet up)


- make the bottommost alien in every column drop a bullet at random intervals. That alien has to
wait a minimum amount of time before it can fire again.
- move bullets, aliens and the player
- detect when aliens or the player are hit by bullets, and reduce their health on hit
- update the grid to reflect the changes in the game state
- display the grid to screen
- remove spaceships when their health is zero; declare GAME OVER when the player's health hits
zero; declare GAME WON when all aliens are removed.

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]

1. Which one of the following statements is true?


a) A class can only implement one interface.
b) A class can inherit instance variables from an interface.
c) An interface cannot be a method parameter type.
d) An abstract class can implement an interface. (1)

_______________________________________________________________________________

2. Which one of the following statements is true?


a) All the variables in an interface must be private. a static method belongs to the class and not
b) All the methods in an interface must be private. a particular instance of a class
c) All the variables in an interface must be static.
d) All the methods in an interface must be static. method are normally abstract, but(1)
can be
default/static
_______________________________________________________________________________

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)

Statement ArrayList contents


// before we start it already contains A,B,C A, B, C

(a) alist.add(“A”); // this is run 1st A,B,C,A

(b) alist.set(1, “F”); // this is run 2nd A,F,C,A

(c) alist.remove(2); // this is run 3rd A,F,A

(d) alist.remove(“A”); // this is run 4th F,A removes first occurence of A

4. Given a correctly implemented queue class Queue, what would the following Java code print?

Queue q = new Queue( );


q.enqueue(“ann”); ann // enqueue returns void therefore cannot print values
q.enqueue(“bob”); ann, bob
System.out.println( q.dequeue( ) ); // remove from q ann // the first element is removed and printed
q.enqueue(“coo”); bob, coo
System.out.println( q.dequeue( ) ); // remove from q bob / the first element is removed and
(2) printed

_______________________________________________________________________________

_______________________________________________________________________________

5. What is a stack? (1)


linear data structure that follows the Last In, First Out (LIFO) principle. elements are added at the end of a
_______________________________________________________________________________
list and the last element added is the first removed, push() o add , pop() to remove top element
Question 8 [9]

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

b) What will happen if “BUTTON 2” is clicked? (1)

Print "Goodbye User"


_______________________________________________________________________________

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)

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

4. Fill in the missing word in each of the following sentences: (3)


a) The African moral theory that emphasises the importance of community and human
dignity is called _____________
b) The South African law that ensures that our private information is not wrongly collected
or used is called _______________________ (just give the acronym i.e. the letters by
which it is known)
c) The international organisation dedicated to protecting our safety online is called
___________ (just give the acronym i.e. the letters by which it is known)

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

public class LinkedList {


private class Node {
private String item;
private Node link;
public Node(String newItem, Node newLink) {
item = newItem;
link = newLink;
}
}
private Node head;
public LinkedList() {
head = null;
}
public void addToStart(String newItem) {
// question (a)
}
public void clear() {
// question (b)
}
public int unnamed(String itemValue) {
int j = 0;
Node position = head;
String positionItem;
while (position != null)
{ positionItem = position.item;
if (positionItem.equals(itemValue))
return j;
position = position.link;
j = j+1;
}
return -1;
}
}

You might also like