100% found this document useful (1 vote)
1K views

BAHRIA UNIVERSITY (Karachi Campus) : Object-Oriented Programming (Csc-210)

This document contains instructions and questions for an Object-Oriented Programming final exam. It asks students to: 1. Code classes and interfaces based on a UML diagram including Vehicle, Motorcycle, and CommercialVehicle classes. 2. Write code for 3 short coding scenarios involving threads, file copying, and static methods checking files. 3. Answer 10 logical questions in 2 lines or less about OOP concepts like inheritance, exceptions, constructor chaining, and file I/O.

Uploaded by

AISHA 20682
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

BAHRIA UNIVERSITY (Karachi Campus) : Object-Oriented Programming (Csc-210)

This document contains instructions and questions for an Object-Oriented Programming final exam. It asks students to: 1. Code classes and interfaces based on a UML diagram including Vehicle, Motorcycle, and CommercialVehicle classes. 2. Write code for 3 short coding scenarios involving threads, file copying, and static methods checking files. 3. Answer 10 logical questions in 2 lines or less about OOP concepts like inheritance, exceptions, constructor chaining, and file I/O.

Uploaded by

AISHA 20682
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BAHRIA UNIVERSITY (Karachi Campus)

Department of Computer Science


Final Term Examination – Spring 2020
OBJECT-ORIENTED PROGRAMMING (CSC-210)

Class : BS(CS)- 2 (A/B) Session: I


Course Instructor(s) : Sameena Javaid Max. Marks: 50

Date : 08th July 2020 Time Allowed: 08 hours

Student’s Name: ________________________________ Enrollment # : ______________

Note: Consider following points carefully


 In addition to upload the solution on LMS, please also email it to course instructor at
[email protected]
 In order to avoid any run-time electricity and internet unavailability situation, it is suggested
that keep your laptop charged. Also activate 3G/4G connections as an alternative of WiFi /
internet option to upload your solution.
 Any form of plagiarism will receive cancellation of paper or zero marks.

QUESTION NO. 01: (UML BASED DEVELOPMENT SKILL - ABSTRACTION) [2+6+7 = 15 Marks]

Consider the UML class diagram given at last of the question to code following in JAVA:

a) Develop an interface CommercialVehicle that is sub interface of PersonalVehicle and includes a


method:
public double calculateAmortizedTax ()

b) Develop an abstract class Vehicle to the following specification:


 The class has four protected instance variables numberPlate, speed, width and turningRadius
 The class has a constructor that initialize the attributes numberPlate, width and turningRadius
 The class has a mutator method for the variable speed
 The class has a boolean method isUturnPossible (double roadWidth) that uses the formula to
calculate that UTurn is possible
o roadWidth > (width + 2 * turningRadius).
 The class has the following abstract method:
o double kilometersPerLiter()
o double distanceToRefueling()
o String toString()
Page 1 of 5
c) Develop a public class MotorCycle to the following specification:
 The constructor initializes engineVolume, numberPlate, width, turningRadiusname.
 Circle class overrides the toString methods to return the namberPlate, kilometerPerLiter,
distanceToRefueling and Tax.
 The kilometerPerLiter() method use the following formula to calculate km per liter:
o 1.0/(0.000008*speed*speed + 0.0002*speed + 0.02)
 The distanceToRefueling() method use the following formula to calculate the distance to
refuel:
o Km per liter * fuel level
 The calculateTax() method use the following formula to calculate the tax amount:
o baseTax * engineVolume
 The calculateAmortizedTax() method uses a try and catch block to generate error message
“Not supported yet.” by throwing an exception using a class
UnsupportedOperationException.

Page 2 of 5
QUESTION NO. 02 (SHORT CASES TO CODE) [5x3 = 15 Marks]

Write down the JAVA code for the following given scenarios:

1. Write down the code for creation of two threads A and B with the single print statement for
thread A: “Thread A will win” and for thread B: “Thread B will win”, and also write down Driver
class for this multi-threaded program contains one part to run thread A and B concurrently.

2. A text file “original.txt” is already saved in hard disk consisting following paragraph about OOP
features.
“There are four major features in object-oriented programming that makes them
different than non-OOP languages: encapsulation, inheritance, polymorphism and
abstraction.”
Copy this file into another file “copied.txt”. Write the statements required in try block only with
proper exception handling in catch block(s).

3. Write a class FinalPaper containing the following static methods:

a. fileExists(), that takes as a parameter a string and returns a boolean value that istrue, if
the file whose name is passed as parameter exists, and false otherwise.
b. isInt(), that takes as parameter a string and returns a boolean value that is true if the
string represents an integer, and false otherwise.
c. isDouble(), that takes as parameter a string and returns a boolean value that istrue if the
string represents a real number, and false otherwise.

NOTE: Use throws clause to handle all exceptions.

QUESTION NO. 3 (LOGICAL QUESTIONS) [10x2 = 20 Marks]

Answer the following statements: (Answer should not exceed 2 lines)

1. Class C implements Interface I containing method m1 and m2 declarations. Class C has


provided implementation for method m2. Can you create an object of Class C? Explain in
either cases.

2. Describe and handle exception(s) in following statement

System.out.println(Integer.parseInt(br.readLine());

Page 3 of 5
3. Following diagram depicts IS-A and HAS-A relationship. Write JAVA class structures for given
relationship.

4. Predict Output or Error(s) in following code:

public class Example


{
public static void main(String[] args)
{
int ratio;
int[] numbers = {100,10,0,5,2,8,0,30};

try
{
for (int i=0; i < numbers.length-1; i++)
{
ratio = numbers[i] / numbers[i+1];

System.out.println(numbers[i]+"/"+numbers[i+1]+
"="+ratio);
}
}
catch (ArithmeticException ae)
{
System.out.println("Couldn't calculate "+
numbers[i]+"/"+numbers[i+1]);
}
}
}

Page 4 of 5
5. Consider the following code:

public abstract class MyClass{


public abstract int a;
public abstract display(); }

Find error (if occurs) and rewrite the statement.

6. Consider UML given in Question No. 01 and analyze the following statements. Perform
downcasting to the statements (1 to 6) where requires.

Vehicle c = new Car(1300,"ABC-456", 2,4);

a) c.setCurrentYear(2019); //Statement 1
b) c.setModelYear(2020); //Statement 2
c) c.setBaseTax(7); //Statement 3
d) c.setSpeed(100); //Statement 4
e) c.setFuelLevel(20); //Statement 5
f) System.out.println(c); //Statement 6

7. “instanceOf is a binary operator in Java, which can resolve DownCasting”. Defend this
statement.

8. In the statement “System.out.println()”; System is a class in java.lang package, out is a static


variable of System class and println() is an overloaded method. Why System class doesn’t
requires instantiation? Give any 2 reasons.

9. Constructor chaining is the process of calling one constructor from another constructor with
respect to current object. Constructor chaining can be done in two ways:
I. Within same class: It can be done using this() keyword for constructors in same class
II. From base class: by using super() keyword to call constructor from the base class.

Construct a chain of follow constructors within a class Temp using this() keyword.
 Temp()
 Temp(int a)
 Temp(int a, int b)

10. A file is having stored numbers from 10 to 59 under the name “numbers.txt”. What value
does read() returns when it has reached the end of a file while working with transfer of
control or conditional statements. Also name the built-in class of read() method.

***************** END OF PAPER ******************


Page 5 of 5

You might also like