Pages

Friday, May 20, 2011

Test your own C Programming skills





Hello World!

Good morning C programmers. Here is a compilation of couple of C and C++ questions to test your C/C++ programming knowledge. Most of the time you will be facing similar questions in job interviews for Software engineer or programmer not only at the fresher level but also at a very senior level. Many companies expect you to be hands on even at senior level. C is a programming language which is easy and at the same time extremely difficult if asked about complex pointer related questions. So, one has to be extremely careful in answering such questions.







Q1. Find the o/p:
main()
{
    char *ptr="hello";
    char ary[]="world";

printf("PTR: &ptr=%x, ptr=%x, *ptr=%x, ptr[0]=%x\n", &ptr,ptr, *ptr, ptr[0]);
printf("ARY: &ary=%x,ary=%x, *ary=%x, ary[0]=%x\n", &ary, ary, *ary, ary[0]);
}

Ans:


Q2. Find the o/p:
main()
{
    struct node {
        char a;
        int b; 
        short c;
        struct node next;
    }n1;   

printf("Ans1:%d\n", sizeof(n1));

return 0;
}

Ans:


Q3. Find the o/p:
main()
{
    struct node {
        char a;
        int b; 
        short c;
        struct node *next;
    }n1;   

printf("Ans1:\n\t node=%x\n \t node+2=%x\n", (char *)&n1, (char *)&n1+2);
printf("Ans2:%d\n", sizeof(n1));

return 0;
}
Ans:


Q4. What is the difference between fopen() and open()? When do you choose to use one over the other?

Ans:



Q5. Find the o/p:
main()
{
if(!printf("Hello ")){}
else
printf("World!");
}
Ans:


Q6. Find the o/p:
main()
{
struct i{
int a;    //4
double b;    //8
char * c;    //4
char d[7];    //8
short e;    //4
int f;    //4
};
printf("sizeof(i)= %d", sizeof(struct i));
}
Ans:


Q6.1. Find the o/p:
main()
{
struct i{
int a;    //4
double b;    //8
char * c;    //4
char d[3];    //4
short e;    //4
int f;    //4
};
printf("sizeof(i)= %d", sizeof(struct i));
}
Ans:



Q7. Find the o/p:
# include < stdio.h >
void main()
{
printf(NULL) ;
return ;
}
Ans:


Q8. Find the o/p:
# include < stdio.h >
void main()
{
int i = 0;
printf(i) ;
return ;
}
Ans:


Q9. Find the o/p:
#include <stdio.h>
void main()
{
printf("%s",NULL) ;
return ;
}
Ans:


Q10. Find the o/p of this last one:
main()
{
printf("%%",7);
}
Ans:


Q11. How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
Ans:


Q12. What is asmlinkage and what is it's use?
Ans:


Q13. What is output?
main()
{
printf("%x",-1<<4); }

Ans:

Link to the earlier post: Test your programming skills.

Enjoy yourself.
Regards,
Kongkon