1. Imam University | CCIS
Doc. No. 006-01-20140514
Page 1 of 10
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 Abdulrahman Albarrak, Dr. Qaisar Abbas, Dr Ashraf
Shahin, Dr. Shahad Alqefari, Dr. Haifa Aldayel, Dr.
Sarah Alhassan, and Dr. Aram AlSedrani
Exam: Final
Semester: Fall 2016
Date: January 08, 2017
Duration: 120 minutes
Marks: 40
Privileges: ☐ Open Book
☐ Calculator
Permitted
☐ Open Notes
☐ Laptop Permitted
Student Name (in
English):
Student ID:
Section No.:
Instructions:
1. Answer 4 questions; there are 4questions in 10 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 7
2 11
3 14
2. Imam University | CCIS
Doc. No. 006-01-20140514
Page 2 of 10
4 8
Total 40
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 1: To be answered in (25) Minutes [ ] / 7 Marks
Select the appropriate option from multiple choice questions and try fill in the following box
with your choice for each point.
1 2 3 4 5 6 7
1.. The constructer is executed when
a) A class is declared.
b) An object is created.
c) An object is used.
d) All the above.
2.. If the superclass member methods access is protected, and its inherited subclass methods
access is private, which statement is true?
a) The superclass member method can be invoked by subclass objects.
b) The subclass member method can be invoked by superclass objects.
c) The superclass member method cannot be invoked in subclass member method.
d) None of the above.
3.. Consider the following program segment, which statement can be added to output 10 20:
a) System.out.println ( this.i + “ “ + i);
b) System.out.println ( super.i + “ “ + i);
c) System.out.println ( i + “ “ + super.i);
d) System.out.println (i + “ “ + i);
class superC
{
protected int i= 20;
}
class subC
{
int i = 10;
public void output()
{
//ADD STATEMENT
}
}
3. Imam University | CCIS
Doc. No. 006-01-20140514
Page 3 of 10
4.. Which statement is wrong about an interface
a) An interface is declared with the keyword interface.
b) By default all interface methods are public abstract.
c) An interface member instance variable may be public.
d) A class that does not implement all the interface methods is an abstract class.
5.. In Exception handling, if several catch blocks accepts the type of the thrown object then..
a) The exact catch block that match a superclass of the thrown object will be executed.
b) The exact catch block that match a subclass of the thrown object will be executed.
c) The first catch block will be executed.
d) A compilation error will occur.
6.. Which statement will cause the compiler to issue a warning message:
a) Stack rawstack = new stack<Double>();
b) Stack <Double> rawstack = new Stack();
c) Stack <Double> rawstack = new Stack<>();
d) Stack rawstack = new Stack();
7.. The compiler will translate the following generic method header
Public Static <T extends Number> T AddNumbers (T[ ] array)
Into:
a) Public Static <T> T AddNumbers (T[ ] array)
b) Public Static <Object> Object AddNumbers (Object [ ] array)
c) Public Static Object AddNumbers (Object[ ] array)
d) Public Static Number AddNumbers (Number[ ] array)
4. Imam University | CCIS
Doc. No. 006-01-20140514
Page 4 of 10
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 2: To be answered in(30) Minutes [ ] / 11 Marks
(a) Write a generic method that takes three parameters from the same types and prints the
maximum (4 points).
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
(b) Write a generic class Test with one data member x to store any type. Define constructor with
one parameter to initialize x, and define get and set functions of data member x (4 points).
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
5. Imam University | CCIS
Doc. No. 006-01-20140514
Page 5 of 10
(c) Rewrite the following code after adding exception-handling code to handle individually
exceptions that can be thrown from the code (3 points).
Scanner sc=new Scanner(System.in);
int x= sc.nextInt();
int y= sc.nextInt();
System.out.println(x/y);
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
6. Imam University | CCIS
Doc. No. 006-01-20140514
Page 6 of 10
Question 3: To be answered in ( 45) Minutes [ ] / 12 Marks
Write a java code to represent a set of activities. There are two types of activity: Reading and Sport.
For reading, subject (String) is needed and for sport the burnt calories (Integer) are recorded. For each
type of activity, you need to record the duration (Integer). We need to know the number of activities in
total and the number of each activity.
(a) Write the needed abstract and concrete classes to represent activities. For each class provide a
toString method that returns a String representation of an object of the class with its attributes and
the attributes of its superclass (6 points).
(b) Declare the Test class that contains main function. In the main take the number of activities to be
added to the system and for each activity allow the user to select the type (Reading or Sport). The
program should be implemented using polymorphism (demonstrating polymorphic behaviour when
dealing with events). Once you have added all the events, print the number of all activities and the
number of each type. Write a loop to print all the activity information (6 points).
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
9. Imam University | CCIS
Doc. No. 006-01-20140514
Page 9 of 10
Student Name (in
English):
__________________________________________ Student
ID:
_____________________________
Question 4: To be answered in(20) Minutes [ ] / 8 Marks
Let be the following code:
abstract class SportGame {
private Location location;
public static int no_games=0;
protected int score;
public SportGame(Location location, int score){
this.location=location;
this.score=score;
System.out.println("SportGame parametrized constructor");
no_games++;
}
public SportGame(){
System.out.println("SportGame default constructor");
}
abstract public void setScore();
public int getScore(){ return score;}
}// end of SportGame class
class Game1 extends SportGame{
public void setScore(){
score++;}
}// end of Game1 class
class Game2 extends SportGame{
public Game2(Location location){
super(location, 10);
System.out.println("Game2 parametrized constructor");
}
public void setScore(){
score*=2;}
}// end of Game2 class
class Location{
public Location(){
System.out.println("Location default constructor");
}
}//end of Location class
10. Imam University | CCIS
Doc. No. 006-01-20140514
Page 10 of 10
Based on the above code, write the output of the following main:
SportGame[] S=new SportGame[4];
for(int i=0;i<S.length;i+=2){
S[i]=new Game1();
S[i].setScore();
S[i+1]=new Game2(new Location());
S[i+1].setScore();
}
System.out.println(SportGame.no_games);
System.out.println(S[0].getScore());
System.out.println(S[3].getScore());