45+ Best C & C++ Interview Questions & Answers [FREQUENTLY ASK]

45+ Best C & C++ Interview Questions & Answers [FREQUENTLY ASK]

Last updated on 03rd Jun 2020, Blog, Interview Questions

About author

Sharan (Sr Technical Project Manager, Capgemini )

She is Possessing 8+ Years Of Experience in C & C++. Her Passion lies in Developing Entrepreneurs & Activities. Also, Rendered her intelligence to the Enthusiastic JOB Seekers.

(5.0) | 16456 Ratings 1225

The C and C++ Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of C++. As good interviewers hardly plan to ask any particular question during the interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what the candidate answers. 

Here is a list of the most frequently asked C and C++ Interview Questions and Answers in technical interviews, suitable for both freshers and experienced professionals at any level.

1.  What Is The Difference Between C And C++ ?

Ans:

  • C is a procedural language on the other hand c++ is an object oriented language.
  • C follows top down approach, c++ follows bottom up approach.
  • C is a low level language, c++ is a middle level language.
  • Input and output functions differ in the two languages, c uses printf and scanf whereas c++ uses >> and << as input and output operators.
  • C++ can be broken down to solve real world problems which is not the case in c.

2. Describe Linkages And Types Of Linkages?

Ans:

When we declare identifiers within the same scope or in the different scopes they can be made to refer to the same object or function with the help of linkages. There are three types of linkages: 

  • External linkage 
  • Internal linkage 
  • None linkage
  1. EXternal Linkages means ‘global, non-static’ functions or variables.
  1. Example: extern int a1
  1. Internal Linkages means static variable and functions. Example: static int a2 None Linkages means local variales.
  1. Example : int a3

3.  What Are Structures And Unions?

Ans:

 While handling real world problems we come across situations when we want to use different data types as one, C allows the user to define it own data type known as structures and unions.Structures and unions gather together different atoms of information that comprise a given entity.

4. What Are Preprocessor Directives In C?

Ans:

  •   The Preprocessor processes the source program before it is passed to the compiler. The features that preprocessor offers are known as Preprocessor Directives.
  • Preprocessing directives are lines in your program that start with `#’. The `#’ is followed by an identifier that is the directive name. For example, `#define’ is the directive that defines a macro. Whitespace is also allowed before and after the `#’. A preprocessing directive cannot be more than one line in normal circumstances. Some directive names require arguments.View answers in details

5. How C Functions Prevents Rework And Therefore Saves The Programmers Time As Well As Length Of The Code ?

Ans:

  •   As we know that c allows us to make functions and call them wherever needed, it prevents rework by calling the same function again and again wherever requires instead for example if we make a function that adds two numbers, it can be called anywhere in the program where ever the addition is needed and we do not need to code again for adding any number. 
  • It also shortens the length of the program as we do not need to code again the same thing for next time we can simply call the function and use it whenever needed.

6. Keyword Mean In Declaration?

Ans:

  •   This keyword indicates that the function or the variable is implemented externally and it emphasizes that the variable ot the function exits external to the file or function.
  • We use this keyword when we want to make anything global in the project, it does not lie within any function.

7. Can Union Be Self Referenced?

Ans:

  No, Union cannot be self referenced because it shares a single memory for all of its data members.View answers in details

8. Which Format Specifier Is Used For Printing A Pointer Value?

Ans:

  • %p is used to display the corresponding argument that is a pointer.
  • %x can also be used to print values in hexadecimal form.

9. What Is The Use Of Register Keywords With Variables?

Ans:

  • Register keyword signifies that it is possible to store variables in the register than store it in the register. 
  • Variables are usually stored in stacks and are passed to and fro to the processor whenever required.
  • Also register keywords when used reduce code size which is an important thing in embedded systems.

10. What Do You Mean By Static Variables?

Ans:

Static is an access qualifier that limits the scope of the variable but causes the variable to exist for the lifetime of the program. This means a static variable is one that is not seen outside the function in which it is declared as its scopeis limited to the block of code in which it has been created but its lifespan remains until the program terminates. The value of such a variable will remain and may be seen even after calls to a function also the declaration statement of such type of a variable inside a function is executed only once.

11. What Is The Difference Between Global Variables And Static Variables?

Ans:

  • The scope of the variable describes that the variable is accessible at a certain point in the program or not.
  • The difference between global variables and static variables lies in this concept only. 
  • The scope of the global variables remains throughout the program also the lifespan of these variables is throughout the program.
  • The scope of the static Variables remains within the block of code in which they are created but the lifespan remains throughout the program.
  • Thus, the main difference is between the scope of both types of variables.

12.  What Is The Difference Between Global Variables And Local Variable?

Ans:

  • First, Global variables are the variables which can be accessed from anywhere throughout the program whereas local variables are those which can only be accessed within the block of code in which they are created.
  • Second, global variables are visible throughout the program whereas local variables are not known to the other functions in the programs i.e. they are visible within the block of code in which they are created.
  • Third, global variables are allocated memory on Data Segment whereas local variables are allocated memory on the stack.

13. What Do You Mean By Volatile Variable?

Ans:

 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.
It is a kind of instruction to the optimizer to not optimize the variable during compilation.

14. What Is The Prototype Of Printf Function?

Ans:

  Prototype of printf function is:

  • int printf( const char *format ,?)
  • In this the Second parameter: ‘?’ (Three continuous dots) are known as ellipsis which indicates the variable number of arguments.

15.Define Macro?

Ans:

  •  Macros are the identifiers that represent statements or expressions. In other words macros are fragments of code which have been given a name. #define directive is used to define a macro.
  • Example, we have define a macro i.e SQUARE(x) x*x. 
  • Here the macro determines the square of the given number. Macro Declaration: #define name text.

16. What Is The Disadvantage Of Using A Macro?

Ans:

The major disadvantage associated with the macro is :

When a macro is invoked no type checking is performed.Therefore it is important to declare a macro correctly so that it gives a correct answer whenever it is called inside the program.

17. What Is A Void Pointer?

Ans:

  •  When we declare a variable as a pointer to a variable of type void, it is known as void pointer. Another name for it is generic pointer.
  • In general we cannot have a void type variable,but if the variable is of void type it does not point to any data and due to this it cannot be dereferenced.

18. What Is AN Uninitialised Pointer?

Ans:

  • When we create a pointer the memory to the pointer is allocated but the contents or value that memory has to hold remains untouched. Uninitialised pointers are those pointers which do not hold any initial value.
  • Example: int *p; is said to be an uninitialised pointer, it is recommended to initialise the pointer before actually using it as it is an error.

19. List the types of inheritance supported in C++.

Ans:

  Single, Multilevel, Multiple, Hierarchical and Hybrid.

20.  What is C++?

Ans:

 C++ is a programming language that is a superset of the C Language. C++ supports Classes which is not present in C. Unlike C, C++ is an object-oriented programming language supporting OOPS concepts like Polymorphism, Inheritance, etc.,

21. Name the OOPS concepts available in C++?

Ans:

  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction
  • Data Binding

22. What is a Class?

Ans:

 A class is basically a user defined custom data type which is like a blueprint that showcases the characteristics and behavior. For example, let’s take a bird. The characteristics of a bird are feathers, beak etc., The behavior or Features of a bird are Flying, eating etc., There are different varieties of birds. So we can define a Class named bird and using this class’s characteristics and behavior we can declare different types of birds.

23. What is Polymorphism?

Ans:

Polymorphism is the concept of having one functionality do multiple things. The words “poly”(many) and “morph” (forms) itself explain the concept.

24. What are the types of Polymorphism?

Ans:

Polymorphism is classified into two types namely:

  • Compile-time Polymorphism or Overloading
  • Run-time Polymorphism or Overriding

    Subscribe For Free Demo

    25. What are access specifiers in C++?

    Ans:

    Access specifiers are the keywords that determine the availability of the data members/member functions of a class outside.  The three access specifiers available in C++ are:

    • Public: Data members and Member functions declared under public specification can be accessed from outside the class.
    • Private: Data members and Member functions declared under private specification can be accessed only within the class and cannot be accessed from outside the class even by derived class objects.
    • Protected: Data members and Member functions declared under protected specification can be accessed by its own class and the derived classes.

    26.  What is a constructor?

    Ans:

    A constructor is a special member function of a class that initializes the class object. The compiler provides a default constructor if the user has not provided a constructor. The constructor function name should be declared as the same name of Class. For example:

    • Class Bird
    • {
    •              char name [50];
    •    public:
    •              Bird ()
    • {
    •     printf(“\nThis is Default Constructor of Class Bird\n”)
    • }
    • };

    There are basically three types of constructor available namely:

    • Default Constructor
    • Parameterized Constructor
    • Copy Constructor

    27.  What is a destructor?

    Ans:

      A destructor is a special member function of a class that destroys the resources that have been allocated to the class object. The destructor class name is the same as that of the class but prefixed with a ~ symbol.

    For example:

    • Class Bird
    • {            char name [50];
    •    public:
    •              ~Bird ()
    • {
    •     printf(“\nThis is Destructor of Class Bird\n”)
    • }
    • };

    28. Explain Virtual Destructor

    Ans:

    A virtual destructor does the same function as that of a normal destructor but along with the destruction of derived class objects too. The virtual keyword must be employed before the function name as shown:

    virtual ~Bird()

    29. What is a copy constructor?

    Ans:

    A copy constructor is one of the types of constructor that is used for initializing a class object with the help of another object. It takes the same name as that of the class with one const class reference object as argument For example:

    • Class Bird
    • {
    • char name [50];
    • public:
    • Bird (const class &B)
    • {
    • name = B.name;
    • }
    • };

    30. Can a constructor be overloaded?

    Ans:

     Yes, but only through a number of input arguments. Example – Default and parameterized constructors.

    31. Can a destructor be overloaded?

    Ans:

      No. A destructor must simply destroy all the resources allocated for the object.

    32: Explain Function Overriding

    Ans:

    Function Overriding is a run time polymorphism which allows derived class to provide its own implementation of the base class member functions. The newly implemented function in the derived class is called Overridden Function.

    Example:

    • class Base
    • {
    • public:
    • void display ()
    • { cout<< “Base Class Display” <<endl; }
    • }; 
    • class Derived: public Base
    • {
    • public:
    • void display ()
    • { cout<< “Derived Class Display” <<endl; }
    • };

    33. What is pure virtual function?

    Ans:

    • Pure virtual function is a type of virtual function which has only declaration and not definition. A pure virtual function declaration is assigned with zero. All derived classes must implement the pure virtual function.
    • virtual Display() = 0;

    34.  What is an Abstract Class?

    Ans:

      A class is said to be abstract when it contains at least one pure virtual function. Instantiation is not allowed for an abstract class. And the deriving class must implement or provide definition for the pure virtual functions.

    Example:

    • class Base { public: virtual void display () = 0; };

    35. What is the use of pure virtual function?

    Ans:

     A pure virtual function does not have implementation in base class. This will be useful during scenarios where no actual implementation is required in the base class context. For example let’s say we have a base class named Shape which is having a function “DrawShape()” as pure virtual. In base class there is actually no need to give a definition. Now, when a Class Square derives from Class Shape, it provides an actual definition for DrawShape() as drawing a square.

    36. Can Constructors be virtual?

    Ans:

    In order to create an object, we need to know the complete information about it or the exact type of what we want to create. Hence, a constructor cannot be virtual.

    37.  Can Destructors be declared as pure virtual functions?

    Ans:

      Yes, but we must define the destructor anyways.

    38. What is virtual class?

    Ans:

     A virtual class resolves the ambiguity caused due to multilevel inheritance. Suppose two Classes B and C derive from a Base Class A. And another derived Class D derives from Classes B and C. Now when we create an object for Class D, it will have multiple copies of Base Class B. But if the Base Class A is derived as virtual by Classes B and C, this will be prevented.

    Example:

    • Class A
    • { … };
    • Class B: virtual public A
    • { … };
    • Class C: virtual public A
    • { … };
    • Class D: public B, public C
    • { … };

    39. What are the different types of inheritance?

    Ans:

    • Multiple: (Class A, B) -> Class C
    • Multi-Level: Class A -> Class B -> Class C
    • Hierarchical: Class A -> (Class B, C) -> Class D

    40. What are new and delete operators?

    Ans:

    The new operator is used to dynamically allocate memory and delete operator is used to destroy the memory allocated by the new operator.

    41.  What are Friend Functions?

    Ans:

    Friend functions are special member functions of a class which can access the data members and member functions from outside the class without using the Class object.

    • #include <iostream>
    • using namespace std;
    • class Length
    • {
    • private:
    •      int value;
    • public:
    •      Length () {  value = 0; }
    •      friend int addExtra(Distance); //friend function
    • };
    • int addFive(Length l) // friend function definition
    • {   
    •  //accessing private data from non-member function
    • l.value += 5;
    • return l.value;
    • }
    • int main()
    • {
    • Length L;
    • cout<<“Length: “<< addExtra(L);

    42.  What is a Friend class?

    Ans:

    Similar to friend function one class can be made as a friend to another class. Let’s say X and Y are separate classes. When X is made friend to class Y, the member functions of Class X become friend functions to Class Y. Member functions of Class X can access the private and protected data of Class Y but Class Y cannot access the same of ClassX.

    • class X;
    • class Y
    • {
    •    // class X is a friend class of class Y
    •    friend class X;
    •    … .. …
    • }
    • class X
    • {
    •    … .. …
    • }

    43. What are actual and formal parameters?

    Ans:

     Actual parameters are variables or arguments that are being passed to the function during a function call. Formal parameters are the variables or arguments that are being received in the functions.

    Course Curriculum

    Best In-Depth Practical Oriented C and C++ Programming Training

    • Instructor-led Sessions
    • Real-life Case Studies
    • Assignments
    Explore Curriculum

    44. Explain command line arguments?

    Ans:

     Main() is the entry point of a C++ program and it behaves like a function. It takes arguments like any other functions except that it cannot be invoked from within the program. Here, command line arguments come into picture. The arguments passed through the command line are received by the main function as input arguments.

    Example:

    • int main( int ARGC, char *ARGV[]) {
    • }
    • Here,ARGC – Count of  number of arguments passed through the command line.
    • ARGV – Is the vector or array containing the values of the arguments
    • ARGC[0] – is the program name.
    • Suppose
    • $ ./a.out 25 35
    • ARGC = 3
    • ARGV [0] = a.out
    • ARGV [1] = 25

    45. What is stack overflow and when does it occur?

    Ans:

    A stack overflow denotes that the stack memory is filled and memory can be stored further in the Stack. A stack overflow occurs when a recursive function keeps on executing without any exit condition or break point. For each and every recursive call, a new function call address will be written into the stack memory thus leading to stack overflow when there is no proper exit condition.

    46. What is shallow copy and deep copy?

    Ans:

    Shallow copy performs bit-by-bit memory dump from one object to another whereas Deep copy performs field by field memory dump. Copy Constructor and overloaded assignment operator are examples of deep copy.

    47. How does a normal object and a pointer object access members of a class?

    Ans:

    •  Normal object access class members using . dot operator.
    • Pointer objects access class members using -> arrow operator.

    48. What are the different types of initialization available in C++?

    Ans:

    • Traditional C Way: int num = 10;
    • Constructor notation: int num(10);

    49. What are exit and abort functions?

    Ans:

    • An exit call invokes the destructor of all constructed objects and then does a graceful termination from the program.
    • An abort call abruptly ends the program and dumps core memory. It does not call any destructor.

    50. Compare C Struct and C++ Struct?

    Ans:

    C Struct C++ Struct

    style=”width: 242px;”>Cannot have

    functions.

    style=”width: 247.6px;”>Can have

    member functions.

    Cannot have static members. Can have static members.

    Struct keyword is necessary while

    declaring  structure type variables.

    Do not require any keyword for

    declaring structure type variables.

    Size of the empty structure is

    undefined.

    Size of the empty structure is 1

    51. What is an interface?

    Ans:

       An interface is class which contains only pure virtual functions. It just provides a group of functions without any definition that must be overridden by the implemented classes. If all the functions in an abstract class are pure virtual then it becomes an interface.

    • Bird (const class &B)
    • {
    • name = B.name;
    • }
    • };

    52. What does delete[] do?

    Ans:

      The delete[] operator deletes the array of dynamically allocated memory allocated by the new[] operator.  

    53. library function which is used to convert string value into value?

    Ans:

       A toi()the function is used in header file is stdio.h

    54. Can we call any clam member function without using the object of the class?

    Ans:

      Yes, with static keyword

    55. loss of scope resolution operator?

    Ans:

      To define member function outside the class

    56. List the operators which can’t be overloaded?

    Ans:

     a.:: b.size of c.?: 4..

    57.  What is polymorphism?

    Ans:

      We can use the same name function with different purposes.

    58. Which operator can change the program statement?

    Ans:

      Conditional operator

    59. What is the role of compression class in C ++?

    Ans:

    Abstract class minimum is a pure virtual function.

    60. What is Namespace?

    Ans:

      Namespace is a set of different classes

    61. What is the delay?

    Ans:

    •  A virtual method (polymorphism)
    • A run time is determined by the late binding

    62. What to do to get out?

    Ans:

       The output () function is used to exit the program and return to the operating system.

    63. Digid Class which can be viewed by the basic class data members?

    Ans:

    In the public and protected section

    64. What kind of function is not an independent function of a class, but what are the earlier methods of class?

    Ans:

    Friend function

    65. Who is the instrument for constructing the process of using another object of the same class?

    Ans:

     Copy the constructor

    66. Can several constructor be in a class?

    Ans:

       Two

    67. What does the following statement mean? Int (* ptr) [10];

    Ans:

      Ptr represents 10 full numbers

    68.  What is the reference to the variable in C ++?

    Ans:

      An alternate variable is already an alternative name for the variable. The variable name and reference for the same location indicate the two points. Each process of the original variable can be achieved using the reference variable

    69. Do you want to throw a strong organ function?

    Ans:

      A standard element function can be implemented by the name of the class before it reaches the end of class objects. It can only update the standard members of the class.

    70. What are the operators / operators used to implement class members?

    Ans:

     Point (.) & Arrow (->)

    71. What are the data types to store the Boolean value?

    Ans:

     bool, some oldest data type introduced in the C ++ programming language.

    Course Curriculum

    Learn Industry Experts Curated C and C++ Certification Course

    Weekday / Weekend BatchesSee Batch Details

    72.  What is Function Overloading?

    Ans:

     Parameters Define multiple functions of the same name with the individual list is called function overloading.

    73.  Do we have the String primitive data type in C++?

    Ans:

      Null, it’s the class from STL (Standard template library).

    74.  Which process specifier/s can help to achieve data hiding in C++?

    Ans:

    Private and Protected.

    75. What is the difference between Exit and Breakdown?

    Ans:

    A smooth process of exit ends, which calls for destruction of all the constructed objects, and they are not called.

    76. Can you call the Testriter openly?

    Ans:

      Yes, but you only want to do this when you use new jobs.

    77. Should virtual inheritance be used in a phase?

    Ans:

      If there is a diamond class sequence, we have to use the virtual property below the diamond.

    78.  Explain the purpose of the resolution operator?

    Ans:

      Allows a program to mark the identifier in a global view, which is hidden by another identifier of the same name in the local area.

    79. Divergence and Reconciliation How do the exceptions differ using the Setjmp and Logjap?

    Ans:

      Throwing action automatically generates products automatically from the entry point for the test.

    80.  What is a transformative builder?

    Ans:

      An analyst who accepts a different type of argument.

    81. What is the difference between a copyrunner and an overloaded assignment?

    Ans:

     The argument creates a new object using the content of the object. An overloaded assignment operator delivers the contents of the existing material to another object of the same class.

    82. What is a Virtual Destroyer?

    Ans:

      The simple answer is one that is virtualized with a virtual destroyer virtual character.

    83. When is a site a better solution than a base class?

    Ans:

    When designing a common class to control other types of goods or to design a different class, the design and behavior of those other forms are important for their control or management, especially when the other types are unknown (thus, common) container or manager class designer.

    84. What is a stable member?

    Ans:

     The function of the class or the function of the function of the class can be edited by the class itself.

    85.  What is an opener architect?

    Ans:

     A replacement keeper declared open words. Compiler does not use an external constructor to implement changes of a particular kind. This is explicitly reserved for the purpose.

    86. What is the standard template library?

    Ans:

    •   A library of container templates approved by the ANSI team to include a standard C ++ specification.
    • The general program model, operating systems, quotations, algorithms, and such, STL is higher than the average of new technology for C ++ programming.

    87. Explain the Run Time Type Identity?

    Ans:

    The ability to determine the type of object of an object by using a directory operator or dynamic_ socket operator.

    88.  What problem will solve the namespace feature?

    Ans:

    Libraries use multiple global identifiers to generate multiple providers.

    89. Can I use this mouse on the constructor?

    Ans:

    Yes, but avoid the invitation of virtual function from the constructor and try to pass this pointer from the boot list to other classes.

    89.  What is a default configuration?

    Ans:

    • A constructor who does not take any argument
    • There is a constructor that contains the argument (with the default value)

    90. What is the Difference Between Class: Vector <int> X; And Class :: vector <int> X ()?

    Ans:

     The first type of variable x type STD :: vector declares <int>. The latter gives a function x std :: vector <int>.

    91.  How does the main structure differ from the main class in C ++?

    Ans:

      In C ++, a class resembles an organization with exception, naturally, all members of a class are individuals; An element members are civilians. Structures are not supported, but classes support.

    92.  Whether to define pure virtual activity?

    Ans:

      A virtual class of pure virtual function is defined as a virtual function. It is implemented in the derived class. A program with a pure virtual function can not declare a program.

    93. Can a Definition Be Defined?

    Ans:

      An alternative tool is an argument tool. It is used by the packaging to keep objects as a type of argument for a type of type.

    94. What Is The Variance Between Declaration And Definition ?

    Ans:

      There are fundamentally two contrasts among announcement and definition:

    • In revelation no space is saved for the variable, the announcement just tells about the ‘type’ of the variable we are utilizing or we will utilize in the program.
    • Definition then again saves the space for the variable and some underlying quality is given to it.
    • Another real distinction is that redeclaration isn’t a blunder though redefinition is a mistake,
    • In straightforward words, when we pronounce no space is saved for the variable and we can redeclare it in the program
    • On the other hand, when we characterize a variable some space is held for it to hold esteems in addition to some underlying quality is likewise given to it, aside from it we can’t give another definition to the variable, for example we can’t characterize it once more.

    Model:

    extern int x – > is a presentation though int y is definition.

    95. If you want to share some functions or variables in some files maintaining the consistency how would you part it?

    Ans:

    To keep up the consistency between a few records right off the bat place every definition in ‘.c’ document than utilizing outside statements place it in ‘.h’ document after it is incorporated .h record we can utilize it in a few documents utilizing #include as it will be in one of the header documents, along these lines to keep up the consistency we can make our own header document and incorporate it any place required.

    96. What Do You Mean By Translation Unit?

    Ans:

    •   A Translation Unit is a lot of source records that is seen by the compiler and it decipher it as one unit which is generally.file and all the header documents referenced in #include mandates.
    • At the point when a C preprocessor extends the source record with all the header documents the outcome is the preprocessing interpretation unit which when further handled deciphers the preprocessing interpretation unit into interpretation unit, further with the assistance of this interpretation unit compiler shapes the item record and at last structures an executable program.

    97.  What Is A Null Object In C++?

    Ans:

      It is an object of some class whose reason for existing is to show that a genuine object of that class does not exist. One basic use for an invalid article is an arrival esteem from a part work that should restore an item with some predetermined properties yet can’t discover such an article.

    98. What will be the datatype to store the Boolean value?

    Ans:

    Bool, It is the primitive data type

    Explore C and C++ Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

    99. Name some standard default streams in C++?

    Ans:

    clog, cerr and cout

    100.  Define unary Expressions?

    Ans:

      An expression with only one operand and one operator is called unary expression.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free