1. Imam University | CCIS | Vice Deanery of Development and Quality
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: Haifa Aldail, Sarah Alhasan and Shahad Alqefari
Exam: Midterm 1
Semester: 1st
semester
Date: Nov 22, 2016
Duration: 75 minutes
Marks: 20
Privileges: ☐ Open Book
☐ Calculator Permitted
☐ Open Notes
☐ Laptop Permitted
Student Name (in English):
Student ID:
Section No.:
Instructions:
1. Answer ALL questions; there are 4 questions in 8 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 3
4 10
Total 20
2. Imam University | CCIS | Vice Deanery of Development and Quality
Doc. No. 006-01-20140514
Student Name (in English): __________________________________________ Student ID: _____________________________
Page 2 of 8
Fill the table with ONE choice:
1 2 3 4
1. According to the given program, if we want to print
the book subject and duration, which of the given
statements is INCORRECT:
a. System.out.print(r.getSubject()+ r.getDuration());
b. System.out.print(r.getSubject()+ a.getDuration());
c. System.out.print(a.getSubject()+ a.getDuration());
d. All of them are correct.
2. Suppose we want all objects of the class Activity to
have a duration of 2. This requirement is best
implemented by modifying int duration in the
declaration to:
a. static const int duration=2;
b. const int duration=2;
c. const int duration;
d. None.
3. The ________ method cannot be overridden:
a. Super
b. Protected Method
c. Final
d. ToString
4. Members with which access specifier are accessible only to the methods of that class?
a. Private.
b. Public.
c. Protected.
d. All the above.
Question 1: Multiple choices [ ] / 4 Marks
class Activity {
protected int duration;
public int getDuration() {
return duration; }
}
class Reading extends Activity
private String subject="How to progarm";
public String getSubject(){
return subject; }
}
public class Test {
public static void main(String[] args) {
Activity a=new Reading();
Reading r=(Reading)a;
//==Printing Subject and Duration==//
}}
3. Imam University | CCIS | Vice Deanery of Development and Quality
Doc. No. 006-01-20140514
Student Name (in English): __________________________________________ Student ID: _____________________________
Page 3 of 8
Find any syntax or runtime errors in the following code and correct:
Note: All classes in the same package
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
package Package;
import static Package.Parking.summerVacation;
class Car{
String type;}
class Parking{
static protected int numBusyParking;
static boolean summerVacation=false;
Car car[];
Parking(int size){
car=new Car[size];}
public static void setnumBusyParking(int numBusyParking){
this.numBusyParking=numBusyParking;}
private int getProfit(){
return Parking.numBusyParking * 10;}
public void addCar(Car newCar,int parkingNum){
car[parkingNum]=newCar;
numBusyParking++;}
}
public class Test{
public static void main(String[] args){
Parking p=new Parking(200);
Car newCar;
p.addCar(newCar, 13);
System.out.print("nIs it summer avcation?"+summerVacation);
System.out.print("nNumber of busy parking:"+ numBusyParking);
System.out.print("nTotal profit at this time:"+p.getProfit());}
}
Question 2: Error Correction [ ] / 3 Marks
4. Imam University | CCIS | Vice Deanery of Development and Quality
Doc. No. 006-01-20140514
Student Name (in English): __________________________________________ Student ID: _____________________________
Page 4 of 8
Line
# Error Correction
5. Imam University | CCIS | Vice Deanery of Development and Quality
Doc. No. 006-01-20140514
Student Name (in English): __________________________________________ Student ID: _____________________________
Page 5 of 8
Consider the following code:
Question 3: Output [ ] /3 Marks
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
class ExpiredDate
{
String Day;
int Date;
ExpiredDate()
{System.out.println("Default ExpiredDate constructor");}
ExpiredDate(String Day,int Date){
this.Day=Day; this.Date=Date;
System.out.println("ExpiredDate constructor");}
}
class Product
{
ExpiredDate obj;
Product(){
System.out.println("Default Product constructor");
obj= new ExpiredDate();}
Product(ExpiredDate obj){
System.out.println("Product constructor");
this.obj= obj;}
}
class Bakery extends Product
{
int Total;
Bakery(){System.out.println("Default Bakery constructor");}
Bakery(int i){
super(new ExpiredDate("Monday",122017));
System.out.println("Bakery constructor");
this.Total=i;}
}
class Bread extends Bakery
{
Bread(){
super(3);
System.out.println("Default Bread constructor");}
Bread(int i){
super(i);
System.out.println("Bread constructor");}
}
Product ExpiredDate
Bread
Bakery
6. Imam University | CCIS | Vice Deanery of Development and Quality
Doc. No. 006-01-20140514
Student Name (in English): __________________________________________ Student ID: _____________________________
Page 6 of 8
Each statement below runs without errors. Write the output for each statement if
there.
At main function Output Mark
Product Obj; 0.5
Obj= new Product(); 1
Obj= new Bread(); 1.5
Question 3: Output [ ] /3 MarksQuestion 3: Output [ ] /3 Marks
7. Imam University | CCIS | Vice Deanery of Development and Quality
Doc. No. 006-01-20140514
Student Name (in English): __________________________________________ Student ID: _____________________________
Page 7 of 8
Based on the following UML and functionalities description, write the following
methods:
EventAcceptPolicy class:
It is an abstract class that contains array of expected visitors
The constructor fills the array of visitors (by default the visitors acceptance
are initialized to false)
printAccptedVisitors() method prints all the information of visitors that are
accepted
EventAcceptAllPolicy class:
The setPolicy() method accept all the visitors.
EventAcceptFirstPolicy class:
The setPolicy() method accepts the first visitors filled in the array with
number numberOfAccepted
1. setPolicy method of EventAcceptPolicy class (1 point)
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Question 4: Write code [ ] /10 Marks
8. Imam University | CCIS | Vice Deanery of Development and Quality
Doc. No. 006-01-20140514
Student Name (in English): __________________________________________ Student ID: _____________________________
Page 8 of 8
2. Constructor method of EventAcceptPolicy class (3 points)
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
3. printAcceptedVisitors method of EventAcceptPolicy class (3 points)
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
4. setPolicy method of EventAcceptFirstPolicy class (3 points)
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………