oops interview questions and answers

1:What is Object Oriented Programming ?
       It is a problem solving technique to develop software systems. It is a technique to think real world in terms of objects. Object maps the software model to real world concept. These objects have responsibilities and provide services to application or other objects.  
      Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.
2:What is a Class ? 
         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.
          Class is a template for a set of objects that share a common structure and a common behavior.
3:What is an Object ? 
         It is a basic unit of a system. 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.
        Object is an instance of a class. It has state,behaviour and identity. It is also called as an instance of a class.

5:What is the relation between Classes and Objects?
           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.
6:What is an Abstract class ? 
         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.  
7:What is POLYMORPHISM in oop?
           Polymorphism literally means taking more than one form. Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to something that was declared in a parent class. 
 for more details 
8:What is meant by static binding?
     Static binding is a binding in which the class association is made during compile time. This is also called as Early binding.
9:What is meant by Dynamic binding?
     Dynamic binding is a binding in which the class association is not made until the object is created at execution time. It is also called as Late binding.
10: What is the use of friend function?
         Sometimes a function is best shared among a number of different classes. Such functions can be declared either as member functions of one class or as global functions. In either case they can be set to be friends of other classes, by using a friend specifier in the class that is admitting them. Such functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class.
11:Explain about encapsulation?
Encapsulation passes the message without revealing the exact functional details of the class. It allows only the relevant information to the user without revealing the functional mechanism through which a particular class had functioned.
12:Static Keyword
    * To implement static keyword functionality to the attributes or the methods will have to be prefix with static keyword.
    * Static properties or methods can be accessible without needing an instantiation of the class.
    * A property declared as static can not be accessed with an instantiated class object.
    * $this is not available inside the method declared as static.
    * Static properties cannot be accessed using the arrow operator ->.
    * Static properties can be accessed using the Scope Resolution Operator (::) operator.
 ClassName::$staticvar= $value;


class Box
   static private $color; 

   function __construct($value)
if($value != "")
Box::$color = $value;

   public function  getColor ()
echo Box::$color;

$a = new Box("RED");
$a = new Box("GREEN");
$a = new Box("");

13: What is Inheritance in OOPS?
  • Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (concept) of object-oriented programming
  • Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes
  • The Class whose methods and variables are defined is called super class or base class
  • The Class that inherits methods and variables are defined is called sub class or derived class
  • Sometimes base class known as generalized class and derived class known as specialized class.
Benefits of using Inheritance
  • Once a behavior (method) or property is defined in a super class(base class),that behavior or property is automatically inherited by all subclasses (derived class).
  • Code reusability increased through inheritance
  • Inheritance provide a clear model structure which is easy to understand without much complexity
  • Using inheritance, classes become grouped together in a hierarchical tree structure
  • Code are easy to manage and divided into parent and child classes
8. What is Compile Time Polymorphism in OOPS?
         Compile time Polymorphism also known as method overloading
Method overloading means having two or more methods with the same name but with different signatures.

9. What is Run Time Polymorphism in OOPS?
     Run time Polymorphism also known as method overriding
Method overriding means having two or more methods with the same name , same signature but with different implementation.
10:Access modifier:
   OOP provides data-hiding capabilities with public, protected, and private data attributes and methods:
Public : A public variable or method can be accessed directly by any user of the class.
Protected : A protected variable or method cannot be accessed by users of the class but can be accessed inside a subclass that inherits from the class.
Private:A private variable or method can only be accessed internally from the class in which it is defined.
    * Generates a storable representation of a value.
    * serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP.
    * unserialize() can use this string to recreate the original variable values.
    * This process makes a storable representation of a value that is useful for storing or passing PHP values.
    * To make the serialized string into a PHP value again, use unserialize().

Before starting your serialization process, PHP will execute the __sleep function automatically. This is a magic function or method.
Before starting your unserialization process, PHP will execute the __wakeup function automatically. This is a magic function or method.
12:What can you Serialize and Unserialize?

    * Variables
    * Arrays
    * Objects
What cannot you Serialize and Unserialize?
    * Resource-type
13: What is a Virtual Function ?
     Virtual functions are normal member functions of a class, which can be over-ridden in the derived classes. The whole functionality can be replaced in the over-riding function
14:What is a virtual class?
        In multiple inheritance when a class D is derived by two base classes B and C and both base class is also inherite by same class A then then class D will get duplicate copy of uppper most base class A.In this case class A will declare as virtual class.
15:What is the difference between a virtual function and pure virtual function?
          The main difference is the body of function.
Vartual Function have a function body.
We define them as :
 Virtual int virFun();
          A pure virtual functions doesn't have a body.
We define them as:
 Virtual int myFun() = 0;
      Pure virtual functions have no body and MUST be overloaded in the derived classes. You cannot create an instance of a class having a pure virtual function, the need is to inherit from it and overload the all pure virtual functions.
16:Why destructor is not over loaded?
      Normally (in fact almost always) you never explicitly call the destructor, it is automatically called by the C++ frame work when the object is deleted/goes out of scope. Therefore you would not need to overload the destructor because the frame work always calls the default destructor.
17:Diversities between an abstract method & virtual method ?
           An Abstract method does not provide an implementation and forces overriding to the deriving class (unless the deriving class also an abstract class), where as the virtual method has an implementation and leaves an option to override it in the deriving class. Thus Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.
18:Can we declare private class in a Namespace?
          No. If you try to create a private class in a Namespace, Compiler will throw a compile time error “Namespace elements cannot be explicitly declared as private, protected, or protected internal”.
     Reason: The message says it all. Classes can only be declared as private, protected or protected internal when declared as nested classes, other than that, it doesn't make sense to declare a class with a visibility that makes it unusable, even in the same module. Top level classes cannot be private, they are "internal" by default, and you can just make them public to make them visible from outside your DLL.
19:What is the difference between superclass and subclass?
    A super class is a class that is inherited whereas sub class is a class that does the inheriting.
20:What is namespace?
    Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.
The form to use namespaces is:
namespace identifier { namespace-body }
Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:
namespace general { int a, b; } In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would have to put:
general::a general::b
The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error.

22:What is friend function?
        As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition.
Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?
Mergesort always makes recursive calls to sort subarrays that are about half size of the original array, resulting in O(n log n) time.


Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Homebrew uninstall