0% found this document useful (0 votes)
12 views6 pages

Sample Written Test

The document contains a sample written test with 4 questions and subquestions about object-oriented programming concepts in Java such as constructors, inheritance, polymorphism, and class relationships. It includes code snippets to analyze and identify outputs.

Uploaded by

erikanaidoo0219
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)
12 views6 pages

Sample Written Test

The document contains a sample written test with 4 questions and subquestions about object-oriented programming concepts in Java such as constructors, inheritance, polymorphism, and class relationships. It includes code snippets to analyze and identify outputs.

Uploaded by

erikanaidoo0219
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/ 6

COMP200P1 – Sample Written Test [50 Marks]

Duration: 1h30

Question 1 [2x5 = 10 Marks]


1) Which of the following statement/s are true?

A. Overloaded constructors have the same number of parameters.


B. At least one constructor must always be defined explicitly.
C. Every class has a default constructor.
D. The default constructor is a no-arg constructor.

2) Which of the statements regarding the super keyword is incorrect?

A. You can use super to invoke a super class constructor.


B. You can use super to invoke a super class method.
C. You can use super.super.p to invoke a method in superclass's parent class.
D. You cannot invoke a method in superclass's parent class.

3) Which of the statements regarding the protected keyword is incorrect?

A. A protected variable or method can be directly access in the child class.


B. A protected variable or method can be accessed in the parent class.
C. A protected method cannot be implemented in the parent class.
D. A protected variable or method can be accessed by any class in the same package.

4) Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a


default constructor. Which of the following is correct?

A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B();

5) When you implement a method that is defined in a superclass, you the


original method.

a. overload
b. override
c. copy
d. call

Question 2 [2x5 = 10 Marks]

1|6
1) Show the output of running the class Test in the following code lines:

public class Test {


public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
public String getInfo() {
return "Student";
}
}
class Person {
public String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}

2) What is the output of the following program?

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

3) What is the output of the following program?

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 ");
}
}

Select three correct options from the following list:

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.

Question 3 [15 Marks]


Study the classes below and answer the questions that follow.

public abstract class Shape {


private String color;
public Shape(){
color = "";
}
public Shape(String color){
this.color=color;
}
public String getColor(){
return color
}
public abstract area();
}

public class Circle extends Shape{


private double radius;
public Circle(){
super ();
radius = 0.0;
}
public Circle(String color, double radius){
super(color);
this.radius=radius;
}
public double getRadius(){
return radius;
}
}

public static void main(String[] args) {


}
i. The Shape class cannot compile, explain why and write the code that will correct the
problem. [2]
ii. A part of the Circle class is missing. Explain what this missing part is. Write the code for
the missing part. [2]

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

Question 4 [3x5 = 15]


Write the java code that implement the following class diagram

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

You might also like