1. Imam University | CCIS
Doc. No. 006-01-20140514
Page 1 of 9
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 Ashraf A. Shahin, Dr. Alaa Eldeen Ahmed, Dr.
Qaisar Abbas, Dr. Talal Albalawi, Dr. Sarah Alhassan,
Dr. Mai Al-Ammar, Dr. Dania Alomar, Dr. Mashael
Almedlej, Dr. Shahad Alqefari, Dr. Aram Sedrani
Exam: Second Midterm
Semester: Spring 2017
Date: 2nd May 2017
Duration: 90 Minutes
Marks: 15
Privileges: ☐ Open Book
☐ Calculator
Permitted
☐ Open Notes
☐ Laptop Permitted
Student Name (in
English): MODEL ANSWER
Student ID:
Section No.:
Instructions:
1. Answer 3 questions; there are 3 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 5
3 6
2. Imam University | CCIS
Doc. No. 006-01-20140514
Page 2 of 9
Total 15
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 1: To be answered in (20) Minutes [ ] / 4 Marks
1. What is the output(s) of the following code segment?
3. Imam University | CCIS
Doc. No. 006-01-20140514
Page 3 of 9
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
//Employee.java
abstract class Employee{
private String name;
public Employee (String name) {
this.name = name;
System.out.print(name + " ");
}
public abstract void getSalary();
}
//HEmployee.java
class HEmployee extends Employee{
private int salary;
public HEmployee(String name, int salary) {
super(name);
this.salary = salary;
}
@Override
public void getSalary (){
System.out.println(salary*10);
}
}
//WEmployee.java
class WEmployee extends Employee{
private int salary;
public WEmployee(String name, int salary) {
super(name);
this.salary = salary;
}
@Override
public void getSalary (){
System.out.println(salary*4);
}
}
//TestApp.java
class TestApp {
public static void main(String[] args) {
Employee e1, e2;
e1 = new HEmployee("Ali", 200);
e1.getSalary();
e2 = new WEmployee("Saad", 1000);
e2.getSalary();
}
}
Answer:
Ali 2000 (1 mark)
Saad 4000 (1 mark)
4. Imam University | CCIS
Doc. No. 006-01-20140514
Page 4 of 9
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 1:
2. What is the output(s) of the following code segment?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//A.java
class A {
public static void main(String[] args) {
int a=0, b=1; int c[] = {1, 2, 3};
try {
System.out.print(c[a/b]);
try {
for(int i=1; i<4; i++)
System.out.print(c[i]);
} catch (ArithmeticException e) {
System.out.print(“4”);
} catch(Exception e) {
System.out.print(“5”);
} finally {
System.out.print(“6”);
}
} catch (Exception e) {
System.out.print(“7”);
} finally { System.out.print(“8”);
}
}
}
Answer
123568 (1 mark)
3. What is the output(s) of the following code segment?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//AA.java
class AA {
static void my() {
int i = 0;
System.out.print("A");
if(i==0)
throw new ArithmeticException("A");
System.out.println("B");
}
public static void main(String args[]) {
try {
my();
} catch (Exception e) {
System.out.print("C");
} finally {
System.out.println("D");
}
}
}
Answer
ACD (1 mark)
5. Imam University | CCIS
Doc. No. 006-01-20140514
Page 5 of 9
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 2: To be answered in(25) Minutes [ ] / 5 Marks
In the following code, there are one compilation error in each code segment. List the line number
and the cause of the error.
(1 mark each if both line # and cause of the error are correct)
Question # Line # Cause of the error
1 8
The class Car should be defined as abstract class because it has an
abstract method
2 10 You can not override final method
3 10
Exception ArrayIndexOutOfBoundsException has already been
caught by Exception. The order of the exception needs subclasses
before the superclasses
4 4 Checked Exception must be caught or declared to be thrown
5 14
Method Print is not a member function of Base class, must perform
down casting to call Print function
1)
1
2
3
4
5
6
7
8
9
10
11
12
//Action.java
public interface Actions {
public void goForward();
public void goBackward();
}
//Car.java
public class Car implements Actions{
public void goForward() {
System.out.println("Car Go Forward");
}
}
2)
1
2
3
4
5
6
7
8
9
10
11
12
13
//A.java
public class A {
public final void addA(String x) {
System.out.println(x);
}
}
//B.java
public class B extends A{
@Override
public void addA(String x) {
System.out.println(“Hello”);
}
}
6. Imam University | CCIS
Doc. No. 006-01-20140514
Page 6 of 9
3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//B.java
class B {
public static void main(String[] args) {
int[] x= new int[5];
int b = 5, c = 4;
try {
x[c/b] = 20;
} catch (Exception e) {
System.out.println(“Exceptions!!!”);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println (“ArrayIndexOutOfBoundsException!!!”);
}
}
}
4)
1
2
3
4
5
6
7
8
9
//A.java
class A {
public A () {
throw new Exception(“class A Exception”);
}
public static void main(String[] args) {
A a = new A();
}
}
5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Base.java
class Base {
}
//Derive.java
class Derive extends Base {
public void print(){
System.out.println (“Hi");
}
}
//Test.java
class Test{
public static void main(String[] args) {
Base B = new Derive ();
B.Print();
}
}
7. Imam University | CCIS
Doc. No. 006-01-20140514
Page 7 of 9
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 3: To be answered in(45) Minutes [ ] / 6 Marks
Using java classes, write a program for a Vending Machine. Each Vending Machine has a name,
numberOfCups, and amountOfWater. The vending machine has two types: OrangeJuiceMachine and
CoffeeMachine. Each OrangeJuiceMachine has number of oranges, and each CoffeeMachine has
amount of coffee. Each cup in OrangeJuiceMachine needs two oranges and 100 grams of water and
each cup in CoffeeMachine needs 7 grams of coffee and 200 grams of water. Each machine type should
override inherited abstract method makeDrink to update its data members.
Please note the following:
Data members in the superclass are protected, and the rest are private.
Write the parametrized constructer in the required classes.
Each class should override toString() method to return its data.
No need to write the main method.
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
public abstract class VendingMachine {
protected String name;
protected int numberOfCups;
protected double amountOfWater;
public VendingMachine(String name, int numberOfCups, double amountOfWater) {
this.name = name;
this.numberOfCups = numberOfCups;
this.amountOfWater = amountOfWater;
}
public abstract void makeDrink();
@Override
public String toString() {
return "Name: " + name + " Number of cups: " + numberOfCups +
" Water Amount: " + amountOfWater;
}
}
0.5 mark
0.5 mark
0.5 mark
0.5 mark
8. Imam University | CCIS
Doc. No. 006-01-20140514
Page 8 of 9
........................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
public class CoffeeMachine extends VendingMachine
{
private double amountOfCoffee;
public CoffeeMachine(String name, int numberOfCups, double amountOfWater, double
amountOfCoffee ) {
super(name, numberOfCups, amountOfWater);
this.amountOfCoffee = amountOfCoffee;
}
@Override
public void makeDrink() {
numberOfCups--;
amountOfWater -= 200;
amountOfCoffee -= 7;
}
@Override
public String toString() {
return super.toString() + " Amount Of Coffee: " + amountOfCoffee;
}
}
public class OrangeJuiceMachine extends VendingMachine
{
private int numberOfOrages;
public OrangeJuiceMachine(String name, int numberOfCups, double amountOfWater, int
numberOfOrages ) {
super(name, numberOfCups, amountOfWater);
this.numberOfOrages = numberOfOrages;
}
@Override
public void makeDrink() {
numberOfCups--;
amountOfWater -= 100;
numberOfOrages -= 2;
}
@Override
public String toString() {
return super.toString() + " Nunber of oranges: " + numberOfOrages;
}
}
0.5 mark
0.5 mark
0.5 mark
0.5 mark
0.5 mark
0.5 mark
0.5 mark
0.5 mark
9. Imam University | CCIS
Doc. No. 006-01-20140514
Page 9 of 9
........................................................................................................................................
.......................................................................................................................................................