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

Key Midterm Exam v2

The document is a midterm exam for a Programming II course consisting of multiple choice questions and coding questions involving classes, interfaces, inheritance and relationships between classes based on a UML diagram provided. The coding questions involve implementing classes and interfaces for a Date class, Talkative interface, Animal class, Dog and Cat subclasses, Pet class and AnimalHospital class based on the UML diagram provided.

Uploaded by

krimo oran
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)
65 views7 pages

Key Midterm Exam v2

The document is a midterm exam for a Programming II course consisting of multiple choice questions and coding questions involving classes, interfaces, inheritance and relationships between classes based on a UML diagram provided. The coding questions involve implementing classes and interfaces for a Date class, Talkative interface, Animal class, Dog and Cat subclasses, Pet class and AnimalHospital class based on the UML diagram provided.

Uploaded by

krimo oran
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

University Of Benghazi

Faculty Of Information Technology


The Midterm Exam2 - Programming II (IT 112)
Spring 2017
Time: 2 hours

Student name:……………………………………………………………………. Student number:……………… Group:……………….

Q1: Multiple Choices: each question has exactly one correct answer (15 point).

1 Which of the following is used in exception Handling


A. static C. abstract
B. final D. Try-catch block

2 Which Java keywords are not allowed in an interface declaration?


A. static C. private
B. final D. all of the above

3 If there is exception or not in the code, the _______ block will be executed.
A. try C. finally
B. catch D. final

4 Generalization is called a __________


A. Base class C. Super class
B. Parent class D. all of the above

5 ____keyword is used in the relations between the interfaces and other interfaces.
A. implements C. extends
B. import D. none of the above

6 The object is an__________ of the class


A. superclass C. instance
B. subclass D. implements

7 Which of these represents the Has-a relationship between Car and engine?
A. class engine extends Cat {} C. class engine extends Car { }
B. class Engine { Car c;} D. class Car { Engine e;}

8 Which of these keywords is used to generate an exception explicitly?


A. throw C. catch
B. try D. finally
9 ________ methods cannot be override in the subclass
A. protected C. abstract
B. static D. final

10 Late binding also called ________


A. Static binding C. Early binding
B. dynamic binding D. All of the above.

11 ________keyword is used in the relations between the interfaces and class.


A. implements C. extends
B. import D. none of the above

12 Exceptions that are checked at ___________called checked exceptions.


A. class time C. compile time
B. exception time D. run time

13 ______________ inheritance between interfaces is supported in Java?


A. single inheritance C. good inheritance
B. multiple inheritance D. bad inheritance

14 ______________ inheritance between class is supported in Java?


A. single inheritance C. good inheritance
B. multiple inheritance D. bad inheritance

15 In the interface must be all methods are__________________.


A. public and abstract C. private and abstract
B. public and static D. private and static

Page 2 of 7 IT 112 - Midterm Exam


Page 3 of 7 IT 112

Q3: What is the output of the following code? (4 point)

A. What is the output: (2 point)

public class programs 1 {


public static void main(){
try{
int i = 0;
float []a=new float[2];
a[2]=5/(++i);
a[1]=5/0;
}
catch (ArithmeticException e) { System.err.print("catch1"); }
catch (ArrayIndexOutOfBoundsException e){
System.err.print("catch2");}
catch (Exception e){
System.err.print("catch3");}
}}

1 catch1 2 catch2 catch3


3 catch2 4 catch1 catch3

B. What is the output: (2 point)

public class Student {


private String Name;
private int Mark;
public Student (String n, int m){ Name=n; Mark=m; }
public String getName(){return Name; }
public int getMark(){return Mark; }
}
public class programs2{
public static void main(){
Student [] list={new Student (“Saleh”,80),
new Student (“Samir”,52),
new Student (“Salem”,55)};
for (int i=0; i<3; i++)
if(list[i].getMark()>50)
System.out.print(list[i].getName());
}}

1 Saleh Salem 2 Samir Salem


3 Saleh Samir 4 Saleh Samir Salem
Q4: There are 5 errors in the following code, Find the error, then Correct (6 points)
final interface Shape { //1: correct interface Shape
int IP; //2: correct int IP=14;
double area();
double perimeter();
}
public class Circle extends Shape { //3: correct public class Circle implements Shape
abstract private double radius; //4: correct private double radius;
public double area() { return PI * radius * radius; }
public abstract double perimeter() { return 2.0 * PI * radius; } //5: error
}
public Static class Run{ //6: error
public static void main( ){
Shape obj1= new Circle ();
}}

Q5: Consider that, we have UML diagram as the following (25 point)

Date Pet
<<interface>>
- Day: int - pets: Animal Talkative
- Month: int - Owner: String
- Year: int + Speak():void
+Pet(Animal p,owner:String)
+ Date(d:int,m:int, y:int) +getPets ():Animal
+getMonth ():int +set Owner(n:String):void
Animal
- Kind: int
AnimalHospital - Name:String
-RecDate: Date
+ Animal (n:String,k:String)
-Type: Pet
+getName():String
+ AnimalHospital (Animal p,owner:String ,d:int,m:int, y:int)
+getRecDate ():Date
+ getType ():Pet

Cat Dog

+ Cat (n:String,k:String) + Dog (n:String,k:String)


+ Speak():void + Speak():void

Page 4 of 7 IT 112 - Midterm Exam


Page 5 of 7 IT 112

1- Write a class Date. (3 point)


public class Date{ // Mark
private int Day; // Mark
private int Month; // Mark
private int Year; // Mark
public Date(int d , int m, int y){Day=d; Month=m; Year=y;} // Mark
public int getMonth (){return Month;} // Mark
}

2- Write a interface Talkative. (1 point)


public interface Talkative { // Mark
void Speak(); // Mark

3- Write a class Animal. (3 point)


abstract public class Animal implements Talkative { // Mark
private String Kind; // Mark
private String Name; // Mark
public Animal(String n,String k){ Name =n; Kind =k;} // Mark
public String getName(){return Name; }// Mark
}
4- Write a class Dog. (2 point)
public class Dog extends Animal { // Mark
public Dog (String n,String k){ super(n,k);} // Mark
public void Speak(){System.out.println(“Wooof !”); }// Mark
}
5- Write a class Cat. (2 point)
public class Cat extends Animal { // Mark
public Dog (String n,String k){ super(n,k);} // Mark
public void Speak(){System.out.println(“Meooo !”); }// Mark
}
6- Write a class Pet (3 point)
public class Pet{// Mark
private Animal Pets; // Mark
private String Owner; // Mark
public Pet (Animal p, String owner){Pets=p; Owner= owner;} // Mark
public Animal getPets(){return Pets;} // Mark
public void setOwner(String n){Owner =;}// Mark
}
7- Write a AnimalHospital class (3.5 point)
public class AnimalHospital{// Mark
private Date RecDate; // Mark
private Pet Type; // Mark
public AnimalHospital(Animal p, String owner ,int d,int m,int y){
Type=new Pet(p,owner); // Mark
RecDate=new Date(d,m,y); // Mark
}
public Pet getType (){return Type;} // Mark
public Date getRecDate (){return RecDate;} // Mark
}
8- Write a class Run that includes main() method to define objects; (4 point)
name of Object Static Type Dynamic Type
C1 Animal Cat (“lolo”,"Siame")
D1 Animal Dog (“Roky”,”Wolf”)
C2 Cat Cat (“Reta”,"Farsi")
P1 Pet Pet (“Sana”,C1)
P2 Pet Pet (“Sara”,C2)
P3 Pet Pet (“Sana”,D1)
Rec1 AnimalHospital AnimalHospital (“Sana”,P1,1,12,1995)
Rec2 AnimalHospital AnimalHospital (“Sara”,P2,5,12,1995)

public class Run{


public static void main(){
Animal C1=new Cat (“lolo”,"Siame"); // Mark
Animal D1=new Dog (“Roky”,”Wolf”); // Mark
Animal C2=new Cat (“Reta”,"Farsi"); // Mark
Pet P1=new Pet (“Sana”,C1); // Mark
Pet P2=new Pet (“Sara”,C2); // Mark

Page 6 of 7 IT 112 - Midterm Exam


Page 7 of 7 IT 112

Pet P3=new Pet (“Sana”,D1); // Mark


AnimalHospital Rec1 =new AnimalHospital (“Sana”,P1,1,12,1995); // Mark
AnimalHospital Rec2 =new AnimalHospital (“Sara”,P2,5,12,1995); // Mark
}}

‫ المطلوب‬. Rec2‫ و‬Rec1 ‫) واسند إليها‬List( ‫ اسمها‬AnimalHospital ‫ اعتبر إن لديك مصفوفة من‬-9
)5.3 point( 21 ‫طباعة قائمة بإسماء الحيوان االليف في شهر‬
for(int i=0;i<2;i++)// Mark
if (List[i].getRecDate().getMonth()==12) // 1 Mark
System.out.printeln(“Name of Pet: ”+List[i]. getType(). getPets(). getName());//2 Mark

‫درجة السؤال تضاف لجميع‬


‫الطلبة بسبب الخطاء المطبعي‬
‫في المطلوب‬

You might also like