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

    1 / 187

    What are the two ways to pass values as arguments to the constructor?
    Answer:

    Two ways to pass values as arguments to the constructor function when an object is declared.

    1. By calling the constructor explicitly.
    2. By calling the constructor implicitly.

    Eg :
    Explicit call:
    Integer int1=integer(0,200);

    Implicit call:
    Integer int1(0,100);

    Please Login First :

    2 / 187

    What is parameterized constructors, Give example?
    Answer:

    It may be necessary to initialize the various data elements of different objects with different values when they are created.
    C++ permits us to achieve this objective by passing an argument to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.

    Eg:
    Class A
    {
    int m,n;
    public:
    A(int x,int y); //parameterized constructor
    ………..
    ………..
    };
    A : : A(int x,int y)
    {
    m=x;
    n=y;
    }

    Please Login First :

    3 / 187

    What is default constructor. Give example?
    Answer:

    A constructor that accepts no parameters is called the default constructor. The default constructor for class A is A::A ( ). If no such constructor is defined, then the compiler supplies a default constructor. Therefore a statement such as A a;
    Invokes the default constructor of the compiler to create the object a.

    Please Login First :

    4 / 187

    What are characteristics of the constructor function?
    Answer:

    The constructor functions have some characteristics:

    • They should be declared in the public section.
    • They got invoked automatically when the objects are created.
    • They do not have return types, not even void and therefore, they cannot return values.
    • They cannot be inherited, through a derived class can call the base class constructor.
    • Like other c++ functions, they can have default arguments.
    • Constructors cannot be virtual.
    • We cannot refer to their addresses.
    • An object with a constructor cannot be used as a member of a union.
    • They make implicit calls to the operators new and delete when memory allocation is required.

    When a constructor is declared for a class, initialization of the class objects becomes mandatory.

    Please Login First :

    5 / 187

    What are properties of the constructor function?
    Answer:

    • They should be declared in the public section.
    • They got invoked automatically when the objects are created.
    • They do not have return types, not even void and therefore, they cannot return values.
    • They cannot be inherited, through a derived class can call the base class constructor.
    • Like other c++ functions, they can have default arguments.
    • Constructors cannot be virtual.
    • We cannot refer to their addresses.
    • An object with a constructor cannot be used as a member of a union.
    • They make implicit calls to the operators new and delete when memory allocation is required.
    • When a constructor is declared for a class, initialization of the class objects becomes mandatory.

    Please Login First :

    6 / 187

    How to declare and define constructor?
    Answer:

    A constructor is declared and defined as

    Class integer
    {
    int m,n;
    public:
    integer(void); //constructor declared
    ……….
    ………..
    };
    integer : : integer(void)//constructor defined
    {
    m=0;
    n=0;
    }

    Please Login First :

    7 / 187

    Explain the concept of reusability in c++?
    Answer:

    C++ strongly supports the concept of reusability. The c++ classes can be reused in several ways .Once a class has been written and tested, it can be adapted by other programmers to suit their requirement .this done by creating new class reusing the properties of the existing ones is called reusability.

    Please Login First :

    8 / 187

    What is Derived class?
    Answer:

    A derived class can be defined by specifying its relationship with the base class in addition to its own details. the general form of defining a derived class is

    Class derived_class_name
    {
    --------------
    --------------//members of derived class
    --------------
    };

    Please Login First :

    9 / 187

    What is meant by visibility mode explain briefly?
    Answer:

    Visibility mode is optional and may be either private or public. The default visibility mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.

    Ex:
    Class abc: private Xyz // private derivation
    {

    members of abc;
    };


    Ex:
    Class abc: private Xyz // public derivation
    {

    members of abc;
    };

    Please Login First :

    10 / 187

    Why do we need virtual function?
    Answer:

    When a function is made virtual, c++ determines which function to use at run-time based on the type of the object pointed to by the base pointer, rather than the type of the pointer. By making the base pointer to point to different objects, we execute different versions of the virtual function.

    Please Login First :