C++ Quiz Questions Set 1

1) Predict the output or error(s) for the following:

#include
using namespace std;
class Sample
{
public:
        int *ptr;
        Sample(int i)
        {
              ptr = new int(i);
        }
        ~Sample()
        {
              delete ptr;
        }
        void PrintVal()
        {
              cout « "The value is " « *ptr;
        }
};
void SomeFunc(Sample x)
{
     cout « "Say i am in someFunc " « endl;
}
int main()
{
     Sample s1= 10;
     SomeFunc(s1);
     s1.PrintVal();
}
Answer:
 prompt> g++ Sample.cpp
 prompt> ./a.out
       Say i am in someFunc 
       Null pointer assignment(Run-time error) 

Explanation: As the object is passed by value to SomeFunc the destructor of the object is called when the control returns from the function. So when PrintVal is called it meets up with ptr that has been freed.

The solution is to pass the Sample object by reference to SomeFunc:

void SomeFunc(Sample &x) 
      cout « "Say i am in someFunc " « endl; 
}

prompt> g++ Sample.cpp
prompt> ./a.out

      Say i am in someFunc
      The value is 10


because when we pass objects by reference that object is not destroyed. while returning from the function.

2) Which is the parameter that is added to every non-static member function when it is called?
 'this' pointer

3) What is the Out Put?
class base
{
        public:
               int bval;
               base()
               { 
                  bval=0;
               }
};
class deri:public base
{
        public:
               int dval;
               deri()
               { 
                  dval=1;
               }
};
void SomeFunc(base *arr,int size)
{
    for(int i=0; i‹size; i++,arr++)
         cout«arr-›bval;
    cout«endl;
}
int main()
{
    base BaseArr[5];
    SomeFunc(BaseArr,5);
    deri DeriArr[5];
    SomeFunc(DeriArr,5);
}
Answer:      
     00000
     01010

Explanation:
The function SomeFunc expects two arguments.The first one is a pointer to an array of base class objects and the second one is the sizeof the array.The first call of someFunc calls it with an array of bae objects, so it works correctly and prints the bval of all the objects. When Somefunc is called the second time the argument passed is the pointeer to an array of derived class objects and not the array of base class objects. But that is what the function expects to be sent. So the derived class pointer is promoted to base class pointer and the address is sent to the function. SomeFunc() knows nothing about this and just treats the pointer as an array of base class objects. So when arr++ is met, the size of base class object is taken into consideration and is incremented by sizeof(int) bytes for bval (the deri class objects have bval and dval as members and so is of size ›= sizeof(int)+sizeof(int) ).


4) What is the Out Put?
class base
{
        public:
              void baseFun()
              { 
                   cout«"from base"«endl;
              }
};
class deri:public base
{
        public:
              void baseFun()
              { 
                   cout« "from derived"«endl;
              }
};
void SomeFunc(base *baseObj)
{
    baseObj->baseFun();
}
int main()
{
    base baseObject;
    SomeFunc(&baseObject);
    deri deriObject;
    SomeFunc(&deriObject);
}
Answer:
       from base
       from base


Explanation:
As we have seen in the previous case, SomeFunc expects a pointer to a base class. Since a pointer to a derived class object is passed, it treats the argument only as a base class pointer and the corresponding base function is called.

5) What is the Out Put?
class base
        {
        public:
         virtual void baseFun(){ cout«"from base"«endl;}
        };
 class deri:public base
        {
        public:
         void baseFun(){ cout« "from derived"«endl;}
        };
void SomeFunc(base *baseObj)
{
        baseObj->baseFun();
}
int main()
{
base baseObject;
SomeFunc(&baseObject);
deri deriObject;
SomeFunc(&deriObject);
}
Answer:        
            from base        
            from derived
Explanation:
Remember that baseFunc is a virtual function. That means that it supports run-time polymorphism. So the function corresponding to the derived class object is called.

No comments:

Post a Comment