You are on page 1of 14

OOPS & EXCEPTION HANDLING

OOPS CONCEPTS

1. What do you understand by copy constructor in Java?

There is no copy constructor in java. However, we can copy the values from one
object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They
are:

 By constructor
 By assigning the values of one object into another
 By clone() method of Object class
2. Can you call the base class method without creating an instance?

Yes, you can call the base class without instantiating it if:

 It is a static method
 The base class is inherited by some other subclass
3. What is the Inheritance?
Inheritance is a mechanism by which one object acquires all the properties and
behaviour of another object of another class. It is used for Code Reusability and
Method Overriding. The idea behind inheritance in Java is that you can create
new classes that are built upon existing classes. When you inherit from an existing
class, you can reuse methods and fields of the parent class. Moreover, you can
add new methods and fields in your current class also. Inheritance represents the
IS-A relationship which is also known as a parent-child relationship.

There are five types of inheritance in Java.

 Single-level inheritance

+91 9000045750 packetprep.com


 Multi-level inheritance
 Multiple Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance

Multiple inheritance is not supported in Java through class.

4. What is composition?

Holding the reference of a class within some other class is known as composition.
When an object contains the other object, if the contained object cannot exist
without the existence of container object, then it is called composition. In other
words, we can say that composition is the particular case of aggregation which
represents a stronger relationship between two objects. Example: A class contains
students. A student cannot exist without a class. There exists composition
between class and students.

5. What is the difference between aggregation and composition?

Aggregation represents the weak relationship whereas composition represents the


strong relationship. For example, the bike has an indicator (aggregation), but the
bike has an engine (composition).

6. What is method overloading?

Method overloading is the polymorphism technique which allows us to create


multiple methods with the same name but different signature. We can achieve
method overloading in two ways.

 By Changing the number of arguments


 By Changing the data type of arguments

+91 9000045750 packetprep.com


Method overloading increases the readability of the program. Method
overloading is performed to figure out the program quickly.

7. What is method overriding?

If a subclass provides a specific implementation of a method that is already


provided by its parent class, it is known as Method Overriding. It is used for
runtime polymorphism and to implement the interface methods.

Rules for Method overriding

 The method must have the same name as in the parent class.
 The method must have the same signature as in the parent class.
 Two classes must have an IS-A relationship between them.

8. Can we override the static method?

No, you can’t override the static method because they are the part of the class,
not the object. It is because the static method is the part of the class, and it is
bound with class whereas instance method is bound with the object, and static
gets memory in class area, and instance gets memory in a heap.

9. What is aggregation?

Aggregation can be defined as the relationship between two classes where the
aggregate class contains a reference to the class it owns. Aggregation is best
described as a has-a relationship. For example, The aggregate class Employee
having various fields such as age, name, and salary also contains an object of
Address class having various fields such as Address-Line 1, City, State, and pin-
code. In other words, we can say that Employee (class) has an object of Address
class.

+91 9000045750 packetprep.com


Consider the following example

10. What is the final class?

If we make any class final, we can’t inherit it into any of the subclasses.

11. Why multiple inheritance is not supported in java?

In Java, a class cannot extend more than one class. Therefore following is illegal

Example

+91 9000045750 packetprep.com


However, a class can implement one or more interfaces, which has helped Java
get rid of the impossibility of multiple inheritances.

The reason behind this is to prevent ambiguity.

Consider a case where class B extends class A and Class C and both class A and
C have the same method display().

Now java compiler cannot decide, which display method it should inherit. To
prevent such situation, multiple inheritances is not allowed in java.

12. What is static polymorphism?


Static polymorphism (static binding) is a kind of polymorphism that occurs at
compile time. An example of compile-time polymorphism is method
overloading.

13. What is dynamic polymorphism?


Runtime polymorphism or dynamic polymorphism (dynamic binding) is a type
of polymorphism which is resolved during runtime. An example of runtime
polymorphism is method overriding.

14. What are virtual functions?


Virtual functions are functions that are present in the parent class and are
overridden by the subclass. These functions are used to achieve runtime
polymorphism.

15. Can we declare an interface as final?


No, we cannot declare an interface as final because the interface must be
implemented by some class to provide its definition. Therefore, there is no sense
to make an interface final. However, if you try to do so, the compiler will show
an error.

+91 9000045750 packetprep.com


16. What is the final blank variable?

A final variable, not initialized at the time of declaration, is known as the final
blank variable. We can’t initialize the final blank variable directly. Instead, we
have to initialize it by using the class constructor. It is useful in the case when the
user has some data which must not be changed by others, for example, PAN
Number.

Consider the following example:

17. What is an abstract class?

An abstract class is a class which cannot be instantiated. Creation of an object is


not possible with an abstract class, but it can be inherited. An abstract class can
contain only an Abstract method. Java allows only abstract method in abstract
class while other languages allow non-abstract method as well.

18. What is the main difference between a class and an object?

An object is an instance of a class. Objects hold multiple information, but classes


don’t have any information. Definition of properties and functions can be done in
class and can be used by the object. A class can have sub-classes, while an object
doesn’t have sub-objects.

+91 9000045750 packetprep.com


19. What is the diamond problem in inheritance?

In case of multiple inheritance, suppose class A has two subclasses B and C,


and a class D has two super classes B and C. If a method present in A is
overridden by both B and C but not by D then from which class D will inherit
that method B or C? This problem is known as diamond problem.

20. What is Static Binding and Dynamic Binding?

Static or early binding is resolved at compile time. Method overloading is an


example of static binding.
Dynamic or late or virtual binding is resolved at run time. Method overriding is
an example of dynamic binding.

INTERFACES, PACKAGES AND ABSTRACT


CLASSES

21. What is Abstraction in Java?


Abstraction in Java is a technique by which we can hide the data that is not
required to users. It hides all unwanted data so that users can work only with the
required data.

22. What is Abstract class in Java? How to define it?


An abstract class in java is a class that is declared with an abstract keyword.

Example of Abstract class

+91 9000045750 packetprep.com


23. What is the difference between abstract class and concrete class?
There are mainly two differences between an abstract class and concrete class.
They are:
 We cannot create an object of abstract class. Only objects of its non-
abstract (or concrete) sub classes can be created.
 It can have zero or more abstract methods that are not allowed in a non-
abstract class (concrete class).
24. What is Abstract in Java?

Abstract is a non-access modifier in java that is applicable for classes,


interfaces, methods, and inner classes.

25. Can abstract modifier applicable for variables?


No.

26. Can an abstract method be declared with private modifier?


No, it cannot be private because the abstract method must be implemented in
the child class. If we declare it as private, we cannot implement it from outside
the class.

27. What is Concrete method in Java?


A concrete method in Java is a method which has always the body. It is also called
a complete method in java.
28. When to use Abstract class in Java?
An abstract class can be used when we need to share the same method to all
non-abstract sub classes with their own specific implementations.

29. When to use Abstract method in Java?


An abstract method can be used

When the same method has to perform different tasks depending on the object
calling it.
When you need to be overridden in its non-abstract subclasses.

+91 9000045750 packetprep.com


30. What will happen if we do not override all abstract methods in subclass?
(Or)
What will happen if we do not provide implementation for all abstract
methods in subclass?
Java compiler will generate compile time error. We will have to override all
abstract methods in subclass.

31. What is the difference between Abstraction and Encapsulation?


Abstraction hides the implementation details from users
whereas, encapsulation wraps (binds) data and code into a single unit.
32. Why abstract class has constructor even though you cannot create object?
We cannot create an object of abstract class but we can create an object of
subclass of abstract class. When we create an object of subclass of an abstract
class, it calls the constructor of subclass.

This subclass constructor has a super keyword in the first line that calls
constructor of an abstract class. Thus, the constructors of an abstract class are
used from constructor of its subclass.

If the abstract class doesn’t have constructor, a class that extends that abstract
class will not get compiled.

33. Why should we create reference to superclass (abstract class reference)?


We should create a reference of the superclass to access subclass features because
superclass reference allows only to access those features of subclass which have
already declared in superclass.
If you create an individual method in subclass, the superclass reference cannot
access that method. Thus, any programmer cannot add their own additional
features in subclasses other than whatever is given in superclass.

34. What is the advantage of Abstract class in Java?


The main advantages of using abstract class are as follows:

+91 9000045750 packetprep.com


 Abstract class makes programming better and more flexible by giving the
scope of implementing abstract methods.
 Programmer can implement abstract method to perform different tasks
depending on the need.
 We can easily manage code.

EXCEPTION HANDILING

35. What is an exception?

Exception is an abnormal condition which occurs during the execution of a


program and disrupts normal flow of the program. This exception must be
handled properly. If it is not handled, program will be terminated abruptly.

36. How the exceptions are handled in java? OR Explain exception handling
mechanism in java?

Exceptions in java are handled using try, catch and finally blocks.

 try block : The code or set of statements which are to be monitored for
exception are kept in this block.
 catch block : This block catches the exceptions occurred in the try block.
 finally block : This block is always executed whether exception is
occurred in the try block or not and occurred exception is caught in the
catch block or not.

37. What is the difference between error and exception in java?

Errors are mainly caused by the environment in which an application is running.


For example, OutOfMemoryError happens when JVM runs out of memory.

+91 9000045750 packetprep.com


Whereas exceptions are mainly caused by the application itself. For example,
NullPointerException occurs when an application tries to access null object.

38. Can we write only try block without catch and finally blocks?

No, It shows compilation error. The try block must be followed by either catch
or finally block. You can remove either catch block or finally block but not
both.

39. There are three statements in a try block – statement1, statement2 and
statement3. After that there is a catch block to catch the exceptions occurred
in the try block. Assume that exception has occurred in statement2. Does
statement3 get executed or not?

No. Once a try block throws an exception, remaining statements will not be
executed. control comes directly to catch block.

40. What is unreachable catch block error?

When you are keeping multiple catch blocks, the order of catch blocks must be
from most specific to most general ones. i.e sub classes of Exception must come
first and super classes later. If you keep super classes first and sub classes later,
compiler will show unreachable catch block error.

41. Explain the hierarchy of exceptions in java?

Checked Exceptions

Checked exceptions are known to compiler i.e they are the exceptions that are
checked at compile time. Checked exceptions are also called compile time
exceptions, because they can be known during compile time.

+91 9000045750 packetprep.com


Unchecked Exceptions

Unchecked exceptions are not known to compiler. They are the exceptions that
are not checked at compile time, because they occur only at run time. That’s why
these exceptions are also called run time exceptions.

42. What are run time exceptions in java. Give example?

The exceptions which occur at run time are called as run time exceptions. These
exceptions are unknown to compiler. All sub classes of
java.lang.RunTimeException and java.lang.Error are run time exceptions. These
exceptions are unchecked type of exceptions. For example,
NumberFormatException, NullPointerException, ClassCastException,
ArrayIndexOutOfBoundException, StackOverflowError etc.

43. What is OutOfMemoryError in java?

OutOfMemoryError is the sub class of java.lang.Error which occurs when JVM


runs out of memory.

44. What are checked and unchecked exceptions in java?

Checked exceptions are the exceptions which are known to compiler. These
exceptions are checked at compile time only. Hence the name checked
exceptions. These exceptions are also called compile time exceptions. Because,
these exceptions will be known during compile time.

Unchecked exceptions are those exceptions which are not at all known to
compiler. These exceptions occur only at run time. These exceptions are also
called as run time exceptions. All sub classes of java.lang.RunTimeException
and java.lang.Error are unchecked exceptions.

+91 9000045750 packetprep.com


45. What is the difference between ClassNotFoundException and
NoClassDefFoundError in java?

In Java, both ClassNotFoundException and NoClassDefFoundError occur


when a particular class is not found at run time. But, they occur at different
scenarios. ClassNotFoundException is an exception which occurs when you try
t o load a class at run time using Class.forName() or loadClass() methods and
mentioned classes are not found in the classpath. On the other
hand, NoClassDefFoundError is an error which occurs when a particular class
is present at compile time but it is missing at run time. In this post, we will see
the differences between ClassNotFoundException Vs NoClassDefFoundError in
Java and when they occur.

46. Can we keep the statements after finally block If the control is returning
from the finally block itself?

No, it gives unreachable code error. Because, control is returning from the
finally block itself. Compiler will not see the statements after it. That’s why it
shows unreachable code error.

47. Does finally block get executed If either try or catch blocks are returning the
control?

Yes, finally block will be always executed no matter whether try or catch blocks
are returning the control or not.

48. What is Re-throwing an exception in java?

Exceptions raised in the try block are handled in the catch block. If it is unable
to handle that exception, it can re-throw that exception using throw keyword. It
is called re-throwing an exception.

+91 9000045750 packetprep.com


49. Why it is always recommended that clean up operations like closing the DB
resources to keep inside a finally block?

Because finally block is always executed whether exceptions are raised in the
try block or not and raised exceptions are caught in the catch block or not. By
keeping the cleanup operations in finally block, you will ensure that those
operations will be always executed irrespective of whether exception is
occurred or not.

I/O EXCEPTIONS

50. What is Java I/O ?


Java I/O (Input and Output) is used to process the input and produce the output.
Java makes use of the stream concepts to make I/O operation fast. The java.io
package contains all the classes required for input and output operations.
51. What is difference between Scanner and BufferedReader?
Scanner is used for parsing tokens from the contents of the stream while
BufferedReader just reads the stream and does not do any special parsing. Usually
we pass a BufferedReader to a scanner as the source of characters to parse.
52. What are the uses of FileInputStream and FileOutputStream in java?
java.io.FileInputStream and java.io.FileOutputStream were introduced in JDK
1.0. These APIs are used to read and write stream input and output. They can also
be used to read and write images.
53. What is the difference between BufferedReader and FileReader in Java?
BufferedReader is a Decorator that provides buffering for faster IO, while
FileReader is used to read data from File.

54. What is the use of the PrintStream class in Java IO? (answer)
PrintStream is used to write data on Console, for example, System.out.println(),
here out is an object of PrintStream class and we are calling println() method from
that class.

+91 9000045750 packetprep.com

You might also like