github

Made bySaurav Hathi

MCQ Test C/C++: Memory Management

Q1:

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;

}

Tags:
Section:
Core Programming
Options:
Q2:

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.

Tags:
Section:
Core Programming
Options:
Q3:

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;

}

Tags:
Section:
Core Programming
Options:
Q4:

Allocation of memory to objects at the time of their construction is known as ……………. of objects.

Tags:
Section:
Core Programming
Options:
Q5:

……………….. provides the flexibility of using different format of data at runtime depending upon the situation.

Tags:
Section:
Core Programming
Options:
Q6:

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;

}

Tags:
Section:
Core Programming
Options:
Q7:

How many bits of memory is needed for internal representation of class in C++?

Tags:
Section:
Core Programming
Options:
Q8:

What is the size of *ptr in a 32-bit machine in C++, (assuming initialization as int *ptr = 10;)?

Tags:
Section:
Core Programming
Options:
Q9:

How much memory will the given C structure take?

#include <stdio.h>

struct test

{

int k;

char c;

};

Tags:
Section:
Core Programming
Options:
Q10:

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;

Tags:
Section:
Core Programming
Options: