Objects and Classes - Programs Multiple Questions and Answers.
Exercise Questions ::
Objects and Classes
1. | What will be the output of the program? |
|
#include<iostream.h>
class PAK
{
public:
int x;
};
int main()
{
PAK *p = new PAK();
(*p).x = 10;
cout<< (*p).x << " " << p->x << " " ;
p->x = 20;
cout<< (*p).x << " " << p->x ;
return 0;
}
|
| A. | 10 10 20 20 |
| B. | Garbage garbage 20 20 |
| C. | 10 10 Garbage garbage |
| D. | Garbage garbage Garbage garbage |
|
|
Published by:Michael Daani
2. | Which of the following statement is correct about the program given below? |
|
#include<iostream.h>
class PAKMCQS
{
static int x;
public:
static void SetData(int xx)
{
x = xx;
}
void Display()
{
cout<< x ;
}
};
int PAKMCQS::x = 0;
int main()
{
PAKMCQS::SetData(33);
PAKMCQS::Display();
return 0;
}
|
| A. | The program will print the output 0. |
| B. | The program will print the output 33. |
| C. | The program will print the output Garbage. |
| D. | The program will report compile time error. |
|
|
Published by:Michael Daani
3. | Which of the following statement is correct about the program given below? |
|
#include<iostream.h>
class PAKMCQS
{
static int x;
public:
static void SetData(int xx)
{
x = xx;
}
static void Display()
{
cout<< x ;
}
};
int PAKMCQS::x = 0;
int main()
{
PAKMCQS::SetData(44);
PAKMCQS::Display();
return 0;
}
|
| A. | The program will print the output 0. |
| B. | The program will print the output 44. |
| C. | The program will print the output Garbage. |
| D. | The program will report compile time error. |
|
|
Published by:Michael Daani
4. | What will be the output of the following program? |
|
#include<iostream.h>
class PAKTeam
{
int x, y;
public:
PAKTeam(int xx)
{
x = ++xx;
}
void Display()
{
cout<< --x << " ";
}
};
int main()
{
PAKTeam objBT(45);
objBT.Display();
int *p = (int*)&objBT;
*p = 23;
objBT.Display();
return 0;
}
|
| A. | 45 22 |
| B. | 46 22 |
| C. | 45 23 |
| D. | 46 23 |
|
|
Published by:Michael Daani
5. | Which of the following statement is correct about the program given below? |
|
#include<iostream.h>
class PAK
{
static int x;
public:
static void SetData(int xx)
{
this->x = xx;
}
static void Display()
{
cout<< x ;
}
};
int PAK::x = 0;
int main()
{
PAK::SetData(22);
PAK::Display();
return 0;
}
|
| A. | The program will print the output 0. |
| B. | The program will print the output 22. |
| C. | The program will print the output Garbage. |
| D. | The program will report compile time error. |
|
|
Published by:Michael Daani