Object Oriented Programming Exam - CP 215
University of Dodoma
College of Informatics and Virtual Education
Department of Computer Science and Engineering
End of Semester University Examination for the 2020/2021 Academic Year
Course Name:Â Object Oriented Programming
Paper Code Number:Â CP 215
Date of Examination:Â [Date]
Time:Â [Time]
Duration:Â 3 Hours
SECTION A: (15 MARKS)
Question One
- Read each question carefully and choose the most correct response. (0.5 Marks Each)
QN i-iii MISSING : Please Submit if Available.
iv. Consider the following program:
class ShapeTest {
public void main(String args []){
Shape S = new Shape ( );
S.Area( );
Square Sq = new Square ( );
Sq.Area ( );
}
}
Which line contains an error?
A. Square Sq = new Square();
B. Sq.Area();
C. Shape S = new Shape();
D. public void main(String args[])
E. class Square extends Shape
v. Consider the ExceDemo class, possible sequence of numbers can be printed by calling firstMethod (v), depending on the value of v. Which correct sequence of numbers will be printed (each number on separate line) when ( v = 6 )?
public class ExceDemo{
public static void firstMethod(int v) {
try {
System.out.println("0");
secondMethod(v);
System.out.println("1");
} catch(Exception e){
System.out.println("2");
}
}
public static void secondMethod(int v) throws Exception {
System.out.println("3");
try {
int c = 7 / v;
System.out.println("4");
if(v == 6) throw new Exception();
System.out.println("5");
} catch(ArithmeticException e){
System.out.println("6");
}
System.out.println("7");
}
}
A. 0 3 6 7 1
B. 0 3 4 5 1 2
C. 0 3 4 5 6 7
D. 0 3 4 7 1
E. 0 3 4 2
Use the following program to answer questions vi to ix. Comments indicate where missing components of the program are to be placed.
public class MyMain {
public static void main(String [] args) {
// (2) print the greeting
// (3) construct a MyClass object called myObject
// (4) update()
}
}
class MyClass {
// (1) definition of MyClass constructor
public static void greetings() {
// definition of greets
}
public void update(int num, String title) {
// definition of update
}
public void print() {
// definition of print
}
private int numOfItems;
private String reportTitle;
}
vi. Suppose you are writing the definition of MyClass at line (1) above. Which of the following function signatures (headers) is correct?
A. public int MyClass (int numOfItems, String reportTitle)
B. public MyClass(int numOfItems, String reportTitle)
C. public void MyClass (int numOfItems, String reportTitle)
D. public void MyClass(int numOfItems, String reportTitle)
E. public MyClass(void)
vii. Suppose you wish to construct MyClass object called myObject at line (3) above. Which of the following statement will correctly do this?
A. MyClass myObject;
B. myObject.MyClass();
C. MyClass myObject = MyClass();
D. MyClass myObject = new(MyClass);
E. MyClass myObject = new MyClass();
vii. Suppose you wish to call the method that prints the greeting, at line (2) above. Which of the following statement(s) will call this method correctly?
A. myObject.greetings();
B. greetings();
C. MyClass.greetings();
D. Both A and B
E. Both B and C
ix. Suppose you wish to call the update method at line (4) above. Which of the following statements will call this method correctly?
A. myObject.update(4, “CP 215”);
B. update(myObject(4, “CP 215”));
C. update(4, “CP 215”);
D. MyClass.myObject.update(4, “CP 215”);
E. MyClass.update(4, “CP 215”);
x. In Java, if there is no explicitly defined super class every class defined is implicitly a sub class of:
A. Exception class
B. Object class
C. Parent class
D. Scanner class
E. Throwable class
Question Two
- Match the item in Column A with its corresponding item in Column B. (0.5 Marks Each)
Column A | Column B |
---|---|
i. Encapsulation | A. Polymorphism |
ii. final | B. Throwable |
iii. Top level containers | C. Collection of abstract class |
iv. Application of inheritance | D. Throws |
v. Translate Java bytecode into machine code | E. private members, get and set methods |
vi. All Swing components inherit from this class | F. protected members, get and set methods |
vii. Parent class of all exceptions | G. Can be inherited or overridden |
viii. Interfaces | H. unchecked exception |
ix. Diving number by zero i.e (5/0) | I. JDK |
x. Class | J. JFrame, JDialog, JApplet |
K. JVM | |
L. checked exceptions | |
M. Cannot be inherited or overridden | |
N. JComponent | |
O. SWING | |
P. Blueprint of an object | |
Q. Specialization | |
R. Instance of class | |
S. Collection of abstract methods | |
T. JButton, JTextField, JTextArea |
Question Three
- Answer the following question precisely.
a. Differentiate between abstract class and inheritance (2 Marks)
b. What is the purpose of garbage collection in java (1 Mark)
c. For each of the following instructions provide correct declaration that will achieve the specified task: (1 Mark Each) i. A class Circle, with one instance variable called radius which can only be accessed within Circle class. ii. Method display, which does not take and return any argument and can be accessed without creating an object. iii. An array called myArray, which can store names of 800 students. iv. Method Area, which takes a double-precision floating-point argument radius and returns a double-precision floating-point.
SECTION B: (45 MARKS)
Attempt THREE (3) out of FOUR (4) questions provided.
Question Four
- Bank account contains a version of class Account that maintains instance variables information such as owner name, account number and balance. Typically bank services many accounts, each with its own balance. Every instance (i.e., object) of class account contains its own copies of both the name, account number and the balance. The class also consists of method that set the initial values to the instance variables, a method that deposit an amount to the account, and method that withdraw money from an account. The method withdraw ensure that the withdrawal amount does not exceed the Account’s balance. If it does, the balance should be left unchanged. Lastly, the class contain display method that print account owner name and balance on the screen whenever is called.
a. Write a Java class Account that implement the given information. (9 Marks) b. Write class AccountTester with main method for the above class that reads in the initial values from the keyboard and call other methods. (6 Marks)
Question Five
- a. Write Java application using GUI Swing components that will calculate the area of triangle. The application should accept base and height inputs from keyboard via dialog boxes and display the area on dialog box. Organize your application into method called calcArea and use main method to test your application. Do not create an object to access calcArea method. (7 Marks)
b. Assume you have a MySQL database named EmployeeDB running on localhost server and it has a table called EmployeeInfo with four columns: p_id, first_name, last_name and phone_number. Write a Java program that will establish JDBC connection to the database and query the database’s EmployeeInfo table to display first name, last name and phone number of the employees. Exception handling is inevitable for database connectivity, therefor provide try-catch-finally block for exception handling where appropriate. Given the driver class is com.mysql.jdbc.Driver, the port number is 3306. Use root for both username and password. (8 Marks)
Question Six
- Carefully read the following requirements and write Java application which implements the specified tasks. Use comments to indicate your implementation on each task. a. Declare a package called football and add class called FootballTeam make sure your class can be accessed from any package (2 Marks)
b. Add the name of the team, number of win and number of losses instance variables into a class FootballTeam, make sure your variables cannot be accessed outside of the class. (2 Marks)
c. Write a constructor that accept the name of the team, the number of wins, and the number of losses as arguments and set the class properties to those values. This constructor should be accessed from any package. (2 Marks)
d. Write a second constructor that takes only the name of team as an argument. This constructor should set the name of the team to the argument and set the number of wins and losses to 0. Note the body of this constructor should be one line and it should call the first constructor. This constructor should be accessed from any package. (2 Marks)
e. Write the methods that return the name of the team, the number of wins and the number of losses. These methods should be accessed from any package. (2 Marks)
f. Write a method that returns true when a team has a good record. The team will have good record when the number of win is greater than number of losses. This method should be accessible from any package. (2 Marks)
g. Finally, add a main method to the FootballTeam class. In the main method construct a FootballTeam named “XYCLUB” with 3 wins and 5 losses. Call the method that return true when the team has good record and print out the results. Print also the name of the team, number of wins and losses on the screen name of team. (3 Marks)
Question Seven
- Consider the Person, Student, Employee, Faculty, and Staff classes describing the University of Dodoma Community members. You are required to design the Java application that will store the information of all people under the following requirements.
a. Declare abstract class Person with name, address, phone number, and email address as its instance variables. The class should have a constructor that will initialize these instance variables. Include toString() method that return each instance variables to other classes. (4 Marks)
b. Make student and employee subclasses of Person class. A student has a class status as undergraduate, postgraduate, or diploma while an employee class has an office, salary, and date hired. For each class include the constructor that will initialize the instance variables and make use of person details. In student… (QN 7b Incomplete : Please Submit if Available)
END OF EXAMINATION PAPER