SlideShare a Scribd company logo
1 of 9
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
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?
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)
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)
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”);
}
}
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();
}
}
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
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
Imam University | CCIS
Doc. No. 006-01-20140514
Page 9 of 9
........................................................................................................................................
.......................................................................................................................................................

More Related Content

Similar to Cs141 mid termexam2_v1answer

Cs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionCs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionFahadaio
 
Online Examination Java Projectreport.docx
Online Examination Java Projectreport.docxOnline Examination Java Projectreport.docx
Online Examination Java Projectreport.docxTanishaPatil4
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.pptrani marri
 
Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Gurpreet singh
 
Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6comp274
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6solutionjug4
 
Cs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionCs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionFahadaio
 
Examview testmanager userguide 8.1
Examview testmanager userguide 8.1Examview testmanager userguide 8.1
Examview testmanager userguide 8.1William McIntosh
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6helpido9
 
Selenium web driver | java
Selenium web driver | javaSelenium web driver | java
Selenium web driver | javaRajesh Kumar
 
01-ch01-1-println.ppt java introduction one
01-ch01-1-println.ppt java introduction one01-ch01-1-println.ppt java introduction one
01-ch01-1-println.ppt java introduction onessuser656672
 
Java.NET: Integration of Java and .NET
Java.NET: Integration of Java and .NETJava.NET: Integration of Java and .NET
Java.NET: Integration of Java and .NETDr Sukhpal Singh Gill
 
Exam viewtestmanageruserguide
Exam viewtestmanageruserguideExam viewtestmanageruserguide
Exam viewtestmanageruserguideWilliam McIntosh
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfmayorothenguyenhob69
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdfamitbhachne
 

Similar to Cs141 mid termexam2_v1answer (20)

Cs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionCs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solution
 
c++ lab manual
c++ lab manualc++ lab manual
c++ lab manual
 
J Unit
J UnitJ Unit
J Unit
 
Online Examination Java Projectreport.docx
Online Examination Java Projectreport.docxOnline Examination Java Projectreport.docx
Online Examination Java Projectreport.docx
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)
 
Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6
 
Cs141 mid termexam v5_solution
Cs141 mid termexam v5_solutionCs141 mid termexam v5_solution
Cs141 mid termexam v5_solution
 
Examview testmanager userguide 8.1
Examview testmanager userguide 8.1Examview testmanager userguide 8.1
Examview testmanager userguide 8.1
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6
 
Linq
LinqLinq
Linq
 
Selenium web driver | java
Selenium web driver | javaSelenium web driver | java
Selenium web driver | java
 
01-ch01-1-println.ppt java introduction one
01-ch01-1-println.ppt java introduction one01-ch01-1-println.ppt java introduction one
01-ch01-1-println.ppt java introduction one
 
Java.NET: Integration of Java and .NET
Java.NET: Integration of Java and .NETJava.NET: Integration of Java and .NET
Java.NET: Integration of Java and .NET
 
Exam viewtestmanageruserguide
Exam viewtestmanageruserguideExam viewtestmanageruserguide
Exam viewtestmanageruserguide
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
Test_your_skill_ppt-1.pptx
Test_your_skill_ppt-1.pptxTest_your_skill_ppt-1.pptx
Test_your_skill_ppt-1.pptx
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
 
Icom4015 lecture8-f16
Icom4015 lecture8-f16Icom4015 lecture8-f16
Icom4015 lecture8-f16
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

Cs141 mid termexam2_v1answer

  • 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 ........................................................................................................................................ .......................................................................................................................................................