C++ Programming - Objects and Classes

Exercise : Objects and Classes - Programs
1.
What will be the output of the following program?
#include<iostream.h> 
class Bix
{
    public:
      int x;
};
int main()
{
    Bix *p = new Bix();

    (*p).x = 10;
    cout<< (*p).x << " " << p->x << " " ;

    p->x = 20;
    cout<< (*p).x << " " << p->x ;

    return 0;
}
10 10 20 20
Garbage garbage 20 20
10 10 Garbage garbage
Garbage garbage Garbage garbage
Answer: Option
Explanation:
No answer description is available. Let's discuss.

2.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
class IndiaBix
{
    static int x; 
    public:
    static void SetData(int xx)
    {
        x = xx; 
    }
    void Display() 
    {
        cout<< x ;
    }
};
int IndiaBix::x = 0; 
int main()
{
    IndiaBix::SetData(33);
    IndiaBix::Display();
    return 0; 
}
The program will print the output 0.
The program will print the output 33.
The program will print the output Garbage.
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.

3.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
class IndiaBix
{
    static int x; 
    public:
    static void SetData(int xx)
    {
        x = xx; 
    }
    static void Display() 
    {
        cout<< x ;
    }
};
int IndiaBix::x = 0; 
int main()
{
    IndiaBix::SetData(44);
    IndiaBix::Display();
    return 0; 
}
The program will print the output 0.
The program will print the output 44.
The program will print the output Garbage.
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.

4.
What will be the output of the following program?
#include<iostream.h> 
class BixTeam
{
    int x, y; 
    public:
    BixTeam(int xx)
    {
        x = ++xx;
    }
    void Display()
    {
        cout<< --x << " ";
    }
};
int main()
{
    BixTeam objBT(45);
    objBT.Display();
    int *p = (int*)&objBT;
    *p = 23;
    objBT.Display();
    return 0; 
}
45 22
46 22
45 23
46 23
Answer: Option
Explanation:
No answer description is available. Let's discuss.

5.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
class IndiaBix
{
    static int x; 
    public:
    static void SetData(int xx)
    {
        this->x = xx; 
    }
    static void Display() 
    {
        cout<< x ;
    }
};
int IndiaBix::x = 0; 
int main()
{
    IndiaBix::SetData(22);
    IndiaBix::Display();
    return 0; 
}
The program will print the output 0.
The program will print the output 22.
The program will print the output Garbage.
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.