Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

QUESTION 1 [ 5 POINTS]: MULTIPLE CHOICES QUESTIONS

1. When you implement a method that is defined in a superclass, you _______ the original
method.
A. Overload C. Override
B. Copy D. Call

2. If we have two objects c1 and c2, the result of c1=c2 will be:
A. c1 and c2 have the same content C. The compile will print True.
and same reference. D. Both have the same content but
B. c1 and c2 have different content. different references.

3. In Java, if an object is no longer referenced then:


A. It will remain in the memory. new reference.
B. It will be treated as a garbage. D. None of the above.
C. The programmer should give it a
4. The superclass in the following code is:
public class Apple extends Fruit { }
class Fruit {
public Fruit (String name) {
System.out.println ("Fruit's constructor is invoked");
}}
A. Apple
B. extends
C. Fruit
D. A and C

5. In Java, the super keyword can be used to:


A. To invoke superclass constructor.
B. To differentiate the members of superclass from the members of subclass.
C. To call subclass method.
D. A and B.

Page 1 From 6
1

QUESTION 2 [ 8 POINTS]: SHORT ANSWER

 What is the output of the following codes? [4 points]

public class Account {

private int customerID;


private double balance; public class SavingsA extends Account
{
Account(int newCustomID, SavingsA(int customerID, double
double newBalance) balance)
{ {
this.customerID = newCustomID; super(customerID,balance);
this.balance = newBalance; }
System.out.print( }
"Current balance:"+balance+"JD");
1. }
}

public class TestAccount {


public static void main (String[] args)
{
SavingsA savings = new SavingsA(2, 2450.88);
}
}
Output:

public class Car{


public String toString()
{ return "A Car";
}
}
public class Truck extends Car{
public String toString()
{ return "A Truck";
}
2. }
public class Test{
public static void main(String[] args) {
Car c = new Truck();
System.out.println(c.toString());
}
}
Output:

 Find the error in each piece of code: [4 points]


Page 7 From 6
1

1. public class Test {


public static void main(String[] args) {
A a = new A(3);
System.out.println( a.instanceof.A ); } }
class A extends B {
public A(int t) {
System.out.println("A's constructor is invoked");
} }
class B {
public B() {
System.out.println("B's constructor is invoked"); } }
Answer:

2 public class Test {


public static void main(String[] args) {
. A a = new A();
a.p(10);
a.p(10.0); } }
class B {
public void p(double i) {
System.out.println(i * 2); } }
class A extends B {
// This method overrides the method in B
public double p(double i) {
System.out.println(i); } }
Answer:

QUESTION 3 [ 2 POINTS]: FILL IN THE BLANKS


 Complete the missing line of code:
public class Test {
public static void main(String[] args) {
@ Define an object from person class
------------------------------------------------------------------------}
}
class Student extends Person {
@Override getInfo method
____________________________________ {
return "Student";
}
}
Page 7 From 6
1

class Person {
public String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}

QUESTION 4 [ 5 POINTS]:
Based on the following figure, write the header needed to declare each class.

QUESTION 5 [ 10 POINTS]: ESSAY


Write a JAVA code to implement the following:
Note: Some parts of the code have been already done.
Write a JAVA code to implement a class named EducationalMaterial, and its subclass called Book.
The details of the classes are described in the following UML diagram:

EducationalMaterial Book
- author: String
- id: int
- title: String

+ EducationalMaterial()
+ Book(id: int, author: String,
+ EducationalMaterial(id: int)
title: String)
+ getID(): int
+ toString(): String
+ setID(id: int): void
+ toString(): String

Additional Specification:
An EducationalMaterial class:
 In EducationalMaterial constructor with one argument: id
Use this keyword to initialise the the class attribute.

Page 7 From 6
1

 Override the toString() method to:


return "EducationalMaterial (id = ?)",
Note : the ? represents the id value.

A Book class:
 In Book constructor:
- Use super keyword to explicitly invoke parents’ constructor and pass the id.
- Use this keyword to reference the data fields author and title, and assign the passed values to them
 Override the toString() method, to
return "Book : EducationalMaterial (id = x), author = y, title = z",
Note: x is the output of the toString()from the superclass
y and z the author and title values of the instance.

Sample of the output:

Book : EducationalMaterial (id = 1001), author = Mark, title =


Java]

package midterm1;

public class EducationalMaterial {

public EducationalMaterial () {
}

@ Override
public String toString() {

}
}

Page 7 From 6
1

Pckage midterm1;

public class Book ____________________________ {

@ Override
public String toString() {

Page 7 From 6

You might also like