C programming questions!!

Well, to all the C programmers out there.. here are some of the questions which I
bet you would surely enjoy solving...


1)
Find the bug in this code.
#include<stdio.h>
int main()
{
int i=3,j=5;
printf("%d",(i+j)++);
return(0);
}
Ans:



2)
What is the output of the following piece of code?
#include<stdio.h>
int main()
{
char c='8';
int d=8;
printf("%d %d %d",d,d+=c>='0'&&c<='9',c++);
return(0);
}
Ans:



3)
x=x*y+1; --------------1
Is this different from x=x*(y+1); --------------2

How to right the assignment operation "2" with out using
parenthesis in one statement.
Ans:




4)
What does this code do?
#include<stdio.h>
void func(char s[],int c)
{
int i,j;
for(i=j=0;s[i]!='\0';i++)
if(s[i]!=c)
s[j++]= s[i] + s[i]==c ;
s[j]='\0';
}

int main()
{
char s[]="aelloworld";
func(s,'k');
printf("%s",s);
return(0);
}
Ans:



5)
Can you find any difference between the two scanf calls made in the
code given below ?

#include<stdio.h>
int main()
{
int x,y;
scanf("%d",&x);
scanf(" %d",&y);
return 0;
}
Ans:




6)
Predict the output of the following piece of code.
Assume that bit position 0 is at the right end and that i and j are
sensible positive values.
#include<stdio.h>
unsigned getbits(unsigned a,int i,int j)
{
return(a>>(i+1-j)) & ~(~0<<j);
}
int main()
{
unsigned num=128;
printf("%d\n",getbits(num,7,5));
return(0);
}
Ans:



7)
What does this code do on input 'abcd' from console?
#include<stdio.h>
int main()
{
char c;
while(c=getchar()!='\n')
printf("%d",c);
return(0);
}
Ans:


8)
What does this code do?
#include<stdio.h>
int main()
{
int i;
int a[20]={1,2,3,4,5,6,7,8,9,10,10,9,8,7,6,1,2,3,4,5};
int n=20;
for(i=0;i<20;i++)
{
printf("%6d%c",a[i],(i%10==9 || i== 19)?'\n':' ');
}
return(0);
}
Ans:



9)
Will this code work properly? If yes why? If not why not?
#include<stdio.h>
void swap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
int main()
{
int a=3617283450;
int b=3617283449;
swap(&a,&b);
printf("%d %d\n",a,b);
return(0);
}
Ans:



10)
What is the output of the following piece of code?
#include<stdio.h>
int main()
{
int a=3,b=1;
a = b<<a + b>>2;
b = b<<a + b>>2;
printf("%d %d",a,b);
return 0;
}
Ans:



11)
Can you predict the output of the following code snippet ?
#include<stdio.h>
int main()
{
int i=9876;

printf("%d\n",printf("%d",printf("%d",i)));

return 0;
}
Ans:




12)
Give the output of the following code.
#include<stdio.h>
typedef struct s
{
int a[10];
}s;
typedef struct t
{
int *a;
}t;

int main()
{
s s1,s2;t t1;
s1.a[0]=0;

t1.a=s1.a;
s1.a[0]++;
s2=s1;
s1.a[0]++;

printf("%d %d %d",s1.a[0],s2.a[0],t1.a[0]);
return 0;
}
Ans:



13)
You just have to write the declaration of an unsigned int p;
Obviously you can't use any spaces or new lines in the declaration !!
Ans:




14)
What is the output of the following code?
#include<stdio.h>
int main()
{
int x=2,y=3,z1,z2;
z1=x + + + y;
z2=x+++y;
printf("%d %d %d %d",x,y,z1,z2);
return 0;
}
Ans:



15)
Predict the output of the following code with the following inputs ->
1. 2c3
2. 2 5
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *i=0,*j=4;
i=(int*)malloc(sizeof(int));
scanf("%dc%d",&i,&j);
printf("%d %d",i,j);
return(0);
}
Ans:




16)
What is the output of the following code for the input "dcba"?
#include<stdio.h>
#include<stdlib.h>
int main()
{
char c;
while (c=getchar()!='a') printf("%d",c);
return 0;
}
Ans:



17)
What is the output of the following code?
#include<stdio.h>
int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))

int main()
{
printf("%d",SIZE);
if(-1<=SIZE) printf("1");
else printf("2");
return 0;
}
Ans:



18)
What is the output of the following code?
#include<stdio.h>
#include<string.h>
int main()
{
char a[]="aaa";
char *b="bbb";
strcpy(a,"cc");
printf("%s",a);
strcpy(b,"dd");
printf("%s",b);
return 0;
}
Ans:




19)
Can you predict the output of the following code snippet ?
#include<stdio.h>
#define sq(x) x*x
int main()
{
int a=3;
printf("%d\n",sq(a+3));
}
Ans:



20)
What is the output of this code?
#include<stdio.h>
int main()
{
int *i=0;
printf(" %p\n",i);
return(0);
}
Ans:




21)
Does the following C code compile ???? If it does .. can you
explain what happens when you run it with some arbitrary text
as input or what does it do ????
[ The input is redirected from a file called "input.txt" ]
int main()
{
for(;scanf("%*[^a-z]")+1;putchar(getchar()));
}
Ans:




22)
What is the output of the following code?
#include<stdio.h>
int main()
{
int a=1,b=3,c,d;
c=(a,b);
d=a,b,c;
printf("%d %d",c,d);
return 0;
}
Ans:




23)
What is the expected output of the code ?????
#include<stdio.h>
#define concatinate(a,b) a##b
#define same1(a) #a
#define same2(a) same1(a)
int main()
{
printf("%s\n",same2(concatinate(1,2)));
printf("%s\n",same1(concatinate(1,2)));
return 0;
}
Ans:



24)
The intention of the following program was to print 42 astericks('*')
But it fails to do so.
You have to add/replace/delete exactly one character in the program
to make it work ???
Find as many solutions as possible.

#include <stdio.h>
int main()
{
int n = 42;
for(int i = 0; i < n; i-- )
printf("*");
return 0;
}
Ans:























25)
What does this code do?
#include<stdio.h>
void insert(char s[],int c)
{
int i,j;
for(i=j=0;s[i]!='\0';i++)
if(s[i]!=c)
s[j++]= s[i];
s[j]='\0';
}

int main()
{
char s[]="helloworld";
insert(s,'l');
printf("%s",s);
return(0);
}
Ans:




26)
What is the output of the following code?
#include<stdio.h>
#include<string.h>
int s(char *A[20],char *B[20])
{
char *a,*b;
a=A;b=B;
while(*a++!=*b++); *a=*b='\0';
return strlen(A);
}
int main()
{
char A[20]="somestring",
B[20]="debugthecbug";
printf("%d %s %s\n",s(&A,&B),A,B);
return 0;
}
Ans:




27)
Can you predict the output ?
#include<stdio.h>
#define print(var) printf("%s : %d\n",#var,(var))
int main()
{
int y = 100;
int *p;
p = new int;
*p = 10;
y = y/*p; /*dividing y by *p */;
print(y);
return 0;
}
Ans:



28)
Take the input as 4, and write the output of the following code.
#include <stdio.h>
main()
{
int i=0;
printf("%d %d\n",scanf(" %d",&i),i);
printf("%d %d\n",i=4,i);
}
Ans:



29)
Predict the output of the following program.
#include <stdio.h>
int main()
{

int a=3, b = 5;
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c\n%c%c\n%c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);

return 0;
}
Ans:





30)
What is the output of the following code?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
return(0);
}
Ans:




31)
What is the output of the following piece of code?
#include<stdio.h>
int main(int k)
{
if(k<10)
printf("%d ",main(k+1));
return k;
}
Ans:





32)
Predict the output or error if any in the following code.
#include<stdio.h>
main()
{
int i = 4;
printf("%d %d %d",++i,i++,i*i);
printf("\n");
printf("%d %d %d",i*i,++i,i++);
system("PAUSE");
}
Ans:



33)
Give the output of the following piece of code.
#include<stdio.h>
int main()
{
int a,b,c=2,d=10;

printf("%d\n ",scanf("%d%d%d",&a,&b,&c,&d) ) ;

printf("%d\n%d\n%d\n%d\n",a,b,c);
return(0);
}
Ans:



34)
#include <stdio.h>
main()
{
int a[15][10][5]={1};
func(a);
}
Which of these is/are correct function prototypes for func ?

void func( int a[][10][5] );
void func( int a[15][][5] );
void func( int a[15][10][] );
Ans:





35)
Give the output of the following piece of code.
#include <stdio.h>
main()
{
float x=3.14;
printf("%e, %E, %f",x,x,x);
}
Ans:



36)
What is the output of the following program?
#include<stdio.h>
int fun1()
{
static int c=20;
return --c;
}

int fun2()
{
static int c=1;
return fun1()+c--;
}

int main()
{
int i=0;
while(i<fun2())
printf("%d ",i++);
return 0;
}
Ans:



37)
What is the output of the following program?
#include<stdio.h>
#include<string.h>
void p(char *a)
{
static int y=1;
if(y=1-y) printf("%c",*a);
return;
}

int main()
{
char *a;a=(char*)malloc(20*sizeof(char));
strcpy(a,"DbugtheCbug");
while(*a!='\0') p(a++);
printf("\n");
return 0;
}
Ans:



38)
Give the output of the following piece of code.
#include <stdio.h>
main()
{
int a=4,b=10;

printf("%d %d %d %d\n",a,a^=b=b^=a=a^=b,b,printf("%d %d %d\n",b,a,a^=b=b^=a=a^=b));

}
Ans:




39)
Predict the output of the following program.
#include<stdio.h>
int main()
{
int x = 5,p = 10;
printf("%*d",x,p);
}
Ans:




40)
What is the output of the following code?
#include<stdio.h>
int main()
{
int i=2;
i=i++;
printf("%d ",i++ + i++);
}
Ans:




41)
Predict the output for the below code.
#include<stdio.h>
int main()
{
int a[]={2,3,4,5,6};
int i=0;
printf("%d",a[i++]+i[a+1]);
return(0);
}
Ans:



42)
What is the output of the following piece of code?
#include<stdio.h>
#include<stdlib.h>
void weird(int *a)
{
a=(int*)malloc(sizeof(int));
}
int main()
{
int *a;
weird(a);
*a=6;
printf("%d\n",*a);
return(0);
}
Ans:



43)
What is the output of the following piece of code?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",*ptr++);
return(0);
}
Ans:




44)
Correct the following code in case of any errors.
#include <stdlib.h>
#include <stdio.h>
#define SIZE 15
int main()
{
int *a, i;
a = (void*)malloc(SIZE*sizeof(int));
for (i=0; i<SIZE; i++)
*(a + i) = (int)i * i;
for (i=0; i<SIZE; i++)
printf("%d\n", *a++);
free(a);
return 0;
}
Ans:




45)
What is the output of the following program?
#include<stdio.h>
int main()
{
char c;
c=255;
printf("%d",c);
return(0);
}



click here for solutions

22 comments:

  1. please some one post the answers for the above questions or send the answers to my my mail dilipkumarraju@gmail.com.

    ReplyDelete
  2. hi... sorry for taking time..
    but the answers shall be put at the latest in another 4 days...

    ReplyDelete
  3. Hi Pradeep,

    can you put answers to the above post as soon as you can.

    Thanks,
    Chaitanya

    ReplyDelete
  4. I have put the link for solutions..
    And any queries about the solutions please post in the comments section..

    ReplyDelete
  5. plzz xplain this ...
    What is the output of the following code?

    int main()
    {
    int a=1,b=3,c,d;
    c=(a,b);
    d=a,b,c;
    printf("%d %d",c,d);
    return 0;
    }

    ReplyDelete
  6. thank u very much... by dinakaran GCT

    ReplyDelete
  7. if some one has good quation for c programming
    than post the quation to me at
    nikhilnico@gmail.com

    ReplyDelete
  8. can i get it solved???plz post answers

    ReplyDelete
  9. What does quotations marks in a cout statement mean?

    ReplyDelete
  10. plz post me solution on my mail id megha.kapalia@gmail.com

    ReplyDelete
  11. Awesome selection of questions. Good work dude.

    ReplyDelete
  12. May u plz send me the complete solutions of the questions before yesterday next day is my interview plz plz......mail id is monikatewari12@gmail.com

    ReplyDelete
  13. I am not a programmer but I have this C language subject this session and have to prepare for it. What all topics should be covered in it?
    And has anyone studied from this course http://www.wiziq.com/course/2118-learn-how-to-program-in-c-language of C tutorial online?? or tell me any other guidance...
    would really appreciate help

    ReplyDelete
  14. #include
    #include
    int main()
    {
    char *ptr="good behaviour";
    printf("%s\n",ptr);
    printf("%c\n",*ptr);
    printf("%s\n",ptr++);
    printf("%c\n",*ptr++);
    printf("%s\n",++ptr);
    printf("\n");
    system("PAUSE");
    return 0;
    ???

    ReplyDelete
  15. Thanks for your ideas. You can also find the details on Affity Solutions, at the C++ Developers. The main object of the Affity Solutions is to provide quality web services and is among the few software development

    company in Nagpur.

    ReplyDelete