1. Imam University | CCIS
Doc. No. 006-01-20140514
Page 1 of 7
Al Imam Mohammad Ibn Saud Islamic University
College of Computer and Information Sciences
Computer Science Department
Course Title: Computer Programming 2
Course Code: CS141
Course Instructor: Dr. Ahmed Khorsi, Dr. Ashraf Shahin, Dr. Yassin Daada,
Dr. Adel Ammar, Dr. Aram Alsedrani, Mse. Ebtesam
Alobood, Mse. Mai Alammar, Mse. Hessa Alawad, Mse.
Shahad Alqefari
Exam: Second Midterm
Semester: Fall 2017
Date:
Duration: 60 Minutes
Marks: 15
Privileges: ☐ Open Book
☐ Calculator Permitted
☐ Open Notes
☐ Laptop Permitted
Student Name (in English):
Student ID:
Section No.:
Instructions:
1. Answer 3 questions; there are 3 questions in 7 pages.
2. Write your name on each page of the exam paper.
3. Write your answers directly on the question sheets. Use the ends of the question pages
for rough work or if you need extra space for your answer.
4. If information appears to be missing from a question, make a reasonable assumption,
state your assumption, and proceed.
5. No questions will be answered by the invigilator(s) during the exam period.
Official Use Only
Question Student Marks Question Marks
1 4
2 3
3 8
Total 15
2. Imam University | CCIS
Doc. No. 006-01-20140514
Page 2 of 7
Student Name (in English): __________________________________________ Student ID: _____________________________
Question 1: To be answered in (20) Minutes [ ] / 4 Marks
1. What is the output of the following code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Sound.java
public interface Sound { public void greeting();}
//Cat.java
public class Cat implements Sound {
public void greeting() {System.out.println("Meow!");}
}
//Dog.java
public class Dog implements Sound {
public void greeting() {System.out.println("Woof!");}
public void greeting(Dog another) {System.out.println("Woooof!");}
}
//BigDog.java
public class BigDog extends Dog {
public void greeting() {System.out.println("Woow!");}
public void greeting(Dog another)
{System.out.println("Wooowww!");}
}
// SoundTest.java
public class SoundTest {
public static void main(String[] args) {
Sound animal1 = new Cat();
animal1.greeting();
Sound animal2 = new Dog();
animal2.greeting();
Sound animal3 = new BigDog();
animal3.greeting();
BigDog bigDog1 = new BigDog();
Dog dog2 = (Dog)animal2;
try {
BigDog bigDog2 = (BigDog)animal3;
Dog dog3 = (Dog)animal3;
dog2.greeting(dog3);
dog3.greeting(dog2);
dog2.greeting(bigDog2);
bigDog2.greeting(dog2);
bigDog2.greeting(bigDog1);}
catch (ClassCastException e) {
System.err.println("Class cast exception 1 occured");}
try {
BigDog dog4 = (BigDog) animal2;
dog4.greeting(dog4); }
catch (ClassCastException e) {
System.err.println("Class cast exception 2 occured");}
catch (Exception e) {
System.err.println("Exception ?");
}}
}
Answer:
Meow! [0.25]
Woof! [0.25]
Woow! [0.25]
Woooof! [0.5]
Wooowww! [0.5]
Woooof! [0.5]
Wooowww! [0.5]
Wooowww! [0.5]
Class cast exception 2 occured
[0.75]
[Any additional output: -0.5]
3. Imam University | CCIS
Doc. No. 006-01-20140514
Page 3 of 7
Student Name (in English): __________________________________________ Student ID: _____________________________
Question 2: To be answered in(15) Minutes [ ] / 3 Marks
In the following code, there are one compilation error in each code segment. List the line number
and the cause of the error.
Question
#
Line # Cause of the error
1
4
[0.25 mark]
Keyword “new” missing after “throw”. [0.75 mark]
2
10
[0.25 mark]
addA() overrides a method with a different signature. [0.75 mark]
3
7
[0.25 mark]
Class C must be declared abstract, because it does not implement the
method print() of the interface with the same signature. [0.75 mark]
1)
1
2
3
4
5
6
public class ExceptionExample {
void method() throws ArithmeticException{
throw ArithmeticException("ArithmeticException Occurred");
}
}
2)
1
2
3
4
5
6
7
8
9
10
11
12
13
//A.java
public class A {
public void addA(String x) {
System.out.println(x);
}
}
//B.java
public class B extends A{
@Override
public void addA() {
System.out.println(“Hello”);
}
}
3)
1
2
3
4
5
6
7
8
9
10
11
//A.java
public interface A {
public void print();
}
//C.java
public class C implements A {
public void print(String s) {
System.out.println(s);
}
}
4. Imam University | CCIS
Doc. No. 006-01-20140514
Page 4 of 7
Student Name (in English): __________________________________________ Student ID: _____________________________
Question 3: To be answered in(45) Minutes [ ] / 8 Marks
Consider the class hierarchy below:
1) Write the code of the class Student. The method toString only returns a string containing the
student’s name and address. The method addCourseGrade adds a new course and its grade to the
arrays courses and grades respectively. A student takes no more than 30 courses for the entire
program. If addCourseGrade is called while the student has already 30 courses, it should throw an
exception. You are not required to write classes Person and Teacher.
2) Write a driver class PersonTest that does the following:
i. Create an array that contains 3 students and 2 teachers.
ii. Add one course and its grade for each student.
iii. For each element of the array:
a. Call toString() polymorphically.
b. If the array’s element is a student, print his grades.
5. Imam University | CCIS
Doc. No. 006-01-20140514
Page 5 of 7
public class Student extends Person {
// private instance variables [0.5 mark]
private int numCourses; // number of courses taken so far
private String[] courses; // course codes
private int[] grades; // grade for the corresponding course codes
private static final int MAX_COURSES = 30; // maximum number of courses
// Constructor [0.5 mark]
public Student(String name, String address) {
super(name, address);
numCourses = 0;
courses = new String[MAX_COURSES];
grades = new int[MAX_COURSES];
}
@Override
public String toString() { [0.25 mark]
return "Student: " + super.toString();
}
// Add a course and its grade
public void addCourseGrade(String course, int grade) {
if (numCourses>MAX_COURSES) [0.25 mark]
{throw new IllegalArgumentException ("Maximum number of courses reached");}
courses[numCourses] = course; [0.25 mark]
grades[numCourses] = grade; [0.25 mark]
++numCourses;} [0.25 mark]
6. Imam University | CCIS
Doc. No. 006-01-20140514
Page 6 of 7
// Print all courses taken and their grade
public void printGrades() { [0.75 mark]
for (int i = 0; i < numCourses; ++i) {
System.out.println(" " + courses[i] + ":" + grades[i]);
}
}
// Compute the average grade
public double getAverageGrade() { [1 mark]
int sum = 0;
for (int i = 0; i < numCourses; i++ ) {
sum += grades[i];
}
return (double)sum/numCourses;
}
}
7. Imam University | CCIS
Doc. No. 006-01-20140514
Page 7 of 7
public class PersonTest {
public static void main(String[] args) {
Student s1 = new Student("Ali", "Riyadh"); [0.25 mark]
Student s2 = new Student("Ahmed", "Makkah"); [0.25 mark]
Student s3 = new Student("Omar", "Taif"); [0.25 mark]
Teacher t1 = new Teacher("Abd-Allah", "Riyadh"); [0.25 mark]
Teacher t2 = new Teacher("Mohammad", "Riyadh"); [0.25 mark]
Person[] personArray = {s1,s2,s3,t1,t2}; [0.25 mark]
s1.addCourseGrade("CS141", 97); [0.25 mark]
s2.addCourseGrade("CS141", 68); [0.25 mark]
s3.addCourseGrade("CS141", 88); [0.25 mark]
for (Person person: personArray)
{
System.out.println(person.toString()); [0.5 mark]
if (person instanceof Student) { [0.5 mark]
Student student = (Student) person; [0.5 mark]
student.printGrades(); [0.25 mark]
}
}
}
}