Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Cs141 mid termexam2_v1
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):
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
3. Imam University | CCIS
Doc. No. 006-01-20140514
Page 3 of 9
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?
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:
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
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
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.
Question # Line # Cause of the error
1
2
3
4
5
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.
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................