Wednesday, October 31, 2012

Question 17: Predict the output.


Question 17: Predict the output. (Considering int for 4 bytes)

class BaseClass
{
public:
    int m_BaseData;
};

class DeriveClass : public BaseClass
{
public:
    int m_DeriveData;
    void printValues()
    {
        std::cout<<"\nBase Data : "<<m_BaseData;
        std::cout<<"\tDerive Data : "<<m_DeriveData;
    }
};

void BaseClassLoop (BaseClass *List, int Count)
{
    int i;

    for (i = 0; i < Count; ++i)
        List[i].m_BaseData = i;

}

int main()
{
    DeriveClass objList[2];
    BaseClassLoop(objList,2);
    for(int i =0;i< 2;i++)
    {
        objList[i].printValues();
    }
}


Options:
(a) Base Data: 0                                Derive Data: Garbage Value
      Base Data: 1                                Derive Data: Garbage Value
(b) Base Data: Garbage Value        Derive Data: Garbage Value
      Base Data: Garbage Value        Derive Data: Garbage Value
(c) Base Data: 0                                 Derive Data: 1
      Base Data: Garbage Value        Derive Data: Garbage Value
(d) None of the above.

Show Solution

Tuesday, October 30, 2012

Question 16: Predict the output.


Question 16: Predict the output. (Considering int for 4 bytes)

    void display(int data[])
    {
        std::cout<<"\nSize of array when passed as argument is : "<<sizeof(data);
    }

    int main()
    {
        int data[] = { 1, 2, 3, 4, 5, 6};

        std::cout<<"\nSize of array is : "<<sizeof(data);
        printSizeOfArray(data);
        
    }

    

    Options:
    (a) Size of array is : 24
         Size of array when passed as argument is : 4

    (b) Size of array is : 24
         Size of array when passed as argument is : 24

    (c) Compile Time error.

    (d) None of the above.



Show Solution

Tuesday, October 23, 2012

Question 15: Predict the output.

    class DemoClass
    {
    public:
        void display()
        {
            std::cout<<"\nDisplay Function 1";
        }
        void display() const
        {
            std::cout<<"\nDisplay Function 2";
        }
    };

    int main()
    {
        DemoClass obj;
        obj.display();
    }


    Options:
    (a) Display Function 1
    (b) Runtime Error
    (c) Compile Time error: error C2535: 'void DemoClass::display(void)' : member function already defined or declared.
    (d) None of the above.


         Answer: a
Question 14: Predict the output.
    class
    {
    public:
        void display()
        {
            std::cout<<"Display function without any class name";
        }
    }classWithoutAnyName;

    int main()
    {
        classWithoutAnyName.display();
    }



    Options:
    (a) Display function without any class name.
    (b) Runtime Error
    (c) Compile Time error: Class doesn’t have any name.
    (d) None of the above.


    Answer: a
Question 13: Predict the output.
    class Base
    {
    public:
        void display()
        {
            std::cout<<"\nBase class display function.";
        }
    };

    class Derive : public Base
    {
    public:
        void display()
        {
            std::cout<<"\nDerive class display function.";
        }
    };

    int main()
    {
        Base* obj = new Derive();
        obj->display();
    }


    Options:
    (a) Base class display function.
    (b) Derive class display function.
    (c) Compile Time error: error C2248: 'Base:: display’ : cannot access private member declared in class 'Base'
    (d) Runtime Error


    Answer: a
Question 12: Predict the output.
    int main()
    {
        char str1[] = "India";
        char str2[] = "India";
        if(str1 == str2)
        {
            std::cout<<"Both the strings are same";
        }
        else
        {
            std::cout<<"Both the strings are not same";
        }
    }



    Options:
    (a) Both the strings are same
    (b) Both the strings are not same
    (c) Compile Time error
    (d) Runtime Error


    Answer: b
Question 11:  Predict the output
    int main()
    {
        int x = 0;
        while(x++<5)
        {
            static int x;
            x += 2;
            std::cout<<x<<" ";
        }
    }


    Options:

    a)     1 2 3 4 5
    b)    2 4 6 8 10
    c)     Compile Time error
    d)    Runtime Error


    Answer: b
Question 10: Predict the output.
    class DemoClass
    {
    public:
        DemoClass()
        {
            std::cout<<"\nDefault Constructor.";
        }
        DemoClass(const DemoClass& copy)
        {
            std::cout<<"\nCopy Constructor.";
        }
        DemoClass& operator = (const DemoClass& obj)
        {
            std::cout<<"\nAssignment Operator.";
            return *this;
        }
    };
    int main()
    {
        DemoClass obj, obj1;
        DemoClass newObj = obj;
        obj1 = obj;
    }


    Options:

    a)     Default Constructor.
         Copy Constructor.
         Default Constructor.
         Assignment Operator.

    b)     Default Constructor.
    Default Constructor.
    Copy Constructor.
    Assignment Operator.

    c)    Copy Constructor.
         Default Constructor.
         Default Constructor.
         Assignment Operator.

    d)     None of the above.


    Answer: b
Question 9: Predict the output.
          class DemoClass
    {
    public:
        DemoClass(int value)
        {
            m_data = value;
        }
        void display()
        {
            std::cout<<"Value : "<<m_data;
        }
    private:
        int m_data;
    };

    int main()
    {
        DemoClass obj = 10;
        obj.display();
    }

    Options:
    a)     Compilation Error    error C2440: 'initializing' : cannot convert from 'int' to 'DemoClass'

    b)    Run time crash.

    c)     Value : 10

    d)    None of the above.


    Answer: c
Question 8: Predict the output.
    class DemoClass
    {
    public:
        DemoClass()
        {
            m_data = 20;
        }
        void print()
        {
            std::cout<<"\nData : "<<m_data;
        }
    private:
        int m_data;
    };

    int main()
    {
        DemoClass obj;
        obj.print();
        int* data = (int*)&obj;
        *data = 30;
        obj.print();
    }



    Options:

    a)     Data : 20
         Data : 30

    b)    Compilation Error

    c)     Run time crash.

    d)    None of the above.



    Answer: a
Question 7: Predict the output.
    class DemoClass
    {
    public:
        DemoClass()
        {
            m_data = 20;
        }
        operator int()
        {
            return m_data;
        }
    private:
        int m_data;
    };

    int main()
    {
        DemoClass obj;
        int value = obj;
        std::cout<<"Value : "<<value;
    }



    Options:
    a)     Value : 20

    b)    Compilation Error. error C2440: 'initializing' : cannot convert from 'DemoClass' to 'int'
    c)     Run time crash.

    d)    None of the above.



    Answer: a
Question 6: Predict the output.
    int& getIncrementedData(int data)
    {
        int incrementedData = data + 1;
        return incrementedData;
    }

    int main()
    {
        int& incrementedValue = getIncrementedData(10);
        std::cout<<"\nIncremented Value : "<<incrementedValue;
        incrementedValue = 12;
        std::cout<<"\nIncremented Value : "<<incrementedValue;
    }


    Options:
    a)     Compilation Error. Returning a local variable as reference.

    b)    Run time crash.

    c)     Incremented Value : 11
    Incremented Value : 12

    d)    None of the above.


    Answer: c
Question 5: Predict the output.
    class BaseClass
    {
    public:
        virtual void display() = 0;
    };

    void BaseClass::display()
    {
        std::cout<<"Defination of Pure Virtual Function\n";
    }

    class DeriveClass : public BaseClass
    {
    public:
        virtual void display()
        {
            std::cout<<"Defination of Derive Class Function\n";
            BaseClass::display();
        }
    };

    int main()
    {
        BaseClass* obj = new DeriveClass();
        obj->display();
    }



    Options:

    a)     Compilation Error. As pure virtual function has a body which is not allowed.

    b)    Run time crash.

    c)     Defination of Derive Class Function.
         Defination of Pure Virtual Function.

    d)    None of the above.



    Answer: c
Question 4: Predict the output.
    class DemoClass
    {
    public:
        void display()
        {
            std::cout<<"Display Function.";
        }
    private:
    };

    int main()
    {
        DemoClass* obj = 0;
        obj->display();
    }




    Options:
    a)     Compilation Error.

    b)    Run time crash.

    c)     Display Function.

    d)    None of the above.



    Answer: c