Comp102 Mock Exam 2023
Comp102 Mock Exam 2023
MOCK EXAM
NOVEMBER 2023
COMP102WP2: Computer Programming
INSTRUCTIONS
1. This paper consists of 13 pages (incl. this one).
2. Pages 11–13 contain reference material that you may find useful.
3. Answer all questions.
4. Start the answers to each question on a new page.
5. You may answer the programming questions in pencil.
6. The use of a calculator is NOT allowed.
7. Code that is unreadable or difficult to read will be penalised.
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
public Numbers(int y) {
this(5, 4);
System.out.print(100 * 4 + " ");
}
class TestNumbers {
public static void main(String[] args) {
Numbers questionTwo = new Numbers(80);
System.out.print(questionTwo.getA() + " "
+ questionTwo.getB());
}
}
2
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
class TestItem {
public static void main(String[] args) {
Item itemOne = Item();
Item itemTwo = Item();
itemOne.setColour("blue");
itemTwo = itemOne;
itemTwo.setColour("red");
System.out.println(itemOne.getColour() + "
" + itemTwo.getColour());
}
}
if(one == two) {
System.out.println("one and two are
equal");
} else {
System.out.println("one and two are
not equal");
}
if(one == three) {
System.out.println("one and three are
equal");
} else {
System.out.println("one and three are
not equal");
}
}
}
3
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
[15]
that will find and return the longest prefix substring from an array of strings.
For example, given an array of the following strings “flower”, “flow”, and
“flight”; the method would return “fl”. Given the strings “dog”, “racer”, and
“car”; the method will return an empty string.
[10]
that displays all of the elements that are “leaders” in an array. An element is
considered a leader if there is no element greater than it on the right side of
it in the array. For example:
• For array [16, 17, 4, 3, 5, 2], the leaders are 17, 5, and 2
• In the array [12, 9, 7, 14, 8, 6], the leaders are 14 and 6
[10]
that displays a number triangle like the one shown below. The method
receives an integer indicating the number of rows the triangle should have.
The number of rows also determines the number of columns that the triangle
has. The triangle below was created with a rows value of 5.
4
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
[10]
Each one of the 250 lines in the transfers.txt text file contains a sender
account number, a recipient account number, the amount sent, and the
date of the transaction. Each piece of data is separated by a semi-colon (;).
A snippet of the contents of the file is shown below:
4389767090;2573919515;7729.02;2023-11-27
4262763389;2362740981;26720.41;2023-10-28
4856174833;3016491397;22080.37;2023-2-28
1769416549;5194517042;30062.86;2022-11-20
import java.time.LocalDate;
class Transfer {
private String sender;
private String recipient;
private LocalDate date;
private double amount;
5
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
return recipient;
}
6
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
or…
or…
--------------
| | | |
--------------
| | | |
--------------
| | | |
--------------
Enter row: 0
Enter column: 1
Your move.
--------------
| | X | |
--------------
| | | |
--------------
| | | |
--------------
Computer's move.
--------------
| O | X | |
--------------
| | | |
--------------
| | | |
--------------
Enter row: 1
Enter column: 1
7
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
Your move.
--------------
| O | X | |
--------------
| | X | |
--------------
| | | |
--------------
Computer's move.
--------------
| O | X | |
--------------
| | X | O |
--------------
| | | |
--------------
Enter row: 3
Enter column: 0
Your move.
(3, 0) is outside grid.
Enter row: 2
Enter column: 0
Your move.
--------------
| O | X | |
--------------
| | X | O |
--------------
| X | | |
--------------
Computer's move.
--------------
| O | X | |
--------------
| | X | O |
--------------
| X | | O |
--------------
Enter row: 0
Enter column: 2
8
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
Your move.
--------------
| O | X | X |
--------------
| | X | O |
--------------
| X | | O |
--------------
This game has been implemented in Java using three classes. A Board
class contains methods used to keep track of the state of the game, to place
a piece on the board, and to check whether the player (X) or the computer
(0) has won the game.
For this question, you are only required to complete the Board class. We’ll
assume that the other classes have been fully implemented. Below are the
contents of the Board class:
class Board {
public static int PLAYER = 1;
public static int COMPUTER = 0;
private int placementCount = 0;
private int[][] grid = new int[3][3];
public Board() {
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[i].length; j++) {
grid[i][j] = -1;
}
}
}
}
At the moment the class only contains four attributes and a constructor. The
constructor initialises each element in the grid matrix to a value of -1, to
indicate that no piece has been placed in it yet.
9
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
6.1. Implement the toString() method for the Board class. This method (10)
should print out a neat image of the current state of the board, showing
where both the player (X) and computer pieces (0) are located.
{{0, 2},
{1, 0},
{2, 1}
}
If there are no more empty spaces, the method must return an empty
2D array.
[30]
10
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
API DOCUMENTATION
String Class
char charAt(int index)
//Returns the char value at the specified index.
boolean isEmpty()
//Returns true if, and only if, length() is 0.
int length()
//Returns the length of this string.
11
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
String toLowerCase()
//Converts all of the characters in this String to lower case.
String toUpperCase()
//Converts all of the characters in this String to upper case.
String trim()
//Returns a copy of the string, with leading and
//trailing whitespace omitted.
StringBuilder Class
StringBuilder()
// Constructs a string buffer with no characters in it and an
// initialcapacity of 16 characters.
StringBuilder(int capacity)
//Constructs a string buffer with no characters in it and the
//specified initial capacity.
StringBuilder(String str)
//Constructs a string buffer initialized to the contents of
//the specified string.
StringBuilder append(char c)
//Appends the string representation of the char argument to
//this sequence.
12
UNIVERSITY OF KWAZULU-NATAL, NOVEMBER 2023 MOCK EXAM: COMP102WP2
StringBuilder reverse()
//Causes this character sequence to be replaced by the
//reverse of the sequence.
String toString()
//Returns a string representing the data in this sequence.
13