C++ Interview Questions and answers: Here, we highlight some common questions you might be asked during a job interview for C++ related positions. All the best for your interview Preparation.
C++ is an opensource, general purpose Multi paradigm, high level programming language. It supports both Procedural and object oriented programming. C++ is a superset of C language. It is one of the most popular programming language for creating large scale applications. C++ Programming language: A Class is a user defined data-type which has data members and member functions. A class is defined in C++ using keyword class followed by the name of class. An Object is an instance of a Class. When class is defined, only the specification for the object is defined; no memory or storage is allocated. Class members can gain accessibility over other class member by placing class declaration prefixed with the keyword “friend” in the destination class.” Storage class are used to specify the visibility/scope and life time of symbols(functions and variables). That means, storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of program. A c++ program consists of various tokens and a token is either a keyword, an identifier, a string literal, or a symbol, a constant A function that calls itself is known as recursive function, this is also known as recursion. Function overriding is a way to implement polymorphism. Function overriding is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes. A virtual function is a member function in base class that you expect to redefine in derived classes. Variables prefixed with the keyword volatile acts as a data type qualifier. The volatile keyword attempts to alter the default way in which the variables are stored and the way the compiler handles the variables. Pointers are special type of variables that are used to store the memory address of the other variables. Pointers are declared normallt as other variables with difference of * that is present in front of the pointer identifier. NULL pointer is not the uninitialized pointer that can point anywhere, the NULL pointers are the one which do not point anywhere that is which do not point to any object or any function. An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used. While handling real world problems we come across situations when we want to use different data type as one, C allows the user to define it own data type known as structures and unions. Structures and unions gathers together different atoms of information are that comprise a given entity. Inheritance provides reusability. Reusability means that one can use the functionalities of the existing class. It eliminates the redundancy of code. Inheritance is a technique of deriving a new class from the old class. The old class is known as the base class, and the new class is known as derived class. Encapsulation is a process of combining data members and functions in a single unit called class. Abstraction is a one of the technique of showing only essential details without representing the implementation details. If the members are defined with a public keyword, then the members are accessible outside also. If the members are defined with a private keyword, then the members are not accessible by the outside methods. Data binding is a process of binding the application UI and business logic. Any change made in the business logic will reflect directly to the application UI. Polymorphism means multiple forms. It means having more than one function with the same name but with different functionalities. Polymorphism is of two types: No, the real purpose of a class is not to export data. Rather, it is to provide services. Class provides a way to abstract behaviour rather than just encapsulating the bits. For More: The biggest benefits of proper inheritance are: a.) substitutability and b.) extensibility. Many projects meet a dead end because of bad inheritance. So, it certainly has the potential to wreck a project. Small projects still have a scope to avoid the complete consequence of bad inheritance if the developers communicate and co-ordinate with an easy system design. This kind of a luxury is not possible in big projects, which means that the code breaks in a way difficult and at times impossible way to fix it. A function should throw an exception when it is not able to fulfil its promise. As soon as the function detects a problem that prevents it from fulfilling its promise, it should throw an exception. If the function is able to handle the problem, recover itself and deliver the promise, the exception should not be thrown. If an event happens very frequently then exception handling is not the best way to deal with it. It requires proper fixation. Yes, there are a few rules about inlining: According to one-definition rule, C++ constructs must be identically defined in every compilation unit they are used in. As per ODR, two definitions contained in different source files are called to be identically defined if they token-for-token identical. The tokens should have same meaning in both source files. Identically defined doesn’t mean character-by-character equivalence. Two definitions can have different whitespace or comments and yet be identical. When the compiler uses this constructor to create an object – the constructor will have no constructor initializer and a null body. The container class can be of two types: Namespaces are used to group entities like classes, objects and functions under a name. Early binding increases the efficiency. Some of the examples of early binding are normal function calls, overloaded function calls, and overloaded operators etc. These are three access specifiers in C++ Following are the 3 types of STL containers: Stack unwinding 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. We can find out if the linked-list is not a cycle by using two pointers. One of them goes 2 nodes every time while the second one goes at 1 node each time. If there is a cycle, the one that goes 2 nodes each time will meet the one that goes slower. If this happens, you can say that the linked-list is a cycle else not. It is a constructore which initializes it’s object member variable with another object of the same class. If you don’t implement a copy constructor in your class, the compiler automatically does it. Copy constructors are called in these situations: When the address of an object is used after its lifetime is over, dangling pointer comes into existence. Some examples of such situations are: Returning the addresses of the automatic variables from a function or using the address of the memory block after it is freed. When you want to use non-static const data members and reference data members you should use initialization list to initialize them. If your class has at least one virtual function, you should have a virtual destructor. This allows you to delete a dynamic object through a baller to a base class object. In absence of this, the wrong destructor will be invoked during deletion of the dynamic object. It is a constant pointer type. It gets created when a non-static member function of a class is called. Mentioning the keyword int as the second parameter in the post increment form of the operator++() helps distinguish between the two forms. A program database (PDB) file contains debugging and project state information that allows incremental linking of a Debug configuration of the program. This file is created when you compile a C/C++ program with /ZI or /Zi or a Visual Basic/C#/JScript .NET program with /debug. To check this, you can simply do the Echo $RANDOM. The results will be: A Stack is a linear structure in which insertions and deletions are always made at one end i.e the top – this is termed as last in, first out (LIFO). Stacks are useful when we need to check some syntex errors like missing parentheses. An external iterator is implemented as a separate class that can be “attach” to the object that has items to step through while an internal iterator is implemented with member functions of the class that has items to step through. With an external iterator many different iterators can be active simultaneously on the same object – this is its basic advantage. The extern here tells the compiler about the existence of a variable or a function, even though 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. This can be done by using the extern “C” linkage specification around the C function declarations. STL stands for Standard Template Library. It is a library of container templates approved by the ANSI committee for inclusion in the standard C++ specification. What is C++?
What are the differences between C and C++?
What is a class?
What is object?
What is friend class?
Can you explain storage classes?
What is Token?
What is Recursion?
What is function overriding?
What is Virtual function?
What is Volatile Variable?
What are Pointers?
What is Null Pointer?
What is enumeration?
What are Structures and Unions?
What is inheritance?
What is Encapsulation?
What is Abstraction?
What is Data binding?
What is Polymorphism?
What is the real purpose of class – to export data?
What things would you remember while making a interface?
Explain the benefits of proper inheritance.
Does improper inheritance have a potential to wreck a project?
How should runtime errors be handled in C++?
When should a function throw an exception?
Where are setjmp and longjmp used in C++?
Are there any special rules about inlining?
Explain one-definition rule (ODR)
What are the advantages of using friend classes?
What is the use of default constructor?
Differentiate between class and structure?
Explain container class?
What is namespace?
Explain explicit container?
Explain class invariant
Differentiate between late binding and early binding. What are the advantages of early binding?
Explain public, protected, private in C++?
What are the different types of STL containers?
Explain Stack unwinding?
How would you find out if a linked-list is a cycle or not?
Explain Copy Constructor
When do you call copy constructors?
Name the implicit member functions of a class
Explain storage qualifiers in C++
Explain dangling pointer
In what situations do you have to use initialization list rather than assignment in constructors.
When does a class need a virtual destructor?
What is the type of “this” pointer? When does it get created?
How would you differentiate between a pre and post increment operators while overloading?
What is a pdb file?
You run a shell on UNIX system. How would you tell which shell are you running?
What are Stacks? Give an example where they are useful
Differentiate between an external iterator and an internal iterator? What is the advantage of an external iterator?
In a function declaration, what does extern mean?
You want to link a C++ program to C functions. How would you do it?
Explain STL?
Related posts:
- C Programming Interview Questions and Answers
- C# Interview Questions and Answers
- Dart Programming Interview Questions and Answers
- Delphi Programming Interview Questions and Answers
- Kotlin Programming Interview Questions and Answers
- Objective-C Interview Questions and Answers
- Pascal Programming Questions and Answers
- Scala Programming Interview Questions and Answers