Saturday, May 31, 2008

C puzzles

The following are some of the tricky C/C++ questions that I faced in various interviews. It might be useful for you, if you work with C/C++.
• 1: Write a Macro to find the number of elements in a array

ANSWER:
#define numelem(ar) sizeof(ar)/sizeof(ar[0])
• 2: What is wrong with this macro to find out the square of a number

#define square(a) a*a

ANSWER:
If you pass an expression to this macro, it fails.
Eg: square(2+3) will be expanded to 2+3*2+3 = 11
whereas the actual result should be 25
Inorder to solve this problem you can write the macro as
#define square(a) ((a)*(a))
• 3: What will be the output of the statement ?

printf("Value is %d\n", printf("Hello World\n"));

ANSWER:
The inner printf statement prints the string "Hello World" on to the console and returns the number of characters printed to substitute "%d" in the outer printf statement - in this case 12. So the program output will be

Hello World
Value is 12

• 4. After you declare
const char *abcd="Test";
you can write a statement

abcd="Hello";

But you can not write a statement
*abcd='H';

Actually after you execute abcd="Hello" and print the value of *abcd using printf("%c", *abcd); you will get the output as H.
then why the compiler doesnt allow *abcd='H' ?

ANSWER:
When you are declaring const char *abcd="Test"; you are specifying abcd as a const pointer. So you can not change the contents of the location pointed to by *abcd, by an assignment *abcd='H'.

But when you are writing abcd="Hello", the contents of the memory pointed to, by abcd is not changing, but the address of abcd is changing to point to a string "Hello"
• 5. Are the statements 1 & 2 right, if we declare str as follows?

char * const str="Hello";

1. *str='W';
2. str="World";

ANSWER:
1-right
2-wrong
• 6. What is the problem with the following piece of code

typedef struct A{
int i;
};

A* RetStruct()
{
A a;
a.i=10;
return &a;
}

ANSWER:
Inside the function RetStruct, the struct variable 'a' will be created in the stack. So when it comes out of the function, 'a' will be deleted automatically. So if you make an assignment statement like the following

A *b=RetStruct();
b will actually point to an invalid location and using b later might create a core dump.
• 7. What does the following piece of code do ?

int k;

int& func()
{
return k;
}

int main()
{
func() = 10;
return 0;
}

ANSWER:
This statement assigns 10 to k; but will not compile in Linux .. lvalue not correct.
• 8. Will this program compile successfully ?

void f(){};
void g() {return f();}
main()
{
g();
}

ANSWER:
No. this will give the following error.

line 2: Error: g() cannot return a value.
line 2: Error: A value of type void is not allowed.

void g() means that it can not return anything-not even a void value. Here return f() means it is trying to return void.
• 9. Will the following program compile successfully?

int main()
{
int a=10;

int &k;
k=a;
}

A: No. You have to initialize k when it is declared.

int &k=a;

Also you can not assign a const like,
int &k=10;

But after initializing the value of k by writing
int &k=a;
you can assign any integer value to k. This will actually change the value of a.
• 10: What is the use of scope resolution operator when it is used like the following ?

int j;

void main
{
int j;

::j=10;
}

ANSWER:
When you write ::j , it referes to the global variable j;
• 11. Can you declare a constant data member inside a class like the following ?

class A {
public:
const int val=10;
};


ANSWER:
No. You should initialize the constant data member in the initialization list of the constructor like the following.

class A {
public:
const int val;
A(): val(10){};
};

• 12. Will the following program compile successfully ?

class A
{
public:
static int a;
A(){a=10;}
};

int main()
{
A b;
printf("%d", b.a);
return 0;
}

ANSWER:
No. you have to define the static member outside the class also.
Add the following line after the class declaration.

int A::a;

Now it will compile.

2 comments:

Anonymous said...

What a shame. Why is a set of C questions without errors so hard to find? And as for the formatting (well, lack of formatting) I assume that is just lazily using whater the blog software allows.

Rahiakil said...

Not all the pages are that bad. I will remove it if you want.
Actually, these were random ramblings, just to give some hints at good questions.
Most of them are findings on a hard working day when you really cannot spend some time trying to modify everything beautifully.

Sorry again, I will try to come out with better pages with time.
These are genuine hands on work Trust me