Sie sind auf Seite 1von 8

College of Computer and Information Systems

Umm Al-Qura University


Instructor: Dr. Aymen M. Akremi
Date: Spring 2020

FINAL EXAMINATION
Object Oriented Programming
Spring 2020
Name:……………………………………………………………………………………………… ID#:…………………………………………………………… Group#:……………….

Instructions:
 There are 20 points possible on this test.
 This is a OPEN book exam.
 Quickly browse each problem before beginning the test. Note that some
questions are worth more points than others.
 The answers must be in English.
The exam consists of the following parts:
 Part A: Basic OOP questions worth 5 points.
 Part B: UML coding questions worth 5 points
 Part C: Exception handling & generic programming questions worth 5
points
 Part D: Putting things together problem set worth 5 points
The time limit for the exam is 120 minutes.
Part Questions Max Points Score
1 2.5
A
2 2.5
1 5
B

1 2.5
C
2 2.5

D 1 5
Total: _____________________/20

1
Part A: Basic Questions. [5 marks]

1. For each of the following questions about Java, circle T (true) or F (false).
[2.5 marks]
a. T F Both interfaces and classes define the type system in Java.
b. T F Java supports multiple inheritance for classes but only single inheritance for
interfaces
c. T F A type (class of interface) in Java can have more than one subtypes.
d. T F An abstract class cannot contain any method implementations.
e. T F In class inheritance, a subclass inherits only the nonprivate members of the
superclass.
f. T F The class java.lang.Object is the only class, standard or programmer-defined,
that has no superclass.
g. T F A program that throws an uncaught exception generates a compile-time rather
than a run-time error.
h. T F the following program will fail to compile
public final class Algorithm {
public static <T> T max(T x, T y) {
return x > y ? x : y;
}
}
i. T F The Vector class is a kind of generic programming implementation.
j. T F The question mark (?) is known as the wildcard in generic programming. It
represents an unknown type.

2. Write next to each method call in “main()” the output that it prints. [2.5 marks]

class A {

public void f(A a) {


System.out.println("fa(A)"); }

public void f(B b) {


System.out.println("fa(B)"); }
}
class B extends A {

public void f(A a) {


System.out.println("fb(A)"); }

public void f(B b) {


System.out.println("fb(B)"); }
}

2
public class TypeMeister {

public static void main(String[] args) {

A a = new A();
B b = new B();
A ba= b;

// Write output next to each of the following:


a.f(a);
a.f(b);
b.f(a);
b.f(b);
a.f(ba);
b.f(ba);
ba.f(a);
ba.f(b);
ba.f(ba);
}
}
Part B: Inheritance & Polymorphism [5 marks]
1. Consider this UML class diagram showing part of a program to manage the
membership information for a professional society:

a) Write a Java version of class ManagementCttee assuming it has this constructor:


public ManagementCttee() [1 marks]

3
b) Write a Java version of class Member assuming it has this constructor:
public Member(String name, String address) and that the method
getFee() is abstract. [1 marks]

c) Write a Java version of class StandardMember assuming it has this constructor:


public StandardMember(String name, String address) and the standard
membership fee is fixed at $50. [1 marks]

d) Write a Java version of class SeniorMember assuming it has this constructor:


public SeniorMember(String name, String address, int fee) where
the membership fee is set when a SeniorMember object is created. [1 marks]

e) Write a Java version of class Society assuming it has this constructor:

4
public Society(String societyName) [1 marks]

Part C: Exception handling & generic programming questions [5 pts]

1. Describe two differences between checked and unchecked exceptions. [1 marks]

2. Following is a divide method that it throws an IllegalArgumentException with an


appropriate message if b is zero.

public static double divide(double a, double b) {

if (b==0)
throw new IllegalArgumentException(“cannot divide by 0”);
return a / b;
}
Change this method so that it catches the IllegalArgumentException thrown by divide and
prints the message “the divisor is zero” if it is thrown. [1.5 marks]

public static void printResult (double a, double b) {

System.out.println(divide(a, b));

}
2. Select the right output of the following codes:
Code1:
import java.util.*;
public class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;

5
}
}
class Output
{
public static void main(String args[])
{
genericstack <String> gs = new genericstack<String>();
gs.push("Hello");
System.out.println(gs.pop());
}
}

a) H b) Hello c) Runtime Error d) Compilation Error


Code 2:
import java.util.*;
public class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack <String> gs = new genericstack<String>();
gs.push("Hello");
System.out.print(gs.pop() + " ");
genericstack <Integer> gs = new genericstack<Integer>();
gs.push(36);
System.out.println(gs.pop());
}
}

a) 0 b) 36 c) Runtime Error d) Compilation Error

Code 3:
import java.util.*;
public class genericstack <E>
{
Stack <E> stk = new Stack <E>();
public void push(E obj)
{
stk.push(obj);
}
public E pop()
{
E obj = stk.pop();
return obj;
}
}
class Output
{
public static void main(String args[])
{
genericstack <Integer> gs = new genericstack<Integer>();
gs.push(36);
System.out.println(gs.pop());
}

6
}

a) H b) Hello c) Runtime Error d) Compilation Error

Part D: Putting things together [5 pts]


a. Write a class FootballTeam that will represent a football team.
b. Add fields for the following properties, which cannot be accessed outside of the
class.
name of the team
number of wins
number of losses
c. Write a constructor that accepts the name of the team, the number of wins, and the
number of losses as arguments and sets the class properties to those values. This
constructor should be accessible from any package.
d. Write a second constructor that takes only the name of the team as an argument. This
constructor should set the name of the team to the argument and the set the number of
wins and losses to 0. (Hint: The body of this constructor should be one line and it
should call the first constructor.) This constructor should be accessible from any
package.
e. Write methods that return the name of the team, the number of the wins, and the
number of losses. These methods should be accessible from any package.
f. Next write a method to increase the numbers of wins by 1 and another method to
increase the number of losses by one. These methods should only be accessible from
inside the football package.
g. Write a method that returns true when a team has a “good record,” meaning the team
has more wins than losses. This method should be accessible from any package.

7
Good Luck

Das könnte Ihnen auch gefallen