Friday, May 31, 2013

OOPS interview questions and answers

Questions 1: What is Object Oriented Programming ?
Answers 1: Object Oriented Programming is a Style of programming that represents a program as a system of objects and enables code-reuse.

Questions 2: What is a Class ?
Answers 2: A class describes all the attributes of objects, as well as the methods that implement the behavior of
member objects. It is a comprehensive data type which represents a blue print of objects. It’s a template of object.

Questions 3: What is an Object ?
Answers 3: An object is an instance of a class. An object is an entity that has attributes, behavior, and identity. Objects
are members of a class. Attributes and behavior of an object are defined by the class definition.

Questions 4: What are class access modifiers?
Answers 4: Access modifiers are keywords used to specify the declared accessibility of a member or a type.
This section introduces the four access modifiers :
Public - Can be accessed by any other class anywhere.
Protected - Can be accessed by classes inside the package or by subclasses ( that means classes
who inherit from this class).
Private - Can be accessed only within the class. Even methods in subclasses in the same package do
not have access.
Default - (Its private access by default) accessible to classes in the same package but not by classes
in other packages, even if these are subclasses.

Questions 5: What are the basic OOPS concepts?
Answers 5: Following are concepts or characteristics of OOPs:

Questions 6: What is the relation between Classes and Objects?
Answers 6: They look very much same but are not same. Class is a definition, while object is instance of the class
created. Class is a blue print while objects are actual objects existing in real world. Example we have
class CAR which has attributes and methods like Speed, Brakes, Type of Car etc.Class CAR is just a
prototype, now we can create real time objects which can be used to provide functionality. Example
we can create a Maruti car object with 100 km speed and urgent brakes.

Questions 7: What is Abstraction?

Answers 7: It allows complex real world to be represented in simplified manner. Example color is abstracted to
RGB.By just making the combination of these three colors we can achieve any color in world. It’s a
model of real world or concept.

Questions 8: What is Encapsulation?
Answers 8: The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Encapsulation containing and hiding information about an object, such as internal data structures and code.
The process of hiding all the internal details of an object from the outside world.

Questions 9: What is Inheritance?
Answers 9: Inheritance is deriving the new class from the already existing one.

Questions 10: What is Polymorphism?

Answers 10: Mean by more than one form. Ability to provide different implementation based on different number/type of parameters.

Questions 11: What is an Abstract class ?

Answers 11: Abstract class defines an abstract concept which can not be instantiated and comparing o interface it
can have some implementation while interfaces can not. Below are some
points for abstract class:-
=>We can not create object of abstract class it can only be inherited in a below class.
=> Normally abstract classes have base implementation and then child classes derive from the
abstract class to make the class concrete.

Questions 12: What are Abstract methods?
Answers 12: Abstract class can contain abstract methods. Abstract methods do not have implementation. Abstract
methods should be implemented in the subclasses which inherit them. So if an abstract class has an
abstract method class inheriting the abstract class should implement the method or else java compiler
will through an error. In this way, an abstract class can define a complete programming interface
thereby providing its subclasses with the method declarations for all of the methods necessary to
implement that programming interface. Abstract methods are defined using "abstract" keyword.
Below is a sample code snippet.
abstract class dhavalGraph

    abstract void draw();
Any class inheriting from "dhavalGraph" class should implement the "draw" method or else the java
compiler will throw an error. so if we do not implement a abstract method the program will not

Questions 13: What is the difference between Abstract classes and Interfaces ?

Answers 13: Difference between Abstract class and Interface is as follows:-
Abstract class can only be inherited while interfaces can not be it has to be implemented.
Interface cannot implement any methods, whereas an abstract class can have implementation.
Class can implement many interfaces but can have only one super class.
Interface is not part of the class hierarchy while Abstract class comes in through inheritance.
Unrelated classes can implement the same interface.

Questions 14: What are the difference between Structure and Class?
Answers 14:
Structures are value type and Classes are reference type
Structures can not have constructors or destructors.
Classes can have both constructors and destructors.
Structures do not support Inheritance, while Classes support Inheritance.

Questions 15: What is difference between Static and Non-Static fields of a class ?
Answers 15: Non-Static values are also called as instance variables. Each object of the class has its own copy of
Non-Static instance variables. So when a new object is created of the same class it will have
completely its own copy of instance variables. While Static values have only one copy of instance
variables and will be shared among all the objects of the class.

Questions 16: What is Overloading?

Answers 16: A process of creating different implementation of a method having a same name as base class, in a derived class. It implements Inheritance.

Questions 17: What is a constructor in class?

Answers 17: Constructor has the same name as the class in which it resides and looks from syntax point of view it
looks similiar to a method. Constructor is automatically called immediately after the object is created,
before the new operator completes. Constructors have no return type, not even void. This is because
the implicit return type of a class' constructor is the class type itself. It is the constructor's job to
initialize the internal state of an object so that the code creating an instance will have a fully
initialized, usable object immediately.

Questions 18: Can constructors be parameterized?

Answers 18: Yes we can have parameterized constructor which can also be termed as constructor overloading.
Below is a code snippet which shows two constructors for pcdsMaths class one with parameter and
one with out.
class dhMaths
    double PI;
    // This is the constructor for the maths constant class.
        PI = 3.14;
    dhMaths(int pi)
        PI = pi;

Questions 19: What is the difference between Overriding and Overloading?

Answers 19:
Overriding : you override a virtual class.
Overloading : you overload a method with different number of parameters or diffent type of parameters.

Questions 20: Define destuctors?

Answers 20: A destructor is called for a class object when that object passes out of scope or is explicitly deleted.
A destructors as the name implies is used to destroy the objects that have been created by a constructors.
Like a constructor , the destructor is a member function whose name is the same as the class name but is precided by a tilde.

Questions 21: What is the use if instanceof keyword? and How do refer to a current instance of object?

Answers 21: "instanceof" keyword is used to check what is the type of object.
we can refer the current instance of object using "this" keyword. For instance if we have class which
has color property we can refer the current object instance inside any of the method using

Questions 22: What is "this" keyword?
Answers 22: this is a reference that each instance has to itself. The this reference gives the instance access to its internal variables and behaviors.

Questions 23: What is difference between Class And Interface?
Answers 23:
Class : is logical representation of object. It is collection of data and related sub procedures with defination.
Interface : is also a class containg methods which is not having any definations.Class does not support multiple inheritance. But interface can support.

Questions 24: What is an identifier?
Answers 24: Identifiers are nothing but names given to various entities uniquely identified in a program.

Questions 25: What is another name for overloading?
Answers 25: Ad-hoc polymorphism is another name for overloading.

Questions 26: Why is the virtual keyword used in code?
Answers 26: The Virtual keyword is used in code to define methods and the properties that can be overridden in derived classes.

Questions 27: What is virtual function?
Answers 27: A virtual function is a member function that is declared within a base class and redefined by a derived class .To create a virtual function, the function declaration in the base class is preceded by the keyword virtual.

Questions 28: What is the use of copy constructor?
Answers 28: Copy constructor is a constructor function with the same name as the class and used to make deep copy of objects.

Questions 29: What is a Static class?
Answers 29: Static class is a class which can be used or accessed without creating an instance of the class.

Questions 30: What are the different ways a method can be overloaded?

Answers 30:
A method can be overloaded :
By varying the numbers of parameters
By using different data types for the parameters
By using different sequence of the parameters.

Questions 31: What are the different categories of inheritance?
Answers 31: Inheritance in OOP is of four types :
Single inheritance : Contains one base class and one derived class.
Hierarchical inheritance : Contains one base class and multiple derived classes of the same base class.
Multilevel inheritance : Contains a class derived from a derived class.
Multiple inheritance : Contains several base classes and a derived class.

Questions 32: What is an Abstract class?

Answers 32: An abstract class is a special kind of class that cannot be instantiated.
It normally contains one or more abstract methods or abstract properties.
It provides body to a class.

Questions 33: What is meant by data encapsulation?

Answers 33: Data encapsulation, also referred to as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user.
The user can only perform a restricted set of operations on the hidden members of the class by executing special functions called methods.

Questions 34: What are the difference between const and readonly?

Answers 34:
A const can not be static, while readonly can be static.
A const need to be declared and initialized at declaration only, while a readonly can be initialized at declaration or by the code in the constructor.
A const's value is evaluated at design time, while a readonly's value is evaluated at runtime.

Questions 35: What are inner classes and what is the practical implementation of inner classes?
Answers 35: Inner classes are nested inside other class. They have access to outer class fields and methods even if
the fields of outer class are defined as private.

public class Dhaval
    class dhEmp
        // inner class defines the required structure
        String first;
        String last;
    // array of name objects
    clsName personArray[] = {new clsName(), new clsName(), new clsName()};
Normally inner classes are used for data structures like one shown above or some kind of helper

Questions 36: Define exceptions ?
Answers 36: An exception is an abnormal condition that arises in a code sequence at run time. Basically there are
four important keywords which form the main pillars of exception handling: try, catch, throw and
finally. Code which you want to monitor for exception is contained in the try block. If any exception
occurs in the try block its sent to the catch block which can handle this error in a more rational
manner. To throw an exception manually you need to call use the throw keyword. If you want to put
any clean up code use the finally block. The finally block is executed irrespective if there is an error or not.

Questions 37: What is a delegate?
Answers 37:
Delegate is a class that can hold a reference to a method or a function.
Delegate class has a signature and it can only reference those methods whose signature is compliant with the class.
Delegates are type-safe functions pointers or callbacks.

Questions 38: What is serialization?How do we implement serialization actually?
Answers 38: Serialization is a process by which an object instance is converted in to stream of bytes. There are
many useful stuff you can do when the object instance is converted in to stream of bytes for instance
you can save the object in hard disk or send it across the network.
In order to implement serialization we need to use two classes from package
ObjectOutputStream and ObjectInputStream. ObjectOutputStream has a method called writeObject,
while ObjectInputStream has a method called readObject. Using writeobject we can write and
readObject can be used to read the object from the stream. Below are two code snippet which used
the FileInputStream and FileOutputstream to read and write from harddisk.

Questions 39: What is Sealed modifiers?
Answers 39:
Sealed types cannot be inherited & are concrete.
Sealed modifiers can also be applied to instance methods, properties, events & indexes. It can't be applied to static members.
Sealed members are allowed in sealed and non-sealed classes.

Questions 40: What is a base class?
Answers 40: Base class is the most generalised class in a class structure. Most applications have such root classes. In Java, Object is the base class for all classes.

Questions 41: What is a superclass?
Answers 41: superclass is a class from which another class inherits.

Questions 42: What is a subclass?
Answers 42: Subclass is a class that inherits from one or more classes

Questions 43: What are the features of abstract class?
Answers 43:
Abstract class can't be static.
Abstract class is used for inheritance.
Abstract class can't be sealed.
Abstract or virtual members can't be private, as we can't override it.
Abstract class can be inherited from an abstract class but the methods in the base class have to be declared abstract.

Questions 44: What are the types of polymorphism?
Answers 44: There are two types of polymorphism are :
Compile time polymorphism
Runtime polymorphism

Questions 45: What is the difference between compile time polymorphism and runtime polymorphism?
Answers 45:
Compile time polymorphism is method and operators overloading. It is also called early binding.
Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.

Questions 46: What is the difference between this() and super()?
Answers 46:
this() can be used to invoke a constructor of the same class whereas
super() can be used to invoke a super class constructor.

Questions 47: What is the difference between Array and vector?
Answers 47:
Array is a set of related data type and static whereas
vector is a growable array of objects and dynamic.

Questions 48: What is the difference between exception and error?
Answers 48:
The exception class defines mild error conditions that your program encounters.
Exceptions can occur when trying to open the file, which does not exist, the network connection is disrupted, operands being manipulated are out of prescribed ranges, the class file you are interested in loading is missing.
The error class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered.

Questions 49: What is the difference between process and thread?
Answers 49:
Process is a program in execution whereas
thread is a separate path of execution in a program.

Questions 50: What is the difference between an argument and a parameter?
Answers 50: While defining method, variables passed in the method are called parameters.
While using those methods, values passed to those variables are called arguments.

No comments:

Post a Comment