Sei sulla pagina 1di 15

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; Is this different from

x=x*(y+1);

--------------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); }

1. What is the output of this statement ? Printf(%d,printf(%d %d,2,2) & printf(%d %d , 2, 2)); a. 22222 b. 22221 c. It will give an error during compilation 2. What is the output of this code snippet
main() {

int *p[10]; printf("%d %d\n",sizeof(*p),sizeof(p)); }

3. Function inlining is best used when a. In a small recursive function b. In large function where number of variables used is small c. In a large function where there are many loops and number of variables used is small d. None of these 4. If there is a large quantum in round robin it will be equivalent to a. First come first serve b. Shortest job first c. Least recently used d. None of these 5 . which of the following will lead to starvation a. Fifo b. Shortest job first c. Round robin d. Least recently used 6 . if the address space is 192.168.36.16/28 which of the following is the broadcast ip a. 192.168.36.0 b. 192.168.36.1 c. 192.168.36.255 d. 192.168.36.31 7. if there are 9 yellow balls, 3 red balls and 2 green balls. What is the probability that the second ball picked is yellow given the first ball is yellow a. 8/13 b. 9/13 c. 8/14 d. 9/14 8. How many processes are created in this snippet? Main() { Fork(); Fork() && fork () || fork (); Fork (); }

a. 15 b. 19 c. 21 d. 27 e. 31 9. which of the following is TRUE about the declaration const char * p a. the pointer cannot be changed but the value to which it points can be changed b. the value is constant but the pointer can be changed c. neither the value nor the pointer can be changed d. none of these 10. If F and L are the pointers to the first and last elements in a linked list, then which of the following operations is dependent on the length of the list? a. delete the first element in the list b. insert a new element as a first element c. delete the last element of the list d. add a new element at the end of the list

1)How do we check whether a linked list is circular? 2)What is the output of this program? #include ##include main() { typedef union { int a; char b[10]; float c; } Struct; Struct x,y = {100}; x.a = 50; strcpy(x.b,"hello"); x.c = 21.50; printf("Union x : %d %s %f \n",x.a,x.b,x.c ); printf("Union y : %d %s %f \n",y.a,y.b,y.c); } 3)What is a Null object?

4)Name some pure object oriented languages. 5)What is a container class? What are the different types of container classes? 6)What will be the output of the following program: #include main() { printf("%x",-1<<4); } 7)What are the major differences between C and C++? 8)What is an incomplete type? 9)What are printf and scanf, call by reference or call by value? 10)What is a const pointer? 11)When should a type cast be used and when should it not be used? 12)Which is the easiest sorting method? 13)Which is the quickest sorting method? 14)Which are the best sorting algorithms to sort a linked list? 15)What is a preprocessor and what will it do for a program? 16)How can a string be converted into a number? 17)How can a number be converted into a string? 18)What is a heap? 19)Why does n++ execute much faster than n+1? 20)What is modular Programming? 21)Which expression always returns true and which expression always returns false? 22)What is the difference between "malloc" and "calloc"? 23)Is C a)A low level language b)A middle level language

c)A high level language ? 24)Why doesn't C support function overloading? 25)What is the difference between global int & static int declaration?

Flex Your Brain-PUZZLE 1 December 29, 2010 Category : Training & Placements
1:A man brought some watermelons to town and sold them. he sold 1/2 more than 1/2 of what he brought and he was left with one melon. how many melons did he bring to town? Ans: 3 2: The reverse of my age is my son's age. One year back, my age was twice my son's age. what are the current ages of father and son? Ans: 73 & 37 3. A husband's watch runs 1 min fast every hour, while his wife's runs 2 minutes slow. How many hours have past, after they set their watches to the same time, if the difference in time between the watches is 1 hour? Ans: 20 hours. diff btwn my watch and hub= 2-(-1)=3 3 mts diff for 1hr 60 mts diff is (60*1)/3

4: There is a circular ring in which there are 12 black mice and one white mice . a cat walks circularly in the ring and eats every 12th mice. where should the cat start so that the white mice is the last one to be eaten by cat? Ans: if the cat moves circularly it has to start from the 11th mice (clockwise) w.r.t. to white one.

5. A ,B ,c are friends they went by cycle and weared a hat. each one weared a hat of one friends and ride the bicycle of another friend. A is wearing C's hat find who is riding a's cycle. Ans: C Sol: if A is wearing c's hat then it will be riding b's cycle. if so B will be wearing A's hat no other way since A's wearing c's hat and riding c's cycle. then remaining c will be wearing b's hat and riding A's cycle

6: In a class there are less than 500 students . when it is divided by 3 it gives a whole number. Similarly when it is divided by 4,5 or 7 gives a whole number. find the no. of students in the class Ans: 420

7: There are three types of birds A,B & C . A costs 5pounds, B costs 3 Pounds and C costs 1/3 of a pound. Find the no. of A,B &C such that u will get 100 birds for 100 pounds. (I think we were suppose to find 3 answers since there were 3 rows in the answer) Ans: A : 4 , B : 18 ,C: 78

8: There are 5 persons who have won top five places in an event in Olympics . one of them asks all the five regarding thier positions, they reply as:

A: "i am not the last" B: "c is in third place" C: "E is behind A" D: "B is in first place" E: "D is not the first" the persons who have won gold and silver have lied .find the positions in order(format: name of first, name of second,..) Ans: B,C,D,A,E

9: A coffee seller has two types of coffee Brand A costing 5 bits per pound and Brand B costing 3 bits per pound. he mixes two brands to get a 40 pound mixture. he sold this at 6 bits per pound. the seller gets a profit of 33 1/2 percent. how much he has used Brand A in the mixture? Ans: 30 pounds

10. Six men decide to cut logs in 1/2 yards for a fire. The men are A, B, C, D, E, F ( these are all their first names). They work in pairs. with A, C and E being leaders of each pair. A and B cut 1 yard logs, C and D cut 2 yard logs and E and F cut 1yard logs. J and K manage to make 26 logs, L and M 27 logs and N and O 28 logs (these are all their last names). The headmaster congratulates J,L and N on their fine leadership. What is B's last name? ( Note actual names were different, some confusing russian names ) Ans: B's last name is M. When you cut 1 and a half logs into half logs you get three half logs. Only 27 is a multiple of 3, and A is a captain.

Potrebbero piacerti anche