Friday, March 09, 2007

C++ Questionaire

Some interesting questions asked in C++ tests
Enjoy !!!

1) Will the following program execute?

void main()
{
void *vptr = (void *) malloc(sizeof(void));
vptr++;
}

It will throw an error, as arithmetic operations cannot be performed
on void pointers. It will not build as sizeof cannot be applied to void* ( error “Unknown size” )

However, According to gcc compiler it won’t show any error, simply it executes. but in general we can’t do arthematic operation on void, and gives size of void as 1.
The program compiles in GNU C while giving a warning for “void main”. The program runs without a crash. sizeof(void) is “1″ hence when vptr++, the address is incremented by 1.

However gcc is a C compiler. When compiled in g++, g++ complains that the return type cannot be void and the argument of sizeof() cannot be void. It also reports that ISO C++ forbids incrementing a pointer of type ‘void*’.



2) Will it execute or not?

void main()
{
char *cptr = 0?2000;
long *lptr = 0?2000;
cptr++;
lptr++;
printf(” %x %x”, cptr, lptr);
}

0?2000 cannot be implicitly converted to a pointer.

Compiling with VC7 results following errors:
error C2440: ‘initializing’ : cannot convert from ‘int’ to ‘char *’
error C2440: ‘initializing’ : cannot convert from ‘int’ to ‘long *

Program does executes in C

The printout:
2001 2004

compiled with some warnings:
warning C4047: ‘initializing’ : ‘char *’ differs in levels of indirection from ‘int’
warning C4047: ‘initializing’ : ‘long *’ differs in levels of indirection from ‘int’

Solution is casting needs to be done. The format has to be:
int *a = (int *)2000;



3) Whats the solution ?

#include "iostream.h"

class example
{
private :
int i;
float j;

public :

example (int ii=0, float jj=0.0)
{
i = ii; j = jj;
}

void showdata()
{
cout << i << j;
}

friend example operator *(example k, example l);
};

example operator *(example k, example l)
{
example temp;
temp.i = k.i*l.i;
temp.j = k.j*l.j;
return (&temp);
}

void main ()
{
example e1(10,3.14);
example e2(1,1.5);
example e3,e4,e5;

e3 = e1 * 2;
e4 = 2 * e2;
e5 = e1 * e2 * 2;

e3.showdata();
e4.showdata();
e5.showdata();
}


a) Error "Cannot access private data of class example"
b) Error "No legal conversion of return value to return type class example *"
c) Either a or b
d) Neither a or b


Answer:- (B) :- error C2664: 'example::example(int,float)' : cannot convert parameter 1 from 'example *__w64 ' to 'int'

4) Whats the solution ?

#include "iostream.h"

class Base
{
public:
virtual void fun1()
{
cout << "fun1 of base";
}
};

class der1: public Base
{
public:
void fun1()
{
cout << "fun1 of der1";
}

virtual void fun2()
{
cout << "fun2 of der1";
}
};

class der2 : public der1 {};

void main()
{
Base *b;
der2 d;
b = &d;
b->fun1();
b->fun2();
};


a) Code runs successfully
b) Error: fun2 is not a member of Base
c) Error: cannot convert from class der2 to class Base
d) Error: cannot convert from class Base to class der2

Answer:- (B):- error C2039: 'fun2' : is not a member of 'Base'

5)Whats the solution ?

#include "iostream.h"

class Sample
{
int i;

public:
Sample(Sample S)
{
i=S.i;
}
};

void main()
{
Sample S1;
Sample S2 = S1;
}


a) Error: Class must contain a zero argument constructor;
b) Error: Illegal copy constructor
c) Error: Illegal initialisation
d) None above


Answer:- (A) :- error C2512: 'Sample' : no appropriate default constructor available

6)Whats the output ?

#include "iostream.h"

void main()
{
class xx
{
public:
int x=0;
char name [] = "hello";
};

class xx *s;
cout << s->x;
cout << s->name;
}

Output ?

Answer:-
error C2864: 'main::xx::x' : only static const integral data members can be initialized within a class
error C2864: 'main::xx::name' : only static const integral data members can be initialized within a class

7)Whats the output ?

#include "iostream.h"

class counter
{
unsigned int count;

public:
counter()
{
count = 0;
}

int getcount()
{
return count;
}

void operator ++ (int x)
{
count ++;
}
}

void main()
{
counter c1, c2;
cout<< c1.getcount();
cout<< c2.getcount();
c1 ++;
c2 ++;
cout<< c1.getcount();
cout<< c2.getcount();
}

Output ?

Answer:- 0011

8)Whats the output ?

#include

union base
{
private:
int i;
float j;
};

union derieve:public base
{
private:
int i;
};

void main()
{
cout<< sizeof(union derieve);
}

Output ?

Answer:- error C2570: 'derieve' : union cannot have base classes

9)Whats the solution ?

#include "iostream.h"
#include "malloc.h"

void * operator new (size_t s)
{
void *q = malloc(s);
return q;
}

void main()
{
int *p = new int;
*p = 25;
cout << *p;
}

a) We cannot overload new
b) We cannot overload new globally
c) No problem
d) None above


Answer:- (C) :- On execution, "25" is displated

10)Whats the solution ?

#include "iostream.h"
float divide (float a, int b)
{
return (a/b);
}

float divide(float a, float b)
{
return (a/b);
}

void main()
{
int x=5, y=2;
float n=5.0, m= 2.0;
cout << divide(x,y);
cout << divide (n,m);
}

a) Program will not compile
b) 2, 2.5
c) Compile but display garbage value
d) 2.5, 2.5

Answer:- (D)


Programming Ring - New Post
Programming Ring - Old Post

8 comments:

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Anonymous said...

I wish not concur on it. I assume warm-hearted post. Especially the title-deed attracted me to read the intact story.

Anonymous said...

Good post and this mail helped me alot in my college assignement. Gratefulness you for your information.

Anonymous said...

Nice brief and this post helped me alot in my college assignement. Thanks you for your information.

Anonymous said...

Hi
Very nice and intrestingss story.

Anonymous said...

Good post and this fill someone in on helped me alot in my college assignement. Say thank you you seeking your information.

Anonymous said...

What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.

Anonymous said...

[url=http://hairtyson.com]phentermine 375[/url] are tablets that resist reduce confederation weight. Everybody of these tabs has to be taken with water, around 20 minutes already a meal, twice a day.