Sample Written Test
Sample Written Test
Duration: 1h30
A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B();
a. overload
b. override
c. copy
d. call
1|6
1) Show the output of running the class Test in the following code lines:
public class D {
public static void main(String[] args) {
A b = new B();
System.out.println(b.z);
}
}
class A {
protected double z;
public A() {
f(11.0, 5.0);
}
public void f(double x, double y) {
z = x ∗ y;
}
}
2|6
class B extends A {
public void f(double x, double y) {
z = x + y;
}
}
a) 16.0
b) 55.0
c) 0
d) No output due to error
e) 11.0
f) 5.0
class Base {
public void method(int i) {
System.out.println("Value is " + i);
}
}
public class Sub extends Base {
public void method(int j) {
System.out.println("This value is " + j);
}
public void method(String s) {
System.out.println("I was passed " + s);
}
public static void main(String args[]) {
Base b1 = new Base();
Base b2 = new Sub();
b1.method(5);
b2.method(6);
}
}
a) I was passed 5
I was passed 6
b) Value is 5
Value is 6
c) This value is 5
This value is 6
d) Value is 5
This value is 6
e) Value is 5
I was passed 6
3|6
4) class Base {
public Base() {
System.out.print("Base ");
}
public Base(String s) {
System.out.print("Base: " + s);
}
}
class Derived extends Base {
public Derived(String s) {
super(); // Stmt−1
super(s); // Stmt−2
System.out.print("Derived ");
}
}
class Test {
public static void main(String [] args) {
Base a = new Derived("Hello ");
}
}
a) Removing Stmt-1 will make the program compilable and it will print the following:
Base Derived.
b) Removing Stmt-1 will make the program compilable and it will print the following:
Base: Hello Derived.
c) Removing Stmt-2 will make the program compilable and it will print the following:
Base Derived.
d) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print
the following: Base Derived.
e) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the
following: Base: Hello Derived.
5) What happens when you try to compile the following code and run the Zebra application?
class Animal {
float weight;
Animal(float weight) {
this.weight = weight;
}
}
class Zebra extends Animal {
public static void main(String[] args) {
Animal a = new Animal(222.2f);
Zebra z = new Zebra(); }
}
}
4|6
a. Class Animal generates a compiler error.
b. Class Zebra generates a compiler error.
c. The code compiles without error. The application throws an exception when the
Animal constructor is called.
d. The code compiles without error. The application throws an exception when the
Zebra constructor is called.
e. The code compiles and runs without error.
5|6
iii. Write a subclass of the Shape class called Rectangle that has two fields representing
the width and the length of the rectangle. [5]
iv. For each of the following statements, states whether it will cause a compilation
problem or not in the main() method. In case there will be a compilation problem,
explain why. [1x6=6]
a. ArrayList<Sahpe> sList = new ArrayList<Shape>();
b. Circle aCircle = new Shape();
c. Shape aCircle = new Circle();
d. Circle aCircle = new Circle(color, fill, radius);
e. Shape aCircle = new Circle(color, radius);
double r = aCircle.getRadius();
f. Object o = new Cicle(color, radius);
double r = o.getRadius();
Person
-id number: int
-name: String
-#age: int
+getters
+setters
+payService(): double
+toString(): String
Member Address
-home number: int -street: string
-work number: int -town: string
-cell number: int -postal code: int
+getters +getters
+setters +setters
+toString(): String +toString(): String
Hints: Only members who are 18 years of age, and older are allowed. Members pay for services.
Members 55 years of age, and older, pay R50 per month, whereas, members under the age
of 55, pay R85 per month.
6|6