0% found this document useful (0 votes)
157 views7 pages

Computer Science Department: Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences

The document contains instructions for a midterm exam for a Computer Programming 2 course. The exam contains 3 questions worth a total of 15 marks and lasts 60 minutes. Question 1 asks students to identify the output of a code snippet worth 3 marks. Question 2 asks students to identify compilation errors in another code snippet worth 4 marks. Question 3 contains multiple parts asking students to write classes and methods related to reservations worth a total of 8 marks.

Uploaded by

Nada FS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views7 pages

Computer Science Department: Al Imam Mohammad Ibn Saud Islamic University College of Computer and Information Sciences

The document contains instructions for a midterm exam for a Computer Programming 2 course. The exam contains 3 questions worth a total of 15 marks and lasts 60 minutes. Question 1 asks students to identify the output of a code snippet worth 3 marks. Question 2 asks students to identify compilation errors in another code snippet worth 4 marks. Question 3 contains multiple parts asking students to write classes and methods related to reservations worth a total of 8 marks.

Uploaded by

Nada FS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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 Dr Albathan, Dr Aljammaz, Dr Alsulaiman,
Instructors: Dr Shahin, Dr Sriti, Ms Alawwad, Ms Aldayel,
Ms Aljaloud, Ms Almajed, Ms Almedlej,
Ms Alqefari
Exam: Midterm Exam 1
Semester: Spring 2018
Date: 25/06/1439-13/03/2018
Duration: 60 minutes
Marks: 15
Privileges: ☐Open Book ☐Open Notes
☐Calculator Permitted ☐Laptop Permitted

Student Name: MODEL ANSWER

Student ID:
Section No.:

Instructions:

1. Answer 3 questions; there are 3 questions in 4 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 3
2 4
3 8
Total 15

Page 1 of 7

Imam University | CCIS | Doc. No. 006-02-20170316


Question 1: To be answered in (10) Minutes [ ] / 3 Marks

What is the output of the following code?

public class Quiz {


public static double[] marks = new double[5];

public Quiz() { this (0.0, 0); }

public Quiz(double mark) { this(mark, 0); }

public Quiz(double mark, int position)


if (position >= 0 && position < marks.length)
marks[position] = mark;
}

public static void main(String[] args) {


Quiz q = new Quiz();
q = new Quiz(4);
q = new Quiz(6, 2);
int sum = 0;
for (double m: Quiz.marks) {
sum += m;
System.out.println("sum: " + sum);
}
System.out.println("avg: " + sum/ Quiz.marks.length);
}
}

Answer: (0.5 mark for each correct line)

sum: 4
sum: 4
sum: 10
sum: 10
sum: 10
avg: 2

Page 2 of 7

Imam University | CCIS | Doc. No. 006-02-20170316


Question 2: To be answered in (15) Minutes [ ] / 4 Marks

In the following code (B.java), there are 4 compilation errors: 1 error in class A and 3 errors in class B.
List the line number and the cause of each error in the table below.

1 // file B.java
2 class A
3 {
4 public int x;
5 private int y;
6 protected int z;
7
8 private final int a;
9
10 public A(int x, int y, int z) {
11 this.x = x;
12 this.y = y;
13 this.z = z;
14 }
15 }
16
17 public class B extends A
18 {
19 public B() {
20 }
21
22 @Override
23 public void print() {
24 System.out.println(x + y + z);
25 }
26 }

Answer: (0.25 mark for each correct line / 0.75 mark for each correct cause)
Error # Class Line # Cause of the error
1 A 8 (accepted lines: 10, or 14) constant a is not initialized

2 B 19 No non-argument (nor default) superclass constructor is defined

3 B 22 (accepted line: 23) Method print() doesn’t override superclass


method
4 B 24 Variable y has private access in the superclass A

Page 3 of 7

Imam University | CCIS | Doc. No. 006-02-20170316


Question 3: To be answered in (35) Minutes [ ] / 8 Marks

For all of the following classes, you don't have to write any getter or setter method. But, you can
consider these methods existing and call them whenever you need.

a) Write a class Reservation where the number of days is initialized by its unique constructor.
Define one method calculatePrice without body that calculates and returns the price based on
the received double variable unitPrice. (2 marks)

Answer a):

public abstract class Reservation // 0.5 mark
{
protected int numberOfDays; // 0.25 mark

public Reservation(int numberOfDays) { // 0.5 mark


this.numberOfDays = numberOfDays;
}

// 0.75 mark
public abstract double calculatePrice(double unitPrice);
}







Page 4 of 7

Imam University | CCIS | Doc. No. 006-02-20170316


b) Write a class HotelReservation, a subclass of Reservation, where the number of days and
number of rooms are initialized by its unique constructor. Override the super class method
calculatePrice that calculates and returns the price, based on the received unit price, by
multiplying the unit price by number of rooms by number of days. (2 marks)

Answer b):

public class HotelReservation extends Reservation // 0.50 mark
{
private int numberOfRooms; // 0.25 mark

public HotelReservation(int numberOfDays, int numberOfRooms) {// 0.50 mark


super(numberOfDays);
this.numberOfRooms = numberOfRooms;
}

@Override
public double calculatePrice(double unitPrice) { // 0.75 mark
return unitPrice * numberOfRooms * numberOfDays; // getNumberOfDays()
}
}

Page 5 of 7

Imam University | CCIS | Doc. No. 006-02-20170316


c) Write a class CarReservation, a subclass of Reservation, where the number of days and age of
the driver are initialized by its unique constructor. Override the super class method
calculatePrice that calculates and returns the price based on the driver age; returns unit price
multiplied by number of days, but, in the case where the driver age is less than 25, the calculated
price is increased by 20%. (2 marks)

Answer c):

public class CarReservation extends Reservation // 0.50 mark
{
private int driverAge; // 0.25 mark

public CarReservation(int numberOfDays, int driverAge) { // 0.50 mark


super(numberOfDays);
this.driverAge = driverAge;
}

@Override
public double calculatePrice(double unitPrice) { // 0.75 mark
double result = unitPrice * numberOfDays; // getNumberOfDays()
if (driverAge < 25)
result *= 1.2;
return result;
}
}

Page 6 of 7

Imam University | CCIS | Doc. No. 006-02-20170316


d) Consider the following code, complete the class Package to print the value of the calculated price
of each created instance by iterating the predefined array using a loop. When the reservation is
about a hotel the unit price is 200, but when is about a car the unit price 100. (2 marks)

You must produce an output similar to the following:



Total price hotel: 800.0
Total price car: 300.0
Total price car: 360.0


Answer d):

public class Package {
public static void main(String[] args) {
Reservation[] pack = new Reservation[3];
pack[0] = new HotelReservation(2, 2);
pack[1] = new CarReservation(3, 55);
pack[2] = new CarReservation(3, 23);

for(Reservation r: pack) // 0.50 mark


if (r instanceof HotelReservation) // 1.00 mark
System.out.println("Total price hotel: " + r.calculatePrice(200));
else //if (r instanceof CarReservation) // 0.50 mark
System.out.println("Total price car: " + r.calculatePrice(80));
}
}

Page 7 of 7

Imam University | CCIS | Doc. No. 006-02-20170316

You might also like