University of Cape Town ~~ Department of Computer Science
Computer Science 1016S ~~ 2018
Class Test 1
** Solutions **
Enter the following details AND shade in the corresponding A 0
blocks to the right with your Student Number. B 1
C 2
Faculty (please tick one): D 3
Science Engineering Commerce Humanities Other:
E 4
F 5
G 6
Student Number : H 7
__________________________________ I 8
J 9
K
Name (optional) :
L
__________________________________ M
N
O
Marks : 30 P
Q
Time : 40 minutes
R
Instructions: S
a) Answer all questions. T
U
b) Write your answers in PEN in the V
spaces provided. W
c) Show all calculations where X
applicable. Y
Z
FOR
Question 1 2 3 4 5 6 7 8
OFFICIAL
Max 5 5 10 10
USE
Marks 0
ONLY: 1
2
3
4
5
6
7
8
9
Marker
Question 1 [5]
Answer MCQ questions by indicating the best answer number (a..d).
1. What is displayed when the following is executed:
double s = 25.5456747;
int x = (int)s ;
System.out.println(x);
a) 26.0
b) 25.0
c) 25
d) 26
c
_______________________________________________________________________________
2. What is printed out following the execution of the code below:
public class QuestionTwo {
public static void main(String[] args) {
int x = 0;
for(int i=6; i>=++x; i--) {
System.out.print(i*x+ “ “);
}
}
}
a) Nothing. The code fails to compile because the ++x isn't used correctly.
b) 6 10 12.
c) 0 5 8 9.
d) Nothing. The for loop is implemented incorrectly.
b
_______________________________________________________________________________
3. Consider the following statements. What will it output when you compile and run it?
double amount = 1254.7;
System.out.printf(“You have won R%9.2f.”, amount);
a) You have won R1254.7.
b) You have won R 1254.70.
c) You have won R001254.70.
2
d) You have won R1254.70.
b
_______________________________________________________________________________
4. What will happen when you attempt to compile and run this code?
Public class MyMain {
Public static void main(String [] args){
System.out.println("Hello cruel world");
}
}
a) The compiler will complain that the class is not defined correctly.
b) The code will compile and when run will print out "Hello cruel world".
c) The code will compile but will complain at run time that no constructor is defined.
d) The code will compile but will complain at run time that the main method is not
correctly defined.
a
_______________________________________________________________________________
5. Which one of the following statements is true?
a) A default constructor is provided automatically if no constructors are explicitly
declared in the class.
b) At least one constructor must always be defined explicitly.
c) Every class has a default constructor.
d) The default constructor is always private.
a
_______________________________________________________________________________
3
Question 2 [5]
1. Java provides three different looping options. Explain when it would be best to use each
of them. [3]
_________________________________________________________________________
for – when the number of iterations are known (1 mark)
while – number of iterations are 0 or more (1 mark)
do while - number of iterations are 1 or more (1 mark)
_________________________________________________________________________
_________________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
2. Given the following code what is the printout for the first println statement in the
main method? Explain. [2]
public class Foo {
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + k;
System.out.println("k is " + k);
}
}
_____________________________________________________________________
_____________________________________________________________________
_______________i + j is (1 mark) 23 (1 mark)___________________
_________________________________________________________________________
_______________________________________________________________________
_________________________________________________________________________
_ _________________________________________________________________________
4
Question 3 [10]
A int is a primitive data type, you would like to create an Integer object. You decide to create a
MyInteger object. The object will contain the following:
public class MyInteger {
// insert instance variables here
// insert constructors here
// insert isEven method here
Now consider a driver program that creates and displays two Book objects:
1 public class TestMyInteger {
2 public static void main(String[] args) {
3 MyInteger x = new MyInteger(129);
4 if (x.isEven())
5 System.out.println(x.getMyInteger() + “ is an “+
6 “even number";
7 else
8 System.out.println(x.getMyInteger() + “ is an “+
9 “odd number";
10 System.out.println(x.equals(120));
11 }
12 }
(i) Write an int data field named value that stores the int value represented by this object. The data
field should be encapsulated appropriately. [2]
_________________________________________________________________________
private (1 mark, case is important – not Private) int value;(1 mark)
______________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
(ii) Write an appropriate constructor in the MyInteger class to create the objects shown in lines 3 of
the driver program. [3]
public MyInteger(int value) { ((1 mark for “public MyInteger” (case important), 1
mark for parameter – any name)
this.value = value;(1 mark, this.value not essential, but must be correct if used)
}
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
5
_________________________________________________________________________
(iii) Write a methods isEven() that return true if the value is even . [5]
public boolean (1 mark) isEven() { (1 marks)
if value %2 == 0 (1 mark)
return true;(1 mark)
return false; (1 mark, could be else return false;)
}_______________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
6
Question 4 [10]
(i) Assume a predefined class Circle2D contains:
■ Two double data fields named x and y that specify the centre of the circle with get methods.
■ A data field radius with a get method.
■ A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius.
■ A constructor that creates a circle with the specified x, y, and radius.
■ A method getArea() that returns the area of the circle.
■ A method getPerimeter() that returns the perimeter of the circle.
■ A method contains(double x, double y) that returns true if the specified point (x, y) is inside this
circle.
Write a test program that creates a Circle2D object c1 (new Circle2D(2, 2, 5.5)), displays its area,
and displays the result of c1.contains(3, 3). [4]
public class TestCircle2D { (1 mark – any good name )
Circle2D c1 = new Circle2D(2, 2, 5.5); (1 mark, name must be c1 )
System.out.println(c1.getArea());(1 mark)
System.out.println(c1.contains(3, 3));(1 mark )
}
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
(ii) What are accessor methods? [1]
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
7
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
Accessor methods allow the programmer to retrieve the value of an object’s instance
variables in a controlled manner. The name of a method typically starts with the word
get
(1 mark for defination)
(iii) Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your
weight in kilograms and dividing by the square of your height in meters. The interpretation of
BMI for people 16 years or older is as follows:
BMI Interpretation
below 16 seriously underweight
16–18 underweight
18–24 normal weight
24–29 overweight
29–35 seriously overweight
above 35 gravely overweight
You have been tasked with designing a class that could be used to calculate and categorise a
persons BMI. The user will provide the class the persons name, weight and height, and then
either get the BMI score or the BMI status. Draw a UML class diagram for the BMI class. [5]
Name should be BMI or similar (1 mark)
Variables shown are required. Will accept if there are others as well.(1 mark)
Variable types must be shown. (1 mark)
Constructor (1 mark)
getBMI() and getStatus() (or similar) methods with return types (1 mark)