M251-M257
Object Oriented Programming Using
Java
Final Examination – KSA
Spring Semester 2023/2024
Date: 6 / May / 2024
Number of Exam Pages: 4 Time Allowed: 3 hours
(including this cover sheet)
Instructions:
1. This exam consists of three parts. It is required to solve ALL of them in the
external answer booklet:
• Part 1: 5 MCQ (10 marks)
• Part 2: 3 short answer questions (35 marks)
• Part 3: 2 problem solving questions (55 marks)
2. Go through all questions before you start answering them.
3. It is not allowed to use any external material.
4. It is not allowed to use any electronic device (calculators, mobiles, …)
5. At the end of the examination:
• Check that you have written your name, student ID and section number
on the first page of your answer booklet
• Place your question paper in the answer booklet and hand over them to the
invigilator.
M251-M257 / Final – KSA Page 1 of 4 2023/2024 Spring
PART 1: MCQ [10 Marks: 5x2]
Question 1: [10 marks] Read the following questions and choose the best answer.
1. Which of the following could have abstract methods?
a. concrete class b. abstract class c. interface
d. A and B e. B and C
2. Which of the following is a checked Exception?
a. FileNotFoundException b. EOFException c. MalformedURLException
d. All the above e. None of the above
3. Given the ArrayList list = [2, 6, 1, 5].
What are the contents of list after the following operation? list.set(1, 3);
a. [2, 6, 1, 1] b. [2, 3, 1, 5] c. [2, 6, 3, 5]
d. [2, 3, 6, 1, 5] e. [2, 6, 3, 1, 5]
4. _________ is the best choice when the user could choose more than an item from a small group of
choices.
a. CheckBox b. RadioButton c. TextField
d. Label e. A and B
5. _________ method moves the thread from the running state to the runnable state.
a. notify b. wait c. sleep
d. yield e. run
PART 2: Short Answer Questions [35 Marks]
Question 2: [12 marks: 2+2+2+2+4]
Given the following code, answer the given questions:
public abstract class Employee { public class Test {
public void welcome() { public static void main(String[] a) {
System.out.println("AOU"); Employee e = new Tutor();
} Tutor t = new Tutor();
public void salary() { // valid or not?
System.out.println(5000); //1// e.teaching();
} //2// Employee f = new Employee();
} e.welcome();
public class Tutor extends Employee { t.welcome();
public void salary() { e.salary();
System.out.println(8000); t.salary();
} }
public void teaching() { }
System.out.println("9 sections");
}
}
A. The relationship between the classes Employee and Tutor is called _________.
B. _________ method is an example of overriding.
C. Is the code in the line that starts with the comment //1// valid? Explain.
D. Is the code in the line that starts with the comment //2// valid? Explain.
E. Provide the exact output of the class Test.
M251-M257 / Final – KSA Page 2 of 4 2023/2024 Spring
Question 3: [13 marks]
Assume that you have a map called section which includes students’ names and their marks in the final
exam. It is required to complete the following method to add 1 mark for each student; For example, if a
student got 83.5 in the final exam, then after calling the method his/her mark will be updated to 84.5, noting
that no student could get more than 100 (i.e. all students whose mark > 99 will not get more than 100).
public static …………… addOneMark(………………………………… section) {
…………………………………
…………………………………
}
Question 4: [10 marks]
Write a recursive method that takes as an argument a word and returns the reverse of this word; For
example, if the word is "EXAM", the method should return "MAXE".
PART 3: Problem Solving Questions [55 Marks]
Question 5: [42 marks]
1. Write the class Member which includes: [15 marks: 2+3+2+2+4+2]
a) 3 attributes: member id (e.g. 935), member name and member age.
b) a zero-arg constructor and a multi-arg constructor.
c) getters for member name and member age attributes.
d) toString() method which overrides the Object's toString() method.
e) equals() method which overrides the Object's equals() method. It is enough to compare the member
id.
f) compareTo() method which implements the Comparable’s compareTo() method. You should use the
member id only in the comparison.
2. Write the class Club which includes: (add the necessary import statements) [22 marks: 1+2+4+8+7]
a) 2 attributes: Club name and Set of Members.
b) one-arg constructor that accepts the Club name as an argument and creates a new empty Set of
Members (the set should be sorted automatically according to the member id).
c) method addMember() that accepts a Member object as an argument and adds it to the Set with a
confirmation message. If the Member is already in the Set, an error message should be displayed.
d) method oldestMember() that takes no arguments and returns the name of the oldest member in the
Club (whose age is the maximum).
e) method saveIntoFile() which accepts a file name as an argument. It stores the Club name and
information of all members of the club in this file (each Member in a different line).
3. Write a code to test your classes by performing the following tasks: [5 marks: 1+2+1+1]
a) create a Club object called myClub where the Club name is "Math Club".
b) create 2 Members and add them to myClub using the method addMember()
c) print the name of the oldest member in the Club by calling the method oldestMember()
d) call the method saveIntoFile() to save the required information.
M251-M257 / Final – KSA Page 3 of 4 2023/2024 Spring
Question 6: [13 marks: 6 + 7]
Based on the classes of Part 3 - Question 5,
Consider the following code which displays the given GUI application and then answer the questions below
it:
public void start(Stage primaryStage) {
//Missing Code (1): Initialization
Club myClub = …………………
Label memberIdL = …………………
Label memberNameL = …………………
Label memberAgeL = …………………
TextField memberIdTF = …………………
TextField memberNameTF = …………………
TextField memberAgeTF = …………………
Button addB = …………………
GridPane pane = …………………
primaryStage.setTitle(…………………);
pane.addColumn(0, memberIdL, …………………, …………………, …………………);
pane.addColumn(1, memberIdTF, …………………, …………………);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
//Missing Code (2): Clicking on the button
…………………
…………………
…………………
}
1. Complete the first missing code to initialize the UI controls.
2. Complete the second missing code such that when the user fills in the required data and clicks the
button "Add Member", a Member object will be created (using this data) and added to myClub.
End of Questions
M251-M257 / Final – KSA Page 4 of 4 2023/2024 Spring
M251-M257
Object Oriented Programming Using Java
Final Examination – Solution Key – KSA
Spring Semester 2023/2024
Part 1 – Question 1 (MCQ): [10 marks: 5x2]
1. e. B and C
2. d. All the above
3. b. [2, 3, 1, 5]
4. a. CheckBox
5. d. yield
Part 2: [35 marks]
Question 2: [12 marks: 2+2+2+2+4]
Answer: 2 marks for each part (except E; 4 marks: 1 for each line)
A. inheritance //or is-a
B. salary()
C. Invalid, e declared as Employee and there is no method teaching() in this class
D. Invalid, abstract class cannot be instantiated
E. //output:
AOU
AOU
8000
8000
M251-M257 / Final_Key – KSA Page 1 of 5 2023/2024 Spring
Question 3: [13 marks]
Answer: 13 marks as in the comments
public static void addOneMark(HashMap<String, Double> section) { //2 or any kind of Map
for (String name: section.keySet()) { //3
double newMark = section.get(name) + 1; //2
if (newMark > 100) //2
newMark = 100; //2
section.put(name, newMark); //2
} //end for
}
Question 4: [10 marks]
Answer: 10 marks as in the comments
public static String reverse(String w) { //2 no marks for public static
int len = w.length(); //no marks
if (len < 2) //2
return w; //2
else //no marks
return reverse(w.substring(1)) + w.charAt(0);
//4: reverse, w.substring(1), +, w.charAt(0)
//or return w.charAt(len-1) + reverse(w.substring(0,len-1));
}
Part 3: [55 marks]
Question 5: [42 marks: 15+22+5]
Answer:
//15 marks as in the comments
public class Member implements Comparable<Member> { //1 for implements
//1 for declaring attributes
private int memberId;
private String name;
private int age;
//1 for 0-arg constructor
public Member() { }
//2 for multi-arg constructor
public Member(int memberId, String name, int age) {
this.memberId = memberId;
this.name = name;
this.age = age;
}
//2 for getters
public String getName() { return name; }
public int getAge() { return age; }
M251-M257 / Final_Key – KSA Page 2 of 5 2023/2024 Spring
@Override //2 for toString() method
public String toString() {
return "Member ID: " + memberId + ", name: " + name + ", age: " + age;
}
@Override //4: 1 for each line
public boolean equals(Object obj) {
if (!(obj instanceof Member)) return false;
Member m = (Member) obj;
return memberId == m.memberId;
}
@Override //2: 1 for each line
public int compareTo(Member m) {
return memberId - m.memberId; //or using if-else
}
}
//22 marks as in the comments
import java.io.*;
import java.util.*;
public class Club {
//1 for attributes
private String name;
private TreeSet<Member> members;
//2 for the constructor
public Club(String name) {
this.name = name;
members = new TreeSet<>();
}
//4: 1 for each line, but only 0.5 for each printing line
public void addMember(Member m) {
if (members.add(m)) //student could use the method contains to check before adding
System.out.println("Member has been added successfully");
else
System.out.println("Member has been added before");
}
//8: 1 for each line
public String oldestMember() {
int max = -1;
String oldName = null; //or oldName = ""
for (Member m: members) //we cannot use the usual for
if (m.getAge() > max) {
max = m.getAge();
oldName = m.getName();
}
return oldName;
}
//7: 2 for the first line and 1 for each other line
public void saveIntoFile(String fileName) throws FileNotFoundException {
PrintWriter out = new PrintWriter(fileName);
out.println(name);
M251-M257 / Final_Key – KSA Page 3 of 5 2023/2024 Spring
for (Member m: members) //we cannot use the usual for
out.println(m);
out.close();
}
}
//5 marks as in the comments
import java.io.FileNotFoundException;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
//1
Club myClub = new Club("Math Club");
//2
Member m1 = new Member(321, "Nader", 23); myClub.addMember(m1);
Member m2 = new Member(245, "Sara", 21); myClub.addMember(m2);
//1
System.out.println("Oldest member is: " + myClub.oldestMember());
//1
myClub.saveIntoFile("M251.txt");
}
}
Question 6: [13 marks: 6+7]
Answer: //13 marks as in the comments
public void start(Stage primaryStage) {
//Missing Code (1): Initialization
//1
Club myClub = new Club("Math Club");
//1
Label memberIdL = new Label("Member ID");
Label memberNameL = new Label("Member name");
Label memberAgeL = new Label("Member age");
//1
TextField memberIdTF = new TextField();
TextField memberNameTF = new TextField();
TextField memberAgeTF = new TextField();
//1
Button addB = new Button("Add Member");
//1
GridPane pane = new GridPane();
primaryStage.setTitle("Math Club");
//1
pane.addColumn(0, memberIdL, memberNameL, memberAgeL, addB);
pane.addColumn(1, memberIdTF, memberNameTF, memberAgeTF);
M251-M257 / Final_Key – KSA Page 4 of 5 2023/2024 Spring
//Missing Code (2): Clicking on the button
//7: 1 for each line and 1 for Integer.valueOf()
addB.setOnAction(e -> { //or addB.setOnMouseClicked(e-> {
int memberId = Integer.valueOf(memberIdTF.getText()); //or Integer.parseInt(...)
String name = memberNameTF.getText();
int age = Integer.valueOf(memberAgeTF.getText());
Member m = new Member(memberId, name, age);
myClub.addMember(m);
});
}
End of Questions
M251-M257 / Final_Key – KSA Page 5 of 5 2023/2024 Spring