C++ Interview Question and Answers – Part 1

1. Explain the difference between reference and pointer?
Answer : a)References are less powerful than pointers. Once a reference is created, it can’t refer to other object later. Pointers provide the powerful facilities than references.
b) References are safer and easier to use than pointers. Pointers are comparatively difficult to use.

2. What are command line arguments?
Answer : 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.
main( int count, char *args[])

3. Explain the pointer – this.
Answer : This, is the pointer variable of the compiler which always holds the current active object’s address.

4. STL stands for ?
Answer : Standard template library

5. What are static members and static functions?
Answer : Static members are created and initialized only once and shared among all the class objects.
Static functions are similar to the static variables and are associated with the class and can only access static variables of a class and can also be called using the scope resolution operator.

6. Can a function overloading depend only on passing by value and passing by reference?
Answer : No, the reason is that whether a function is called the passing a parameter as a value or by reference, it appears similar to the caller of the function.

7. How can a struct in C++ differs from a struct in C?
Answer : The differences between struct in C++ and C are listed in the following points:
In C and C++, the variables of the structures are public; however, in C, the variable cannot be declared as private or protected. On the contrary, in C++, the variables can be declared as private or protected.
In C, the concept of inheritance is not supported. In C++, the concept of inheritance is fully supported.
On declaring a struct in C, the addition of the struct keyword is must. On the contrary, there is no need of the struct keyword on declaring struct in C++.
In C, the initialization cannot be done outside the scope of a structure. However, in C++, the initialization can be done outside the scope of a structure.
In C, structures do not have direct functions or methods.

8. What are the types of STL containers?
Answer : deque
hash map

9. What is a modifier?
Answer : A modifier, also called a modifying function is a member function that changes the value of at least one data member.
In other words, an operation that modifies the state of an object.
Modifiers are also known as mutators.

10. What is the difference between array and a list?
Answer : Array is a collection of homogeneous elements while list is a collection of heterogeneous elements.
Array memory allocation is static and continuous while List memory allocation is dynamic and random.
In Array, users don’t need to keep in track of next memory allocation while In list user has to keep in track of next location where memory is allocated.