Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C++ Interview Questions and Answers Part 1

    • 0
    • 4
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.62k
    Comment on it

    In this tutorial, We are providing you C++ questions and answers. The content here will help all the Freshers as well as experienced people to get through with their interviews for software industry on campus or off campus. For any person who wants to get into an IT industry is expected to have the basic knowledge of these programming language ,i.e, C and C++ ,as most of the universities and colleges have C or C++ as a part of their curriculum. Whether a Fresher or an experienced, the interviewer hits atleast few questions based on C or C++ concepts to them.

    One common factor in the fresher interview is that there are few questions that has been repeatedly asked over years. Therefore, we have accumulated few questions and related answers based on C++ concepts and programming for freshers and experienced,both.Go through them,develop an understanding towards it.This will surely help you out during your placements and interviews.

    1. What is difference between C and C++ ?

    [Usually this is the first question that people face in their interview rounds. When you answer this question,don't give the text-book explanations.Rather supplement your answer with real software related examples. Answer for this interview question can include below points.]

    1. C++ is a kind of superset of C, most of C programs except few exceptions work in C++ as well.
    2. C is a procedural programming language, while C++ supports both procedural and Object oriented programming.
    3. Since C++ supports object oriented programming, it supports features like function overloading, templates, inheritance, virtual functions, friend functions. These features are absent in C.
    4. C++ supports exception handling at language level,but in C exception handling is done in traditional if-else style.
    5. C++ supports references but C doesnt.
    6. In C, scanf() and printf() are mainly used for input/output.While C++ mainly uses streams to perform input and output operations. 'cin' is standard input stream and 'cout' is standard output stream.
    1. What is a class?

    [Probably this would be the first question for a Java/c++ technical interview for freshers and sometimes for experienced as well. Try to give examples when you answer this question.]

    When we define any class, we are not defining any data, we just define a structure or a blueprint, as to what the object of that class type will contain and what operations can be performed on that object.

    Class definition starts with the keyword "class" followed by the class name. Then inside the curly braces comes the class body, that is data members and member functions, whose access is bounded by access specifier.Here is an example below:

        class office
        {    public:        int numberOfemployees;        double empbasicsalary;       
         void calculategross()
        {            // code to calculate the gross salary      }};
    
    1. What is an Object/Instance?

    A class variable is called object or instance. In other words, an object would be the variable. From the above example, we can create instance of class office as given below:

    Office officeObject;
    
    1. What do you mean by C++ access specifiers ?

    [Questions regarding access specifiers are common not just in c++ interview but for other object oriented language interviews as well.]

    Access specifiers are used to define how the members (functions and variables) can be accessed outside the class.The members of a class are classified into three categories: private, public, and protected. private, protected, and public are reserved words and are called member access specifiers.

    1. Private:
      Members declared as private are accessible only with in the same class and they cannot be accessed outside the class they are declared.
    2. Public:
      Members declared as public are accessible from any where.
    3. Protected:
      Members declared as protected are accessible from members of their same class and also from members of their derived classes.
    1. What are the basics concepts of OOP?

    [ The most basic and common question that is asked in the interview. You should be able to give real life examples for each of these concepts, so prepare yourself with few examples before appearing for the interview. ]

    Some of the important object oriented features are namely:

    • Objects
    • Classes
    • Inheritance
    • Data Abstraction
    • Data Encapsulation
    • Polymorphism
    1. Objects and Classes:
      Refer Questions 2 and 3 for the concepts about classes and objects.
    2. Inheritance:
      Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class. The new class that is formed is called derived class. Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming.The code below will help you understand further:
    3. class Shape
      {   protected:  float width, height;
      public:void set_data(float a, float b)
      { width=a;
        height=b;
      }
      };
      class rectangle:public shape
      {
      public:float area()
      {
      return(width*height);
      }
      } 
      
      In the above example, class Rectangle inherits the properties and methods of class Shape. Inheritance helps to modularise the code, improve reusability and reduces tight coupling between components of the system.
    4. Data abstraction:
      Data Abstraction increases the power of programming language by creating user defined data types. Data Abstraction also represents the needed information in the program without presenting the details. In C++ data abstraction is implemented using interfaces and abstract classes.
    5. Class shape{
      public:virtual void draw()const=0;
      };
      class Ellipse:shape{
      public:void draw()const=0 {API.drawEllipse();
      };
      
      In the above example, Shape provides no implementation of the draw functions.It abstracts the draw function from other shapes meaning that it defines functions that all type of shapes should necessarily have.
    6. Data Encapsulation:
      Data Encapsulation combines data and functions into a single unit called Class. When using Data Encapsulation, data is not accessed directly; it is only accessible through the functions present inside the class. Data Encapsulation enables the important concept of data hiding possible.
    7. class Add
      {
      private : int  x,y,r;
      public :int Addition(int x, int y)
      {
      r=x+y;
      return r;
      }
      void show()
      { cout<<"The sum is::" << r << "\n";}
      }s;
      void main()
      {
      Add s;
      s.addition(10,4);
      s.show();
      }
      
      In the above encapsulation example the integer values "x,y,r" of the class "Add" can be accessed only through the function "Addition". These integer values are encapsulated inside the class "Add".
    1. What is the use of volatile keyword in c++? Give an example.

    Volatile is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time without any action being taken by the code the compiler finds nearby.

    A variable should be declared volatile whenever its value could change unexpectedly. Most of the times compilers will do optimization to the code to speed up the program. For example in the below code-

    int a = 10;
    while( a == 10){     // Do something}
    

    compiler may think that value of 'a' is not getting changed from the program and replace it with 'while(true)', which will result in an infinite loop. In actual scenario the value of 'a' may be getting updated from outside of the program.
    In the above example if variable 'a' was declared using volatile, compiler will not optimize it. In short, value of the volatile variables will be read from the memory location directly.

    In the next session, we will continue with other important C++ questions asked by Interviewer.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: