C++ Interview Questions & Answers

Posted On:June 6, 2019, Posted By: Latest Interview Questions, Views: 227, Rating :

Best C++ Interview Questions and Answers 

Dear Readers, Welcome to C++ Interview Questions and Answers have been designed specially to get you acquainted with the nature of questions you may encounter during your Job interview for the subject of C++. These C++ Questions are very important for campus placement test and job interviews. As per my experience good interviewers hardly plan to ask any particular questions during your Job interview and these model questions are asked in the online technical test and interview of many IT companies.

1. What is C++?

Released in 1985, C++ is an object-oriented programming language created by Bjarne Stroustrup. C++ maintains almost all aspects of the C language, while simplifying memory management and adding several features - including a new datatype known as a class (you will learn more about these later) - to allow object-oriented programming. C++ maintains the features of C which allowed for low-level memory access but also gives the programmer new tools to simplify memory management.

C++ used for:

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++.

Interview Questions On C++

2. What is an accessor in C++?

An accessor is a class operation that does not modify the state of an object in C++. The accessor functions need to be declared as const operations

 

3. Differentiate between a template class and class template in C++?

Template class: A generic definition or a parametrized class not instantiated until the client provides the needed information. It's jargon for plain templates. Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed. It's jargon for plain classes.

 

4. When does a name clash occur in C++?

A name clash occurs when a name is defined in more than one place. For example., two different class libraries could give two different classes the same name. If you try to use many class libraries at the same time, there is a fair chance that you will be unable to compile or link the program because of name clashes.

 

5. Define namespace in C++?

It is a feature in C++ to minimize name collisions in the global name space. This namespace keyword assigns a distinct name to a library that allows other libraries to use the same identifier names without creating any name collisions. Furthermore, the compiler uses the namespace signature for differentiating the definitions.

 

6. What is the use of 'using' declaration in C++?

A using declaration in C++ makes it possible to use a name from a namespace without the scope operator.

 

7. Differentiate between the message and method in C++?

Message in C++ :

* Objects communicate by sending messages to each other.

* A message is sent to invoke a method in C++.

Method in C++ :

* Provides response to a message.

* It is an implementation of an operation in C++.

 

8. What is an adaptor class or Wrapper class in C++?

A class that has no functionality of its own is an Adaptor class in C++. Its member functions hide the use of a third party software component or an object with the non-compatible interface or a non-object-oriented implementation.

 

9. What is a Null object in C++?

It is an object of some class whose purpose is to indicate that a real object of that class does not exist. One common use for a null object is a return value from a member function that is supposed to return an object with some specified properties but cannot find such an object.

 

10. What is class invariant in C++?

A class invariant is a condition that defines all valid states for an object. It is a logical condition to ensure the correct working of a class. Class invariants must hold when an object is created, and they must be preserved under all operations of the class. In particular all class invariants are both preconditions and post-conditions for all operations or member functions of the class.

 

11. What do you mean by Stack unwinding in C++?

Stack unwinding in C++ is a process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught.

 

12. What are the conditions that have to be met for a condition to be an invariant of the class?

? The condition should hold at the end of every constructor.

? The condition should hold at the end of every mutator (non-const) operation.

 

13. Name some pure object oriented languages?

pure object oriented languages are Smalltalk, Java, Eiffel, Sather.

 

14. What is a node class in C++?

A node class is a class that,

? relies on the base class for services and implementation,

? provides a wider interface to the users than its base class,

? relies primarily on virtual functions in its public interface

? depends on all its direct and indirect base class

? can be understood only in the context of the base class

? can be used as base for further derivation

? can be used to create objects.

A node class is a class that has added new services or functionality beyond the services inherited from its base class.

 

15. What is an orthogonal base class in C++?

If two base classes have no overlapping methods or data they are said to be independent of, or orthogonal to each other. Orthogonal in the sense means that two classes operate in different dimensions and do not interfere with each other in any way. The same derived class may inherit such classes with no difficulty.

 

16. What is a container class? What are the types of container classes in C++?

A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.

 

17. What is polymorphism in C++?

Polymorphism in C++ 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.

 

18. How can you tell what shell you are running on UNIX system?

You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -1 and look for the shell with the highest PK).

 

19. What is the difference between realloc() and free()?

The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer.

 

20. What is the difference between declaration and definition?

The declaration tells the compiler that at some later point we plan to present the definition of this

declaration.

E.g.: void stars () //function declaration

The definition contains the actual implementation.

E.g.: void stars () // declarator

{

for(intj=10; j > =0; j—- //function body

cout« *;

cout« endl; }

 

21. What are the advantages of inheritance in C++?

It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.

 

22. What do you mean by inline function?

The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.

 

23. What is public, protected, private in C++?

* 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.

 

24. What is the difference between class and structure in C++?

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.

 

25. What is RTTI in C++?

Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type of an object and to convert the type of a pointer or reference (that is, dynamic typing). The need came from practical experience with C++. RTTI replaces many homegrown versions with a solid, consistent approach.

 

26. What is encapsulation in C++?

Packaging an object's variables within its methods is called encapsulation.

 

27. What do you mean by inheritance?

Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.

 

28. What is a COPY CONSTRUCTOR and when is it called?

A copy constructor is a method that accepts an object of the same class and copies it's data members to the object on the left part of assignement:

class Point2D{ int x; int y;

public int color;

protected bool pinned;

public Point2D() : x(0), y(0) {} //default (no argument) constructor

public Point2D( const Point2D & );

};

Point2D::Point2D( const Point2D & p )

{

this->x = p.x; this->y = p.y; this->color = p. color; this->pinned = p.pinned;

}

main(){

Point2D MyPoint;

MyPoint.color = 345;

Point2D AnotherPoint = Point2D( MyPoint); // now AnotherPoint has color = 345

 

29. What is Boyce Codd Normal form in C++?

A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a-> , where a and b is a subset of R, at least one of the following holds:

* a- > b is a trivial functional dependency (b is a subset of a)

* a is a superkey for schema R

 

30. What is virtual class and friend class?

Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.

 

31. What do you mean by binding of data and functions? 

Encapsulation.

 

32. What is friend function in C++?

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.

 

33. What is abstraction in C++?

Abstraction is of the process of hiding unwanted details from the user.

 

34. What are virtual functions in C++?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

 

35. What is a scope resolution operator?

A scope resolution operator (::), can be used to define the member functions of a class outside the class.

 

36. What do you mean by pure virtual functions?

A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero. class Shape { public: virtual void draw() = 0; };

 

37. What is polymorphism in C++? Explain with an example?

"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object. Example: function overloading, function overriding, virtual functions. Another example can be a plus '+' sign, used for adding two integers or for using it to concatenate two strings.

 

38. What is the best way to declare and define global variables?

The best way to declare global variables is to declare them after including all the files so that it can be used in all the functions.

 

39. What does extern mean in a function declaration in C++?

It tells the compiler that a variable or a function exists, even if the compiler hasn't yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.

 

40. Explain the scope resolution operator.

It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.

 

41. What is an explicit constructor?

A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction.

 

42. What is the Standard Template Library (STL)?

A library of container templates approved by the ANSI committee for inclusion in the standard C++ specification.

A programmer who then launches into a discussion of the generic programming model, iterators, allocators, algorithms, and such, has a higher than average understanding of the new technology that STL brings to C++ programming.

 

43. In C++, what is the difference between method overloading and method overriding?

Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

 

44. What methods can be overridden in Java?

In C++ terminology, all public methods in Java are virtual. Therefore, all Java methods can be overwritten in subclasses except those that are declared final, static, and private.

 

45. What are the defining traits of an object-oriented language?

The defining traits of an object-oriented langauge are:

* encapsulation

* inheritance

* polymorphism