You are on page 1of 4

Quiz3, Sec:H Time:30Min CSC1205:OOP1

Name: Supantha Bappy ID:18-38010-2


Part A:

1. class Test extends Exception { }

class Main {
public static void main(String args[]) {
try {
throw new Test();
}
catch(Test t) {
System.out.println("Got the Test Exception");
}
finally {
System.out.println("Inside finally block ");
}
}
}

(A) Got the Test Exception


Inside finally block
(B) Got the Test Exception
(C) Inside finally block
(D) Compiler Error

2. class Main {
public static void main(String args[]) {
int x = 0;
int y = 10;
int z = y/x;
}
}

(A) Compiler Error


(B) Compiles and runs fine
(C) Compiles fine but throws ArithmeticException exception

3. class Test
{
String str = "a";

void A()
{
try
{
str +="b";
B();
}
catch (Exception e)
{
str += "c";
}
}

void B() throws Exception


{
try
{
str += "d";
C();
}
catch(Exception e)
{
throw new Exception();
}
finally
{
str += "e";
}

str += "f";

void C() throws Exception


{
throw new Exception();
}

void display()
{
System.out.println(str);
}
public static void main(String[] args)
{ Test object = new Test();
object.A();
object.display();
}

(A) abdef
(B) abdec
(C) abdefc
4. Please identify the error in the below code?

interface A
{
private int i;
}

Explain Error:In this interface abstract class was not declared.

Interface A {

Public int i ()

5. abstract class Base {

Base() { System.out.println("Base Constructor Called"); }

abstract void fun();

class Derived extends Base {

Derived() { System.out.println("Derived Constructor Called"); }

void fun() { System.out.println("Derived fun() called"); }

class Main {

public static void main(String args[]) {

Derived d = new Derived();

Output:
Part B: 2*5= 10

1. Difference between abstract class and interface in Java?

ANS:-

Abstract Class Interface


1.contains Constructors 1.does not contains contructor
2.contains Data Member 2.does not contain Data Member
3.subclasses extends abstract class 3.subclasses implements interfaces
4.abstracrt keyword 4.interface keyword
5.abstract and non-abstract mathods 5.Only have abstract methods

2. What is method overriding? What is polymorphism? How are they related? What is runtime
polymorphism

ANS:- Method overriding means having two methods with the same method name and
parameters .One of the methods is in the parent class and the other is in the child class.Overriding
allows a child class to provide a specific implementation of method that is already provided its
parent class.

Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means many forms.There are two types of polymorphism in
Java: compile-time polymorphism and runtime polymorphism.

You might also like