Sample Questions Best site for GRE, LSAT, SAT, GMAT, TOEFL, CCNA, CCSA and interview sample questions  

Technical Sample Questions : C |  C++ |  Oracle |  Java | Unix |  Operating Systems |  Data Structure

Sample Technical Questions

C++ and OOPS Sample Question

  1. 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.
  2. 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.
  3. void main()
    {
    	int a, *pa, &ra;
    	pa = &a;
    	ra = a;
    	cout «"a="«a «"*pa="«*pa «"ra"«ra ;
    }

    Answer:

    Compiler Error: 'ra',reference must be initialized

    Explanation:

    Pointers are different from references. One of the main differences is that the pointers can be both initialized and assigned, whereas references can only be initialized. So this code issues an error.
  4. const int size = 5;
    void print(int *ptr)
    {
    	cout«ptr[0];
    }
    
    void print(int ptr[size])
    {
    	cout«ptr[0];
    }
    
    void main()
    {
    	int a[size] = {1,2,3,4,5};
    	int *b = new int(size);
    	print(a);
    	print(b);
    }

    Answer:

    Compiler Error : function 'void print(int *)' already has a body

    Explanation:

    Arrays cannot be passed to functions, only pointers (for arrays, base addresses) can be passed. So the arguments int *ptr and int prt[size] have no difference as function arguments. In other words, both the functoins have the same signature and so cannot be overloaded.

    « Previous || Next »

    C++ Sample Question number : 1-3 | 4-7 | 8-10 | 11-13 | 14-21 | 22-28 | 29-35 | 36-42 | 43-49 | 50-54 | 55-58 | 59-66 | 67-73 | 74-80
Sample Test Questions
GRE Sample Questions
CAT Sample Questions
GMAT Sample Questions
TOEFL Sample Questions
ACT Sample Questions
SAT Sample Questions
LSAT Sample Questions
PSAT Sample Questions
MCAT Sample Questions
PMP Sample Questions
GED Sample Questions
ECDL Sample Questions
DMV Sample Questions
CCNA Sample Questions
MCSE Sample Questions
Network+ Sample Questions
A+ Sample Questions
Technical Sample Questions
WASL Sample Questions
CISA Sample Questions

Other Sample Questions
Sample Interview Questions
Sample Teacher Interview Questions
Sample Citizenship Questions
Accuplacer Sample Questions
Science Bowl sample Questions
Driving Test Sample Questions
Sample Survey Questions Sample Essay Questions
Sample Behavioral Interview Questions

Copyright © 2004-2013, Best BSQ. All Rights Reserved.