SlideShare a Scribd company logo
1 of 7
Download to read offline
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

Similar to Cs141 mid termexam2_fall2017_v1.1_solution

Cs141 mid termexam v3
Cs141 mid termexam v3Cs141 mid termexam v3
Cs141 mid termexam v3Fahadaio
 
Cs141 mid termexam v1
Cs141 mid termexam v1Cs141 mid termexam v1
Cs141 mid termexam v1Fahadaio
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfsuresh640714
 
Cs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionCs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionFahadaio
 
Assignment 7
Assignment 7Assignment 7
Assignment 7IIUM
 
Cs141 mid termexam2_v1answer
Cs141 mid termexam2_v1answerCs141 mid termexam2_v1answer
Cs141 mid termexam2_v1answerFahadaio
 
Cs141 mid1-2017-fall-solution2
Cs141 mid1-2017-fall-solution2Cs141 mid1-2017-fall-solution2
Cs141 mid1-2017-fall-solution2Fahadaio
 
C++ 1.MAIN OBJECTIVEThe goal of this project is to design.pdf
C++ 1.MAIN OBJECTIVEThe goal of this project is to design.pdfC++ 1.MAIN OBJECTIVEThe goal of this project is to design.pdf
C++ 1.MAIN OBJECTIVEThe goal of this project is to design.pdfnishadvtky
 
import school.; import school.courses.;public class Main { p.pdf
import school.; import school.courses.;public class Main { p.pdfimport school.; import school.courses.;public class Main { p.pdf
import school.; import school.courses.;public class Main { p.pdfannaiwatertreatment
 
DDD Modeling Workshop
DDD Modeling WorkshopDDD Modeling Workshop
DDD Modeling WorkshopDennis Traub
 
Chapter 8 Polymorphism
Chapter 8 PolymorphismChapter 8 Polymorphism
Chapter 8 PolymorphismOUM SAOKOSAL
 
Inheritance (1)
Inheritance (1)Inheritance (1)
Inheritance (1)sanya6900
 
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdfTeacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdfkareemangels
 
9608 Computer Science Cambridge International AS level Pre-Release May June p...
9608 Computer Science Cambridge International AS level Pre-Release May June p...9608 Computer Science Cambridge International AS level Pre-Release May June p...
9608 Computer Science Cambridge International AS level Pre-Release May June p...Isham Rashik
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdfaplolomedicalstoremr
 
29. Code an application program that keeps track of student informat.pdf
29. Code an application program that keeps track of student informat.pdf29. Code an application program that keeps track of student informat.pdf
29. Code an application program that keeps track of student informat.pdfarishaenterprises12
 
Java questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdfJava questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdfforwardcom41
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 

Similar to Cs141 mid termexam2_fall2017_v1.1_solution (20)

Cs141 mid termexam v3
Cs141 mid termexam v3Cs141 mid termexam v3
Cs141 mid termexam v3
 
Cs141 mid termexam v1
Cs141 mid termexam v1Cs141 mid termexam v1
Cs141 mid termexam v1
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdf
 
Cs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionCs141 mid termexam v5_solution
Cs141 mid termexam v5_solution
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
Cs141 mid termexam2_v1answer
Cs141 mid termexam2_v1answerCs141 mid termexam2_v1answer
Cs141 mid termexam2_v1answer
 
Cs141 mid1-2017-fall-solution2
Cs141 mid1-2017-fall-solution2Cs141 mid1-2017-fall-solution2
Cs141 mid1-2017-fall-solution2
 
C++ 1.MAIN OBJECTIVEThe goal of this project is to design.pdf
C++ 1.MAIN OBJECTIVEThe goal of this project is to design.pdfC++ 1.MAIN OBJECTIVEThe goal of this project is to design.pdf
C++ 1.MAIN OBJECTIVEThe goal of this project is to design.pdf
 
import school.; import school.courses.;public class Main { p.pdf
import school.; import school.courses.;public class Main { p.pdfimport school.; import school.courses.;public class Main { p.pdf
import school.; import school.courses.;public class Main { p.pdf
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
DDD Modeling Workshop
DDD Modeling WorkshopDDD Modeling Workshop
DDD Modeling Workshop
 
Chapter 8 Polymorphism
Chapter 8 PolymorphismChapter 8 Polymorphism
Chapter 8 Polymorphism
 
Inheritance (1)
Inheritance (1)Inheritance (1)
Inheritance (1)
 
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdfTeacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
 
9608 Computer Science Cambridge International AS level Pre-Release May June p...
9608 Computer Science Cambridge International AS level Pre-Release May June p...9608 Computer Science Cambridge International AS level Pre-Release May June p...
9608 Computer Science Cambridge International AS level Pre-Release May June p...
 
static methods
static methodsstatic methods
static methods
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
 
29. Code an application program that keeps track of student informat.pdf
29. Code an application program that keeps track of student informat.pdf29. Code an application program that keeps track of student informat.pdf
29. Code an application program that keeps track of student informat.pdf
 
Java questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdfJava questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdf
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 

Recently uploaded

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 

Recently uploaded (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

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