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