1. Imam University | CCIS
Doc. No. 006-01-20140514
Page 1 of 8
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
3. Imam University | CCIS
Doc. No. 006-01-20140514
Page 3 of 8
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:
4. Imam University | CCIS
Doc. No. 006-01-20140514
Page 4 of 8
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
2
3
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);
}
}
5. Imam University | CCIS
Doc. No. 006-01-20140514
Page 5 of 8
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.
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................