SCHOOL OF MATHEMATICS, STATISTICS & COMPUTER SCIENCE
MOCK TEST 1
AUGUST 2024
COMP102WP2: Computer Programming
DURATION: 90 minutes TOTAL MARKS: 50
INTERNAL EXAMINERS: S. Dlamini & T. Zengeya
EXTERNAL EXAMINER: R. Els
INSTRUCTIONS
1. This paper consists of 4 pages (incl. this one).
2. Answer all questions.
3. You can write your code immediately after the question text and/or on the
back of the page
4. You may answer the programming questions in pencil. However, make sure
that any rough work is clearly scratched out
5. Code that is unreadable or difficult to read will be penalised
6. Assume that you’re writing your code within the main method of a class.
You don’t need to include the class and main method code in your
solution
7. Do not write any import statements. Assume that all of the classes that
you may need have been imported into the .java file
8. You may not use any utility method in the java.util.Arrays class
Question 1: Reverse Integer [15 marks]
Write a program that will prompt the user for an integer. The integer may be of any
length. The program must then reverse that integer and display it on the console. Do
not treat the original integer as a string. However, you can cast individual
digits into strings or characters as you construct the reversed integer.
Example run 1
Enter an integer: 123
Reversed integer: 321
Example run 2
Enter an integer: 987654321
Reversed integer: 123456789
1
Question 2: Peak Element [20 marks]
A peak element in an array is an element that is either greater than or equal to its
neighbours. When a peak element is the first or last element in the array, it is called
a corner peak element. For these corner peak elements, there will only be one
neighbour element. Given an integer array called integers, write a Java program
to find the peak element and display it on the console. You may assume that the
array is non-empty and that there is always only one peak element.
Example run 1
Array: 1 2 4 3 0
Peak element: 4
Example run 2
Array: 1 4 6 6 9
Peak element: 9
2
Question 3: Palindrome [15 marks]
Write a program that prompts a user for a string and then determines whether the
string is a palindrome or not. A word is considered to be a palindrome if its
appearance does not change when you reverse the letters. For example, the
following words are palindromes:
“abba” “level” “racecar” “madam” “rotator”
Example run 1
Enter string: level
“level” is a palindrome
Example run 2
Enter first string: cat
“cat” is not a palindrome