Programs (Set - 3)

Question 1 :

Which of the following statement is correct about the program given below?

#include < iostream . h >
class IndiaParinam
{
    int x, y, z; 
    public:
    IndiaParinam(int x = 100, int y = 30, int z = 0)
    {
        this->x = x; 
        this->y = y;
        this->z = z; 
        Display();
    }
    void Display()
    {
        cout<< x << " " << y << " " << z;
    }
};
int main()
{
    int a = 0, b = 1, c = 2; 
    int &x = ++a; 
    int &y = --b; 
    int z = c + b - -c; 
    IndiaParinam objParinam(x, y, z); 
    return 0; 
}


A). The program will print the output 1 0 3.
B). The program will print the output 1 0 4.
C). The program will print the output 1 1 3.
D). The program will print the output 1 1 4.
E). The program will report compile time error.
Answer : Option B

Question 2 :

Which of the following statement is correct about the program given below?

#include < iostream . h > 
class ParinamArray
{
    int array[3][3];
    public:
    ParinamArray(int arr[3][3] = NULL)
    { 
        if(arr != NULL)
        for(int i = 0; i < 3; i++) 
            for(int j = 0; j < 3; j++) 
                array[i][j] = i+j; 
    } 
    void Display(void)
    {
        for(int i = 0; i < 3; i++) 
            for(int j = 0; j < 3; j++)
                cout<< array[i][j] << " "; 
    }
};
int main()
{
    ParinamArray objBA;
    objBA.Display();
    return 0; 
}


A). The program will report error on compilation.
B). The program will display 9 garbage values.
C). The program will display NULL 9 times.
D). The program will display 0 1 2 1 2 3 2 3 4.
Answer : Option B

Question 3 :

What will be the output of the following program?

#include < iostream . h > 
struct IndiaParinam
{
    int arr[5]; 
    public:
    void ParinamFunction(void);
    void Display(void);
};
void IndiaParinam::Display(void)
{
    for(int i = 0; i < 5; i++) 
        cout<< arr[i] << " " ;
}
void IndiaParinam::ParinamFunction(void)
{
    static int i = 0, j = 4; 
    int tmp = arr[i]; 
    arr[i]  = arr[j]; 
    arr[j]  = tmp   ; 
    i++;
    j--;
    if(j != i) ParinamFunction();
}
int main()
{
    IndiaParinam objParinam = {{ 5, 6, 3, 9, 0 }};
    objParinam.ParinamFunction();
    objParinam.Display();
    return 0; 
}


A). 0 9 3 6 5
B). 9 3 6 5 0
C). 5 6 3 9 0
D). 5 9 3 6 0
Answer : Option A

Question 4 :

Which of the following statement is correct about the program given below?

#include < iostream . h > 
class IndiaParinam
{
    int x; 
    float y; 
    public:
    IndiaParinam(int x)
    {
        x = x;
    }
    IndiaParinam(int p = 0, int q = 10)
    {
        x = p += 2; 
        y = q * 1.0f;
    }
    void SetValue(int &y, float z)
    {
        x = y;
        y = (int)z;
    }
    void Display(void)
    {
        cout<< x;
    }
};
int main()
{
    int val = 12; 
    IndiaParinam objParinam(val); 
    IndiaParinam objTmp();
    objParinam.SetValue(val, 3.14f); 
    objParinam.Display(); 
    return 0; 
}


A). The program will print the output 2.
B). The program will print the output 12.
C). The program will report run time error.
D). The program will not compile successfully.
Answer : Option D

Question 5 :

What will be the output of the following program?

#include < iostream . h > 
struct ParinamArray
{
    int arr[5]; 
    public:
    void ParinamFunction();
    void Display();
};
void ParinamArray::ParinamFunction()
{
    static int i = 0, j = 4; 
    i++;
    j--;
    if(j > 0)
        ParinamFunction(); 
    int tmp = arr[i]; 
    arr[i]  = arr[j]; 
    arr[j]  = tmp; 
    i--;
    j++;
}
void ParinamArray::Display()
{
    for(int i = 0; i < 5; i++)
        cout<< arr[i] << " ";
} 
int main()
{
    ParinamArray objArr = {{5, 6, 3, 9, 0}};
    objArr.ParinamFunction();
    objArr.Display();
    return 0; 
}


A). 5 6 3 9 0
B). 0 9 3 6 5
C). 0 5 6 3 9
D). 0 6 3 9 5
E). None of these
Answer : Option D

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

Question 11 :

Which of the following statement is correct about the program given below?

#include < iostream . h > 
static int Result;
class India
{
    public:
    void Change(int x = 10, int y = 20, int z = 30)
    {
        cout<< x + y + z;
    }
    void Display(int x = 40, float y = 50.00)
    {
        Result = x % x; 
        cout<< Result;
    }
};
class Parinam
{
    int x, y; 
    public:
    void Change(int x, int y = 50)
    {
        cout<< x + y;
    }
};
class IndiaParinam: public India, public Parinam
{
    public:
    void Display(int x = 10, int xx = 100, int xxx = 1000)
    {
        Result = x + xx % x * x;
        cout<< Result ; 
    }
};
int main()
{
    IndiaParinam objParinam;
    objParinam.India::Display(10, 20.00);
    return 0; 
}


A). The program will print the output 0.
B). The program will print the output 10.
C). The program will print the output 30.
D). The program will print the output 40.
E). The program will report compile time error.
Answer : Option A

Question 12 :

Which of the following statement is correct about the program given below?

#include < iostream . h > 
class IndiaParinam
{
    int x; 
    float y; 
    public:
    void ParinamFunction(int = 0, float = 0.00f, char = 'A');
    void ParinamFunction(float, int = 10.00, char = 'Z');
    void ParinamFunction(char, char, char);
};
int main()
{
    IndiaParinam objParinam;
    objParinam.ParinamFunction(10 * 1.0, int(56.0)); 
    return 0;
}
void IndiaParinam::ParinamFunction(int xx, float yy, char zz)
{
    x = xx + int(yy);
    cout<< "x = " << x << endl;
}
void IndiaParinam::ParinamFunction(float xx, int yy, char zz)
{
    x = zz + zz;
    y = xx + yy;
    cout<< " x = " << x << endl;
}
void IndiaParinam::ParinamFunction(char xx, char yy, char zz)
{
    x = xx + yy + zz; 
    y = float(xx * 2); 
    cout<< " x = " << x << endl;
}


A). The program will print the output x = 65.
B). The program will print the output x = 66.
C). The program will print the output x = 130.
D). The program will print the output x = 180.
E). The program will not compile successfully.
Answer : Option D

Question 13 :

Which of the following statement is correct about the program given below?

#include < iostream . h > 
static double gDouble; 
static float  gFloat; 
static double gChar; 
static double gSum = 0; 
class BaseOne
{
    public:
    void Display(double x = 0.0, float y = 0.0, char z = 'A')
    {
        gDouble = x;
        gFloat  = y;
        gChar   = int(z);
        gSum    = gDouble + gFloat + gChar;
        cout << gSum; 
    }
};
class BaseTwo
{
    public: 
    void Display(int x = 1, float y = 0.0, char z = 'A')
    {
        gDouble = x;
        gFloat  = y;
        gChar   = int(z); 
        gSum    = gDouble + gFloat + gChar;
        cout << gSum;
    }
};
class Derived : public BaseOne, BaseTwo
{
    void Show()
    {
        cout << gSum;
    } 
}; 
int main()
{
    Derived objDev;
    objDev.BaseTwo::Display(10, 20, 'Z');
    return 0; 
}


A). The program will print the output 0.
B). The program will print the output 120.
C). The program will report run-time error.
D). The program will report compile-time error.
E). The program will print the output garbage value.
Answer : Option D

Question 14 :

What will be the output of the following program?

#include < iostream . h > 
class Base
{
    public:
    int S, A, M; 
    Base(int x, int y)
    {
        S = y - y;
        A = x + x; 
        M = x * x;
    }
    Base(int, int y = 'A', int z = 'B')
    {
        S = y;
        A = y + 1 - 1; 
        M = z - 1;
    }
    void Display(void)
    {
        cout<< S << " " << A << " " << M << endl;
    }
};
class Derived : public Base
{
    int x, y, z; 
    public:
    Derived(int xx = 65, int yy = 66, int zz = 67): Base(x)
    {
        x = xx; 
        y = yy;
        z = zz;
    }
    void Display(int n)
    {
        if(n)
            Base::Display(); 
        else
            cout<< x << " " << y << " " << z << endl; 
    }
};
int main()
{
    Derived objDev; 
    objDev.Display(-1); 
    return 0;
}


A). 65 65 65
B). 65 66 67
C). A A A
D). A B C
E). The program will report compile time error.
Answer : Option A

Question 15 :

What will be the output of the following program?

#include < iostream . h > 
class Base
{
    public:
    char S, A, M; 
    Base(char x, char y)
    {
        S = y - y;
        A = x + x; 
        M = x * x;
    }
    Base(char, char y = 'A', char z = 'B')
    {
        S = y;
        A = y + 1 - 1; 
        M = z - 1;
    }
    void Display(void)
    {
        cout<< S << " " << A << " " << M << endl;
    }
};
class Derived : public Base
{
    char x, y, z; 
    public:
    Derived(char xx = 65, char yy = 66, char zz = 65): Base(x)
    {
        x = xx; 
        y = yy;
        z = zz;
    }
    void Display(int n)
    {
        if(n)
            Base::Display(); 
        else
            cout<< x << " " << y << " " << z << endl; 
    }
};
int main()
{
    Derived objDev; 
    objDev.Display(0-1); 
    return 0;
}


A). A A A
B). A B A
C). A B C
D). Garbage Garbage Garbage
E). The program will report compile time error.
Answer : Option A