C++



21. What are the basic concepts of OOP?  

Data Abstraction, Data Hiding, Data Encapsulation, Inheritance and Polymorphism are the basic concepts of OOP.

  1. Classes and Objects
  2. Encapsulation
  3. Data abstraction
  4. Inheritance


22. What is Encapsulation?  

Encapsulation is the mechanism by which data and associated operations/methods are bound together and thus hide the data from outside world. It's also called data hiding. In c++, encapsulation achieved using the access specifiers (private, public and protected). Data members will be declared as private (thus protecting from direct access from outside) and public methods will be provided to access these data. Consider the below class
class Person
{
   private:
      int age;
   public:
      int getAge(){
        return age;
      }
      int setAge(int value){
        if(value > 0){
            age = value;
        }
      }
};

In the class Person, access to the data field age is protected by declaring it as private and providing public access methods. What would have happened if there was no access methods and the field age was public? Anybody who has a Person object can set an invalid value (negative or very large value) for the age field. So by encapsulation we can preventing direct access from outside, and thus have complete control, protection and integrity of the data.


23. What is Data abstraction?  

Data abstraction refers to hiding the internal implementations and show only the necessary details to the outside world. In C++ data abstraction is implemented using interfaces and abstract classes.
class Stack
{
    public:
        virtual void push(int)=0; 
        virtual int pop()=0;
};
 
class MyStack : public Stack
{
    private:
        int arrayToHoldData[]; //Holds the data from stack
 
    public:
        void push(int) {
            // implement push operation using array
        }
        int pop(){
            // implement pop operation using array
        }
};

The above example, the outside world only need to know about the Stack class and its push, pop operations. Internally stack can be implemented using arrays or linked lists or queues or anything that you can think of. This means, as long as the push and pop method performs the operations work as expected, you have the freedom to change the internal implementation with out affecting other applications that use your Stack class.


24. What is Inheritance?  

Inheritance allows one class to inherit properties of another class. In other words, inheritance allows one class to be defined in terms of another class.
class SymmetricShape
{
    public:
        int getSize()
        {
            return size;
        }
        void setSize(int w)
        {
            size = w;
        }
    protected:
        int size;
};
 
// Derived class
class Square: public SymmetricShape
{
    public:
        int getArea()
        { 
            return (size * size); 
        }
};

The above example, class Square inherits the properties and methods of class SymmetricShape. Inheritance is the one of the very important concepts in C++/OOP. It helps to modularise the code, improve reusability and reduces tight coupling between components of the system.



25. What is abstract class?  

A class with no instances (no objects) is known as abstract class.



26. What is concrete class?  

A class having objects is known as concrete class.



27. What is a constructor? What are its features?  

It is a member function of class with the following unique features.
It has same name as the name of the class they belong to.
It has no return type.
It is defined in public visibility mode.
It is automatically called & executed when an object is declared/created.
Constructor can be overloaded.


28. What does a destructor do?  

A destructor deinitializes an object and deallocates all allocated resources.



29. Define inheritance?  

Inheritance is a process of creating new classes (derived classes) from existing classes (base classes). The derived classes not only inherit capabilities of the base class but also can add new features of own. The most important aspect of inheritance is that it allows reusability of code.



30. Define Base class and derived class.  

Base Class: A class from which another class inherits.

Derived Class: A class inheriting properties from another class.


31. What are the different forms of inheritance in C++?  

Single level inheritance, Multilevel inheritance, Hierarchical inheritance, Multiple inheritance and Hybrid inheritance.



32. What is virtual base class in C++?What is its significance?  

Multipath inheritance may lead to duplication of inherited members from a grandparent base class. This may be avoided by making the common base class a virtual base class. When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited.

Multipath inheritance may lead to duplication of inherited members from a grandparent base class. This may be avoided by making the common base class a virtual base class. When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited.



33. How are binary files different from text files in C++?  

A text file store information in ASCII characters. In text files, each line of text is terminated, with a special character known as EOL character.

A binary file store information in the same format in which the information is held in memory. In binary file, there is no delimeter for a line.



34. What is a stream? Name the streams generally used for file I/O.  

A stream is a sequence of byte.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.


35. Difference between get() and getline()?  

get() does not extract the delimeter newline character from input stream. On the other hand getline() does extract the delimeter newline character from the input stream so that the stream is empty after getline() is over.



36. Difference between ios::app and ios::out.  

The ios::out is the default mode of ofstream. With the mode of the file does not exist, it gets created but if the file exists then its existing contents get deleted.

The ios::app is output mode of ofstream. With the mode of the file does not exist, it gets created but if the file exists then its existing contents are retained and new information is appended to it.



37. What is pointer?  

Pointer is an address of a memory location. A variable, which holds an address of a memory location, is known as a Pointer variable (or Simply Pointer). For example int *P;



38. What is pointer arithmetic ? How is it performed?  

Some arithmetic operators can be used with pointers:
Increment and decrement operators ++, --
Integers can be added to or subtracted from pointers using the operators +, -, +=, and -=
Each time a pointer is incremented by 1, it points to the memory location of the next element of its base type.
If "p" is a character pointer then "p++" will increment "p" by 1 byte.
If "p" were an integer pointer its value on "p++" would be incremented by 2 bytes.


39. Differentiate between static and dynamic allocation of memory?  

In the static memory allocation, the amount of memory to be allocated is predicted and preknown. This memory is allocated during the compilation itself.

In the dynamic memory allocation, the amount of memory allocated is not known beforehead. This memory is allocated during run time as and when required.


40. What do you understand by memory leaks? How can memory leaks be avoided?  

Memory leak is a situation in which there lie so many orphaned memory blocks that are still allocated but no pointers are referencing to them.



.Net Interview Question

PHP Interview Question

Java Interview Question

AngularJS Interview Questions