0% found this document useful (0 votes)
7 views21 pages

Final 246

The document outlines the final exam details for the Object Oriented Design with Java course, including the date, time, and integrity statement regarding exam conduct. It contains various sections with questions related to Java programming concepts, including multiple-choice questions, code tracing, and implementation tasks for classes such as Person, Manager, and Employee. The exam is structured to assess students' understanding of object-oriented programming principles and their ability to apply these concepts in Java.

Uploaded by

MohammadShamun
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)
7 views21 pages

Final 246

The document outlines the final exam details for the Object Oriented Design with Java course, including the date, time, and integrity statement regarding exam conduct. It contains various sections with questions related to Java programming concepts, including multiple-choice questions, code tracing, and implementation tasks for classes such as Person, Manager, and Employee. The exam is structured to assess students' understanding of object-oriented programming principles and their ability to apply these concepts in Java.

Uploaded by

MohammadShamun
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/ 21

Department of Computer Science

Object Oriented Design with Java (1501 246)


Final Exam, Spring 2022/2023
DATE: Saturday, May 13, 2023; TIME: 13:30-15:30

Name: ________________________________________ ID#: _____________________ Section: _______

Integrity Statement: Read carefully!


Article (5) of the University By-Laws: In the event, a student violates the rules of exams, disobeys the instructions
of the supervisor of an examination hall, or persistently disrupts peace and quiet, he/she shall be asked to hand in
his/her exam paper and leave the exam hall
“Students are not allowed to take their mobiles/smart watches, air-pods or any blue tooth devices with them into the
exam hall. If a student was caught with her/his mobile in the exam hall, it will be considered as a cheating case
regardless of if she/he used it or not”.
Instructions:
• Closed Books, Closed Notes.
• No E-Dictionaries, No Mobile/Portable Devices, No Smart Watches.
• Answer all the questions in each section.
• Do not forget to write down your name and ID on this paper.
• If you change your answer in MCQ, clearly indicate your last answer.
Outcome-Based Marking Scheme:

Outcome Weight Questions Score

A. Develop, test, and debug computer applications and applets. 20 Part II

Apply advanced concepts of OO programming such as classes and


B. 15 Part I
objects, array manipulation, inheritance, and polymorphism.

C. Apply the concepts of GUI programming and event handling. 5 Part III

D. Use and manipulate Graphics and Graphics2D objects.

TOTAL MARK _________ / 40

1/21
Part I: (15 Marks)
Question 1. [10 marks] Select the correct answer (Each has one mark)

1. What is the output of the following code snippet?

int a = 25;
double b = 76.78956;
String c = "Sharjah";
System.out.println("12345678901234567890");
System.out.printf("%4d %5.2f%n%15s%n”, a, b, c);

a) 12345678901234567890
25 76.78956 Sharjah
b) 12345678901234567890
25 76.78956
Sharjah
c) 12345678901234567890
25 76.79
Sharjah
d) 12345678901234567890
25 76.79 Sharjah

2. Consider the following String object definitions.

String s1 = "Sharjah";
String s2 = new String("Sharjah");
String s3 = "Sharjah";

Which of the following statements is NOT correct?

a) s1 and s2 refer to different String objects.


b) The statement, if ( s1 == s3 ), returns True value
c) s3 refers to an interned instance.
d) This definition generates a syntax error since “new String()” was not used for s1 and
s3.

2/21
3. Consider the following method definition:
public boolean myMethod( const int[] arr1, const int[] arr2 )
{
if ( arr1.length() != arr2.length() )
return false;

for ( int i = 0; i < arr1.length(); i++ )


if ( arr1[i] != arr2[i] )
return false;

return true;
}

Which of the following best describes the behavior of this method?

a) This method checks if the lengths of two arrays are the same.
b) This method checks if the contents of two arrays are the same.
c) This method can also be used for double arrays because of the const keyword.
d) None of the above.

4. Consider the following classes:

public class Main { class myClass {


public static void main(String[] args) { int value = 7;
myClass myObj = new myClass(5); }
System.out.println("value = " + myObj.value);
}

What is wrong with these codes?


a) There is no public or private keyword in the definition for myClass.
b) myObj cannot be created in a static method.
c) There is no constructor in the definition for myClass.
d) There is no get() method for value.

5. Which of the following regarding method overloading is correct?

a) It is exactly the same concept as method overriding.


b) The return type of overladed methods may be different.
c) Overloaded methods have the same name but different parameter lists.
d) Different modifiers can be used when overloading a method.

6. Which of the following concept in Object Oriented refers to the bundling of data with the mechanisms
or methods that hide, access, and operate on the data?

a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation

3/21
7. Consider the following class hierarchy:

Animal

Mammal Bird

Cat Dog Parrot Eagle

Assume the following code is given:

Animal myAnimal = new Cat();


Bird myBird = new Bird();

Which of the following statement is NOT true?

a) myAnimal is an instance of Animal.


b) myAnimal is an instance of Mammal.
c) myBird is an instance of Animal.
d) myAnimal is an instance of Dog.

8. Which of the following is true?

a) A subclass cannot modify the implementation of a method defined in the superclass.


b) Static methods cannot be overridden.
c) Static methods cannot be inherited.
d) A subclass can override a private method of its superclass.

4/21
9. Which of the following defines a legal abstract class?

a) class abstractClass {
abstract void method() {
// method implementation
________;
}
}

b) class abstractClass {
abstract void method ();
}

c) abstract class abstractClass {


abstract void method ();
}

d) abstract class abstractClass {


abstract void method () {
// method implementation
__________;
}
}

10. Which of the following is the correct interface?

a) interface myInterface {
public abstract void printObject();
}

b) interface myInterface {
public abstract void printObject() {
//method implementation
_________;
}
}

c) interface myInterface {
default void printObject();
}

d) interface myInterface {
public static void printObject()
}

5/21
Question 2. [5 marks] Trace the Code

1. What is the output of the following code snippet? [2 marks]

public class Main {


public static void main(String[] args) {

Animal myAnimal = new Animal();


Cat myCat = new Cat();

myAnimal.printAnimal();
myCat.printAnimal();
}
}

public class Cat extends Animal {

private String name = "Cat";

public String getName() {


return this.name;
}
}

public class Animal {

private String name = "Animal";

public String getName() {


return this.name;
}

public void printAnimal() {


System.out.println( getName() );
}
}

Answer:

6/21
2. What is the content of myArray after executing the following code snippet? [2 Marks]

int[] myArray = new int[10];

for ( int i = 0; i < 5; i++ ) {


myArray[i] = 2 * (i + 1);
myArray[10-i-1] = myArray[i] + (10 - i) % 2;
}

Answer:

3. What is the output of the following code snippet? The first line of the output is provided.
[1 Mark]

int num= 96;


double rate = 15.50;

System.out.println("123456789012345");
System.out.printf("%5d %6.2f %n", num, rate);

Answer:

123456789012345
_______________

7/21
Part II (20 marks)
Q1. Implement a Java Application (15 marks)

You are asked to implement a Java application to keep track of which employee is working under which
manager in a company. You need to develop Person, Manager, and Employee classes as described
below. The main() method and its output are given as follows:

public class Main {

public static void main(String[] args) {

Manager[] managers = new Manager[3];


Employee[] employees = new Employee[5];

managers[0] = new Manager("Kasim", 52, "Marketing");


managers[1] = new Manager("Fatima", 45, "Personal");
managers[2] = new Manager("Ahmad", 55, "Production");

System.out.println("Managers:");
for (int i = 0; i < Manager.numberOfManager; i++)
System.out.println(managers[i].toString());

System.out.println();

employees[0] = new Employee("Farid", 23);


employees[1] = new Employee("Aysha", 42);
employees[2] = new Employee("Davud", 33);
employees[3] = new Employee("Hamdan", 52);
employees[4] = new Employee("Aliya", 28);

managers[0].addStaff(employees[0]);
managers[0].addStaff(employees[1]);
managers[0].addStaff(employees[0]);
managers[0].addStaff(employees[2]);
managers[0].addStaff(employees[3]);
managers[1].addStaff(employees[2]);

System.out.println();
System.out.println("Employee:");
for (int i = 0; i < Employee.numberOfEmployee; i++)
System.out.println(employees[i].toString());

System.out.println();
System.out.println("Managers:");
for (int i = 0; i < Manager.numberOfManager; i++)
System.out.println(managers[i].toString());
}

8/21
Output:

Managers:
Manager: Name: Kasim Age: 52 Department: Marketing Number of Employee: 0
Manager: Name: Fatima Age: 45 Department: Personal Number of Employee: 0
Manager: Name: Ahmad Age: 55 Department: Production Number of Employee: 0

Farid allocated successfully...


Aysha allocated successfully...
Farid already allocated to this manager...
Davud allocated successfully...
Manager is full. Hamdan not alocated..
Davud allocated successfully...

Employee:
Employee: Name: Farid Age: 23 Manager: Kasim Department: Marketing
Employee: Name: Aysha Age: 42 Manager: Kasim Department: Marketing
Employee: Name: Davud Age: 33 Manager: Fatima Department: Personal
Employee: Name: Hamdan Age: 52 Manager: Not assigned Department: Not assigned
Employee: Name: Aliya Age: 28 Manager: Not assigned Department: Not assigned

Managers:
Manager: Name: Kasim Age: 52 Department: Marketing Number of Employee: 3
Manager: Name: Fatima Age: 45 Department: Personal Number of Employee: 1
Manager: Name: Ahmad Age: 55 Department: Production Number of Employee: 0

9/21
1.1 [3 marks]
Implement the Person class which stores the name (String) and age (int) of a person. The class has the
following methods:
− A constractor with arguments ( name and age )
− get() and set() methods for the data fields.
− toString() method to return the name and age of the person.
Answer:

10/21
1.2 [6 marks]
Implement the Manager class which extends the Person class and stores the following data fields:
− The total number of managers created so far, numberOfManager.
− The department name of the manager, department.
− A list of Person taken by the manager, employeeList (Assume that a manager can take
up to 3 persons).
− The number of employees allocated to this manager, numberOfEmploee (Initially no
employee is allocated).
The class has the following methods:
− A constractor with arguments ( name, age, and department )
− get() and set() methods for the data fields.
− toString() method to return the name and age of the manager as well as his/her
department and the current number of employees allocated to his/her.
− addStaff() method to assign an employee to the manager.

This method takes an Employee object as an argument.

First, if the manager’s employee list is full it outputs an error message (check the sample
output for the message) and returns false.

Then it checks whether the employee has already been added to the manager’s employee list
(search for the name of the Employee object in the employee list).

If the employee is not on the list;


assign the Employee object to the list,
update the Employee object’s manager field with this Manager object using the
addManager() method of the Employee class (defined in the Employee Class),
output the message as depicted in the sample output, and return true.

If the employee is on the list, output the error message as shown in the sample output and
return false.

11/21
Answer I.1.2:

12/21
Answer I.1.2 (continue):

13/21
1.3 [6 marks]
Implement the Employee class which extends the Person class and stores the following data fields:
− a Manager object, manager, indicating the manager of this employee (The default value is
null).
− The total number of employees created so far, numberOfEmployee.
The class has the following methods:
− A constractor with arguments ( name and age )
− getManager() method which returns the assigned manager’s name using the
getName() method of manager. If manager is null it returns the message “Not
assigned”.
− Department() method which returns the assigned department using the
getDepartment() method of manager. If manager value is null it returns the
message “Not assigned”.
− toString() method to return the name and age of the employee as well as his/her
manager’s name and department (Check the sample output).

Answer I.1.3:

14/21
Answer I.1.3 (continue):

15/21
Q2. Fill in the blanks (5 marks)

Consider the following class definition for the Rectangle class:

public class Rectangle {

private double side1, side2;

Rectangle () {
this(1.0, 1.0);
}

Rectangle( double side1, double side2 ){


setSides(side1, side2);
}

public void setSides(double side1, double side2) {


if (side1 > 1.0 )
this.side1 = side1;
else
this.side1 = 1.0;

if (side2 > 1.0 )


this.side2 = side2;
else
this.side2 = 1.0;
}

public double getSide1() {


return this.side1;
}

public double getSide2() {


return this.side2;
}

public double area() {


return this.side1 * this.side2;
}
public double circumference() {
return 2 * (this.side1 + this.side2);
}
public String toString() {
return "[ Rectangle: side1 = "+this.side1+" side2 = "+this.side2+" ]";
}
}

16/21
The following Test program generates the Sample Output:
public class Main {
public static void main(String[] args) {

Box myBox = new Box( 5.0, 4.0, 6.0 );

System.out.println( myBox.toString() );
System.out.println( "Area: " + myBox.area() );
System.out.println( "Volume: " + myBox.volume() );
}
}

Sample Output:
< Box: [ Rectangle: side1 = 5.0 side2 = 4.0 ], Height: 6.0 >
Area: 148.0
Volume: 120.0

17/21
Fill out the following implementation of a class named Box using the Rectangle class as its superclass.

public class Box ________________ _________________ {

______________ double height;

Box() {
__________________;
this.height = 1.0;
}

Box( double ___________, double ___________, double ___________) {


super(side1, side2);
setHeight(height);
}

_____________ _____________ setHeight(double height) {


if ( height > 0.0 )
______________________ = height;
else
______________________ = 1.0;
}

public double getHeight() {


_______________ ________________;
}

public double area(){


return 2 * ( _______________ + super.getSide1() * _______________ +
super.getSide2() * _______________);
}

public double volume(){


return _______________ * _______________;
}

public String toString(){


return "< Box: " + _______________ + ", Height: " + _______________ + " >";
}
}

18/21
Part III : JavaFX (5 Marks)
MCQ: Select the correct one ( Each has a half mark)

1. Which one of the following is NOT part of a JavaFX program structure?

a. Stage
b. Pane
c. Scene
d. Scanner

2. What is the containment structure used in JavaFX?

a. Stage → Scene → Node


b. Scene → Stage → Node
c. Scene → Node → Stage
d. Stage → Node → Scene

3. Which of the following is true?

a. Pane is a container for JavaFX elements like text, shapes, and text fields.
b. The JavaFX main class implements javafx.application.Application class.
c. A Stage object called primaryStage is automatically created by the Java Virtual Machine
when the JavaFX application is started.
d. A JavaFX program can have only one stage which can accommodate multiple Panes.

4. Which of the following correctly sets the color of the outline of a rectangle object, rect, to red?

a. rect.setFill(color.RED);
b. rect.setStroke(Color.RED);
c. Rectangle.setStroke(color.RED);
d. Rect.setStrokeLine(Color.RED);

5. Which of the following ImageView method or methods is/are used for changing image size associated
with an ImageViwe object?

a. setX() and setY()


b. setFitHeight() and setFitWidth()
c. setLayoutX() and setLayoutY()
d. maxHeight() and maxWidth()

19/21
6. Which of the following is correct?

a. javafx.scene.image.Image objects without a


javafx.scene.image.ImageView object can be used to display an image.
b. A javafx.scene.image.ImageView object cannot be used to display images without
creating a corresponding javafx.scene.image.Image object.
c. javafx.scene.image.ImageView objects can be created from a URL directly.
d. javafx.scene.image.ImageView objects support TIFF image format.

7. Which of the following is NOT correct?

a. FlowPane arranges the nodes horizontally from left to right or vertically from top to bottom
in the order in which they were added.
b. Pane is the base class for layout panes.
c. In a GridPane, the nodes are placed in the specified row and column indices.
d. HBox and VBox lay out their children in a single horizontal row or a single vertical column,
respectively.

8. Which of the following is used for adding a node to a FlowPane pane?

a. pane.add(node,2,3);
b. pane.getChildren().add(node);
c. pane.add(node);
d. pane.getChildren().add(node, 2, 3);

9. Which of the following is NOT correct for registering a source for an action event?

a. source.setOnActionHandler( myHandlerObject );

b. source.setOnAction( new HandlerClass() );

c. source.setOnAction( new EventHandler<Action Event> () {


public void handle(ActionEvent e ) {
//Handling the event
______________________;
}
}

d. source.setOnAction( e -> {
//Handling the event
______________________;
});

20/21
10. Which of the following will change the text of a Button node, bt, between “OK” and “CANCEL” after
clicking the mouse?

a. bt.changeText(“OK”, “CANCEL”, setOnMousePressed() );

b. bt.setOnMouseClicked(
bt.setText(“OK”), bt.setText(“CANCEL”) );

c. bt.setOnAction( e -> {
if ( bt.getText() == “OK”)
bt.setText(“CANCEL”);
else
bt.setText(“OK”);
});

d. bt.setOnMouseClicked ( e-> {
bt.changeText(“OK”, “CANCEL”);
});

21/21

You might also like