INTERVIEW QUESTIONS » IMPORTANT C++ interview questions and answers for freshers

IMPORTANT C++ interview questions and answers for freshers

C++ is a powerful general-purpose programming language. It can be used to create small programs or large applications. It can be used to make CGI scripts or console-only DOS programs. C++ allows you to create programs to do almost anything you need to do. The creator of C++, Bjarne Stroustrup, has put together a partial list of applications written in C++.

Class is a blue print which reflects the entities attributes and actions. Technically defining a class is designing an user defined data type.

Inheritance is the process of acquiring the properties of the exiting class into the new class. The existing class is called as base/parent class and the inherited class is called as derived/child class.

A reference variable is an alias name for the existing variable. Which mean both the variable name and reference variable point to the same memory location. Therefore updation on the original variable can be achieved using reference variable too.

A static variable does exit though the objects for the respective class are not created. Static member variable share a common memory across all the objects created for the respective class. A static member variable can be referred using the class name itself.

Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects. A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don’t change. The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed. An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

A constructor is the member function of the class which is having the same as the class name and gets executed automatically as soon as the object for the respective class is created.

Every class does have a constructor provided by the compiler if the programmer doesn’t provides one and known as default constructor. A programmer provided constructor with no parameters is called as default constructor. In such case compiler doesn’t provides the constructor.

A function which is not a member of the class but still can access all the member of the class is called so. To make it happen we need to declare within the required class following the keyword ‘friend’.

A copy constructor is the constructor which take same class object reference as the parameter. It gets automatically invoked as soon as the object is initialized with another object of the same class at the time of its creation.

• Public, protected and private are three access specifiers in C++.
• Public data members and member functions are accessible outside the class.
• Protected data members and member functions are only available to derived classes.
• Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.

Polymorphism is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.

Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private.

The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. First argument represents the count of arguments (below in count) and updated automatically by operating system.

By using the static method there is no need creating an object of that class to use that method. We can directly call that method on that class. For example, say class A has static function f(), then we can call f() function as A.f(). There is no need of creating an object of class A.

Advantages of throw and catch in c++ :
– Code isolation: All code that is to be handled when an exception raises is placed in the catch block, which is not part of the original code.
– The logic is pertaining to the type of the error, so that the developer can concentrate only on that exception.
– Developer can be selective about trapping various exceptions.

– C++ provides a mechanism to handle exceptions which occurs at runtime. C++ uses the keywords – throw, catch and try to handle exception mechanism. An entity called an exception class need to be created.
– The application should have a separate section called catch block. The code in which an exception is predicted is authored in the try block.

Stack is a collection of objects that works in LIFO (Last in First out) mechanism while Queue is FIFO (First in First out). This means that the object that is inserted first is removed last in a stack while an object that is inserted first is removed first in a queue.

Virtual Destructor :
– The explicit destroying of object with the use of delete operator to a base class pointer to the object is performed by the destructor of the base-class is invoked on that object.
– The above process can be simplified by declaring a virtual base class destructor.
– All the derived class destructors are made virtual in spite of having the same name as the base class destructor. In case the object in the hierarchy is destroyed explicitly by using delete operator to the base class pointer to a derived object, the appropriate destructor will be invoked.

– Exception handling in C++ is implemented by using the try{} and catch(){} statements.
– When a try block throws an exception, the program leaves the try block and enters the catch statement of the catch block.
– If they type of the object thrown matches the arg type in the catch block, catch block is executed for handling the code.
– If they are not caught, abort() function is executed by default.
– When no exception is deteted or thrown then the control goes to the statement below the catch block.

– new and delete operators are provided by C++ for runtime memory management. They are used for dynamic allocation and freeing of memory while a program is running.
– The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new.

Static member functions are used to maintain a single copy of a class member function across various objects of the class. Static member functions can be called either by itself, independent of any object, by using class name and :: (scope resolution operator) or in connection with an object.
Restrictions on static member functions are :

1. They can directly refer to other static members of the class.
2. Static member functions do not have this pointer.
3. Static member function can not be virtual.

Pre-Condition :
A condition that should return true when a member function is invoked. In order to use a function correctly a precondition should return true. If a precondition fails to hold, an operation will not take responsibility to perform any action of sensibility. For example, the interface invariants of stack class respond nothing about pushing even though the stack is already full. In this scenario, sinful () is a precondition for push operation.

Post-Condition :
A condition that should return true before returning from an invoked function. In order to use a function correctly a post condition should return true. Taking a stack as an example, is empty () must necessarily be true after pushing the element into the stack when an element is pushed. The function is empty () is a post condition.

An array is a collection of variables of the same type that are referred to through a common name. A specific array element is accessed by an index. In C++ arrays consist of contiguous memory location. The lowest memory address corresponds to first element and the highest to the last element of the array. Arrays could be single dimensional or multi dimensional. A string is most common example of an array. It is a single dimensional array of characters.

A matrix is simply a 2-D array, which is widely used in scientific programming to perform calculations. There are various ways to implement matrix class: using arrays, using pointers, using vectors.

Multidimensional arrays are capable of storing data in a two or more dimensional structure. For example, you can use a 2 dimensional array to store the current position of pieces in a chess game, or position of players in a tic-tac-toe program.

– A pointer is a variable that holds a memory address. This address is the location of another object (typically, a variable) in memory. That is, if one variable contains the address of another variable, the first variable is said to point to the second.
– A pointer declaration consists of a base type, an *, and the variable name.

A function has a physical location in the memory which is the entry point of the function. And this is the address used when a function is called. This address can be assigned to a pointer. Once a pointer points to a function, the function can be called through that pointer. Function pointers also allow functions to be passed as arguments to other functions. The address of a function is obtained by using the function’s name without any parenthesis or arguments.

A NULL pointer has a fixed reserved value that is not zero or space, which indicates that no object is referred. NULL pointers are used in C and C++ as compile-time constant. NULL pointer represents certain conditions, like successor to the last element in linked list, while a consistent structure of the list of nodes are maintained.
A void pointer is a special pointer that points to an unspecified object. A null pointer can not be dereferenced. The address manipulation can directly be done by pointer casting to and from an integral type of sufficient size.

When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved by preceding the base class’ name with the word virtual.