SlideShare a Scribd company logo
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
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]
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);
}
}
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.
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]
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;
}
}
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]
}
}
}
}

More Related Content

DOCX
Examf cs-cs141-2-17 solution
PPTX
cpp-2013 #15 Databases
PDF
3 handouts section2-4
DOCX
Cs141 mid termexam2_fall2017_v1.1
DOC
Cs141 mid termexam v1
DOCX
Cs141 mid termexam2_v1
DOCX
Examf cs-cs141-2-17
DOCX
Cs141 mid termexam2_v1answer
Examf cs-cs141-2-17 solution
cpp-2013 #15 Databases
3 handouts section2-4
Cs141 mid termexam2_fall2017_v1.1
Cs141 mid termexam v1
Cs141 mid termexam2_v1
Examf cs-cs141-2-17
Cs141 mid termexam2_v1answer

Similar to Cs141 mid termexam2_fall2017_v1.1_solution (20)

PDF
Mid1 cs141-1-17-1-final version
DOCX
Cs141 final exam-143810-v2
DOCX
Assignment 7
DOCX
Cs141 mid termexam v5_solution
DOCX
Cs141 mid1-2017-fall-solution2
DOCX
Cs141 mid termexam v3
PPT
Chapter 13 - Inheritance and Polymorphism
PPT
Java căn bản - Chapter13
PDF
Basic program in java
DOCX
Java Program
PPT
Java Inheritance
DOCX
Uta005
DOCX
Unit-3 Practice Programs-5.docx
PDF
E3
PPSX
Java.lang.object
PPT
Conceitos Fundamentais de Orientação a Objetos
PPT
Questões de Certificação SCJP
KEY
About java
DOCX
JAVAPGMS.docx
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
Mid1 cs141-1-17-1-final version
Cs141 final exam-143810-v2
Assignment 7
Cs141 mid termexam v5_solution
Cs141 mid1-2017-fall-solution2
Cs141 mid termexam v3
Chapter 13 - Inheritance and Polymorphism
Java căn bản - Chapter13
Basic program in java
Java Program
Java Inheritance
Uta005
Unit-3 Practice Programs-5.docx
E3
Java.lang.object
Conceitos Fundamentais de Orientação a Objetos
Questões de Certificação SCJP
About java
JAVAPGMS.docx
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
Ad

Recently uploaded (20)

PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
Trump Administration's workforce development strategy
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
Computer Architecture Input Output Memory.pptx
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
20th Century Theater, Methods, History.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
Unit 4 Computer Architecture Multicore Processor.pptx
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
AI-driven educational solutions for real-life interventions in the Philippine...
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Virtual and Augmented Reality in Current Scenario
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Trump Administration's workforce development strategy
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Computer Architecture Input Output Memory.pptx
A powerpoint presentation on the Revised K-10 Science Shaping Paper
20th Century Theater, Methods, History.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Indian roads congress 037 - 2012 Flexible pavement
Practical Manual AGRO-233 Principles and Practices of Natural Farming
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
B.Sc. DS Unit 2 Software Engineering.pptx
Ad

Cs141 mid termexam2_fall2017_v1.1_solution

  • 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] } } } }