Exercise

1.What is the output of this program?
Note:Includes all required header files

using namespace std;
class Empty {};
int main()
{
cout << sizeof(Empty);
return 0;
}

A. A non-zero value.
B. 0
C. Compiler Error
D. Runtime Error

View Answer


2.What is the output of this program?
Note:Includes all required header files

using namespace std;
class Empty { };
class Derived: Empty { int a; };
int main()
{
cout << sizeof(Derived);
return 0;
}

A. 1
B. 2
C. 4
D. Error

View Answer


3.What is the output of this program?
Note:Includes all required header files

class Test {
int x;
};
int main()
{
Test t;
cout << t.x;
return 0;
}

A. 0
B. Garbage value
C. Runtime error
D. Complier error

View Answer


4.Assume that an integer and a pointer each takes 4 bytes. Also, assume that there is no alignment in objects. Predict the output following program.
Note:Includes all required header files

using namespace std;
class Test
{
static int x;
int *ptr;
int y;
};
int main()
{
Test t;
cout << sizeof(t) << " ";
cout << sizeof(Test *);
}

A. 12 12
B. 12 4
C. 8 4
D. 8 8

View Answer


5.Which of the following is true about the following program
Note:Includes all required header files

class Test
{
public:
int i;
void get();
};
void Test::get()
{
std::cout << "Enter the value of i: ";
std::cin >> i;
}
Test t; // Global object
int main()
{
Test t; // local object
t.get();
std::cout << "value of i in local t: "< ::t.get();
std::cout << "value of i in global t: "<<::t.i<<'n';
return 0;
}

A. Compiles and runs fine
B. Compiler Error in Line "::t.get();
C. Compiler Error: Cannot have two objects with same class name
D. Runtime error

View Answer


6.What will be the output of this program?
Note:Includes all required header files

using namespace std;
class sample
{
int x;
}
int main()
{
sample obj;
obj.x=100;
cout<<"x="<< obj.x;
}

A. 10
B. 100
C. Error
D. None of the above

View Answer


7.What will be the output of this program?
Note:Includes all required header files

using namespace std;
//Empty class
class test
{
};
int main()
{
test testObj;
cout<<"size ="<< sizeof(testObj);
return 0;
}

A. Error
B. size =Garbage
C. size =1
D. Compile but no output

View Answer


8. What will be the output of the following program?
Note:Includes all required header files

class find
{
public:
int x;
};
int main()
{
find *p = new find();
(*p).x = 5;
cout<< (*p).x << " " << p->x << " " ;
p->x = 10;
cout<< (*p).x << " " << p->x ;
return 0;
}

A. 5 5 10 10
B. Garbage garbage 10 10
C. 5 5 Garbage garbage
D. Garbage garbage Garbage garbage

View Answer


9. What will be the output of the following program?
Note:Includes all required header files

class find
{
static int x;
public:
static void Set(int xx)
{
x = xx;
}
void Display()
{
cout<< x ;
}
};
int find::x = 0;
int main()
{
find::Set(33);
find::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.

View Answer


10. What will be the output of the following program?
Note:Includes all required header files

class course
{
int x, y;
public:
course(int xx)
{
x = ++xx;
}
void Display()
{
cout<< --x << " ";
}
};
int main()
{
course obj(20);
obj.Display();
int *p = (int*)&obj ;
*p = 5;
obj.Display();
return 0;
}

A. 20 4
B. 21 4
C. 20 5
D. 21 5

View Answer



Discussion

* You must be logged in to add comment.