[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

Technical Interview Questions and Answers :: C++

    11 / 187

    What is the difference between class and structure?
    Answer:

    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.

    Please Login First :

    12 / 187

    What is public, protected, and private?
    Answer:

    Ø Public, protected and private are three access specifier 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.

    Please Login First :

    13 / 187

    What is constructor? What is the return type of it?
    Answer:

    A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associatied class is created.
    It is called constructor because it construct the values of data members of the class.

    Please Login First :

    14 / 187

    What are the types of constructor?
    Answer:

    a. default constructor(or)empty constructor
    b. default argument constructor
    c. constructor polymorphism(or)multiple constructor(or)constructor overloading
    d. copy constructor
    e. parameterized constructor.

    Please Login First :

    15 / 187

    What is constructor overloading?
    Answer:

    When there is more than one constructor in a class with different parameters and with different data types we say that the constructor is overloaded.

    Please Login First :

    16 / 187

    What is overloading constructor?
    Or
    What is multiple constructor?
    Answer:

    The sharing the same name by two or more functions is referred to as function overloading. Similarly when more than one constructor function is defined in a class. We say that the constructor is overloaded. It is also called multiple constructors.

    Example :
    Complex() { } //constructor with no arg.
    Complex (float a) { x=y=a;} //constructor with one arg.
    Complex (float real, float imag) //constructor with two arg.

    Please Login First :

    17 / 187

    How to use delete operator in destructor?
    Answer:

    Whenever new is used to allocate memory in the constructors. We should use delete to free that memory. For example, the destructor for the matrix class discussed above may; be defined as follows

    Matrix :: ~matrix( )
    {
    for(int i=0;i delete p[i];
    delete p;
    }

    Please Login First :

    18 / 187

    Difference between constructor and destructor?
    Answer:

    Constructor:

    1. The constructor is a ‘special’ member function whose task is to initialize the objects of its class.
    2. It constructs the values of data members of the class

    Destructor:
    1. The destructor is a member function whose name is the same as the class name but is preceded by a tilde.
    2. It destroys the objects that have been created by a constructor.

    Please Login First :

    19 / 187

    What is dynamic constructor?
    Answer:

    The constructor can also be used to allocate memory while creating objects.
    This will enable the system to allocate the right amount of memory for each object when the objects are not of the same size, thus resulting in the saving of memory;. Allocation of memory to object at the time of their construction is known as a dynamic construction of objects. The memory is allocated with the help of the new operator. The use of new in constructors that are used to construct strings in objects.

    Please Login First :

    20 / 187

    What is default argument constructor?
    Answer:

    It is possible to define constructors with default arguments. for example, the constructor complex( ) can be declared as follows:

    Complex(float real, float imag=0)
    The default value of the argument imag is zero.then the statement
    Complex c(5.0);
    Assigns the value 5.0 to the real variable and 0.0 to imag (by default). However, the statement
    Complex c(2.0,3.0);
    Assigns 2.0 to real and 3.0 to imag. The actual parameter, when specified, overrides the default value. As pointed out earlier, the missing arguments must be the trailing ones.

    Please Login First :