Predict the output of following C++ program.
#include<iostream>
#include<stdlib.h>
using namespace std;
class Test
{
public:
Test()
{ cout << "Constructor called"; }
};
int main()
{
Test *t = (Test *) malloc(sizeof(Test));
return 0;
}
Which of the following is true about new when compared with malloc?
1) new is an operator, malloc is a function
2) new calls constructor, malloc doesn't
3) new returns appropriate pointer, malloc returns void * and pointer needs to be typecast to appropriate type.
What will be the output of this C++ program?
#include <iostream>
using namespace std;
class Test
{
int x;
Test() { x = 5;}
};
int main()
{
Test *t = new Test;
cout << t->x;
}
Allocation of memory to objects at the time of their construction is known as ……………. of objects.
……………….. provides the flexibility of using different format of data at runtime depending upon the situation.
What is the output of this C++ program?
#include <iostream>
#include <exception>
using namespace std;
class base { virtual void dummy() {} };
class derived: public base { int a; };
int main ()
{
try
{
base * pba = new derived;
base * pbb = new base;
derived * pd;
pd = dynamic_cast<derived*>(pba);
if (pd == 0)
cout << "Null pointer on first type-cast" << endl;
pd = dynamic_cast<derived*>(pbb);
if (pd == 0)
cout << "Null pointer on second type-cast" << endl;
}
catch (exception& e)
{
cout << "Exception: " << e.what();
}
return 0;
}
How many bits of memory is needed for internal representation of class in C++?
What is the size of *ptr in a 32-bit machine in C++, (assuming initialization as int *ptr = 10;)?
How much memory will the given C structure take?
#include <stdio.h>
struct test
{
int k;
char c;
};
For what minimum value of x in a 32-bit Linux OS would make the size of s equal to 8 bytes?
struct temp
{
int a : 13;
int b : 8;
int c : x;
}s;