Programs (Set - 3)

Question 6 :

What will be the output of the following program?

#include < iostream . h >
#include < string . h > 
class ParinamString
{
    char x[50]; 
    char y[50]; 
    char z[50]; 
    public:
    ParinamString()
    { }
    ParinamString(char* xx)
    {
        strcpy(x, xx); 
        strcpy(y, xx);
    }
    ParinamString(char *xx, char *yy = " C++", char *zz = " Programming!")
    {
        strcpy(z, xx); 
        strcat(z, yy); 
        strcat(z, zz);
    } 
    void Display(void)
    {
    cout<< z << endl;
    } 
}; 
int main()
{
    ParinamString objStr("Learn", " Java");
    objStr.Display();
    return 0; 
}


A). Java Programming!
B). C++ Programming!
C). Learn C++ Programming!
D). Learn Java Programming!
E). Learn Java C++ Programming!
Answer : Option D

Question 7 :

What will be the output of the following program?

#include < iostream . h > 
class BaseCounter
{
    protected:
    long int count;

    public:
    void CountIt(int x, int y = 10, int z = 20)
    {
        count = 0;
        cout<< x << " " << y << " " << z << endl;
    } 
    BaseCounter()
    {
        count = 0;
    }
    BaseCounter(int x)
    {
        count = x ;
    } 
}; 
class DerivedCounter: public BaseCounter
{
    public:
    DerivedCounter()
    { }
    DerivedCounter(int x): BaseCounter(x) 
    { }
};
int main()
{
    DerivedCounter objDC(30); 
    objDC.CountIt(40, 50); 
    return 0; 
}


A). 30 10 20
B). Garbage 10 20
C). 40 50 20
D). 20 40 50
E). 40 Garbage Garbage
Answer : Option C

Question 8 :

What will be the output of the following program?

#include < iostream . h > 
class Number
{
    int Num; 
    public:
    Number(int x = 0)
    {
        Num = x;
    }
    void Display(void)
    {
        cout<< Num;
    }
    void Modify(); 
};
void Number::Modify()
{
    int Dec;
    Dec = Num % 13; 
    Num = Num / 13; 
    
         if(Num  > 0 ) Modify()   ; 
         if(Dec == 10) cout<< "A" ; 
    else if(Dec == 11) cout<< "B" ; 
    else if(Dec == 12) cout<< "C" ; 
    else if(Dec == 13) cout<< "D" ;
    else               cout<< Dec ;
}
int main()
{
    Number objNum(130);
    objNum.Modify();
    return 0; 
}


A). 130
B). A0
C). B0
D). 90
Answer : Option B

Question 9 :

What will be the output of the following program?

#include < iostream . h > 
class Base
{
    int x, y; 
    public:
    Base() 
    {
        x = y = 0; 
    } 
    Base(int xx)
    {
        x = xx;
    }
    Base(int p, int q = 10)
    {
        x = p + q;
        y = q; 
    } 
    void Display(void)
    {
        cout<< x << " " << y << endl;
    } 
}objDefault(1, 1);

class Derived: public Base
{
    Base obj; 
    public:
    Derived(int xx, int yy): Base(xx, xx + 1)
    { }
    Derived(Base objB = objDefault)
    { } 
}; 
int main()
{
    Derived objD(5, 3);
    Derived *ptrD = new Derived(objD);
    ptrD->Display();
    delete ptrD;
    return 0; 
}


A). 3 2
B). 8 3
C). 11 6
D). 11 10
E). The program will not compile successfully.
Answer : Option C

Question 10 :

What is correct about the following program?

#include < iostream . h > 
class Base
{
    int x, y, z; 
    public: 
    Base()
    {
        x = y = z = 0;
    }
    Base(int xx, int yy = 'A', int zz = 'B')
    {
        x = xx;
        y = x + yy;
        z = x + y;
    }
    void Display(void)
    {
        cout<< x << " " << y << " " << z << endl;
    }
};
class Derived : public Base
{
    int x, y; 
    public:
    Derived(int xx = 65, int yy = 66) : Base(xx, yy)
    {
        y = xx; 
        x = yy;
    }
    void Display(void)
    {
        cout<< x << " " << y << " ";
        Display(); 
    }
};
int main()
{
    Derived objD;
    objD.Display();
    return 0; 
}


A). The program will report compilation error.
B). The program will run successfully giving the output 66 65.
C). The program will run successfully giving the output 65 66.
D). The program will run successfully giving the output 66 65 65 131 196.
E). The program will produce the output 66 65 infinite number of times (or till stack memory overflow).
Answer : Option C