COMSATS University Islamabad, Lahore Campus
Block–C, Department of Computer Science
COMSATS University Islamabad, Lahore Campus 1.5KM Defence Road, Off Raiwind Road, Lahore
Final Exam – FA22
Credit
Course Title: Object Oriented Programming Course Code: CSC241 4 (3,1)
Hours:
Nadeem Ghafoor Chaudhry, Dr.
Course Muhammad Aksam Iftikhar, Programme
BSCS, BSSE
Instructors: Muhammad Shahid Bhatti, Name:
Muntaha Iqbal
Thursday, January
Semester: Batch: Section: Date:
26th, 2023
Time Allowed: 90 minutes Maximum Marks: 60
Student’s Name: Reg. No.
Part A: MCQs[CLO-1,2] [ 30 marks] Select the correct option(s)
Which of the following statements regarding the throw Which of the following statements is true?
point of an exception is false? a. Overloaded methods are often used to perform similar
a. It specifies the point at which the exception must be operations on different types of data.
handled. b. When the compiler encounters a method call, it attempts
b. It is the initial point at which the exception occurs. to locate a method declaration with a name and parameters
c. It is specified as the top row of the method-call stack at that are compatible with the argument types in the method
the time the exception occurred. call.
d. All of the above statements are true. c. Both a) and b) are true.
d. Neither a) nor b) is true.
Which of the following is not a GUI component (control or When the compiler translates a generic method into Java
widget)? bytecodes, it uses __________ to replace the type
a. String. parameters with actual types.
b. Button. a. erasure.
c. Menu. b. compile-time type replacement.
d. Combo box. c. compile-time type checking.
d. compile-time type safety.
Class ________ represents a dynamically resizable Which of the following statements is false?
array-like data structure. a. Storage of data variables and arrays is temporary.
a. Array b. Data is lost when a local variable “goes out of scope.”
c. Files are used for long-term retention of large amounts
b. ArrayList of data.
c. Arrays d. Data maintained in files is often called transient
d. None of the above. data.
Which of the following is a feature of generics in Java? A static method can ________.
a. Allows for type-safe collections a. Call only other static methods of the same class directly
b. Enables code reuse b. Manipulate only static fields in the same class directly
c. Improves performance c. Be called using the class name and a dot (.)
All of the above d. All of the above.
Collections method sort that accepts a List as an Which statement regarding Java files is false?
argument. It sorts the List elements, which must a. Java imposes no structure on a file.
implement the __________ interface. b. Notions like “record” do not exist in Java files.
c. The programmer must structure files to meet the
a. Comparable. requirements of applications.
b. Comparator. d. Records in a Java sequential file are stored in order
c. Compare. by record key.
d. Ordering.
Polymorphism allows for a single method to be used with What does the following statement do?
multiple data types. How does this feature effect code Scanner scanner = new
reuse? Scanner(Paths.get("test.txt"));
a. It eliminates the need to write multiple methods for a. Opens a binary file for input.
different data types b. Opens a binary file for output.
b.It increases the number of lines of code in the program c. Opens a text file for input.
c. It creates the code for some classes automatically d. Opens a text file for output.
d) It has no effect on code reuse
Which of these is not an example of a "real-life" collection? Which of the following performs a boxing conversion?
a. The cards you hold in a card game. a. int x = 7;
b. Your favorite songs stored in your computer. b. Integer x = 7;
c. The players on a soccer team.
d. The number of pages in a book.
c. Neither of the above.
d. Both of the above.
An advantage of inheritance is that: Which of the following Java classes is used to handle file
a. All methods can be inherited. input and output operations?
b. All instance variables can be uniformly accessed by a) File
subclasses and superclasses. b) FileInputStream
c. Objects of a subclass can be treated like objects of their c) FileOutputStream
superclass. d) All of the above
d. None of the above.
When an exception occurs it is said to have been Overriding a method differs from overloading a method
________. because:
a. caught. a. Overloaded methods have the same signature.
b. thrown. b. Overridden methods have the same
c. declared. signature.
d. handled. c. Both of the above.
d. Neither of the above.
Which of the following is the correct way to add a button Comparator method compare should return ________ if the
to a JavaFX VBox layout named as root? first argument is greater than the second argument.
a.root.add(new Button("Button")); a. a positive int value.
b.root.addNode(new Button("Button")); b. zero.
c.root.getChildren().add(new Button("Button")); c. a negative int value.
d.root.addChild(new Button("Button")); d. a String.
Which of the following performs an unboxing conversion? What is the purpose of the finally block in exception
Assume x refers to an Integer object. handling?
a. int y = x; a) Handle exceptions
b. Integer y = x; b) Handle errors
c. Neither of the above. c) Execute code regardless of error/exception
d. Both of the above. d) Handle exceptions/errors in catch block
Which statement is false? Which of the following exceptions is a checked exception?
a. A List is a Collection. a. ArithmeticException.
b. A List cannot contain duplicate elements. b. IOException.
c. A List is sometimes called a sequence. c. RuntimeException.
d. Lists use zero-based indices. d. InputMismatchException.
A(n) ________ path starts from the directory in which the When a generic class is instantiated without specifying a
application began executing. type argument, it is said to have a __________.
a. absolute a. empty type.
b. relative b. raw type.
c. parallel c. null type.
d. comparative d. abstract type..
Java supports type inferencing with the <> notation in Which statement is false?
statements that declare and create generic type variables a. The compiler always creates a default constructor for a
and objects. For example, the following line: class.
List<String> list = new ArrayList<String>(); b. If a class’s constructors all require arguments and a
can be written as: program attempts to call a no-argument constructor to
a. List<> list = new ArrayList<>(); initialize an object of the class, a compilation error occurs.
b. List<> list = new ArrayList<String>(); c. A constructor can be called with no arguments only if the
c. List<String> list = new ArrayList<>();
class does not have any constructors or if the class has a
public no-argument constructor.
d. List<String> list = new ArrayList();
d. None of the above.
Which superclass members are inherited by all Streams that input bytes from and output bytes to files
subclasses of that superclass? are known as ________.
a. bit-based streams
a. private instance variables and methods.
b. byte-based streams
b. protected instance variables and c. character-based streams
methods. d. Unicode-based streams
c. private constructors.
d. protected constructors
Part B: Apply the concepts of Object Oriented Programming and write down the output of the
following codes. [3X10=30]
CLO<2> Bloom Taxonomy Level <Applying>
No Code Output/Error
I. import java.util.ArrayList; Theory
public class TerminalTest { Best of luck
public static void main(String[] args) { OOP Exam
ArrayList<String> list = new
ArrayList<>();
list.add("Best of luck");
list.add("OOP Exam");
list.add("Terminal");
list.add(0,"Theory");
list.set(3,"Lab");
list.remove(list.get(3));
for (String val :list) {
System.out.println(val);
}
}
}
II. public class TerminalTest { false
public static void main(String[] args) { false
String s1 = "OOP Terminal"; true
String s2 = new String("OOP Terminal");
String s3 = new String(s1);
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s1.equals(s2));
}
}
III.public class TerminalTest { 1
public static void main(String[] args) { Ali
A obj = new A(); 3.45
System.out.println(obj.id);
System.out.println(obj.name);
System.out.println(obj.cgpa);
}
}
class A
{
int id;
String name;
double cgpa;
public A()
{
this(1,"Ali");
}
public A(int id, String name) {
this(2,"Talha", 3.45);
this.id = id;
this.name = name;
}
public A(int id, String name, double cgpa) {
this.id = id;
this.name = name;
this.cgpa = cgpa;
}
}
IV. public class TerminalTest { I am Parent class
public static void main(String[] args) { I am child Class
GrandChild obj = new GrandChild(); I am grand child class
}
}
class Parent
{
public Parent()
{
System.out.println("I am Parent class");
}
}
class Child extends Parent
{
public Child()
{
System.out.println("I am child Class");
}
}
class GrandChild extends Child
{
public GrandChild()
{
System.out.println("I am grand child
class");
}
}
V. public class TerminalTest { I am orange
public static void main(String[] args) { I am fruit
Fruit f[] = new Fruit[2]; Price per KG:500.0
f[0] = new Orange();
f[1] = new Mango();
for (int i = 0; i < f.length; i++) {
f[i].method();
if(f[i] instanceof Mango)
{
System.out.println("Price per
KG:"+Mango.PRICE);
}
}
}
}
class Fruit
{
void method()
{
System.out.println("I am fruit");
}
}
class Orange extends Fruit
{
@Override
void method() {
System.out.println("I am orange");
}
}
class Mango extends Fruit
{
public static final double PRICE = 500;
}
VI. import java.util.Scanner; 10
public class TerminalTest { RuntimeException
public static void main(String[] args) { occured
Scanner input = new Scanner(System.in); Finally Block
try {
int array[] = new int[]{10};
for (int i = 0; i <=array.length; i+
+) {
System.out.println(array[i]);
}
}
catch (ArithmeticException e)
{
System.out.println("ArithmeticException
Ocured");
}
catch (RuntimeException e)
{
System.out.println("RuntimeException
occured");
}
catch (Exception e)
{
System.out.println("Exception
Occured");
}
finally {
System.out.println("Finally Block");
}
}
}
VII. public class TerminalTest { Chicken: Fry it
public static void main(String[] args) { Apple: Make apple cider
Edible obj[] = {new Chicken(), new Orange: Make orange
Apple(), new Orange()}; juice
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i].howToEat());
}
}
}
interface Edible
{
public String howToEat();
}
class Chicken implements Edible
{
@Override
public String howToEat() {
return "Chicken: Fry it";
}
}
abstract class Fruit implements Edible {
}
class Orange extends Fruit
{
@Override
public String howToEat() {
return "Orange: Make orange juice";
}
}
class Apple extends Fruit
{
@Override
public String howToEat() {
return "Apple: Make apple cider";
}
}
VIII. public class TerminalTest { 1:Circle
public static void main(String[] args) { 2:Rectangle
Shape shapes[] = new Shape[2];
for (int i = 0; i < shapes.length; i++)
{
shapes[i] = new Shape();
System.out.println(shapes[i].id+":"+shapes[i].na
me);
}
}
}
class Shape
{
static int id;
String name;
public Shape() {
id++;
if(id==1)
{
name = "Circle";
}
else
{
name = "Rectangle";
}
}
}
IX. public class TerminalTest { 12345
public static void main(String[] args) { OOP EXAM
Integer[] arr = {1, 2, 3, 4, 5};
Character[] arr2 = {'O','O','P','
','E','X','A','M'};
printArray(arr);
printArray(arr2);
}
public static <E> void printArray(E[]
elements) {
for (E element : elements) {
System.out.print(element);
}
System.out.println();
}
}
X. public class TerminalExam { Value of number:10
public static void main(String[] args) { Value of number:20
Super.Sub.display();
Super ob1 = new Super();
Super.Sub.display();
}
}
class Super
{
public static int number = 5;
public Super()
{
number+=5;
}
static class Sub extends Super
{
public static void display()
{
number+=5;
System.out.println("Value of
number:"+number);
}
}
}
Q 3. Look at the code and the set of classes given below carefully. Then, answer the questions given
after the code.
i) (3 marks) The definition of class A has an error. Identify the error and then
complete the definition of class A to remove the error. (Only write the code
for completing class A, and do not rewrite complete class definition).
Solution:
The class A must also implement the method doIt() from Interface1.
class A implements Interface2 {
public double t;
A(double t) {
this.t = t;
}
@Override
public void doItNow() {
}
@Override
public void doIt() {
}
ii) (4 marks) Given the definition of class B, which of the following statements
are correct/incorrect. Describe the error in case of incorrect statement.
B<Double> b2 = new B<>(4.4, 0.9);
B<String> b3 = new B<String>(4.4, 0.25);
Solution:
No error in first statement
The second statement contains error. The first parameter must be of string
type.
iii) (6 marks) Consider the following ArrayList:
ArrayList<C> list=new ArrayList<>();
Write down statement(s) to add 2 elements to this arraylist with values of
your choice. (Hint: You can First create objects of C, then add to array
list)
Solution:
ArrayList<C> list=new ArrayList<>();
C c1 = new C(new A(1), new ArrayList<String>());
C c2 = new C(new A(1), new ArrayList<String>());
list.add(c1);
list.add(c2);
iv) (6 marks) Consider a static method with the following signature:
public static void doMagic(Interface2 i1);
Identify each of the method call given below as correct/incorrect. Also,
describe the reason in case of incorrect statement?
Solution:
doMagic(new A(0.0)); // No Error
doMagic(new C(null, null)); // Error: C is NOT an instance of Interface2
doMagic(new Interface2()); // Error: cannot create an object of Interface2
v) (5 Marks) Suppose that class C also implements the Comparable interface. You
have to write the compareTo method for comparing 2 objects of class C based
on the value of field t of object a1.
Solution:
@Override
public int compareTo(C c2) {
if (this.a1.t > c2.a1.t)
return 1;
if (this.a1.t < c2.a1.t)
return -1;
else
return 0;
}
vi) (2 mark) Is the following statement correct? Give justification in case of error OR given output of the
statement in case of no error.
System.out.println( (new A(0.0)) instanceof Interface1);
Solution:
The statement is correct, and the output is true
Q 4.
(6 Marks) Differentiate between the Stage, Scene and Pane classes. What is the
relationship between the Stage and Scene classes (Inheritance, Composition,
Association, Polymorphism etc.)?
Solution:
The Stage class represent the parent window, the scene class represents the main view
of window and the pane class represents different possible layouts of a container.
Relationship between Stage and Scene class is composition.
(9 Marks) Briefly describe the following object(s)/concept(s) in the context of event
handling. Give a code example of event handling and identifying each of the following
object/concept in the example
Registering an event
Event Source
Event Handler
Solution:
Registering an event means setting an event listener that respond to a certain event. Event source is the object
responsible for generating the event, event handler is the object/method used for handling the event.
Code Example:
Button btn = new Button(“OK”);
Btn.addActionEvent(new EventHandler<ActionEvent>(){
Public void handle(ActionEvent e)
{
// do something
}
}
);