Вы находитесь на странице: 1из 627

1 1) ans::b 2) what is the postfix of the given infix expression (a+b)*c/d-e+f ans::ab+c*d/e-f+ 3) void main() { int i,*j; i=35;

??????//j=&i; printf("%d",*j); } To get the 35 as the output,which statement is replaced by ?????? To print a single character in ouptut,which function is used? a) getchar() b) gets() c) putchar() d) puts() ans::c The effective way of sorting is done by _______________ a) single linked list b) arrays c) Doubly linked list d) Binary search trees ans::c or d?? void main() { struct emp { int a; struct emp b; }; struct emp m; printf("%d",sizeof(m)); } ans::error If you dont know the array size which one do you prefer? a) array b) linked list

4)

5)

6)

7)

#define t 10 void main() { printf("%d",t); } a) 10 b) Error:Unfined symbol 't' c) Error:Improper placement of preprocessor d) none of the above ans::a data structures used for stack a) array b) linked list c) both d) none ans::c Explicit datatype conversion is called a) Type casting b) conversion c) separation d) none ans::a what is the size of char *a,int *b,float *q,if the size of the double pointer is 2 bytes a) 1 1 1 b) 2 2 2 c) 4 4 4 d) 1 2 4 ans::b The data structure used for searching is a) arrays b) single linked list c) double linked list D) Binary search trees ans::d For what purpose queues are used? i) expression evaluation ii) device queue for processes a) i only b) ii only c) both i and ii d) neither i nor ii ans::b

8)

9)

10)

11)

12)

13)

The data structure used for sorting without moving the data is a) arrays b) single linked list c) double linked list D) Binary search trees ans::d struct stack { int a[10]; int top; }; struct stack *s;

14)

How will add an element called 'item' in top the stack? ans::s->a[s->top]=item 15) predict the ouput strcut emp { char name[50]; struct emp e; }*oemp; oemp->name="wipro"; ans::error 16) Predict the output struct a { int x; }e1; struct b { int y; }e2; if(e1==e2)//structure cannot be comapred simplys printf("Equal"); else printf("Unequal");

17)

ans::error main() { int i,j; for(i=0;i<2;i++) { for(j=0;j<5;j++) { if(i==j) continue; printf("wipro"); } } }

//wipro would be printed 8 times

18)

which one is used to get a multiword string? a) gets b) printf c) puts d) getchar ans::a. main() { char *a="\12345\n"; printf("%d",sizeof(a)); } ans::2

19)

20)

21)

int fun() { int k=10; return(&k); } main() { int *p; p=fun(); printf("%d",*p); } ans::10 i) char *str="welcome"; ii) char str[50]="welcome";

5 for these 2 definitions will they give you the same output for the given statement? printf("%c",str[3]); yes 22) void fun() { return(a>100?100:200); } void main() { printf("%d",fun(5)); } ans::error void disp(int a,int b) { return(a>b?a+b:a*b); } void main() { printf("%d",,disp(5,6)); } ans::error

23)

1) void main() { int i=0; while(i==0) { } printf("%d",i); } ans:: no output//infinte loop 2) void main() { int i=1; while(++i==1) {} printf("%d",i); }

6 ans::2 char s[]={'a','b','c','\0'}; printf("%s",s); output::abc int arr[3][3]={2,3,4, 5,6,7, 8,9,10}; for(i=0;i<=2;i++) for(j=0;j<=2;++j) printf("%d",*(*(arr+i)+j)); //*(arr[i]+j)--arr[i][j] output=2345678910 5) int b=20; printf("%d",++*&b); ans::21 which can be replaced by a switch block? a) do..while b) for c) while d) else if ans::d which of these is not a operator in c? a) ~ b) ^ c) % d) :: ans: d:: which of these is not a valid character constant a) "A" b) '*' c) '+' d) 'h' ans::a struct student { int i; char c[10]; struct student *s; }; struct student stud,*stu; stu=&stud; printf("%d %d",sizeof(stud)); ans::14

3)

4)

6)

7)

8)

9)

10)

insert(root,2); insert(root,1); insert(root,3); insert(root,4); insert(root,5); What is the inorder for the above tree? ans::1,2,3,4,5 insert(root,2); insert(root,1); insert(root,3); insert(root,4); insert(root,5); What is the preorder for the above tree? ans::2,1,3,4,5 The postfix expression is ab+c*d/e-.If the values of a,b,c,d,e are 5,2,3,3,1 respectively,then what will be the output? ans::6 What is the order in which the preorder is evaluated? i) root node ii) left node iii) right node a) i,ii,iii b) ii,iii,i c) iii,ii,i d) ii,i,iii

11)

12)

13)

14)

ans::a which operations are more effective in linked list than arrays? i) insertion ii) deletion iii) traversal a) i,ii only b) iii only c) all the above d) i,iii only

ans::a 15) what is the output of the following program? void main()

8 { int i=12; int *q; q=&i; *q=*q-8; printf("%d",i); } ans::4 what is the use of precedence in C programs for operators? a) Precendence are used to replace the variables used in operations b) precendence are used to allocate memory space c) precendence are used to evaluate the expression first d) None of the above ans::c

16)

17)

aaa(){ printf("Hai")}; bbb(){ printf("Hello")}; ccc(){ printf("Bye")}; void main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; } ans::nothing it will print to call function we sud write (*ptr[0])(); 18) In Unsigned char,signed value is represented by 1 in most significant bit. a) true b) false ans::b int i=1; switch(i*2) { case 1: printf("case1"); break; case 2: printf("case 2"); default: printf("default"); break; } a) case1 case2 default

19)

9 b) case2 c) case2 default d) compiler error ans::c struct node { int info; struct node *left,*right; }; if nd is a pointer which is pointing to the first node,then which one of the following is correct? a) nd->right = NULL; b) nd->left = NULL; c) nd = NuLL; d) nd->left->right = NULL; ans::b struct a { char *i; char *j; }; struct b { struct a x; int i; }*y; printf("%d %d",sizeof(y),sizeof(*y)); ans::2 6 #define t printf void main() { int i=10; t("%d",i); } ans::10 int a[3][4]={1,2,3,4, 5}; printf("%d",a[0][4]); ans::5 which one is used to check the given string is palindrome or not a) Single Linked list b) doubly linked list

20)

21)

22)

23)

24)

10 c) Arrays D) none of the above ans::c 25) #define HELLO hai void main() { printf("HELLO"); } ans::hello The formal arguments has a default value zero a) true b) false ans::b void fun() { ????? x++; printf("%d",x); } int x; void main() { x=7; printf("%d",x); x++; printf("%d",x); fun(); } if the outputs are 7,8,9,then which is replaced instead of ????? a) extern int x; b) auto int x; c) static int x; d) register int x; ans::a if rear=-1,front=-1 then what will be my queue? a) Queue is empty b) Queue is full c) Queue has one element D) Queue has max-1 element ans::a void main() { int i; for(i=1;i<=5;i++)

26)

27)

28)

29)

11 {} printf("%d",i); } ans::6 30) If p=(int *) malloc(sizeof(int)*10),then which one is true? i) It allocates 20 bytes of memory ii) The value in the memory is allocated to zero a) i only b)ii only c) both i and ii d) neither i nor ii ans::a If p=(int *) calloc(sizeof(int)*10),then which one is true? i) It allocates 20 bytes of memory ii) The value in the memory is allocated to zero a) i only b)ii only c) both i and ii d) neither i nor ii ans::c

31)

32) Predict the output int i=1; while(i<3) { switch(i) { case 1: printf ("Case 1"); break; case 2: printf ("case 2"); break; default: printf ("Default"); } i++: } ans::case1 case2 33) If rear=0 front=0 what is queue? ans::queue has one element 34) if rear=max, front=0, what is queue? ans::q is full 35) for (i=1; i=2; i++)

12 { printf ("HAI"); } ans::hai hai hai....infinite 36) *Consider a linked list where ptr is a pointer which is not pointing to the first node and not to the last node.If there is another node called "new" which is like new->next=NULL,then what is the scenario? a) It is the first node b) It is the last node c) It is in the middle d) none of these ans::b If 'a' is the integer which is not statically initialized then what is the value of 'a'? a) zero b) garbage c) none of these ans::b struct emp { int a=20;//intialization cannot be done here.. char name[10]="AAA"; }; void main() { struct emp oemp; printf("%s",oemp->name); printf("%d",oemp->a); } ans::error//if struct element cannot be intialized +oemp cannot use -> only ".". Which of the following is considered as tokens in C? a) Variables b) Keywords c) Constants d) All the above ans::d C is a ____________________ language a) Platform independent programming b) Platform dependent programming c) Object oriented programming d) None of the above ans::b main()

37)

38)

39)

40)

41)

13 { int i=3; switch(i) { default: printf("default"); case 1: printf("case 1"); case 2: printf("case 2"); break; } ans::default case1 case2 #define FIRST 1 #define SECOND FIRST+FIRST main() { printf("%d",SECOND * SECOND); } ans::3 int b[5]; main() { static int a[5]; int i; for(i=0;i<5;i++) printf("%d %d",a[i],b[i]); } output::o o o o o o o o o o which is having the more precendence a) * b)+ c)== d) ans::a main() { char *x="girl"; int n,i; n=strlen(x); *x=x[n];//g will be replaced by \0 for(i=0;i<n;i++) { printf("%s \n",x);// //irl//rl//r// x++; } } irl rl l

42)

43)

44)

45)

ans::

14

46)

int sample(int x) { int *y; y=&x; x=*y+2; return(x); } void main() { int x=5; printf("%d",sample(x)); } ans::7

47)

void main() { int *j,*k; int a=5,b=8; j=&a; k=&b; j=j*2; k=j/2; printf("%d %d",*j,*k); } ans::error//mul n div cannot be done on pointers To make a pointer 'cursor'as a tail node,which line is correct a) cursor->right==NULL b) cursor->left==NULL c) cursor==NULL d) cursor->left==0 ans::a Single linked list has a lnk to the next address a) true b) false ans::a int 5[x]={1,2,3,4,5}; printf("%d",2[x]); ans::error//declaration terminated incorrectly int arr[]={1,2,3,4,5}; printf("%d %d",arr,&arr);

48)

49)

50)

51)

15 garbage garbage//same both array statrting address 52) int a[3][4]={1,2,3,4, 5,6,7,8 ,9,0}; printf("%d %d",a,a[2][1]); ans::garbage(%u sud be used) 0 The argc,argv are initialised in a) header file b) within main c) outside main d) none ans::a Queue is i) First in first out ii) used in expression evaluation. a) i only b) ii only c) both i and ii d) neither i nor ii ans::a which is correct? a) Auto variables are local variables b) Extern variables are global variables c) static variables are global variables d) registers are local variables ans::b main() { int i=5; int *p; p=&5; printf("%d %d",i,*p); } ans::error//must take an adress of a variable int foo(int a,float b) { float c; c=(float)a+b; printf("%c",c); } main() { ????? foo(8,12.5);

53)

54)

55)

56)

57)

16 } which line is replaced for ?????? to get the ouput of 20.5 ans::nuthing//not reqd %f sud be used tats it 58) main() { float v; v=10/4.0; printf("%f",v); } ans::2.5000000 59) In a doubly linked list, p1 and p2 are the 2 nodes and node nd is inserted as nd->next=NULL nd->prev=p1->next ans::watever the question nd is last node till data provided void main() { int i=12,j,*p; p=&i; j=++*p+8; printf("%d",j); } ans::21

60)

1.int a=123.12; int b=-123.12; int c=a+b; printf("%2f",c); a) 0.00 b) 123.12 c) 246.24 d) None of these ans::none of these abnormal floating point 2. struct emp { int a; char *name; }; main() { struct emp *e; printf("%d",sizeof(e));

17 } ans::2 3. which is the best thing for linked list than arrays? i) Insertion ii) Deletion iii) Traversal a) (i) only b) (i),(ii) only c) ii,iii only d) iii only ans::b 4. consider the character array of size 10.When a string is more than 10 a) Error b) Overwrites c) Nothing d) garbage value ans::error/too many intializers ans::a 5. main() { char *str; char *fun(); str=fun(); printf("%s",str); } char * fun() { char *buffer; strcpy(buffer,"Hello world"); return buffer; } ans::runtime error //as buffer is lost when it goes out of a function bettet to use static char *buffer or malloc to allocate memory for buffer. a) hello world b) Compiler error c) Runtime error d) None of the above ans::c 6. main()

18 { char *str; char *fun(); str=fun(); printf("%s",str); } char * fun() { char *buffer; buffer=(char *) malloc(sizeof(char)); strcpy(buffer,"Hello world"); return buffer; } a) hello world b) Compiler error c) Runtime error d) None of the above ans::a 7) what is the prefix expression for the given Infix expression A+B*C/D ans::+a/*bcd 8) int a; a=65535; printf("%d",a); a) 0 b) Garbage c) 65535 d) -32768 output::garbage ans::to be precise will be -1//key here if no>+32767 then o/p is -(65536-number) and if no<-32768 then no=(65536+(-no)); 9) main() { char p=65; printf("%c",p); } a) 65 b) p c) error d) none of the above ans::none of the above//aoutput capital "A" 10) In a function call, _____________ is passed as arguments.

19 a) variables b) constants c) Expressions d) All the above all the above 11) The value of EOF is 26__-1_______. ascII=26 12) () is used for __________ a) function body b) Arguments c) Return type d) Declaration of function ans::b 13) How does the string ends? a) /n b) /t c) /0 d) none ans::none...\\it terminated by'\0'not/0 ok 14) The range of Unsigned integer is a) 127 to -128 b) 0 to 65535 c) Depend up on the compiler d) -32768 to 32767 ans::c 15) which one of the following is a illegal real constants a) 32.535 b) -1.5E25 to 3.5E65 c) "53682" d) -6.583 ans::"53682" 16) main() { int i,a[5]; a[1]=5; for(i=0;i<5;i++) { printf("%d",a[i]); a++; }

20 a) 0 0 0 0 0 b) Garbage value c) 0 5 0 0 0 d) Compiler error ans::d 17) The size of int is 2 bytes,then what is the size of short int? a) 1 b) 2 c) 4 d) 8 ans::b 18) In a queue,what condition applies a) First In First Out b) Last in first out c) Elements are inserted and deleted at one end d) Elements can be inserted and deleted in different ends ans::a 19) which of the following statements are true in the case of doubly linked list i) Every node is connected to other node ii) We can traverse in both the directions a) i) only b)ii) only c) Both i and ii d) neither i nor ii ans::c 20) main() { char str[]="Wipro Infotech"; char *s; s=&str[13]-13; while(*s) printf("%c",*s++); } ans::wipro infotech 21) char *a="Malayalam"; char *s; Char *str; str=a; s=str+8; for(;s>=str;s--) printf("%c",*s);

21 a) Malayalam b) malayalaM c) error d) None of the above ans::b 22) struct main { int a; int b; struct main *e1; }; printf("%d %d",sizeof(e1),sizeof(*e1)); ans::2 6 23) #define wipro Hai void main() { printf("wipro"); } ans::wipro 24) Is allocating a block of memory effectively the same as defining an array? a) True b) false ans::false 25) the size of a node of a doubly linked list is always greater than the single linked list a) True b) false ans::true 26) Queue is used for i) Expression Evaluation ii) Scheluding the job in First come First serve a) i) only b) ii only c) both i & ii d) neither i nor ii ans::b 27) what should be replace ????? in the program to get the output 25? ????? void main() { int x=2,y=3,j; j=SQRT(x+y);

22 printf("%d",j); } a) #define SQRT(int) (int * int) b) #define SQRT(int) (int) * (int) c) #define SQRT(int) (int + int) d) #define SQRT(int) (int) + (int) ans::b 28) void fun() { static char p[]="Hello"; return p; } main() { printf("%s",fun()); } what will be the output? a) Compiler Error b) Hello c) Garbage value d) None ans::a 29) void main() { int *p; p=(int *)malloc(sizeof(int)); for(i=0;i<5;i++) printf("%d ",p[i]); } What is the output? ans::garbage if calloc is used o/p is 00000 30) main() { int i,j; for(i=1,j=10;i<j;i++,j--); printf("%d %d",i,j); } ans::1 10,2 9,3 8,4 7,5 6 31) Which of these will pass the address of the pointer *ptr to the function demofun()? a) demofun(ptr) b) demofun(&ptr) c) demofun(*ptr) d) demofun(*&*ptr);

23 ans::b 32) which is not a valid expression a) x!=y b) x! c) !x d) x!y ans::x! and x!y 33) If max=10,rear=max,front=0,then what will be my queue? a) Queue Empty b) Queue Full c) Queue has one element d) Queue has max-1 elements ans::c or b?? 34) which is an indefinite loop? a) do while(0); b) for(;0;); c) while(1); d) while(0); ans::c 35) int *ptr; ptr=(int *)malloc(10*sizeof(int)); which is correct? i) All Values amory for 10 integer data a) i onlyre intialised to garbage values ii) Creates me b) ii only c) both i and ii d) neither i nor ii ans::c 36) int *ptr; ptr=(int *)calloc(10*sizeof(int)); which is correct? i) All Values are intialised to zero ii) Creates memory for 10 integer data a) i only b) ii only c) both i and ii d) neither i nor ii ans::b 37) Struct queue {

24 int rear; int front; int a[100]; } struct queue *q; then how will you add a new element called 'item' in the queue? a) q->rear[a]=item; b) q->a[q->rear]=item; c) q->a[rear]=item; d) q->rear[q->a]=item; ans::b 38) In which of the following we can sort the data without moving the data a) Array b) Single Linked list c) Doubly linked list d) Binary search trees ans::d 39) Char d=128; printf("%c",d); a)128 b)-128 c)error d)Garbage values ans::d 40) In the following definition struct node *ptr; ptr=(struct node *)calloc(sizeof(ptr)); a) ptr is allocated 4 bytes b) ptr will be allocated sizeof struct node c) Error d) ptr will have 8 bytes ans::a 41) In a doubly linked list ,if the first node is first and the last node is end, what will be the output? traverse(struct node*end) { while(end!=NULL)

25 traverse(end->prev); printf("%d",end->data); } if the input is 1,2,3,4,5 then the output will be a) 1,2,3,4,5 b) 5,4,3,2,1 c) compilation error d) none ans::b 42) void main() { int b=20; printf("%d"*&b++); } what will be the output? a) 21 b)20 c) error d) Garbage value ans::c//must take an adress of memory location *&b will work 43) how will you refer the last node in the doubly linked list which is pointed by the pointer variable 'cursor'? a)cursor==NULL b)cursor->link=NULL c)Cursor->link=0 d)cursor->data=NULL ans::b 44) how will you refer the previous node of the pointer 'cursor' in the doubly linked list (cursor is not in the first or in the last)? a)cursor->link++ b)cursor=cursor->left c) Cursor++ d) cursor->left++ ans::b

26 1)Consider the following structure struct node { int info; struct node *link; }; Suppose ptr is a pointer which is not pointing to the first or the last node.Then if we are going to delete a node after ptr, then the code will be a) ptr=ptr->link; b) ptr->link=ptr; c) ptr->link=ptr->link->link; d) ptr=ptr->link->link; ans::c

2) Consider the following structure struct node { int info; struct node *link; }; Suppose start is a pointer pointing to the first node of the linked list. s1 and ptr are the two pointers(they are not pointing to the first or last node).Then if we are going to execute the following code, i) start->link=s1; ii) s1->link=ptr; iii) ptr->link=start; then the list is a) It is having only 3 nodes with start,s1,ptr in the list,having start as the first node b) It is a circular linked list c) It is a doubly linked list d) None of the above ans::b 3) In a queue, if rear=front then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element

27 d) none of the above ans::c 4) In a queue,if rear=0,front=0 then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) none of the above ans::c 5) 4) In a queue,if rear=0,front=1 then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) Queue is circular ans::d 6) In a queue,if rear=-1,front=-1 then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) none of the above ans::a 7) In a queue,if rear=max-1,front=0 then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) none of the above ans::b 8) The postfix expression is ab+c*d/e-.The values of a,b,c,d,e are 2,4,5,2,1 respectively. Then the output is a) 14 b) 11 c) 20 d) 15 ans::a 9) The infix expression is a+b*(c-d)/(e+f)*h then my postfix expression is a) ab+cd-*ef+h*/ b) abcd-ef+*/h* c) abcd-*ef+/h*+ d) abcdef+-*/h*+ ans::c 10) In the stack,if top=0 then the stack is

28 a) stack is empty b) stack is full c) stack has only one element d) none of the above ans::c 11) Conside the structure struct node { int info; Struct node *left; struct node *right; }; We have 10 elements in the list.If the following executes what will be the output? for(ptr=start;ptr;ptr=ptr->right) { if(ptr->data%2==0) printf("%d",ptr->data); } a) Only even numbers are printed b) Only odd numbers are printed c) Compiler error d) Only garbage values ans::c /well if ptr is defined then even no are printed 12) Struct node { int data; struct node *left,*right; }; Suppose nd is a node which is not in the beginning and also not in the end. How will you delete a node after nd?

a) nd->right=nd->right->left;nd->right->left=nd->left->right; b) nd->right=nd->right->right;nd->right->left=nd; c) nd->right=nd->right->left;nd->right->left=nd->right; d) nd->right=nd->left->right;nd->left->right=nd; ans::b 13) Struct node { int data;

29 struct node *left,*right; }; Suppose nd is a node which is not in the beginning and also not in the end. How will you delete a node before nd?

a) nd->left=nd->right->left;nd->right->left=nd->left->right; b) nd->left=nd->right->right;nd->left->right=nd->right; c) nd->left=nd->left->left;nd->left->right=nd; d) nd->left=nd->left->right;nd->left->right=nd; ans::c 14) Struct node { int data; struct node *left,*right; }; Suppose ptr is a node which is not in the beginning and also not in the end. How will you delete a node ptr?

a) ptr->left->right=ptr->right;ptr->right->left=ptr->left;free(ptr); b) ptr->left->right=ptr->right->right;ptr->left->right=ptr->right;free(ptr); c) ptr->left->right=ptr->left->left;ptr->left->right=ptr;free(ptr); d) ptr->left->right=ptr->left;ptr->left->right=ptr->left;free(ptr); ans::a 15) Struct node { int data; struct node *left,*right; }; Suppose ptr is a node which is not in the beginning and also not in the end. nd is the new node. Here is the coding: i) nd->right->left=nd; ii) nd->left=ptr; iii) nd->left->right=nd; iv) nd->right=ptr->right; Then what sequence does it follows for inserting nd after ptr?

30 a) i,ii,iii,iv b) ii,iv,i,iii c)iv,iii,ii,i d) ii,iii,i,iv ans::b

16) In the Given Infix expression which is the root node for ur expression tree (A+B)-(C*D)+G/H*I a) + b) c) * d) / ans::a 17) Consider a binary search tree insert(10,root); insert(25,root); insert(5,root); insert(8,root); insert(13,root); insert(45,root); insert(70,root); insert(32,root); delete(13,root); insert(66,root); insert(13,root); insert(36,root); What will be the preorder traversal is

a) 5,8,10,13,25,32,36,45,66,70 b) 10,5,8,25,13,45,32,36,70,66 c) 10,8,5,13,32,45,36,66,32,70 d) 8,5,32,36,10,66,45,70,25,13 ans::b 18) The postoder traversal is 7,14,3,55,22,5,17 Then ur Inorder traversal is

31

a) 3,5,7,14,17,22,55 b) 14,55,5,7,22,17,3 c) 3,5,14,7,22,17,55 d) 55,22,17,14,7,5,3 ans::a 19) The preorder traversal is 5,3,66,30,77,70 .What will be the root node a) 5 b) 66 c)70 d)30 ans::5 20) which one of the following is true for the binary tree i) root is greater than the left sub tree and lesser than the right sub tree ii) root is lesser than the left sub tree and greater than the right sub tree ans::i a) only i b) only ii c) both i and ii d) neither i nor ii ans::a 1) void main() { int i=0; while(i==0) { } printf("%d",i); } ans::infinite loop 2) { int i=1; while(++i==1) { } printf("%d",i); } ans::2 3) char s[]={'a','b','c','\0'}; printf("%s",s); void main()

32 ans::abc 4) int arr[3][3]={2,3,4,5,6,7,8,9,10}; for(i=0;i<=2;i++) for(j=0;j<=2;++j) printf("%d",*(*(arr+i)+j)); ans::2 3 4 5 6 7 8 9 10 5) ans::21 6) which can be replaced by a switch block? a) do..while b) for c) while d) else if which of these is not a operator in c? a) ~ b) ^ c) % d) :: int b=20; printf("%d",++*&b);

ans::d 7)

ans::d 8) which of these is not a valid character constant a) "A" b) '*' c) '+' d) 'h' ans::a 9) struct student { int i; char c[10]; struct student *s; }; struct student stud,*stu; stu=&stud; printf("%d %d",sizeof(stud)); ans::14 10) insert(root,2); insert(root,1); insert(root,3); insert(root,4);

33 insert(root,5); What is the inorder for the above tree? ans::1 2 3 4 5 11) insert(root,2); insert(root,1); insert(root,3); insert(root,4); insert(root,5); What is the preorder for the above tree? ans::2 1 3 4 5 12) 13) The prefix expression is ab+c*d/e-.If the values of a,b,c,d,e are 5,2,3,3,1 respectively,then what will be the output? What is the order in which the preorder is evaluated? i) root node ii) left node iii) right node a) i,ii,iii b) ii,iii,i c) iii,ii,i d) ii,i,iii 14) which operations are more effective in linked list than arrays? i) insertion ii) deletion iii) traversal a)i,ii only b)iii only c) all the above d) i,iii only 15) what is the output of the following program? void main() { int i=12; int *q; q=&i; *q=*q-8; printf("%d",i); }

34

16)

what is the use of precedence in C programs for operators? a) Precendence are used to replace the variables used in operations b) precendence are used to allocate memory space c) precendence are used to evaluate the expression first d) None of the above

17)

aaa(){ printf("Hai")}; bbb(){ printf("Hello")}; ccc(){ printf("Bye")}; void main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; } In Unsigned char,signed value is represented by 1 in most significant bit. a) true b) false int i=1; switch(i*2) { case 1: printf("case1"); break; case 2: printf("case 2"); default: printf("default"); break; } a) case1 case2 default b) case2 c) case2 default d) compiler error struct node { int info; struct node *left,*right;

18)

19)

20)

35 }; if nd is a pointer which is pointing to the first node,then which one of the following is correct? a) nd->right = NULL; b) nd->left = NULL; c) nd = NuLL; d) nd->left->right = NULL;

21)

struct a { char *i; char *j; }; struct b { struct a x; int i; }*y; printf("%d %d",sizeof(y),sizeof(*y)); #define t printf void main() { int i=10; t("%d",i); } int a[3][4]={1,2,3,4,5}; printf("%d",a[0][4]); which one is used to check the given string is palindrome or not a) Single Linked list b) doubly linked list c) Arrays D) none of the above #define HELLO hai void main() { printf("HELLO");

22)

23)

24)

25)

36 } 26) The formal arguments has a default value zero a) true b) false void fun() { ????? x++; printf("%d",x); } int x; void main() { x=7; printf("%d",x); x++; printf("%d",x); fun(); } if the outputs are 7,8,9,then which is replaced instead of ????? a) extern int x; b) auto int x; c) static int x; d) register int x; if rear=-1,front=-1 then what will be my queue? a) Queue is empty b) Queue is full c) Queue has one element D) Queue has max-1 element void main() { int i; for(i=1;i<=5;i++) {} printf("%d",i); } If p=(int *) malloc(sizeof(int)*10),then which one is true? i) It allocates 20 bytes of memory ii) The value in the memory is allocated to zero

27)

28)

29)

30)

37 a) i only b)ii only c) both i and ii d) neither i nor ii 31) If p=(int *) calloc(sizeof(int)*10),then which one is true? i) It allocates 20 bytes of memory ii) The value in the memory is allocated to zero a) i only b)ii only c) both i and ii d) neither i nor ii

32) Predict the output int i=1; while(i<3) { switch(i) { case 1: printf ("Case 1"); break; case 2: printf ("case 2"); break; default: printf ("Default"); } i++: } 33) If rear=0 front=0 what is queue? 34) if rear=max, front=0, what is queue? 35) for (i=1; i=2; i++) { printf ("HAI"); } 36) Consider a linked list where ptr is a pointer which is not pointing to the first node and not to the last node.If there is another node called "new" which is like new->next=NULL,then what is the scenario?

38 a) It is the first node b) It is the last node c) It is in the middle d) none of these 37) If 'a' is the integer which is not statically initialized then what is the value of 'a'? a) zero b) garbage c) none of these struct emp { int a=20; char name[10]="AAA"; }; void main() { struct emp oemp; printf("%s",oemp->name); printf("%d",oemp->a); } Which of the following is considered as tokens in C? a) Variables b) Keywords c) Constants d) All the above C is a ____________________ language a) Platform independent programming b) Platform dependent programming c) Object oriented programming d) None of the above main() { int i=3; switch(i); { default: printf("default"); case 1: printf("case 1"); case 2: printf("case 2"); break;

38)

39)

40)

41)

39 } 42) #define FIRST 1 #define SECOND FIRST+FIRST main() { printf("%d",SECOND * SECOND); } int b[5]; main() { static int a[5]; int i; for(i=0;i<5;i++) printf("%d %d",a[i],b[i]); } which is having the more precendence a) * b)+ c)== d) main() { char *x="girl"; int n; n=strlen(x); *x=x[n]; for(i=0;i<n;i++) { printf("%s \n",x); x++; } } int sample(int x) { int *y; y=&x; x=*y+2; return(x); } void main() { int x=5;

43)

44)

45)

46)

40 printf("%d",sample(x)); }

47)

void main() { int *j,*k; int a=5,b=8; j=&a; k=&b; j=j*2; k=j/2; printf("%d %d",*j,*k); }

48)

To make a pointer 'cursor'as a tail node,which line is correct a) cursor->right==NULL b) cursor->left==NULL c) cursor==NULL d) cursor->left==0 Single linked list has a lnk to the next address a) true b) false int 5[x]={1,2,3,4,5}; printf("%d",2[x]); error int arr[]={1,2,3,4,5}; printf("%d %d",arr,&arr); int a[3][4]={1,2,3,4,5,6,7,8,9,0}; printf("%d %d",a,a[2][1]); The argc,argv are initialised in a) header file b) within main c) outside main

49)

50)

51)

52) 53)

41 d) none ans:: 54) Queue is i) First in first out ii) used in expression evaluation. a) i only b) ii only c) both i and ii d) neither i nor ii which is correct? a) Auto variables are local variables b) Extern variables are global variables c) static variables are global variables d) registers are local variables main() { int i=5; int *p; p=&5; printf("%d %d",i,*p); } int foo(int a,float b) { float c; c=(float)a+b; printf("%c",c); } main() { ????? foo(8,12.5); } which line is replaced for ?????? to get the ouput of 20.5 main() { float v; v=10/4.0; printf("%f",v);

55)

56)

57)

58)

42 } ans::2.500000 59) In a doubly linked list, p1 and p2 are the 2 nodes and node nd is inserted as nd->next=NULL nd->prev=p1->next

60)

void main() { int i=12,j,*p; p=&i; j=++*p+8; printf("%d",j); } ans::21 1) void main() { int i=0; while(i==0) { } printf("%d",i); } output::infinet loop 2) { int i=1; while(++i==1) {} printf("%d",i); } 3) char s[]={'a','b','c','\0'}; printf("%s",s); int arr[3][3]={2,3,4,5,6,7,8,9,10}; for(i=0;i<=2;i++) void main()

4)

43 for(j=0;j<=2;++j) printf("%d",*(*(arr+i)+j)); 5) int b=20; printf("%d",++*&b); which can be replaced by a switch block? a) do..while b) for c) while d) else if which of these is not a operator in c? a) ~ b) ^ c) % d) :: which of these is not a valid character constant a) "A" b) '*' c) '+' d) 'h' struct student { int i; char c[10]; struct student *s; }; struct student stud,*stu; stu=&stud; printf("%d %d",sizeof(stud)); insert(root,2); insert(root,1); insert(root,3); insert(root,4); insert(root,5); What is the inorder for the above tree?

6)

7)

8)

9)

10)

44 11) insert(root,2); insert(root,1); insert(root,3); insert(root,4); insert(root,5); What is the preorder for the above tree? The prefix expression is ab+c*d/e-.If the values of a,b,c,d,e are 5,2,3,3,1 respectively,then what will be the output? What is the order in which the preorder is evaluated? i) root node ii) left node iii) right node a) i,ii,iii b) ii,iii,i c) iii,ii,i d) ii,i,iii 14) which operations are more effective in linked list than arrays? i) insertion ii) deletion iii) traversal a)i,ii only b)iii only c) all the above d) i,iii only 15) what is the output of the following program? void main() { int i=12; int *q; q=&i; *q=*q-8; printf("%d",i); } 16) what is the use of precedence in C programs for operators? a) Precendence are used to replace the variables used in operations b) precendence are used to allocate memory space

12) 13)

45 c) precendence are used to evaluate the expression first d) None of the above

17)

aaa(){ printf("Hai")}; bbb(){ printf("Hello")}; ccc(){ printf("Bye")}; void main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; } In Unsigned char,signed value is represented by 1 in most significant bit. a) true b) false int i=1; switch(i*2) { case 1: printf("case1"); break; case 2: printf("case 2"); default: printf("default"); break; } a) case1 case2 default b) case2 c) case2 default d) compiler error struct node { int info; struct node *left,*right; }; if nd is a pointer which is pointing to the first node,then which one of the following is correct? a) nd->right = NULL;

18)

19)

20)

46 b) nd->left = NULL; c) nd = NuLL; d) nd->left->right = NULL;

21)

struct a { char *i; char *j; }; struct b { struct a x; int i; }*y; printf("%d %d",sizeof(y),sizeof(*y)); #define t printf void main() { int i=10; t("%d",i); } int a[3][4]={1,2,3,4,5}; printf("%d",a[0][4]); which one is used to check the given string is palindrome or not a) Single Linked list b) doubly linked list c) Arrays D) none of the above #define HELLO hai void main() { printf("HELLO"); } The formal arguments has a default value zero a) true

22)

23)

24)

25)

26)

47 b) false 27) void fun() { ????? x++; printf("%d",x); } int x; void main() { x=7; printf("%d",x); x++; printf("%d",x); fun(); } if the outputs are 7,8,9,then which is replaced instead of ????? a) extern int x; b) auto int x; c) static int x; d) register int x; if rear=-1,front=-1 then what will be my queue? a) Queue is empty b) Queue is full c) Queue has one element D) Queue has max-1 element void main() { int i; for(i=1;i<=5;i++) {} printf("%d",i); } If p=(int *) malloc(sizeof(int)*10),then which one is true? i) It allocates 20 bytes of memory ii) The value in the memory is allocated to zero a) i only b)ii only c) both i and ii d) neither i nor ii

28)

29)

30)

48

31)

If p=(int *) calloc(sizeof(int)*10),then which one is true? i) It allocates 20 bytes of memory ii) The value in the memory is allocated to zero a) i only b)ii only c) both i and ii d) neither i nor ii

32) Predict the output int i=1; while(i<3) { switch(i) { case 1: printf ("Case 1"); break; case 2: printf ("case 2"); break; default: printf ("Default"); } i++: } 33) If rear=0 front=0 what is queue? 34) if rear=max, front=0, what is queue? 35) for (i=1; i=2; i++) { printf ("HAI"); } 36) Consider a linked list where ptr is a pointer which is not pointing to the first node and not to the last node.If there is another node called "new" which is like new->next=NULL,then what is the scenario? a) It is the first node b) It is the last node c) It is in the middle d) none of these

49

37)

If 'a' is the integer which is not statically initialized then what is the value of 'a'? a) zero b) garbage c) none of these struct emp { int a=20; char name[10]="AAA"; }; void main() { struct emp oemp; printf("%s",oemp->name); printf("%d",oemp->a); } Which of the following is considered as tokens in C? a) Variables b) Keywords c) Constants d) All the above C is a ____________________ language a) Platform independent programming b) Platform dependent programming c) Object oriented programming d) None of the above main() { int i=3; switch(i); { default: printf("default"); case 1: printf("case 1"); case 2: printf("case 2"); break; } #define FIRST 1 #define SECOND FIRST+FIRST

38)

39)

40)

41)

42)

50 main() { printf("%d",SECOND * SECOND); } 43) int b[5]; main() { static int a[5]; int i; for(i=0;i<5;i++) printf("%d %d",a[i],b[i]); } which is having the more precendence a) * b)+ c)== d) main() { char *x="girl"; int n; n=strlen(x); *x=x[n]; for(i=0;i<n;i++) { printf("%s \n",x); x++; } } int sample(int x) { int *y; y=&x; x=*y+2; return(x); } void main() { int x=5; printf("%d",sample(x)); }

44)

45)

46)

51

47)

void main() { int *j,*k; int a=5,b=8; j=&a; k=&b; j=j*2; k=j/2; printf("%d %d",*j,*k); }

48)

To make a pointer 'cursor'as a tail node,which line is correct a) cursor->right==NULL b) cursor->left==NULL c) cursor==NULL d) cursor->left==0 Single linked list has a lnk to the next address a) true b) false int 5[x]={1,2,3,4,5}; printf("%d",2[x]); int arr[]={1,2,3,4,5}; printf("%d %d",arr,&arr); int a[3][4]={1,2,3,4,5,6,7,8,9,0}; printf("%d %d",a,a[2][1]); The argc,argv are initialised in a) header file b) within main c) outside main d) none Queue is

49)

50)

51)

52) 53)

54)

52 i) First in first out ii) used in expression evaluation. a) i only b) ii only c) both i and ii d) neither i nor ii 55) which is correct? a) Auto variables are local variables b) Extern variables are global variables c) static variables are global variables d) registers are local variables main() { int i=5; int *p; p=&5; printf("%d %d",i,*p); } int foo(int a,float b) { float c; c=(float)a+b; printf("%c",c); } main() { ????? foo(8,12.5); } which line is replaced for ?????? to get the ouput of 20.5 main() { float v; v=10/4.0; printf("%f",v); }

56)

57)

58)

59)

In a doubly linked list, p1 and p2 are the 2 nodes and node nd is inserted as

53 nd->next=NULL nd->prev=p1->next

60)

void main() { int i=12,j,*p; p=&i; j=++*p+8; printf("%d",j); }

1. What is the output of the following: unsigned i=32768; void main() { printf("%d",i); } a. 32768 Ans::b 2. int i; void main() { static int i=3; printf("%d",i); } a. b. c. d. 3 Multiple declaration (since I is static) 0 None of the above Ans::3 3. What is the output of the following: main() { void change(char *); char *t="test"; change(t); printf("%s",t); } b. -32768 c. Error d. None of the above

54 void change(char *t) { char *ab="new test"; *t=*ab; } a. new test b. test

c. nest

d. None of the above

4. What would be printed: #include<stdio.h> int i=0; void main() { while(i) { switch(i) { case 3<2:printf("Hi"); break; case 3>2:printf("Hello"); break; default: printf("welcome"); } ++i; } } a. Hi Ans::c b. Welcome c. No output d. Error

55 # include<stdio.h> int get(); void main() { int x=20; x=get(); printf("%d ",x); } int get() { return(60); } a. 60

b. Garbage

c. Error

d. 20

5. swap(int *a,int *b) { int *t; t=a; a=b; b=t; } void main() { int a=10,b=20; swap(&a,&b); printf(%d %d,a,b); } a. 10 20 Ans::a 7. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*++p; printf("%s %s",p,p1); } a. ibj!gsjfoet ans::d b. hbj!gsjfoet c. hbj!gsjfoet hbj!gsjfoet d. None b. 20 10 c. Error d. Garbage value

56

8. main() { char a[100]={abcdef}; a++; printf(%s,&a[1]); } a) bcdef b) abcdef c)compilation error d) none of the above

9. When fopen() fails to open a file it returns _______ a. NULL b. -1 c. 1 d. None of the above

57 10. int i=5; fun( ) { printf("%d\n", i * 3); } main( ) { int i= 2; { int i = 3; printf(" %d", i); fun(); } } a. 3, 15 11. main() { static int i=3; printf("%d",i--); return i>0?main():0; } a. 3 2 1 0 b. 3 2 1 c. 2 1 0 d. 2 1 b. 3, 6 c. 3 d. 0

12. P is a character pointer variable then, what will be the output of the following statement. printf("%d %d",sizeof(p),sizeof(*p)); a. 1 2 b. 2 1 c. 2 2 d. 1 1

13. void main() { char *s[]={"dharma","hewlet-packard","siemens","ibm"}; char **p; p=s; printf("%s",++*p); printf("\n%s",*p++); printf("\n%s",++*p); } a. dharma harma ewlet-packard b. harma hewlet-packard

58 siemens c. harma harma hewlet-packard d. harma harma ewlet-packard

59 14. void main() { char *ptr="Ramco Systems"; (*ptr)++; printf("%s\n",ptr); ptr++; printf("%s",ptr); } a. Samco Systems Samco Systems c. amco Systems amco Systems 15. #include <stdio.h> main() { switch (5) { case 5: printf(" 5 "); default: printf(" 10 "); case 6: printf(" 6 "); } } A. 5 B. 5 10 6 C. 5 10 D. 5 6 b. Samco Systems amco Systems d. amco Systems mco Systems

16. Which of the following is not a storage class in C? A. Stack B. Register C. Extern D. Static

17. Which of the following function does not return an integer value? A. printf B. scanf C. strcpy D. strlen

18. int i=5; int abc(int z) { return i/2; } main() { int i=4; printf("%d",abc(i=i/4)); } a) error b) 5 c) 2 d) 0

60 19. What will be the output of the following program : int main() { int val=5; val=printf("C") + printf("Skills"); printf("%d",val); return(0); } (a) 7 (b) C7 (c) Compile-Time Error (d) CSkills7

61 20. #include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } a. 3 hello b. Compiler Error c. Run time error d. use dot (.) operator

21. int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); } a. 20 10 b. 10 20 c. 20 20 d. 10 10 22. main() { char *p = ayqm; char c; c = ++*p++; printf(%c,c); } a) a 23. main() { float i=1.5; switch(i) { case 1: printf("1"); case 1.5: printf("2"); default : printf("0"); } b) b c) y d) z

62 } a. 0

b. 0 1 2

c. 1 2 0

d. Compiler Error

e. 2 0

24. If the CPU fails to keep the variables in CPU registers, in that case the variables are assumed a) static b) external c) global d) auto d. None of the above

25. The EOF is equivalent to a. -1 b. 1 c. 0

26. In a queue, if rear=max-1, front=0 then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) None of the above 27. The postfix expression is ab-cd+*ef/-.The values of a, b, c, d, e,f are 4, 2, 5, 2, 6, 3 respectively. Then the output is a) -6 b) 12 c) 20 d) None d) None

28. In the stack, if top=0 then the stack is a) Stack is empty b) Stack is full c) Stack has only one element 29. struct node { int data; struct node *left,*right; };

nd is a node which is not in the beginning and also not in the end. How will you remove a node after nd from the list? a) nd->right=nd->right->left;nd->right->left=nd->left->right; b) nd->right=nd->right->right;nd->right->left=nd; c) nd->right=nd->right->left;nd->right->left=nd->right; d) nd->right=nd->left->right;nd->left->right=nd; 30. In the Given Infix expression which is the root node for your expression tree (A+B)-(C*D)+G/H*I a) + b) c) * d) /

31. Consider the following structure

63 struct node { int info; struct node *link; }; Suppose ptr is a pointer which is not pointing to the first also not to the last node. Then if you remove a node after ptr from the list, then the code will be a) ptr=ptr->link; b) ptr->link=ptr; c) ptr->link=ptr->link->link; d) ptr=ptr->link->link;

64 32. What does below code do, if temp is pointing to a node other than first and last node temp -> prev ->next = temp ->next; temp ->next -> prev = temp -> prev; free(temp); a) b) c) d) no effect inserts a node deletes a node shuffling of pointers

33. What is the Infix expression for - + A / * B C D / * E F G a) A + B * C / D E / F * G b) A + B / C * D E * F / G c) A + B * C / D E * F / G d) A - B * C / D + E * F / G 34. What is the postfix expression for A + B * C / D E * F / G a) ABC*D/+EFG*/b) ABC*D/+EF*G/c) ABCD/*+EF*G/d) None of these. 35. A binary tree with 15 nodes have _____ null branches a) 15 b) 14 c) 16 d) 17 1. union u { Int a; Char ch[2]; }u1; U1.a=5; U1.ch[0]=4 ; u1.ch[1]=2; Printf(%d,a); } (a) 5 (b) 42 ( c) 1028 (d) 24 36. Here is an infix expression: 6+2*(1*5-9). Suppose that we are using the usual stack algorithm to convert the expression from infix to postfix notation. What is the maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression? a. 1 b. 2 c. 3 d. 4

65 e. 5 39.Suppose that p is a pointer variable that contains the NULL pointer. What happens if your program tries to read or write *p? a. b. c. d. 45. a. b. c. d. 50. a. b. c. d. A syntax error always occurs at compilation time. A run-time error always occurs when the program finishes. The results are unpredictable. A run-time error always occurs when *p is evaluated What kind of list is best to access the item at given position n?" Doubly-linked lists. Lists implemented with an array. Singly-linked lists. Doubly-linked or singly-linked lists are equally best Which of the following applications may use a stack? A parentheses balancing program. Evolution of postfix expression. Syntax analyzer for a compiler. All of the above.

#include<stdio.h> void main() { union a { int i; char ch[2]; }; union a u; u.ch[0]= 3; u.ch[1]= 2; printf("%d %d %d",u.ch[0],u.ch[1],u.i); } a) 3 2 515 b)515 2 3 c) 3 2 5 d) none of these main() { struct student { char name[30];

66 struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); } a) It scans perfectly with error compilation Error b) scanf format is incorrect c) No Error d)

In printf(),the appearance of the output of the output can be affected by 1) field with 3) flag 2) conversion character 4) all of the above

Any of the following programs in c has access to three standard files: 1) standard input file, standard output file, standard error file 2) stdin,stdout, stderr 3) keyboard,screen,screen 4) all the above Heap 1) is a region from where memory is allocated 2) lies between you program and the stack 3) is a finite area 4) all of the above Function definition void check(int i ,char*j) is 1) call by value 2)call by reference 3) both (1) and (2) 4)in valid function definition Masking is used 1) to copy a portion of a given bit pattern to a new variable, while the remainder of the new variable is filled with 0s(using the bitwise AND)

67 2) to copy a portion of a given bit pattern to a new variable, while the reminder of the new variable is filled with 1s (using the bitwise OR) 3) to copy a portion of a given bit pattern to a new variable, while the remainder of the original bit pattern is inverted within the new variable 4) all of the above A fields width specifier in a printf() function 1) specifies the maximum value of a number 2) controls the size of type used to print numbers 3) controls the merging of the program listing 4) specifies how many characters positions will be used for a number

The global variables by default belong to 1) the register type 3) the auto type 2) the static type 4) the dynamic type

What will be the output of the following program : void main() { unsigned x=0xf880,y=5,z; z=x<<y; printf("%#x %#x",z,x>>y-1); } (a)1000 f87 (b)8800 0xf88 (d)0x1000 0xf88 Ans. (d)

(c)1000 f88

int num[26],temp; num[0]=100; num[25]=200; temp=num[25]; num[25]=num[0]; num[0]=temp; printf("\n%d %d",num[0],num[25]); o/p: 200 100

68 2. int array[26],i; for(i=0;i<=25;i++) { array[i]='A'+i; printf("\n%d, %c", array[i],array[i]); } o/p:65=A to 97-Z int sub[50],i; for(i=0;i<=48;i++) { sub[i]=i; printf("\n%d", sub[i]); } o/p:0-48 4. int a[5]={3,4,5,6,7}; printf("%d",a); o/p: address of a 5. int i,a[5]={3,4,5,6,7}; for(i=0;i<5;i++) printf("\n%d",a+i); o/p: a[i]=a+i adresss of all ints is printed 6. int i,a[5]={3,4,5,6,7}; printf("\n%d",a[5]); o/p: adress of a[5] is printed 7. int i,a[5]={3,4,5,6,7}; for(i=0;i<5;i++) printf("\n%d",a[++i]); o/p: 4,6,adress value 8. int i,a[3]; a[3]=8; printf("\n%d",a[3]); o/p:8

3.

69 9. int i,a[2][2]={1,2,3,4}; for (i=0;i<2;i++) printf("%d",a[i][i]); o/p: 1,4 10. int i,a[2][2]={1,2,3,4}; for (i=0;i<2;i++) printf("%d",a[i]); o/p: 8230 8234 ( adresss of ints) 11. int i,a[2][2]={1,2,3,4}; for (i=0;i<2;i++) printf("%d , %d\n",a[i]+1); o/p:8682,8999 8686,8999 12. int a[2][2]={1,2,3,4}; printf("%d",a); o/p: base address of a (1st element) 8686 13. char a[5][10]={"akshay", "Parag", "Chirag", "Hemal", "Hetal"}; printf("%s",a); o/p: akshay 14. char a[5][10]={"akshay", "Parag", "Chirag", "Hemal", "Hetal"}; printf("%s",a+2); o/p:chirag 15. char a[5][10]={"akshay", "Parag", "Chirag",

70 "Hemal", "Hetal"}; printf("%s",a[+2]); o/p:chirag 16. char a[5][10]={"akshay", "Parag", "Chirag", "Hemal", "Hetal"}; printf("%c",a[2]); o/p:chirag 17. char a[5][10]={"akshay", "Parag", "Chirag", "Hemal", "Hetal"}; printf("%c",a[2][4]); o/p:a 18. char a[5][10]={"akshay", "Parag", "Chirag", "Hemal", "Hetal"}; printf("%c",a[2][7]); o/p: (some symbol) 19. char c[2]="A"; printf("\n%c",c[0]); printf("\n%s",c); o/p:A A 20. char s[]="Get organized ! learn C"; printf("\n%s",&s[2]); printf("\n%s",s); printf("\n%s",&s);

71 o/p: t organized ! learn C Get organized ! learn C"; Get organized ! learn C"; 21. char s[]="No two viruses work similarly"; int i=0; while (s[i]!=0) { printf("\n%c",s[i]); printf("\n%c",i[s]); i++; } char str1[]={'H','e','l','l','o'}; char str2[]="Hello"; printf("\n%s", str1); printf("\n%s", str2); o/p: Hello garbage values Hello 23. 24. 25. printf(5+"Good Morning"); o/p: Morning printf("%c","abcdefgh"[4]); o/p:e printf("\n%d%d%d",sizeof('3'),sizeof("3");sizeof(3)); o/p:1 2 2

22.

1.

#include<stdio.h> void display(); void main() { printf("\n Only smarties use C?"); display(); } void display() { printf("\n Brilliants too use C!"); main(); } o/p: error cannot call main() from the program

72 2. #include<stdio.h> void main() { printf("\n Only smarties use C?"); main(); } o/p: error cannot call main() from the program 3. #include<stdio.h> int check(int); void main() { int i=45,c; c=check(i); printf("\n%d",c); } int check(int ch) { if(ch>=45) return (100); else return (10); } o/p:100 4. #include<stdio.h> int check(int); void main() { int i=45,c; c=check(i *1000); printf("\n%d",c); } int check(int ch) { if(ch>=40000) return (ch/10); else return (10); }

73

o/p:10 5. int x=10; void display(); void main() { int x=20; printf("\n%d",x); display(); } void display() { printf("Inside display %d",x); } o/p: 20,inside display 10

6.

#include<stdio.h> int i; void increment(); void decrement(); void main() { printf("\ni=%d",i); increment(); increment(); decrement(); decrement(); } void increment() { i=i+1; printf("\non incrementing i=%d",i); }

74 void decrement() { i=i-1; printf("\non decrementing i=%d",i); } o/p:1 2 1 0 7. void fun(int,int) void main() { int i=5,j=2; fun(i,j); printf("\n%d%d",i,j); } void fun(int i,int j) { i=i*i; j=j*j; } o/p:5,2 (call by value ex:) 8. void fun(int *,int *); void main() { int i=5,j=2; fun(&i,&j); printf("\n%d%d",i,j); } void fun(int *i,int *j) { *i=*i * *i; *j=*j * *j; } o/p: 25 4(call by reference) 9. void fun(int *x,int y) { int i=4,j=2; fun(&i,j); printf("\n%d%d",i,j); }

75 void fun(int *i,int j) { *i=*i * *i; j=j*j; } 10. void fun(void *); int i; void main() { void *vptr; vptr=&i; fun(vptr); } void fun(void *p) { int **q; q=(int **)&p; printf("%d",**q); } o/p:0 30. float x=1.1; while(x==1.1) { printf("\n%f",x); x=x-0.1; } o/p: no output 31. int x=3,y=0,z; while (x>=0) { x--; y++; if(x==y) continue; else printf("\n%d%d",x,y); } o/p: 2 1 12

76 03 -1 4 32. int x=4,y=0,z; while(x>=0) { if(x==y) break; else printf("\n%d%d",x,y); x--; y++; } o/p:4 0 31 33. int i=0; for(i=1;i<=5;printf("\n%d",i)); i++; o/p: infinite 1s 34. int i; for(;i;) printf("\n Here is some mail for you"); 35. int i=1, j=1; for(;;) { if(i>5) break; else j+=i; printf("\n%d",j); i+=j; } o/p:2 5 36. char suite=3; switch(suite) { case 1: printf("\n Diamond"); case 2:

77 printf("\n Spade"); default: printf("\n Heart"); } o/p:Heart 37. int c=3; switch(c) { case '3': printf("You never win the silver prize"); break; case 3: printf("You always lose the gold prize"); break; default: printf("Ofcourse provided you win a prize"); } o/p: You always lose the gold prize 38. int i=3; switch(i) { case 0: printf("\nCustomers are dicey"); case 1+0: printf("\nMarkets are pricey"); case 4/2: printf("\nInvestors are moody"); case 8%5: printf("\nAtleast employees are good"); } o/p: Atleast employees are good 39. int k; float j=2.0; switch(k=j+1) { case 3: printf("\n Trapped");

78 break; default: printf("\n Caught"); } o/p:Trapped 40.int ch='a'+'b'; switch(ch) { case 'a': case 'b': printf("\nYou entered b"); case 'A': printf("\na as in ashar"); case 'b' + 'a': printf("\n You entered a and b"); } o/p: You entered a and b 41. int i=1; switch(i-2) { case -1: printf("\nFeeding Fish"); case 0: printf("\n Weeding grass"); case 1: printf("\n Mending roof"); default : printf("\n Just to survive"); } 42. 43. 44. int x=25; printf("%d%d%d",x==25,x=50,x<78); o/p:0 50 1 int x=25; printf("%d%d%d",x==50,x=50,x<78); int x=50; printf("%d%d%d",x<78,x=100,x==50);o/p:0 100 1

79

1.

int b[]={10,20,30,40,50}; int i; for(i=0;i<=4;i++) printf("\n%d",*(b+i)); o/p: 10,20,30,40,50

2.

int b[]={0,20,0,40,5}; int i,*k; k=b; for(i=0;i<=4;i++) { printf("\n%d",*k); k++; } o/p: 0,20,0,40,5

3.

char s[]="No two viruses work similarly"; int i=0; while (s[i]!=0) { printf("\n%c",*(s+i)); printf("\n%c",*(i+s)); i++; } char s[]="Churchgate:no church no gate"; char t[25]; char *ss,*tt; ss=s; while(*ss!='\0') *ss++=*tt++; printf("\n%s",t); o/p:error

4.

5.

int arr[]={10,20,36,72,45,36}; int *j,*k;

80

j=&arr[4]; k=(arr +4); if(j==k) printf("The two pointers point to the same location"); else printf("The two pointers do not point to the same location"); o/p: The two pointers point to the same location 6. #include<stdio.h> void main() { int s[2][2]={ {2,2}, {3,3} }; int i,j; for(i=0;i<2;i++) for(j=0;j<2;j++) printf("%d\n",*(*(s+i)+j)); } o/p:2 2 3 3 7. void main() { float a=13.5; float *b,*c; b=&a; c=b; printf("\n%u %u %u",&a,b,c); printf("\n%f %f %f %f %f",a,*(&a),*&a,*b,*c); } int *p,a[3]={1,2,3}; p=a; printf("%d",*p++); printf("%d",*a);

8.

81 printf("%d",*p); o/p:1 1 2 9. int *p,a; a=20; p=&a; scanf("%d",p); printf("A= %d and *P = %d",a,*p); o/p:20 20 10. int **q,*p,a,b=40; a=20; p=&a; q=&p; *q=&b; printf("A= %d and *P = %d and **Q = %d",a,*p,**q); float *p; int a; a=30; p=&a; printf("A= %d and *P=%f",a,*p); float *p; int a; a=30; p=&a; printf("Address of A= %u and Value of P=%u",&a,p); float a; int *p; a=30.90; p=&a; printf("Address of A= %u and Value of P=%u",&a,p); int a[]={10,20,30,40,50}; int *p; p=a; printf("The value of *P is %d\n",*p); p++;

11.

12.

13.

14.

82 printf("Now the value of *P is %d\n",*p); 15. int a[]={10,20,30,40,50}; int *p; p=a; printf("The value of a[0] is %d\n",*p); p++; *p=300; printf("Now the value of a[1] is %d\n",a[1]); int a[]={10,20,30,40,50}; int *p; p=a; p=p*2; printf("%d",*p); void main() { char *str; str="%d\n"; printf(str,300); } void main() { char *str; str="%d\n"; str++; printf(str-1,300); } void main() { char *str; str="%4s"; printf(str,"K"); } void main() {

16.

17.

18.

19.

20.

83 int i,*j; i=3; j=&i; printf("%d",i**j*i+*j); } 21. 1. #define disp printf

void main() { disp("Hello World"); } 2. #define newline "\n" void main() { printf("Hello newline World"); } 3. #define pi 3.14 void main() { float d=pi; printf("%f",d); } 4. #define pi 3.14 void main() { float pi=2.11; printf("%f",pi); } 5. #define pi 3.14 void main() { printf("%f",pi); scanf("%f",&pi);

84 printf("%f",pi); } 6. #define maxi(a,b) a>b?a:b void main() { float x,y; scanf("%f%f",&x,&y); printf("%f",maxi(x,y)); } (1) which of the following lines gives/prints the output 20 void main() { int i=20; int *k,**j; j=&i; k=&j; printf("\n%d",i); printf("\n%d",**k); printf("\n%d",***&k); } (2)only 7 (4)all 5,6,7

lint1: line2: line3: line4: line5: line6: line7: (1) only 5 (3) only 6 ans:(4)

(2) void main() { int x,y=6; x=y+NULL; printf("%d",x); }

85 o/p: 6 (3) void main() { static int arr[12]; printf("%d",sizeof(arr)); } o/p: 24

(4) void main() { float a[]={1.2,3.4,5.7,12.3}; int i; for(i=0;i<=5;++i) printf("%f ",a[i]); } o/p: 1.200000 3.400000 5.700000 12.300000 0.000000 0.000000

(5)A user-defined function returns a pointer (yes/No) yes (6)In a normal c program the library will be defined in ________________ (7) void main() { printf("%s",7["wipro infortech"); }

86

(8) #include<stdio.h> void main() { char *p="hello"; char q[]={'h','e','l','l','o'}; printf("%d %d",sizeof(*p),sizeof(*q)); }

o/p: 1 1

(9) #include<stdio.h> void main() { int i=0; while() { switch(i) { case 3<2: printf("Hi"); break; case 3>2: printf("Hello"); break; default: printf("welcome"); } ++i; } } o/p: error

87

(10) void main() { int i=0; while(i<3) { switch(i) { case 1: printf("case1"); case 2: printf("case2"); break; default: printf("Default"); } ++i; } } o/p:default case1 case2 case2

(11) #include<stdio.h> void main() { char str1[]="Hello"; char str2[]="Hello" if(str1==str2) printf("\nEqual"); else printf("\nNot equal"); } 0/p: Error

(12) which one of the comment line statement is correct

88

(1) (2) (3) (4)

/* hello world */ //hello world// */ hello world */ /&hello world &/

Ans: (1)

(13) The value of EOF is _____ (1) NULL (2)0 (3)-1 (4)1 Ans: (3) (14) #include<stdio.h> void main() { int i; for(i=2;i=0;i--) { printf("%d",&i); } } No output (15) struct addr { char city[5]; char street[5]; }; struct emp { char name[20]; int empno;

89 struct addr *a; }; struct emp *pemp; How will you access city from the *pemp; ans: pemp->a->city

(16) If you want to allocate 2 blocks of memory for a set of 5 integers,which will you use? (a) malloc (c)neither (a)&(b) Ans:(d) (b)calloc (d)either (a)&(b)

(17) #include<stdio.h> void main() { int i=10; printf("%d ",i=50); } 0/p: 50

(18) #include<stdio.h> #define HELLO hi void main() { printf("HELLO"); }

90

o/p: HELLO (19) Convert to prefix (a+b)*(c/d) ans: *+ab/cd

(20) #include<stdio.h> struct person { char *name; char *age; char *c; }; struct emp { int a; struct person *addr; }; void main() { struct emp *e1; printf("%d",sizeof(e1)); } o/p: 4 (21) #include<stdio.h> void main() { int i;

91 i=10/4; printf("%d",i); } o/p: 2

(22) #include<stdio.h> void main() { float i; i=10/4; printf("%f",i); } o/p: 2.000000

(23) To arrange nodes in ascending order ________ is used 1)inorder ans: inorder 2)preorder 3)postorder 4)all the above

(24) int i=6,*j; line1: j=&i; line2: j=&35; line&: j=&(i+25); Which of the above lines are correct?

92 Ans: line1

(25) #include<stdio.h> void main() { int i=10; printf("%d",i==10); } o/p: 1

(26) #include<stdio.h> int get(); void main() { int x=20; x=get(); printf("%d ",x); } int get() { return(60); } o/p: 60 (27) #include<stdio.h> void main() { int a[3][4]={1,2,3,4,5,6,7,8,9}; printf("%d ",a[2][1]); }

93

o/p: 0

(28) #include<stdio.h> void main() { static int a[100]; int i; for(i=0;i<4;i++) printf("%d",a[i]); } o/p: 0 0 0 0

(29) which is better memory allocation when size is not known 1)static ans: dynamic (30) struct city { char name[10]; char address[0]; }; struct details { struct city c; }; struct details *d; 2)dynamic 3)both 4)neither

94 How will u access address(member) 1)d->c.address Ans:d->c.address 2)d.c.address 3)d.c->adddress

(31) #include<stdio.h> void main() { int array[]={10,20,30,40,50},*p; p=&array[0] + 4; //p=array+4 for(;p>=array;p--) printf("%d ",*p); } Ans:50 40 30 20 10 (32) A complete binary tree is (a) non-leaf nodes have two child (b) all left subtree less than root and root less than right tree which is correct 1) a alone 2)b alone 3)neither (a) nor (b)

(33) Linked list is better than array because of 1) insertion 2)deletion 3)traversal a) (1) & (2) (34) In the doubly linked list ,to add a new element in the tail node b) (2) & (3)

95

(35) #include<stdio.h> #define wipro main void wipro() { printf("main"); } o/p: main (36) #include<stdio.h> #define output(int) printf("%d ",int) void main() { int a=2,b=3; output(a); output(b); } o/p: 2 3 (37) #include<stdio.h> #define wipro printf void main() { wipro("welcome"); } o/p: welcome

96

(38) Which one of the following statement is false for binary search tree 1) root is less than left and right subtree 2)root is less than left subtree but greater than right a) 1 only b) 2 only c)both 1 and 2 d)neigher 1 nor 2

(39) #include<stdio.h> aaa() { printf("hi"); } bbb() { printf("hello"); } ccc() { printf("bye"); } void main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[1](); } o/p: hello

(40) a=15,b=3,c=5,d=3 a*b+c-d evaluate the expression

97

Ans:47 (41) #include<stdio.h> void main() { int i,j; for(i=0;i<2;i++) { for(j=0;j<5;j++) { if(i==j) break; printf("wipro"); } } } How many times wipro will be printed o/p: 1 time

42) #include<stdio.h> void main() { int a=10,b=10; a+=1; printf((a>b)?"hi":"hello"); } o/p: hi

43)

98 which is not an operator in c? 1)& 2)! 3)~ 4):: 44) Which operator is highest precedence? a)"==" b)"*" c)"+" d)"->"

45) #include<stdio.h> void main() { static int a[5]; int i; for(i=0;i<5;++i) printf("%d ",a[i]); } o/p: 0 0 0 0 0 46) For allocating 2 blocks of 5 elements of integer type,what we will use? 1)malloc( ) or calloc( ) 3)calloc( ) Ans: (1) 47) #include<stdio.h> struct emp { int i; char name[15]; }; void main() { struct emp e1={1,"hi"},*e2; e2=&e1; 2)only malloc 4)neither malloc( ) nor calloc( )

99 if(e1==e2) printf("\nstructures are equal"); else printf("\nstructures are not equal"); } Ans: error 48) The arguments in a function call are known as a) Actual parameters b)Formal parameters

49) #include<stdio.h> void main() { char *fun(); char *s; s=fun(); printf("%s",s); } char *fun() { return("Hello"); } o/p:Hello

50) The range of unsigned integers is _________ 51)

10 prefix to infix postfix to infix

52)Which is initialized as zero 1)global variable 3)register variable a)(1) and (2) c) (1),(2)and (3) Ans: a 53) which statement used to read from file 1)fgets 2)fread 3)fscanf 4)sscanf 2)static variable 4)extern variable b) (1) and (3) d) (1),(2),(3) and (4)

54) #include<stdio.h> fun1() { int a=5; static int b=5; a++; b++; printf("%d ",a); printf("%d\n",b); } void main() { fun1(); fun1(); } o/p: 66 67

10

55) In what aspect linked list is efficient than array 1)Insertion a)1 and 2 Ans:(a) 2)deletion b)3 only 3)Traversal c)2 and 3 only

56) #include<stdio.h> void main() { int i; for(i=16/3;i<5;i++) printf("%d",i); } o/p: no output 57) #include<stdio.h> void main() { int a; a=9/4.5; printf("%d",a); } Ans: 2 58) #include<stdio.h> #define TRUE 1 #define FALSE 0 void main()

10 { if(TRUE) printf("True"); else printf("False"); } o/p: True 59) #include<stdio.h> void main() { char str1[]="Hello"; char str2[]="Hello"; if(str1==str2) printf("True"); else printf("False"); } o/p:false 60) Which of the following is correct a) To read a character by character in files getc is used. b) To read a character by character in files fgetc is used. c) To read a line by line in a file gets is used. d) To read a line by line in a file fgets is used. Ans:(a),(b) and (d) 61) #include<stdio.h> itn one_d[]={1,2,3}; void main() { int *ptr; ptr=one_d;

10 ptr+=3; printf("%d",*ptr); } /*#include<stdio.h> void main() { int array[] = {10,20,30,40,50}; int *p; p = &array+4; for (;p>=array; p--) { printf ("%d \n", *p); } }*/ //______________________________________________________________________ ________ /*#include<stdio.h> struct city { char name[10]; char *address; }; struct details { struct city c; }; void main() { struct details *d1; scanf("%s",d1->c.address); }*/ //______________________________________________________________________ ________ /* A complete binary tree has statement 1: non-leaf nodeshave two child

10 statement 2: All left subtree values less than root and root less than right tree Which is correct 1 alone 2 alone either 1 or 2 neither 1 nor 2*/ //______________________________________________________________________ ________ /* In doubly linked list, to add a new element in the tail node 1. nd->next = NULL: 2. nd-> prev->next = tail; 3 nd - > prev = tail; which order is correct 123 321 3 1 2 */ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int c[] ={2.8,3.4,4,4.6,7.5}; int j, *p=c, *q = c; for (j=0; j<5; j++) { printf("%d", *c); ++q; } for (j=0; j<=5; j++) { printf("%d", *p); ++q; } }*/

10 //______________________________________________________________________ ________ /* Which of the following is correct 1. signed char range is -128 to 127 2. unsigned char range is 0 to 255 3. int by default is short int 4. short int by default is signed short int */ //______________________________________________________________________ ________ /*#include<stdio.h> #include<string.h> void main() { int i,n; char *x = "OOAD"; n = strlen(x); *x = x[n]; for(i=0; i <n; ++i) { printf ("%s \n", x); x++; } }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i, j; for(i=0; i <2; i++) { for(j=0; j<5; j++) { if(i==j)

10

break; printf ("wipro"); } } }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i=2; switch(i) { case 2: printf ("Case2"); case 1: printf ("Case1"); break; } }*/ //______________________________________________________________________ ________ /* When pointer is assign to null pointer what will be the output a. compile Error b. runtime error while acessing the pointer c. some output*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int *p, j; ???? *p = 35;

10 printf ("%d", j); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> aaa() { printf("hi"); } bbb() { printf ("Hello"); } ccc() { printf("bye"); } void main() { int (*ptr[3])(); ptr[0] = aaa; ptr[1] = bbb; ptr[2] = ccc; ptr[2](); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> #define wipro main wipro() { printf ("main"); } void main() {

10 wipro(); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> #define output(int) printf ("%d", int); void main() { int a=2,b=3; output(a); output(b); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> #define wipro printf main() { wipro("Hai"); }*/ //______________________________________________________________________ ________ /* Which one of the following statements is true for binary searc tree 1. root is less than left & right sub tree 2. root is less than left sub tree but greater than right sub tree*/ //______________________________________________________________________ ________ /* The Range fo unsigned integer is _____________________________*/ //______________________________________________________________________ ________ /*#include<stdio.h>

10 void main() { auto int i=0; static int j=0; extern int k =0; register int l=0; i++; j++; k++; l++; printf ("%d %d %d", i, j,l); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { const char a="b"; }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i=10; while(++i>10) { printf ("something"); } }*/ //______________________________________________________________________ ________ /*#include<stdio.h> define mul(a) (a) * (a); /*They will ask u what will be the define statement to get the output as hundred and the define statement have four opitons. This is correct option to get the output*/

11

/*void main() { int result; result = mul(10); printf ("%d", result); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> struct a { int no; int *no1; int *no2; } *a1; void main() { printf ("%d %d %d %d", sizeof(a1->no),sizeof(a1->no1), sizeof(*a1), sizeof(a1)); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { static int a[5]; int i; for(i=0; i<5; i++) { printf ("%d", a[i]); } }*/ //______________________________________________________________________ ________ /*#include<stdio.h>

11

void main() { int i=10; printf ("%d", i= =10); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int x,y=6; x=y+NULL; printf("%d",x); } */ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { static int arr[12]; printf("%d",sizeof(arr)); } */ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { float a[]={1.2,3.4,5.7,12.3}; int i; for(i=0;i<=5;++i) printf("%f ",a[i]); }*/ //______________________________________________________________________ ________ //A user-defined function returns a pointer (yes/No) //______________________________________________________________________ ________

11 /*#include<stdio.h> void main() { char *p="hello"; char q[]={'h','e','l','l','o'}; printf("%d %d",sizeof(*p),sizeof(*q)); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i=0; while() { switch(i) { case 3<2: printf("Hi"); break; case 3>2: printf("Hello"); break; default: printf("welcome"); } ++i; } }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i=0; while(i<3) { switch(i) { case 1: printf("case1"); case 2:

11 printf("case2"); break; default: printf("Default"); } ++i; } }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { char str1[]="Hello"; char str2[]="Hello"; if(str1==str2) printf("\nEqual"); else printf("\nNot equal"); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i; for(i=2;i=0;i--) { printf("%d",&i); } }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i=5; printf ("%d", ~i);

11 }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i=10; printf("%d ",i=50); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> #define HELLO "hi" void main() { printf("HELLO"); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> struct person { char *name; char *age; char *c; }; struct emp { int a; struct person *addr; }; void main() { struct emp *e1; printf("%d",sizeof(e1)); }*/

11 //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int i; i=10/4; printf("%d",i); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int a[3][4]={1,2,3,4,5,6,7,8,9}; printf("%d ",a[2][1]); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> void main() { int array[]={10,20,30,40,50},*p; p=&array[0] + 4; //p=array+4 for(;p>=array;p--) printf("%d ",*p); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> #define wipro main void wipro() { printf("main"); }*/

11

//______________________________________________________________________ ________ /*#include<stdio.h> void main() { int a=10,b=10; a+=1; printf((a>b)?"hi":"hello"); }*/ //______________________________________________________________________ ________ /*#include<stdio.h> struct emp { int i; char name[15]; }; void main() { struct emp e1={1,"hi"},*e2; e2=&e1; if(e1==e2) printf("\nstructures are equal"); else printf("\nstructures are not equal"); }*/ //______________________________________________________________________ ________ 6. The size of long double is a. 8 bytes b. 10 Bytes c. Compiler Dependent d. There is no datatype called long double. Ans::b 7. What is the output of the following: unsigned i=32768; void main() { printf("%d",i); }

11

b. 32768 c. -32768 d. Error e. None of the above Ans::b 8. How many times Wipro Technologies printed in the following code: for(i=0;i<5;i++) for (j=0;j<5;j--) { if (i==j) break; printf(Wipro Technologies); } a. 4 times b. Error c. Infinite loop d. Not predictable Ans::c 9. What is the output of the following int i; void main() { static int i=3; printf("%d",i); } e. 3 f. Multiple declaration (since I is static) g. 0 h. None of the above Ans::a(3) 10. What is the output of the following: main() { void change(char *); char *t="test"; change(t); printf("%s",t); } void change(char *t) { char *ab="new test"; *t=*ab;

11 } b. new test c. test d. nest e. None of the above Ans::c 6. main() { char *fun(); printf("%s",fun()); } char * fun() { char buffer[]=Hello World; return buffer; } a) hello world b) Compiler error c) Garbage Value d) None of the above ans::c//if static char buffer is used it would have worked 11. main() { int a=10,*p=&a,*q=p; *q++;//valid printf(%d %d %d,a,*p,*q); } a. 11 11 11 b. 10 10 11 c. Garbage value; d. Error Garbage value//actaually 10 10 garbage Ans::c 12. #define c(a) a*a #define c(b) b+b void main() { printf(%d,c(10)); } e. f. g. h. 100 20 400 Error

11 Ans::f 13. void rec(int); void main() { rec(3); } void rec(int n) { if (n<=0) return; rec(n-1); printf(%d,n); rec(--n); } a. 1 2 1 3 1 2 1 b. 0 1 2 0 c. Infinite loop d. Error Ans::a 14. int z, x=5,y=-10,a=4,b=2; z=x++ - --y * b / a; What number will z in the sample code above contain? a. 5 b. 6 c. 10 d. 11 ans::c 10. char* myFunc (char *ptr) { ptr += 3; return (ptr); } int main() { char *x, *y; x = "HELLO"; y = myFunc (x); printf ("y = %s \n", y); return 0; } What will print when the sample code above is executed? a. y = HELLO b. y = ELLO

12 c. y = LLO d. y = LO Ans::d 11. What would be the output? void main() { printf("%s",7["wipro infotech"); } a. f b. Garbage value c. fotech d. Error Ans::b Garbage value //used aswipro infotech+7 12. Predict the output char p[]="hello"; char q[]={'h','e','l','l','o'}; printf("%d %d",sizeof(p),sizeof(q)); a. 5 5 b. 6 6 c. 6 5 d. 5 6 Ans::c 13. What would be printed: #include<stdio.h> int i=0; void main() { while(i) { switch(i) { Case 3<2: printf("Hi"); break; case 3>2: printf("Hello"); break; default: printf("welcome"); } ++i; } }

12 b. Hi c. Welcome d. No output e. Error Ans::c 14. struct addr { char city[5]; char street[5]; }; struct emp { char name[20]; int empno; struct addr *a; }; struct emp *pemp; How will you access city from the *pemp; a. pemp->a->city b. pemp->*a.city c. pemp->a.*city d. All the above. Ans::a //rest invalid 15. #include<stdio.h> int get(); void main() { int x=20; x=get(); printf("%d ",x); } int get() { return(60); } b. 60 c. Garbage d. Error e. None of the above Ans::a 16. What would be the out put of the following void main()

12 { int a=10,b=20,c=30; a++ || b-20 && ++c; printf("%d %d %d",a,b,c); } a. 11 20 31 b. 11 0 30 c. 11 20 30 d. Error Ans::c 17. What will be the output of the following program : void main() { printf("Hi!"); if (-1) printf("Bye"); } (a)No Output (b)Hi! (c)Bye Ans::d 18. void main() { printf("Hi!"); if !(0) printf("Bye"); } (a)Compile-Time error (b)Hi! (c)Bye Bye Ans::a 19. void main() { printf("Hi!"); if (-1+1+1+1-1-1-1+(-1)-(-1)) printf("Bye"); } (a)No Output (b)Hi! (c)Bye Ans::d 20. void main() { int a=1,b=2,c=3,d=4,e; if (e=(a & b | c ^ d)) printf("%d",e); } (a)0 (b)7 (c)3 Nas:: 21. auto int a=5; void main()

(d)Hi!Bye

(d)Hi!

(d)Hi!Bye

(d)No Output

12 { printf("%d",a); } (a)Compile-Time error (b)Run-Time error (c)5 (d)Unpredictable Ans::a//auto cannot be declared outside any fuction 22. main() { int a=1; if (a) printf("Test"); else; printf("Again"); } (a)Again (b)Test (c)Compile-Time Error (d)TestAgain Ans::b 23. void main() { float i; for (i=0.1; i<0.4; i+=0.1) printf("%.1f",i); } (a)0.10.20.3 (b)Compile-Time Error (c)Run-Time Error Output Ans::a 24.Point out the error if any #include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; };//name sud be given. struct yy *q; }; } a. Compile time error b. Run time error

(d)No

12 c. No error. d. Compiler dependent ans::a 5. Heap 1) is a region from where memory is allocated 2) lies between you program and the stack 3) is a finite area 4) all of the above Ans::4 26. Predict the output int a = 4, b = 7,c; c = a = = b; printf("%i",c); a) 0 b) error c) 1 d) garbage value ans::error//= = would not work == will do 27. What will be the output of the following statements ? int a[2][2] = { 3,2,5,4 }; printf("%d",*(*(*(a)))); a) error b) 3 c) garbage value d) 2 Ans::a 28. Which of the functions is most apt for reading a multi-word ? a) puts() b) gets() c) scanf() d) vsscanf() Ans::b 29. What will be the output of the following program ? #include<stdio.h> #include<math.h> void main() { int n; char *str = "324.89"; n = atoi(str); printf("%d",n); }

12

a) 300 b) 324 c) 32489 d) 89 Ans::324 30. #include<stdio.h> void main() { printf("%d"); } a) error b) no output c) %d d) 0 Ans::d 31. #include<stdio.h> void main() { int a = 36, b = 9; printf("%d",a>>a/b-2); } a) 9 b) 7 c) 5 d) none of these Ans::a 32. #include<stdio.h> void main() { int a,b; void small(int,int); printf("Enter 2 numbers\n"); scanf("%d %d",&a,&b); } void small(int x,int y) { if (x<y) printf("A is less than B"'); else printf("B is less than A"); } If a=10, b=10 What is the output a. B is less than A b. A is less than B

12 c. Error d. No output Ans::d 33. swap(int *a,int *b) { int *t; t=a; a=b; b=t; } void main() { int a=10,b=20; swap(&a,&b); printf(%d %d,a,b); } b. 10 20 c. 20 10 d. Error e. Garbage value Ans::a 34. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*++p; printf("%s %s",p,p1); } a. ibj!gsjfoet b. hbj!gsjfoet c. hbj!gsjfoet hbj!gsjfoet d. None of the above Ans::d//abnormal program termination 35. main( ) { char *q; int j; for (j=0; j<3; j++) scanf(%s ,(q+j)); for (j=0; j<3; j++) printf(%c ,*(q+j)); for (j=0; j<3; j++) printf(%s ,(q+j)); } If the input given are Wipro Technologies Chennai

12 What will be the output a. WTChennai TChennai Chennai b. Chennai Chennai Chennai c. Wipro Technologies Chennai d. None of the above Ans::d 36. main() { int i, n; char *x = OOAD; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(%s\n,x); x++; } } a. OOAD OAD AD b. OAD AD D c. No output d. None of the above Ans::b 37. #define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); } a. TRUE b. FALSE c. TRUE FALSE

12 d. Error Ans::a 38. main() { int i=5,j=6,z; printf("%d",i---j); } a. -1 b. Error c. 0 d. None of the above Ans::a 39. main() { char *p; int *q; long *r; p=0; q=0; r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); } a. 0001 0002 0004 b. 0000:0001 0000:0002 0000:0004 c. 1 2 4 d. Error Ans::a//inc by their type value in p,q r set to zero 40. # include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa;

12 ptr[1]=bbb; ptr[2]=ccc; (*ptr)(); } a. hi b. hello c. bye d. Error Ans::hi 1) Which of the following is a compound assignment statement? a)a=b b) a=b=c c) a+=b d) a=b;b=c; ans::c 2) given that the file is being read using fgetc command,as ch =fgetc(fp),then what is the condition to check for the end of the file a) ch!=EOF b) ch!=NULL c) ch==NULL d) ch=='\0' ans::a 3) which of the following accepts only the specified number of characters from a file a) fgets b) fread c) fputc d) fputs ans::a 4) which of the following is the best for getting a string with space from the standard input a) gets b) getc c) fgets d) puts ans::fgets//gets 5)how to represent a character pointer named 'message' pointing to the array"hello" a) char message="hello" b) char message[ ]="hello" a) char *message="hello" a) char *message[ ]="hello" ans::c 6) which data structure is best for searching an element in the given list a) arrays b) single linked list

13 c) doubly linked list d) binary search trees ans::d 7) assume that integer is 4 bytes,pointer as 4 byter and character as 1 byte,then predict the output struct student { int a; char name[10]; int *p; }s1,*s2; printf("%d%d",sizeof(s1),sizeof(*s2)); a) 18,18 b)18,4 c) 4,18 d) 4,4 ans::a 8) what would the statement strcmp("Astring","Astring"); return? a) 0 b) <1 c)>1 d) -1 ans::a 9) which statement is used to compare the two strings ? a) strcmp b)strcompare c) stringcompare d) str_cmp ans::a 10) int fun(int x) { int y=55; return((x-y)?y:x); } main() { int a=20; fun(a); printf("%d",y); } ans::error 11) main() { static int a[10]={1,2,3,4,5};

13 int i; for(i=0;i<10;i++) printf("%d ",a[i]); } ans::1 2 3 4 5 0 0 0 0 0 12) main() { char *y; y=x(); printf("%s",y); } char *x() { char result[ ]="Hello"; strcpy(result,"anything good"); return result; } ans::buffer die after function get lost 13) The post order is 5,20,10.what will be the inorder? A)5,10,20 b) 20,5,10 c) 20,10,5 d) none of the above ans::a 14) int fact(int x) { ?????? return(x*fact(x-1)); } main() { fact(5); } what would be replaced in terms of ??????? a) if(x==0) return 0; b) if(x==1) return 1; c) if(x==2) return 2; d) none of the above ans::b 15) Allocating a 2 blocks of memory for 'n' intgers is by using a) malloc b) calloc c) both malloc and calloc d) none of the above

13 ans::b 16) In queue using linked list, which is correct? a) both insertion and deltionis made at the front end b) insertion is made at the beginning and deletion is made at the last c) insertion is made at the last and deletion is made at the beginning d) both insertion and deltionis made at the back end ans::c 17) main() { int i=10; printf("%d",i==10); } ans::1 18) main() { char c='p'; printf("%c",c); } ans::p 19) main() { int i=0; while(i<3) { switch(i) { case 3>2: printf("case1"): break; case 3<2: printf("case2"); break; defalut: printf("default"); break; } i++; } ans::case2case1 20) which is correct? a) unsigned char short b) unsigned short char c) short unsigned char d)noneof the above ans::b

13 21) { char *str1="hai"; str=str1; } main() { char *str="Hello"; fun(str); printf("%s",str); } ans::hello 22) struct city { char *name[20]; int age; }*s; how to allocatethe memory for the member 'name'? s->name[]=(char*) 23) what is the header file to be included for doing mathematical calculations? 24) what is the way to declare a float pointer? float *f; 25) what kind of error does the syntax error is? 26) float fun( f1) { return(f1*f1); } main() { float f1=3.25; fun(f1); printf("%f",f1); } ans::3.25//also f1 will take int value only 27) for(i=0;i<2;i++) { for(j=0;j<5;j++) { if(i==j) break; printf("wipro"); } } void fun(char *str)

13 how many times would wipro to be printed? ans::wipro will be printed only one times. 28) main() { FILE *fp; char data[100]; printf("Enter the file name"); gets(data); ???? fclose(fp); } fp=fopen(data,"r"); what would be replaced for ???? to open a file in a read mode 29) main() { int 5[a]={10,20,30,40,50}; int *p; p=a[2]; printf("%d",2[a]); } error 30) which of these isn an infinite loop i) for(;;); ii)for(i=1;;i++); iii) for(i=0;i<=5;i++) { --i;} a) i,ii only b) i only c)all the three d) none of the above ans::c 31)which of these is replaced by a switch block? a) while loop b) do while c) for loop d)else if 32) void disp(char *string) { printf("%d\n",string);

13 printf("%s\n",string); } void main() { char string[ ]="Hello world"; printf("%d\n",string); disp(string); } 33) char *gxxx() { static char x[1024]; printf("%s\n",x); return x; } void main() { char *g="string"; strcpy(gxxx(),g); g=gxxx(); strcpy(g,"old string"); printf("%s\n",gxxx()); } ans::old string 34) main() { char s[ ]="\12345s\n"; printf("%d\n",sizeof(str)); } ans::6 35) main() { char *k[ ]={" Good Morning","Good Evening","Good night"}; printf("%s\n",k[0]); printf("%s\n",*(k+1)); } ans::Good Morning Good Evening 36) which one of the following is incorrect? a) signed char a; b)char signed a; c) char a signed; d) none of the above ans::c

13 37) all the local variables are stored in ----------------a) stack b) heap c) queue d) none ans::a 38) Once you call a function,all the return addresses are stored in ---------------a) stack b) heap c) queue d) none ans::a 39) Which of the following is true about binary tree i) all nodes except leaf node has exactly two child ii) root node is greater than the left sub tree and lesser than the right sub tree. a) i only b)ii only c) both i and ii d) neither i nor ii ans::b 40) Which of the following is true about complete binary tree i) all nodes except leaf node has exactly two child ii) root node is greater than the left sub tree and lesser than the right sub tree. a) i only b)ii only c) both i and ii d) neither i nor ii ans::c 41) struct node { int data; struct node *left,*right; }; suppose start and end are the pointers pointing to the beginning and ending node reapectively.then, what will be the output of the following snippet front=start; back=end; while(back!=NULL) { printf("%d",back->data); back=back->left; } reverse printing 42) struct node

13 { int data; struct node *left,*right; }; suppose start and end are the pointers pointing to the beginning and ending node reapectively.then,what will be the output of the following snippet front=start; back=end; while((front!=back)&&(back->left!=front)) { temp=front->data; front->data=back->data; back->data=temp; } 43) the declaration of the pinter to an array ofint having the name iptr is ------------------a) int *iptr b) int &iptr c) int *&iptr d)iptr *int ans::a 44) int one_d={0,1,2}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); } ans::error 45) struct a { char *b; char *c; char *d; }; struct e { char *f; struct a *g; }abc; printf("%d",sizeof(abc)); ans::4

13

46) struct a { char *b; char *c; char *d; }; struct e { char *f; struct a g; }abc; printf("%d",sizeof(abc)); ans::8 47)#define wipro main wipro() { printf("main"); } ans::main 48) void prod(int x,int y,int z) { int p; p=x*y*z; printf("%d",p); } void main() { int a=3,b=4,c=5; prod(a,b,c); } 49) int meq(); void main() { printf("Hai"); meq(); printf("Hello"); } meq() { printf("Bye"); } ans::HaiByeHello 49) void main() {

13 char *p="welcome"; char q[ ]={'w','e','l','c','o','m','e'}; printf("%d %d",sizeof(*p),sizeof(*q)); } ans::11 50) The size of the doubly linked list is always greater than the single linked list a) true b) false 51) char *ptr="abcdefgh"; char *sptr; sptr=ptr+5; printf("%s",sptr); ans::fgh 52) If the integer occupies 2 bytes then short int will take how many bytes? 53) In C, syntax error is generated by --------------------a) compiler b) Interpreter C) linker d) none 54) char *ptr="Madam"; char *sptr=ptr+4; while(sptr>=ptr) { printf("%c",*sptr); sptr--; } ans::madaM 55) void main() { int i; i=10/20; printf("%d",i); } ans::0 56) The arguments in a function call is known as --------------------a) formal arguments b) actual arguments 57) main()

14 { int i; for(i=0;i<10;i++); printf("%d",i); } ans::10 58) int i=10; main() { int i=20; { int i=30; printf("%d",i); } printf("%d",i); } ans::3020 59) void fun(); main() { fun(); } void fun() { printf("good"); } ans::good 60) void main() { int i=10;j=20,k=30,l=40,m; int *a[4]; a[0]=&i; a[1]=&j; a[2]=&k; a[3]=&l; for(m=0;m<4;m++) printf("%d",*a[m]); } ans::10203040 61) if the insertion is made only at the beginning then which data structure is used a) array b) stack

14 c) queue d) linked list 62) to access the 4th element in the array 'num' we need ------------a) num[4] b) num[1] c)num[3] d) none of the above 63) void main() { char *a[ ]={"Dharma","Hewlett packard","New city","ibm"}; char **ptr=a; printf("%s",++*p); printf("%s",*p++); printf("%s",*p); } ans::harmaharmaHewlett packard 64)

C Questions Note : All the programs are tested under Turbo C/C++ compilers. It is assumed that, Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed). Predict the output or error(s) for the following: 1. void main() { int const * p=5; printf("%d",++(*p)); } Answer:

14 Compiler error: Cannot modify a constant value. Explanation: p is a pointer to a "constant integer". But we tried to change the value of the "constant integer". 2. main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); } Answer: mmmm aaaa nnnn Explanation: s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i]. 3. main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); } Answer: I hate U Explanation: For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double. Rule of Thumb: Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) . 4. main() { static int var = 5; printf("%d ",var--);

14 if(var) main(); } Answer: 54321 Explanation: When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively. 5. main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } } Answer: 2222223465 Explanation: Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed. 6. main() { extern int i; i=20; printf("%d",i); } Answer: Linker Error : Undefined symbol '_i' Explanation: extern storage class in the following declaration, extern int i; specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred .

14 7. main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); } Answer: 00131 Explanation : Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression i++ && j++ && k++ is executed first. The result of this expression is 0 (1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for 0 || 0 combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1. 8. main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); } Answer: 12 Explanation: The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2. 9. main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } } Answer : three

14 Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match. 10. main() { printf("%x",-1<<4); } Answer: fff0 Explanation : -1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value. 11. main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); } Answer: Compiler Error : Type mismatch in redeclaration of function display Explanation : In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs. 12. main() { int c=- -2; printf("c=%d",c); } Answer: c=2; Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus. Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.

14

13. #define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); } Answer: sizeof(i)=1 Explanation: Since the #define replaces the string int by the macro char 14. main() { int i=10; i=!i>14; Printf ("i=%d",i); } Answer: i=0 Explanation: In the expression !i>14 , NOT (!) operator has more precedence than > symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero). 15. #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } Answer: 77 Explanation: p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. Now performing (11 + 98 32), we get 77("M"); So we get the output 77 :: "M" (Ascii is 77).

14

16. #include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); } Answer: SomeGarbageValue---1 Explanation: p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array. 17. #include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } Answer: Compiler Error Explanation: You should not initialize variables in declaration 18. #include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; };

14 struct yy *q; }; } Answer: Compiler Error Explanation: The structure yy is nested within structure xx. Hence, the elements are of yy are to be accessed through the instance of structure xx, which needs an instance of yy to be known. If the instance is created after defining the structure the compiler will not know about the instance relative to xx. Hence for nested structure yy you have to declare member. 19. main() { printf("\nab"); printf("\bsi"); printf("\rha"); } Answer: hai Explanation: \n - newline \b - backspace \r - linefeed 20. main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } Answer: 45545 Explanation: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result. 21. #define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); } Answer: 64

14 Explanation: the macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64 22. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); } Answer: ibj!gsjfoet Explanation: ++*p++ will be parse in the given order *p that is value at the location currently pointed by p will be taken ++*p the retrieved value will be incremented when ; is encountered the location will be incremented that is p++ will be executed Hence, in the while loop initial value pointed by p is h, which is changed to i by executing ++*p and pointer moves to point, a which is similarly changed to b and so on. Similarly blank space is converted to !. Thus, we obtain value in p becomes ibj! gsjfoet and since p reaches \0 and p1 points to p thus p1doesnot print anything. 23. #include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); } Answer: 50 Explanation: The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken. 24. #define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); } Answer: 100 Explanation:

15 Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.The input program to compiler looks like this : main() { 100; printf("%d\n",100); } Note: 100; is an executable statement but with no action. So it doesn't give any problem 25. main() { printf("%p",main); } Answer: Some address will be printed. Explanation: Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers. 27) main() { clrscr(); } clrscr(); Answer: No output/error Explanation: The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function). 28) enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); } Answer: 0..1..2

15 Explanation: enum assigns numbers starting from 0, if not explicitly defined. 29) void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); } Answer: 4..2 Explanation: the second pointer is of char type and not a far pointer 30) main() { int i=400,j=300; printf("%d..%d"); } Answer: 400..300 Explanation: printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments given in the program,then printf will take garbage values. main() { char *p; p="Hello"; printf("%c\n",*&*p); } Answer: H Explanation: * is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H. main() { int i=1;

31)

32)

15 while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } Answer: Compiler error: Undefined label 'here' in function main Explanation: Labels have functions scope, in other words The scope of the labels is limited to functions . The label 'here' is available in function fun() Hence it is not visible in function main. 33) main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); } Answer: Compiler error: Lvalue required in function main Explanation: Array names are pointer constants. So it cannot be modified. void main() { int i=5; printf("%d",i++ + ++i); } Answer: Output Cannot be predicted exactly. Explanation: Side effects are involved in the evaluation of i

34)

15 35) void main() { int i=5; printf("%d",i+++++i); } Answer: Compiler Error Explanation: The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators. #include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } Answer: Compiler Error: Constant expression required in function main. Explanation: The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error). Note: Enumerated types can be used in case statements. main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } Answer: 1 Explanation: Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1. #define f(g,g2) g##g2 main() {

36)

37)

38)

15 int var12=100; printf("%d",f(var,12)); } Answer: 100 39) main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } Answer: 1 Explanation: before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop). 40) #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } Answer: M Explanation: p is pointing to character '\n'.str1 is pointing to character 'a' ++*p meAnswer:"p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10. then it is incremented to 11. the value of ++*p is 11. + +*str1 meAnswer:"str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. both 11 and 98 is added and result is subtracted from 32. i.e. (11+98-32)=77("M"); #include<stdio.h> main() { struct xx {

41)

15 int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); } Answer: Compiler Error Explanation: Initialization should not be done for structure members inside the structure declaration 42) #include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; } Answer: Compiler Error Explanation: in the end of nested structure yy a member have to be declared. main() { extern int i; i=20; printf("%d",sizeof(i)); } Answer: Linker error: undefined symbol '_i'. Explanation: extern declaration specifies that the variable i is defined somewhere else. The compiler passes the external variable to be resolved by the linker. So compiler doesn't find an error. During linking the linker searches for the definition of i. Since it is not found the linker flags an error.

43)

15 44) main() { printf("%d", out); } int out=100; Answer: Compiler error: undefined symbol out in function main. Explanation: The rule is that a variable is available for use from the point of declaration. Even though a is a global variable, it is not available for main. Hence an error. main() { extern out; printf("%d", out); } int out=100; Answer: 100 Explanation: This is the correct way of writing the previous program. main() { show(); } void show() { printf("I'm the greatest"); } Answer: Compier error: Type mismatch in redeclaration of show. Explanation: When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error. The solutions are as follows: 1. declare void show() in main() . 2. define show() before main(). 3. declare extern void show() before the use of show(). main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};

45)

46)

47)

15 printf(%u %u %u %d \n,a,*a,**a,***a); printf(%u %u %u %d \n,a+1,*a+1,**a+1,***a+1); } Answer: 100, 100, 100, 2 114, 104, 102, 3 Explanation: The given array is a 3-D one. It can also be viewed as a 1-D array. 2 4 7 8 3 4 2 2 2 3 3 4 100 102 104 106 108 110 112 114 116 118 120 122 thus, for the first printf statement a, *a, **a give address of first element . since the indirection ***a gives the value. Hence, the first line of the output. for the second printf a+1 increases in the third dimension thus points to value at 114, *a+1 increments in second dimension thus points to 104, **a +1 increments the first dimension thus points to 102 and ***a+1 first gets the value at first location and then increments it by 1. Hence, the output. 48) main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(%d ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(%d ,*p); p++; } } Answer: Compiler error: lvalue required. Explanation: Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for the any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue. 49) main( ) {

15 static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); *ptr++; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); *++ptr; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); ++*ptr; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); } Answer: 111 222 333 344 Explanation: Let us consider the array and the two pointers with some address a 0 1 2 3 4 100 102 104 106 108 p 100 102 104 106 108 1000 1002 1004 1006 1008 ptr 1000 2000 After execution of the instruction ptr++ value in ptr becomes 1002, if scaling factor for integer is 2 bytes. Now ptr p is value in ptr starting location of array p, (1002 1000) / (scaling factor) = 1, *ptr a = value at address pointed by ptr starting value of array a, 1002 has a value 102 so the value is (102 100)/(scaling factor) = 1, **ptr is the value stored in the location pointed by the pointer of ptr = value pointed by value pointed by 1002 = value pointed by 102 = 1. Hence the output of the firs printf is 1, 1, 1. After execution of *ptr++ increments value of the value in ptr by scaling factor, so it becomes1004. Hence, the outputs for the second printf are ptr p = 2, *ptr a = 2, **ptr = 2. After execution of *++ptr increments value of the value in ptr by scaling factor, so it becomes1004. Hence, the outputs for the third printf are ptr p = 3, *ptr a = 3, **ptr = 3. After execution of ++*ptr value in ptr remains the same, the value pointed by the value is incremented by the scaling factor. So the value in array p at location 1006 changes from 106 10 108,. Hence, the outputs for the fourth printf are ptr p = 1006 1000 = 3, *ptr a = 108 100 = 4, **ptr = 4.

15

50)

main( ) { char *q; int j; for (j=0; j<3; j++) scanf(%s ,(q+j)); for (j=0; j<3; j++) printf(%c ,*(q+j)); for (j=0; j<3; j++) printf(%s ,(q+j)); } Explanation: Here we have only one pointer to type char and since we take input in the same pointer thus we keep writing over in the same location, each time shifting the pointer value by 1. Suppose the inputs are MOUSE, TRACK and VIRTUAL. Then for the first input suppose the pointer starts at location 100 then the input one is stored as M O U S E \0 When the second input is given the pointer is incremented as j value becomes 1, so the input is filled in memory starting from 101. M T R A C K \0 The third input starts filling from the location 102 M T V I R T U A L \0 This is the final value stored . The first printf prints the values at the position q, q+1 and q+2 = M T V The second printf prints three strings starting from locations q, q+1, q+2 i.e MTVIRTUAL, TVIRTUAL and VIRTUAL.

51)

main( ) { void *vp; char ch = g, *cp = goofy; int j = 20; vp = &ch; printf(%c, *(char *)vp); vp = &j; printf(%d,*(int *)vp); vp = cp; printf(%s,(char *)vp + 3); } Answer: g20fy Explanation: Since a void pointer is used it can be type casted to any other type pointer. vp = &ch stores address of char ch and the next statement prints the value stored in vp after type casting it to the proper data type pointer. the output is g. Similarly the output from second printf is 20. The third printf

16 statement type casts it to print the string from the 4 th value hence the output is fy. 52) main ( ) { static char *s[ ] = {black, white, yellow, violet}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(%s,*--*++p + 3); } Answer: ck Explanation: In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 1 = s . the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is ck. main() { int i, n; char *x = girl; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(%s\n,x); x++; } } Answer: (blank space) irl rl l Explanation: Here a string (a pointer to char) is initialized with a value girl. The strlen function returns the length of the string, thus n has a value 4. The next statement assigns value at the nth location (\0) to the first location.

53)

16 Now the string becomes \0irl . Now the printf statement prints the string after each iteration it increments it starting position. Loop starts from 0 to 4. The first time x[0] = \0 hence it prints nothing and pointer value is incremented. The second time it prints from x[1] i.e irl and the third time it prints rl and the last time it prints l and the loop terminates. 54) int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); } Answer: Runtime error: Abnormal program termination. assert failed (i<5), <file name>,<line number> Explanation: asserts are used during debugging to make sure that certain conditions are satisfied. If assertion fails, the program will terminate reporting the same. After debugging use, #undef NDEBUG and this will disable all the assertions from the source code. Assertion is a good debugging tool to make use of. main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); } Answer: i = -1, +i = -1 Explanation: Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator). What are the files which are automatically opened when a C file is executed? Answer: stdin, stdout, stderr (standard input,standard output,standard error).

55)

56)

57) what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR); Answer : a: The SEEK_SET sets the file position marker to the starting of the file. b: The SEEK_CUR sets the file position marker to the current position

16 of the file. 58) main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute? Answer: First it checks for the leading white space and discards it.Then it matches with a quotation mark and then it reads all character upto another quotation mark. What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF) ; Answer & Explanation: fgets returns a pointer. So the correct end of file check is checking for != NULL. main() { main(); } Answer: Runtime error : Stack overflow. Explanation: main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error. main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); } Answer: Compiler error (at line number 4): size of v is Unknown. Explanation: You can create a variable of type void * but not of type void, since void is an empty type. In the second line you are creating variable vptr of type void * and v of type void hence an error.

59)

60)

61)

16

62)

main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); } Answer: 255 Explanation: In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character). The third sizeof is similar to the second one. main() { char not; not=!2; printf("%d",not); } Answer: 0 Explanation: ! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0. #define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); } Answer: TRUE Explanation: The input program to the compiler after processing by the preprocessor is, main(){ if(0)

63)

64)

16 puts("NULL"); else if(-1) puts("TRUE"); else puts("FALSE"); } Preprocessor doesn't replace the values given inside the double quotes. The check by if condition is boolean value false so it goes to else. In second if -1 is boolean value true hence "TRUE" is printed. 65) main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); } Answer: 1==1 is TRUE Explanation: When two strings are placed together (or separated by white-space) they are concatenated (this is called as "stringization" operation). So the string is as if it is given as "%d==1 is %s". The conditional operator( ?: ) evaluates to "TRUE". main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); } Answer: 2000 is a leap year Explanation: An ordinary program to check if leap year or not. #define max 5 #define int arr1[max] main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; arr2 name="name"; printf("%d %s",list[0],name); }

66)

67)

16 Answer: Compiler error (in the line arr1 list = {0,1,2,3,4}) Explanation: arr2 is declared of type array of size 5 of characters. So it can be used to declare the variable name of the type arr2. But it is not the case of arr1. Hence an error. Rule of Thumb: #defines are used for textual replacement whereas typedefs are used for declaring new types. 68) int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } Answer: 30,20,10 Explanation: '{' introduces new block and thus new scope. In the innermost block i is declared as, const volatile unsigned which is a valid declaration. i is assumed of type int. So printf prints 30. In the next block, i has value 20 and so printf prints 20. In the outermost block, i is declared as extern, so no storage space is allocated for it. After compilation is over the linker resolves it to global variable i (since it is the only variable visible there). So it prints i's value as 10. main() { int *j; { int i=10; j=&i; } printf("%d",*j); } Answer:

69)

16 10 Explanation: The variable i is a block level variable and the visibility is inside that block only. But the lifetime of i is lifetime of the function so it lives upto the exit of main function. Since the i is still allocated space, *j prints the value stored in i since j points i. 70) main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); } Answer: i = -1, -i = 1 Explanation: -i is executed and this execution doesn't affect the value of i. In printf first you just print the value of i. After that the value of the expression -i = -(-1) is printed. #include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } Answer: Compiler error Explanation: i is a constant. you cannot change the value of constant #include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d..%d",*p,*q); } Answer: garbagevalue..1 Explanation:

71)

72)

16 p=&a[2][2][2] you declare only two 2D arrays. but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. now q is pointing to starting address of a.if you print *q meAnswer:it will print first element of 3D array. 73) #include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); } Answer: hello 5 Explanation: if you declare i as register compiler will treat it as ordinary integer and it will take integer value. i value may be stored either in register or in memory. main() { int i=5,j=6,z; printf("%d",i+++j); } Answer: 11 Explanation: the expression i+++j is treated as (i++ + j) struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i;

74)

76)

16 printf("%d",x); } Answer: 2 Explanation: above all statements form a double circular linked list; abc.next->next->prev->next->i this one points to "ghi" node the value of at particular node is 2. 77) struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); } Answer: origin is(0,0) origin is(0,0) Explanation: pp is a pointer to structure. we can access the elements of the structure either with arrow mark or with indirection operator. Note: Since structure point is globally declared x & y are initialized as zeroes 78) main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); } Answer: 9 Explanation: return(i++) it will first return i and then increments. i.e. 10 will be returned.

16 79) main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); } Answer: 0001...0002...0004 Explanation: ++ operator when applied to pointers increments address according to their corresponding data-types. main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; } Answer: Compiler error Explanation: declaration of convert and format of getc() are wrong. main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; } Answer:

80)

81)

17 Compiler error. Explanation: argv[1] & argv[2] are strings. They are passed to the function sum without converting it to integer values. 82) # include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); } Answer: garbage value Explanation: ptr pointer is pointing to out of the array range of one_d. # include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); } Answer: bye Explanation: ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in effect of writing ccc(), since ptr[2] points to ccc. #include<stdio.h> main()

83)

85)

17 { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); } Answer: contents of zzz.c followed by an infinite loop Explanation: The condition is checked against EOF, it should be checked against NULL. 86) main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); } Answer: 0..0 Explanation: The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed. The values of i and j remain unchanged and get printed. main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; } Answer: 1000 Explanation: Normally the return value from the function is through the information from the accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000.

87)

17 88) int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p Answer: 4--0 3--1 2--2 Explanation: Let us assume some x= scanf("%d",&i)-t the values during execution will be, t i x 4 0 -4 3 1 -2 2 2 0 main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); } Answer: hello Explanation: The comma operator has associativity from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if becomes true so, "hello" will be printed. main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); } Explanation: i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and control comes out of the loop. In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){

89)

90)

91)

17 int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ } Answer: *k = &a Explanation: The argument of the function is a pointer to a pointer. 92) What are the following notations of defining functions known as? i. int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ } Answer: i. ANSI C notation ii. Kernighan & Ritche notation main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); } Answer: 300 Explanation: The pointer points to % since it is incremented twice and again decremented by 2, it points to '%d\n' and 300 is printed. main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); }

93)

94)

17 abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); } Explanation: The base address is modified only in function and as a result a points to 'b' then after incrementing to 'c' so bc will be printed. 95) func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); } Answer: The value if process is 0 ! Explanation: The function 'process' has 3 parameters - 1, a pointer to another function 2 and 3, integers. When this function is invoked from main, the following substitutions for formal parameters take place: func for pf, 3 for val1 and 6 for val2. This function returns the result of the operation performed by the function 'func'. The function func has two integer parameters. The formal parameters are substituted as 3 for a and 6 for b. since 3 is not equal to 6, a==b returns 0. therefore the function returns 0 which in turn is returned by the function 'process'. void main() { static int i=5; if(--i){ main(); printf("%d ",i); } }

96)

17 Answer: 0000 Explanation: The variable "I" is declared as static, hence memory for I will be allocated for only once, as it encounters the statement. The function main() will be called recursively unless I becomes equal to 0, and since main() is recursively called, so the value of static I ie., 0 will be printed every time the control is returned. 97) void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); } Answer: Here value is 7 Explanation: The int ret(int ret), ie., the function name and the argument name can be the same. Firstly, the function ret() is called in which the sizeof(float) ie., 4 is passed, after the first expression the value in ret will be 6, as ret is integer hence the value stored in ret will have implicit type conversion from float to int. The ret is returned in main() it is printed after and preincrement. 98) void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); } Answer: here in 3 6 Explanation: The char array 'a' will hold the initialized string, whose length will be counted from 0 till the null character. Hence the 'I' will hold the value equal to 5, after the pre-increment in the printf statement, the 6 will be printed. void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit);

99)

17 printf("%u \n",gotit=--giveit); } Answer: 0 65535 Explanation: 100) void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } Answer: Ok here Explanation: Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok here" is printed. void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); } Answer: Compiler Error. We cannot apply indirection on type void*. Explanation: Void pointer is a generic pointer type. No pointer arithmetic can be done on it. Void pointers are normally used for, 1. Passing generic pointers to functions and returning such pointers. 2. As a intermediate pointer type. 3. Used when the exact pointer type will be known at a later point of time. void main() { int i=i++,j=j++,k=k++; printf(%d%d%d,i,j,k); } Answer:

101)

102)

17 Garbage values. Explanation: An identifier is available to use in program code from the point of its declaration. So expressions such as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage value. Garbage in is garbage out (GIGO). 103) void main() { static int i=i++, j=j++, k=k++; printf(i = %d j = %d k = %d, i, j, k); } Answer: i=1j=1k=1 Explanation: Since static variables are initialized to zero by default. void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } } Answer: Garbage values Explanation: The inner printf executes first to print some garbage value. The printf returns no of characters printed and this value also cannot be predicted. Still the outer printf prints something and so returns a non-zero value. So it encounters the break statement and comes out of the while statement. main() { unsigned int i=10; while(i-->=0) printf("%u ",i); } Answer: 10 9 8 7 6 5 4 3 2 1 0 65535 65534.. Explanation:

104)

104)

17 Since i is an unsigned integer it can never become negative. So the expression i-- >=0 will always be true, leading to an infinite loop. 105) #include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); } Answer: Garbage-value 0 Explanation: The value of y%2 is 0. This value is assigned to x. The condition reduces to if (x) or in other words if(0) and so z goes uninitialized. Thumb Rule: Check all control paths to write bug free code. main() { int a[10]; printf("%d",*a+1-*a+3); } Answer: 4 Explanation: *a and -*a cancels out. The result is as simple as 1 + 3 = 4 ! 107) #define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); } Answer: 10 Explanation: The macro expands and evaluates to as: x+2*y-1 => x+(2*y)-1 => 10 main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }

106)

108)

17 Answer: 1 Explanation: Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to post-increment on i the value of i while printing is 1. 109) main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); } Answer: -1 Explanation: Unary + is the only dummy operator in C. So it has no effect on the expression and now the while loop is, while(i--!=0) which is false and so breaks out of while loop. The value 1 is printed due to the postdecrement operator. 113) main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); } Answer: Line no 5: Error: Lvalue required Line no 6: Cannot apply leftshift to float Line no 7: Cannot apply mod to float Explanation: Enumeration constants cannot be modified, so you cannot apply ++. Bit-wise operators and % operators cannot be applied on float values. fmod() is to find the modulus values for floats as % operator is for ints. 110) main() { int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i);

18 } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); } Answer: Compiler error: unknown type integer Compiler error: undeclared function write Explanation: Pascal keyword doesnt mean that pascal code can be used. It means that the function follows Pascal argument passing mechanism in calling the functions. 111) void pascal f(int i,int j,int k) { printf(%d %d %d,i, j, k); } void cdecl f(int i,int j,int k) { printf(%d %d %d,i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); } Answer: 10 11 12 13 12 11 10 13 Explanation: Pascal argument passing mechanism forces the arguments to be called from left to right. cdecl is the normal C argument passing mechanism where the arguments are passed from right to left.

112). What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); } Answer

18 -128 Explanation Notice the semicolon at the end of the for loop. THe initial value of the i is set to 0. The inner loop executes to increment the value from 0 to 127 (the positive range of char) and then it rotates to the negative value of -128. The condition in the for loop fails and so comes out of the for loop. It prints the current value of i that is -128. 113) main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); } Answer infinite loop Explanation The difference between the previous question and this one is that the char is declared to be unsigned. So the i++ can never yield negative value and i>=0 never becomes false so that it can come out of the for loop. 114) main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); } Answer: Behavior is implementation dependent. Explanation: The detail if the char is signed/unsigned by default is implementation dependent. If the implementation treats the char to be signed by default the program will print 128 and terminate. On the other hand if it considers char to be unsigned by default, it goes to infinite loop. Rule: You can write programs that have implementation dependent behavior. But dont write programs that depend on such behavior. 115) Is the following statement a declaration/definition. Find what does it mean? int (*x)[10]; Answer Definition. x is a pointer to array of(size 10) integers.

18 Apply clock-wise rule to find the meaning of this definition. 116). What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); } Answer Compiler error: Multiple declaration for error Explanation The name error is used in the two meanings. One means that it is a enumerator constant with value 1. The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the compiler cannot distinguish the meaning of error to know in what sense the error is used: error g1; g1=error; // which error it refers in each case? When the compiler can distinguish between usages then it will not issue error (in pure technical terms, names can only be overloaded in different namespaces). Note: the extra comma in the declaration, enum errorType{warning, error, exception,} is not an error. An extra comma is valid and is provided just for programmers convenience. 117) typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }

Answer 1 Explanation The three usages of name errors can be distinguishable by the compiler at any instance, so valid (they are in different namespaces). Typedef struct error{int warning, error, exception;}error;

18 This error can be used only by preceding the error by struct kayword as in: struct error someError; typedef struct error{int warning, error, exception;}error; This can be used only after . (dot) or -> (arrow) operator preceded by the variable name as in : g1.error =1; printf("%d",g1.error); typedef struct error{int warning, error, exception;}error; This can be used to define variables without using the preceding struct keyword as in: error g1; Since the compiler can perfectly distinguish between these three usages, it is perfectly legal and valid. Note This code is given here to just explain the concept behind. In real programming dont use such overloading of names. It reduces the readability of the code. Possible doesnt mean that we should use it! 118) #ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); } Answer: Compiler error : undefined symbol some Explanation: This is a very simple example for conditional compilation. The name something is not already known to the compiler making the declaration int some = 0; effectively removed from the source code. 119) #if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing);

18 } Answer 00 Explanation This code is to show that preprocessor expressions are not the same as the ordinary expressions. If a name is not known the preprocessor treats it to be equal to zero. 120). What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); } Answer 1 Explanation This is due to the close relation between the arrays and pointers. N dimensional arrays are made up of (N-1) dimensional arrays. arr2D is made up of a 3 single arrays that contains 3 integers each . arr2D arr2D[1] arr2D[2] arr2D[3]

The name arr2D refers to the beginning of all the 3 arrays. *arr2D refers to the start of the first 1D array (of 3 integers) that is the same address as arr2D. So the expression (arr2D == *arr2D) is true (1). Similarly, *arr2D is nothing but *(arr2D + 0), adding a zero doesnt change the value/meaning. Again arr2D[0] is the another way of telling *(arr2D + 0). So the expression (*(arr2D + 0) == arr2D[0]) is true (1). Since both parts of the expression evaluates to true the result is true(1) and the same is printed. 121) void main() { if(~0 == (unsigned int)-1)

18 printf(You can answer this if you know how values are represented in memory); } Answer You can answer this if you know how values are represented in memory Explanation ~ (tilde operator or bit-wise negation operator) operates on 0 to produce all ones to fill the space for an integer. 1 is represented in unsigned value as all 1s and so both are equal. 122) int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); } Answer x = 20 y = 10 Explanation This is one way of swapping two values. Simple checking will help understand this. 123) main() { char *p = ayqm; printf(%c,++*(p++)); } Answer: b main() { int i=5; printf("%d",++i++); } Answer: Compiler error: Lvalue required in function main Explanation: ++i yields an rvalue. For postfix ++ to operate an lvalue is required.

124)

18 125) main() { char *p = ayqm; char c; c = ++*p++; printf(%c,c); } Answer: b Explanation: There is no difference between the expression ++*(p++) and + +*p++. Parenthesis just works as a visual clue for the reader to see which expression is first evaluated. 126) int aaa() {printf(Hi);} int bbb(){printf(hello);} iny ccc(){printf(bye);} main() { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); } Answer: bye Explanation: int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns the type int. By the assignment ptr[0] = aaa; it means that the first function pointer in the array is initialized with the address of the function aaa. Similarly, the other two array elements also get initialized with the addresses of the functions bbb and ccc. Since ptr[2] contains the address of the function ccc, the call to the function ptr[2]() is same as calling ccc(). So it results in printing "bye". 127) main() { int i=5; printf(%d,i=++i ==6); } Answer:

18 1 Explanation: The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result. 128) main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); } Answer: A Explanation: Due to the assignment p[1] = c the string becomes, %c\n. Since this string becomes the format string for printf and ASCII value of 65 is A, the same gets printed. 129) void ( * abc( int, void ( *def) () ) ) (); Answer:: abc is a ptr to a function which takes 2 parameters .(a). an integer variable.(b). a ptrto a funtion which returns void. the return type of the function is void. Explanation: Apply the clock-wise rule to find the result. 130) main() { while (strcmp(some,some\0)) printf(Strings are not equal\n); } Answer: No output Explanation: Ending the string constant with \0 explicitly makes no difference. So some and some\0 are equivalent. So, strcmp returns 0 (false) hence breaking out of the while loop. main() { char str1[] = {s,o,m,e}; char str2[] = {s,o,m,e,\0}; while (strcmp(str1,str2))

131)

18 printf(Strings are not equal\n); } Answer: Strings are not equal Strings are not equal . Explanation: If a string constant is initialized explicitly with characters, \0 is not appended automatically to the string. Since str1 doesnt have null termination, it treats whatever the values that are in the following positions as part of the string until it randomly reaches a \0. So str1 and str2 are not the same, hence the result. 132) main() { int i = 3; for (;i++=0;) printf(%d,i); } Answer: Compiler Error: Lvalue required. Explanation: As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation. 133) void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(%d,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(%d,*cptr); } Answer: garbage-value 0 Explanation: The memory space allocated by malloc is uninitialized, whereas calloc returns the allocated memory space initialized to zeros. void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(%d, i); }

134)

18 Answer: 32767 Explanation: Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. This continues till the integer value rotates to positive value (32767). The while condition becomes false and hence, comes out of the while loop, printing the i value. 135) main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); } Answer: 10 10 Explanation: The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as: if(i,j) { if(i,j) j = i; else j = j; } else j = j; 136) 1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations. Answer: 1. 'const' applies to char * rather than 'a' ( pointer to a constant char ) *a='F' : illegal a="Hi" : legal 2. 'const' applies to 'a' rather than to the value of a (constant pointer to char ) *a='F' a="Hi" : legal : illegal

19 3. Same as 1. 137) main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); } Answer: 1 10 Explanation: The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result. 138) main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); } Answer: 41 Explanation: The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expressions truth value is 1. Because it is followed by || and true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression, when any of the operands become false, the whole expressions truth value becomes false and hence the remaining expression will not be evaluated. false && (anything) => false where (anything) will not be evaluated. 139) main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); } Answer: Compier Error: '&' on register variable Rule to Remember:

19 & (address of ) operator cannot be applied on register variables. 140) main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } Answer: Compiler Error: switch expression not integral Explanation: Switch statements can be applied only to integral types. 141) main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i); } } Answer: Linker Error : Unresolved external symbol i Explanation: The identifier i is available in the inner block and so using extern has no use in resolving it. 142) main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); } Answer: 16 16 16 Explanation: f1 and f2 both refer to the same memory location a. So changes through f1 and f2 ultimately affects only the value of a. 143) main()

19 { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); } Answer: sizeof(p) = 2, sizeof(*p) = 1, strlen(p) = 4 sizeof(a) = 5, strlen(a) = 4 Explanation: sizeof(p) => sizeof(char*) => 2 sizeof(*p) => sizeof(char) => 1 Similarly, sizeof(a) => size of the character array => 5 When sizeof operator is applied to an array it returns the sizeof the array and it is not the same as the sizeof the pointer variable. Here the sizeof(a) where a is the character array and the size of the array is 5 because the space necessary for the terminating NULL character should also be taken into account. 144) #define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(The dimension of the array is %d, DIM(arr, int)); } Answer: 10 Explanation: The size of integer array of 10 elements is 10 * sizeof(int). The macro expands to sizeof(arr)/sizeof(int) => 10 * sizeof(int) / sizeof(int) => 10. int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(The dimension of the array is %d, DIM(arr)); } Answer: 1 Explanation:

145)

19 Arrays cannot be passed to functions as arguments and only the pointers can be passed. So the argument is equivalent to int * array (this is one of the very few places where [] and * usage are equivalent). The return statement becomes, sizeof(int *)/ sizeof(int) that happens to be equal in this case. 146) main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } } Answer: 1 2 3 4 5 6 7 8 9 1 4 7 2 5 8 3 6 9 1 2 3 4 5 6 7 8 9 1 4 7 2 5 8 3 6 9

Explanation: *(*(p+i)+j) is equivalent to p[i][j]. 147) main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; } Answer: x=10 y=8 Explanation:

19 Using ^ like this is a way to swap two variables without using a temporary variable and that too in a single statement. Inside main(), void swap(); means that swap is a function that may take any number of arguments (not no arguments) and returns nothing. So this doesnt issue a compiler error by the call swap(&x,&y); that has two arguments. This convention is historically due to pre-ANSI style (referred to as Kernighan and Ritchie style) style of function declaration. In that style, the swap function will be defined as follows, void swap() int *a, int *b { *a ^= *b, *b ^= *a, *a ^= *b; } where the arguments follow the (). So naturally the declaration for swap will look like, void swap() which means the swap can take any number of arguments. 148) main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); } Answer: 11 Explanation: The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes are taken by casting it to char * and get printed. 149) main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); } Answer: 21 Explanation: The integer value 257 can be represented in binary as, 00000001 00000001. Remember that the INTEL machines are small-endian machines. Small-endian means that the lower order bytes are stored in the higher memory addresses and the higher order bytes are stored in lower addresses. The integer value 258 is stored in memory as: 00000001 00000010.

19 150) main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); } Answer: 556 Explanation: The integer value 300 in binary notation is: 00000001 00101100. It is stored in memory (small-endian) as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory representation as: 00101100 00000010. So the integer corresponding to it is 00000010 00101100 => 556. 151) #include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); } Answer: 0 Explanation: After ptr reaches the end of the string the value pointed by str is \0. So the value of str is less than that of least. So the value of least finally is 0. Declare an array of N pointers to functions returning pointers to functions returning pointers to characters? Answer: (char*(*)( )) (*ptr[N])( ); main() { struct student { char name[30]; struct date dob; }stud; struct date

152)

153)

19 { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.month, &student.dob.year);

&student.dob.day,

} Answer: Compiler Error: Undefined structure date Explanation: Inside the struct definition of student the member of type struct date is given. The compiler doesnt have the definition of date structure (forward reference is not allowed in C in this case) so it issues an error. 154) main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); } Answer: Compiler Error: Undefined structure date Explanation: Only declaration of struct date is available inside the structure definition of student but to have a variable of type struct date the definition of the structure is required. 155) There were 10 records stored in somefile.dat but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(somefile.dat,r); while(!feof(fp)) {

19 fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } } Explanation: fread reads 10 records and prints the names successfully. It will return EOF only when fread tries to read another record and fails reading EOF (and returning EOF). So it prints the last record again. After this only the condition feof(fp) becomes false, hence comes out of the while loop. 156) Is there any difference between the two declarations, 1. int foo(int *arr[]) and 2. int foo(int *arr[2]) Answer: No Explanation: Functions can only pass pointers and not arrays. The numbers that are allowed inside the [] is just for more readability. So there is no difference between the two declarations. What is the subtle error in the following code segment? void fun(int n, int arr[]) { int *p=0; int i=0; while(i++<n) p = &arr[i]; *p = 0; } Answer & Explanation: If the body of the loop never executes p is assigned no address. So p remains NULL where *p =0 may result in problem (may rise to runtime error NULL pointer assignment and terminate the program). What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; } Answer & Explanation:

157)

158)

19 assert macro should be used for debugging and finding out bugs. The check s != NULL is for error/exception handling and for that assert shouldnt be used. A plain if and the corresponding remedy statement has to be given. 159) What is the hidden bug with the following statement? assert(val++ != 0); Answer & Explanation: Assert macro is used for debugging and removed in release version. In assert, the experssion involves side-effects. So the behavior of the code becomes different in case of debug version and the release version thus leading to a subtle bug. Rule to Remember: Dont use expressions that have side-effects in assert statements. void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; } Answer: Undefined behavior Explanation: The second statement results in undefined behavior because it points to some location whose value may not be available for modification. This type of pointer in which the non-availability of the implementation of the referenced location is known as 'incomplete type'. #define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); } Answer: No output Explanation: The else part in which the printf is there becomes the else for if in the assert macro. Hence nothing is printed. The solution is to use conditional operator instead of if statement,

160)

161)

19 #define assert(cond) ((cond)?(0): (fprintf (stderr, "assertion failed: \ %s, file %s, line %d \n",#cond, __FILE__,__LINE__), abort())) Note: However this problem of matching with nearest else cannot be solved by the usual method of placing the if statement inside a block like this, #define assert(cond) { \ if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) \ } 162) Is the following code legal? struct a { int x; struct a b; } Answer: No Explanation: Is it not legal for a structure to contain a member that is of the same type as in this case. Because this will cause the structure declaration to be recursive without end. Is the following code legal? struct a { int x; struct a *b; } Answer: Yes. Explanation: *b is a pointer to type struct a and so is legal. The compiler knows, the size of the pointer to a structure even before the size of the structure is determined(as you know the pointer to any type is of same size). This type of structures is known as self-referencing structure. Is the following code legal? typedef struct a { int x; aType *b; }aType Answer:

163)

164)

20 No Explanation: The typename aType is not known at the point of declaring the structure (forward references are not made for typedefs). 165) Is the following code legal? typedef struct a aType; struct a { int x; aType *b; }; Answer: Yes Explanation: The typename aType is known at the point of declaring the structure, because it is already typedefined. Is the following code legal? void main() { typedef struct a aType; aType someVariable; struct a { int x; aType *b; }; } Answer: No Explanation: When the declaration, typedef struct a aType; is encountered body of struct a is not known. This is known as incomplete types. void main() { printf(sizeof (void *) = %d \n, sizeof( void *)); printf(sizeof (int *) = %d \n, sizeof(int *)); printf(sizeof (double *) = %d \n, sizeof(double *)); printf(sizeof(struct unknown *) = %d \n, sizeof(struct unknown *)); } Answer : sizeof (void *) = 2

166)

167)

20 sizeof (int *) = 2 sizeof (double *) = 2 sizeof(struct unknown *) = 2 Explanation: The pointer to any type is of same size. 168) char inputString[100] = {0}; To get string input from the keyboard which one of the following is better? 1) gets(inputString) 2) fgets(inputString, sizeof(inputString), fp) Answer & Explanation: The second one is better because gets(inputString) doesn't know the size of the string passed and so, if a very big input (here, more than 100 chars) the charactes will be written past the input string. When fgets is used with stdin performs the same operation as gets but is safe. Which version do you prefer of the following two, 1) printf(%s,str); // or the more curt one 2) printf(str); Answer & Explanation: Prefer the first one. If the str contains any format characters like %d then it will result in a subtle bug. void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(%d,k); } Answer: Compiler Error: Unexpected end of file in comment started in line 5. Explanation: The programmer intended to divide two integers, but by the maximum munch rule, the compiler treats the operator sequence / and * as /* which happens to be the starting of comment. To force what is intended by the programmer, int k = *ip/ *jp; // give space explicity separating / and * //or int k = *ip/(*jp); // put braces to force the intention will solve the problem. void main() {

169)

170)

171)

20 char ch; for(ch=0;ch<=127;ch++) printf(%c %d \n, ch, ch); } Answer: Implementaion dependent Explanation: The char type may be signed or unsigned by default. If it is signed then ch++ is executed after ch reaches 127 and rotates back to -128. Thus ch is always smaller than 127. 172) Is this code legal? int *ptr; ptr = (int *) 0x400; Answer: Yes Explanation: The pointer ptr will point at the integer in the memory location 0x400. main() { char a[4]="HELLO"; printf("%s",a); } Answer: Compiler error: Too many initializers Explanation: The array a is of size 4 but the string constant requires 6 bytes to get stored. 174) main() { char a[4]="HELL"; printf("%s",a); } Answer: HELL%@!~@!@???@~~! Explanation: The character array has the memory just enough to hold the string HELL and doesnt have enough space to store the terminating null character. So it prints the HELL correctly and continues to print garbage values till it accidentally comes across a NULL character. 175) main() { int a=10,*j;

173)

20 void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); } Answer: Compiler error: Cannot increment a void pointer Explanation: Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers. 176) main() { { { } printf("%d",i); } printf("%d",i); } int i; 177) Printf can be implemented by using __________ list. Answer: Variable length argument lists 178) char *someFun() { char *temp = string constant"; return temp; } int main() { puts(someFun()); } Answer: string constant Explanation: The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack, so this doesnt lead to dangling pointers. extern int i; int i=20; const volatile unsigned i=30; printf("%d",i);

20 179) char *someFun1() { char temp[ ] = string"; return temp; } char *someFun2() { char temp[ ] = {s, t,r,i,n,g}; return temp; } int main() { puts(someFun1()); puts(someFun2()); } Answer: Garbage values. Explanation: Both the functions suffer from the problem of dangling pointers. In someFun1() temp is a character array and so the space for it is allocated in heap and is initialized with character string string. This is created dynamically as the function is called, so is also deleted dynamically on exiting the function so the string data is not available in the calling function main() leading to print some garbage values. The function someFun2() also suffers from the same problem but the problem can be easily identified in this case. C Aptitude Aptitude Note : All the programs are tested under Turbo C/C++ compilers. It is assumed that, Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed). Predict the output or error(s) for the following: 26. void main() { int const * p=5; printf("%d",++(*p)); } Answer: Compiler error: Cannot modify a constant value.

20 Explanation: p is a pointer to a "constant integer". But we tried to change the value of the "constant integer". 27. main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); } Answer: mmmm aaaa nnnn Explanation: s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

28. main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); } Answer: I hate U Explanation: For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double. Rule of Thumb: Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) . 29. main()

20 { static int var = 5; printf("%d ",var--); if(var) main(); } Answer: 54321 Explanation: When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively. 30. main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } } Answer: 2222223465 Explanation: Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed. 31. main() { extern int i; i=20; printf("%d",i); } Answer: Linker Error : Undefined symbol '_i' Explanation: extern storage class in the following declaration, extern int i; specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred .

20

32. main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); } Answer: 00131 Explanation : Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression i++ && j++ && k++ is executed first. The result of this expression is 0 (1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for 0 || 0 combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1. 33. main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); } Answer: 12 Explanation: The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

34. main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; }

20 } Answer : three Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match. 35. main() { printf("%x",-1<<4); } Answer: fff0 Explanation : -1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value. 36. main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); } Answer: Compiler Error : Type mismatch in redeclaration of function display Explanation : In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.

37. main() { int c=- -2; printf("c=%d",c); } Answer: c=2; Explanation:

20 Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus. Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable. 38. #define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); } Answer: sizeof(i)=1 Explanation: Since the #define replaces the string int by the macro char 39. main() { int i=10; i=!i>14; printf("i=%d",i); } Answer: i=0 Explanation: In the expression !i>14 , NOT (!) operator has more precedence than > symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero). 40. #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } Answer: 77 Explanation: p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then

21 incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. Now performing (11 + 98 32), we get 77("M"); So we get the output 77 :: "M" (Ascii is 77). 41. #include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); } Answer: SomeGarbageValue---1 Explanation: p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array. 42. #include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } Answer: Compiler Error Explanation: You should not initialize variables in declaration 43. #include<stdio.h> main() { struct xx { int x; struct yy

21 { char s; struct xx *p; }; struct yy *q; }; } Answer: Compiler Error Explanation: The structure yy is nested within structure xx. Hence, the elements are of yy are to be accessed through the instance of structure xx, which needs an instance of yy to be known. If the instance is created after defining the structure the compiler will not know about the instance relative to xx. Hence for nested structure yy you have to declare member. 44. main() { printf("\nab"); printf("\bsi"); printf("\rha"); } Answer: hai Explanation: \n - newline \b - backspace \r - linefeed 45. main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } Answer: 45545 Explanation: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result. 46. #define square(x) x*x main() { int i;

21 i = 64/square(4); printf("%d",i); } Answer: 64 Explanation: the macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64 47. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); } Answer: ibj!gsjfoet Explanation: ++*p++ will be parse in the given order *p that is value at the location currently pointed by p will be taken ++*p the retrieved value will be incremented when ; is encountered the location will be incremented that is p++ will be executed Hence, in the while loop initial value pointed by p is h, which is changed to i by executing ++*p and pointer moves to point, a which is similarly changed to b and so on. Similarly blank space is converted to !. Thus, we obtain value in p becomes ibj! gsjfoet and since p reaches \0 and p1 points to p thus p1doesnot print anything. 48. #include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); } Answer: 50 Explanation: The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken. 49. #define clrscr() 100 main() {

21 clrscr(); printf("%d\n",clrscr()); } Answer: 100 Explanation: Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.The input program to compiler looks like this : main() { 100; printf("%d\n",100); } Note: 100; is an executable statement but with no action. So it doesn't give any problem 50. main() { printf("%p",main); } Answer: Some address will be printed. Explanation: Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers. 27) main() { clrscr(); } clrscr(); Answer: No output/error Explanation: The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function). 28) enum colors {BLACK,BLUE,GREEN} main() {

21

printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); } Answer: 0..1..2 Explanation: enum assigns numbers starting from 0, if not explicitly defined.

29)

void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); } Answer: 4..2 Explanation: the second pointer is of char type and not a far pointer

30)

main() { int i=400,j=300; printf("%d..%d"); } Answer: 400..300 Explanation: printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments given in the program,then printf will take garbage values. main() { char *p; p="Hello"; printf("%c\n",*&*p); } Answer:

31)

21 H Explanation: * is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H. 32) main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } Answer: Compiler error: Undefined label 'here' in function main Explanation: Labels have functions scope, in other words The scope of the labels is limited to functions . The label 'here' is available in function fun() Hence it is not visible in function main. main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); } Answer: Compiler error: Lvalue required in function main Explanation: Array names are pointer constants. So it cannot be modified.

33)

21

34)

void main() { int i=5; printf("%d",i++ + ++i); } Answer: Output Cannot be predicted exactly. Explanation: Side effects are involved in the evaluation of i void main() { int i=5; printf("%d",i+++++i); } Answer: Compiler Error Explanation: The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators. #include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } Answer: Compiler Error: Constant expression required in function main. Explanation: The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error). Note: Enumerated types can be used in case statements. main() { int i;

35)

36)

37)

21 printf("%d",scanf("%d",&i)); // value 10 is given as input here } Answer: 1 Explanation: Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1. 38) #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } Answer: 100 main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } Answer: 1 Explanation: before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop). 40) #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } Answer: M Explanation:

39)

21 p is pointing to character '\n'.str1 is pointing to character 'a' ++*p meAnswer:"p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10. then it is incremented to 11. the value of ++*p is 11. + +*str1 meAnswer:"str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. both 11 and 98 is added and result is subtracted from 32. i.e. (11+98-32)=77("M"); 41) #include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); } Answer: Compiler Error Explanation: Initialization should not be done for structure members inside the structure declaration #include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; } Answer: Compiler Error Explanation: in the end of nested structure yy a member have to be declared. main()

42)

43)

21 { extern int i; i=20; printf("%d",sizeof(i)); } Answer: Linker error: undefined symbol '_i'. Explanation: extern declaration specifies that the variable i is defined somewhere else. The compiler passes the external variable to be resolved by the linker. So compiler doesn't find an error. During linking the linker searches for the definition of i. Since it is not found the linker flags an error. 44) main() { printf("%d", out); } int out=100; Answer: Compiler error: undefined symbol out in function main. Explanation: The rule is that a variable is available for use from the point of declaration. Even though a is a global variable, it is not available for main. Hence an error. main() { extern out; printf("%d", out); } int out=100; Answer: 100 Explanation: This is the correct way of writing the previous program. main() { show(); } void show() { printf("I'm the greatest"); } Answer:

45)

46)

22 Compier error: Type mismatch in redeclaration of show. Explanation: When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error. The solutions are as follows: 1. declare void show() in main() . 2. define show() before main(). 3. declare extern void show() before the use of show(). 47) main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(%u %u %u %d \n,a,*a,**a,***a); printf(%u %u %u %d \n,a+1,*a+1,**a+1,***a+1); } Answer: 100, 100, 100, 2 114, 104, 102, 3 Explanation: The given array is a 3-D one. It can also be viewed as a 1-D array. 2 4 7 8 3 4 2 2 2 3 3 4 100 102 104 106 108 110 112 114 116 118 120 122 thus, for the first printf statement a, *a, **a give address of first element . since the indirection ***a gives the value. Hence, the first line of the output. for the second printf a+1 increases in the third dimension thus points to value at 114, *a+1 increments in second dimension thus points to 104, **a +1 increments the first dimension thus points to 102 and ***a+1 first gets the value at first location and then increments it by 1. Hence, the output. 48) main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(%d ,*a); a++; } p = a; for(j=0; j<5; j++) {

22 printf(%d ,*p); p++; } } Answer: Compiler error: lvalue required. Explanation: Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for the any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue. 49) main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); *ptr++; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); *++ptr; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); ++*ptr; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); } Answer: 111 222 333 344 Explanation: Let us consider the array and the two pointers with some address a 0 1 2 3 4 100 102 104 106 108 p 100 102 104 106 108 1000 1002 1004 1006 1008 ptr 1000 2000 After execution of the instruction ptr++ value in ptr becomes 1002, if scaling factor for integer is 2 bytes. Now ptr p is value in ptr starting location of array p, (1002 1000) / (scaling factor) = 1, *ptr a = value at address pointed by ptr starting value of array a, 1002 has a value 102 so

22 the value is (102 100)/(scaling factor) = 1, **ptr is the value stored in the location pointed by the pointer of ptr = value pointed by value pointed by 1002 = value pointed by 102 = 1. Hence the output of the firs printf is 1, 1, 1. After execution of *ptr++ increments value of the value in ptr by scaling factor, so it becomes1004. Hence, the outputs for the second printf are ptr p = 2, *ptr a = 2, **ptr = 2. After execution of *++ptr increments value of the value in ptr by scaling factor, so it becomes1004. Hence, the outputs for the third printf are ptr p = 3, *ptr a = 3, **ptr = 3. After execution of ++*ptr value in ptr remains the same, the value pointed by the value is incremented by the scaling factor. So the value in array p at location 1006 changes from 106 10 108,. Hence, the outputs for the fourth printf are ptr p = 1006 1000 = 3, *ptr a = 108 100 = 4, **ptr = 4. 50) main( ) { char *q; int j; for (j=0; j<3; j++) scanf(%s ,(q+j)); for (j=0; j<3; j++) printf(%c ,*(q+j)); for (j=0; j<3; j++) printf(%s ,(q+j)); } Explanation: Here we have only one pointer to type char and since we take input in the same pointer thus we keep writing over in the same location, each time shifting the pointer value by 1. Suppose the inputs are MOUSE, TRACK and VIRTUAL. Then for the first input suppose the pointer starts at location 100 then the input one is stored as M O U S E \0 When the second input is given the pointer is incremented as j value becomes 1, so the input is filled in memory starting from 101. M T R A C K \0 The third input starts filling from the location 102 M T V I R T U A L \0 This is the final value stored . The first printf prints the values at the position q, q+1 and q+2 = M T V The second printf prints three strings starting from locations q, q+1, q+2 i.e MTVIRTUAL, TVIRTUAL and VIRTUAL. 51) main( ) { void *vp; char ch = g, *cp = goofy; int j = 20; vp = &ch;

22 printf(%c, *(char *)vp); vp = &j; printf(%d,*(int *)vp); vp = cp; printf(%s,(char *)vp + 3); } Answer: g20fy Explanation: Since a void pointer is used it can be type casted to any other type pointer. vp = &ch stores address of char ch and the next statement prints the value stored in vp after type casting it to the proper data type pointer. the output is g. Similarly the output from second printf is 20. The third printf statement type casts it to print the string from the 4 th value hence the output is fy. 52) main ( ) { static char *s[ ] = {black, white, yellow, violet}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(%s,*--*++p + 3); } Answer: ck Explanation: In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 1 = s . the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is ck. main() { int i, n; char *x = girl; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(%s\n,x);

53)

22 x++; } } Answer: (blank space) irl rl l Explanation: Here a string (a pointer to char) is initialized with a value girl. The strlen function returns the length of the string, thus n has a value 4. The next statement assigns value at the nth location (\0) to the first location. Now the string becomes \0irl . Now the printf statement prints the string after each iteration it increments it starting position. Loop starts from 0 to 4. The first time x[0] = \0 hence it prints nothing and pointer value is incremented. The second time it prints from x[1] i.e irl and the third time it prints rl and the last time it prints l and the loop terminates. int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); } Answer: Runtime error: Abnormal program termination. assert failed (i<5), <file name>,<line number> Explanation: asserts are used during debugging to make sure that certain conditions are satisfied. If assertion fails, the program will terminate reporting the same. After debugging use, #undef NDEBUG and this will disable all the assertions from the source code. Assertion is a good debugging tool to make use of. main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); } Answer: i = -1, +i = -1 Explanation:

54)

55)

22 Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator). 56) What are the files which are automatically opened when a C file is executed? Answer: stdin, stdout, stderr (standard input,standard output,standard error).

57) what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR); Answer : a: The SEEK_SET sets the file position marker to the starting of the file. b: The SEEK_CUR sets the file position marker to the current position of the file. 58) main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute? Answer: First it checks for the leading white space and discards it.Then it matches with a quotation mark and then it reads all character upto another quotation mark. What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF) ; Answer & Explanation: fgets returns a pointer. So the correct end of file check is checking for != NULL. main() { main(); } Answer: Runtime error : Stack overflow. Explanation: main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error. main()

59)

60)

61)

22 { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); } Answer: Compiler error (at line number 4): size of v is Unknown. Explanation: You can create a variable of type void * but not of type void, since void is an empty type. In the second line you are creating variable vptr of type void * and v of type void hence an error. 62) main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); } Answer: 255 Explanation: In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character). The third sizeof is similar to the second one. main() { char not; not=!2; printf("%d",not); } Answer: 0 Explanation: ! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0. #define FALSE -1 #define TRUE 1 #define NULL 0 main() {

63)

64)

22 if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); } Answer: TRUE Explanation: The input program to the compiler after processing by the preprocessor is, main(){ if(0) puts("NULL"); else if(-1) puts("TRUE"); else puts("FALSE"); } Preprocessor doesn't replace the values given inside the double quotes. The check by if condition is boolean value false so it goes to else. In second if -1 is boolean value true hence "TRUE" is printed.

65)

main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); } Answer: 1==1 is TRUE Explanation: When two strings are placed together (or separated by white-space) they are concatenated (this is called as "stringization" operation). So the string is as if it is given as "%d==1 is %s". The conditional operator( ?: ) evaluates to "TRUE". main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year");

66)

22 } Answer: 2000 is a leap year Explanation: An ordinary program to check if leap year or not. 67) #define max 5 #define int arr1[max] main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; arr2 name="name"; printf("%d %s",list[0],name); } Answer: Compiler error (in the line arr1 list = {0,1,2,3,4}) Explanation: arr2 is declared of type array of size 5 of characters. So it can be used to declare the variable name of the type arr2. But it is not the case of arr1. Hence an error. Rule of Thumb: #defines are used for textual replacement whereas typedefs are used for declaring new types.

68)

int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } Answer: 30,20,10

22 Explanation: '{' introduces new block and thus new scope. In the innermost block i is declared as, const volatile unsigned which is a valid declaration. i is assumed of type int. So printf prints 30. In the next block, i has value 20 and so printf prints 20. In the outermost block, i is declared as extern, so no storage space is allocated for it. After compilation is over the linker resolves it to global variable i (since it is the only variable visible there). So it prints i's value as 10. 69) main() { int *j; { int i=10; j=&i; } printf("%d",*j); } Answer: 10 Explanation: The variable i is a block level variable and the visibility is inside that block only. But the lifetime of i is lifetime of the function so it lives upto the exit of main function. Since the i is still allocated space, *j prints the value stored in i since j points i. main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); } Answer: i = -1, -i = 1 Explanation: -i is executed and this execution doesn't affect the value of i. In printf first you just print the value of i. After that the value of the expression -i = -(-1) is printed. #include<stdio.h> main() { const int i=4; float j; j = ++i;

70)

71)

23 printf("%d %f", i,++j); } Answer: Compiler error Explanation: i is a constant. you cannot change the value of constant 72) #include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d..%d",*p,*q); } Answer: garbagevalue..1 Explanation: p=&a[2][2][2] you declare only two 2D arrays. but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. now q is pointing to starting address of a.if you print *q meAnswer:it will print first element of 3D array. #include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); } Answer: hello 5 Explanation: if you declare i as register compiler will treat it as ordinary integer and it will take integer value. i value may be stored either in register or in memory. main() { int i=5,j=6,z; printf("%d",i+++j); } Answer: 11

73)

74)

23 Explanation: the expression i+++j is treated as (i++ + j) 76) struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); } Answer: 2 Explanation: above all statements form a double circular linked list; abc.next->next->prev->next->i this one points to "ghi" node the value of at particular node is 2. struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); } Answer: origin is(0,0) origin is(0,0) Explanation: pp is a pointer to structure. we can access the elements of the structure either with arrow mark or with indirection operator.

77)

23 Note: Since structure point is globally declared x & y are initialized as zeroes 78) main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); } Answer: 9 Explanation: return(i++) it will first return i and then increments. i.e. 10 will be returned. main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); } Answer: 0001...0002...0004 Explanation: ++ operator when applied to pointers increments address according to their corresponding data-types. main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32;

79)

80)

23 } Answer: Compiler error Explanation: declaration of convert and format of getc() are wrong. 81) main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; } Answer: Compiler error. Explanation: argv[1] & argv[2] are strings. They are passed to the function sum without converting it to integer values. # include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); } Answer: garbage value Explanation: ptr pointer is pointing to out of the array range of one_d. # include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){

82)

83)

23 printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); } Answer: bye Explanation: ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in effect of writing ccc(), since ptr[2] points to ccc. 85) #include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); } Answer: contents of zzz.c followed by an infinite loop Explanation: The condition is checked against EOF, it should be checked against NULL. main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); } Answer: 0..0 Explanation: The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed. The values of i and j remain unchanged and get printed.

86)

23

87)

main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; } Answer: 1000 Explanation: Normally the return value from the function is through the information from the accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000.

88)

int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p Answer: 4--0 3--1 2--2 Explanation: Let us assume some x= scanf("%d",&i)-t the values during execution will be, t i x 4 0 -4 3 1 -2 2 2 0 main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); } Answer: hello

89)

23 Explanation: The comma operator has associativity from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if becomes true so, "hello" will be printed. 90) main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); } Explanation: i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and control comes out of the loop. In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ } Answer: *k = &a Explanation: The argument of the function is a pointer to a pointer. What are the following notations of defining functions known as? i. int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ } Answer: i. ANSI C notation ii. Kernighan & Ritche notation

91)

92)

23

93)

main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); } Answer: 300 Explanation: The pointer points to % since it is incremented twice and again decremented by 2, it points to '%d\n' and 300 is printed. main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); } Explanation: The base address is modified only in function and as a result a points to 'b' then after incrementing to 'c' so bc will be printed. func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); }

94)

95)

23 Answer: The value if process is 0 ! Explanation: The function 'process' has 3 parameters - 1, a pointer to another function 2 and 3, integers. When this function is invoked from main, the following substitutions for formal parameters take place: func for pf, 3 for val1 and 6 for val2. This function returns the result of the operation performed by the function 'func'. The function func has two integer parameters. The formal parameters are substituted as 3 for a and 6 for b. since 3 is not equal to 6, a==b returns 0. therefore the function returns 0 which in turn is returned by the function 'process'. 96) void main() { static int i=5; if(--i){ main(); printf("%d ",i); } } Answer: 0000 Explanation: The variable "I" is declared as static, hence memory for I will be allocated for only once, as it encounters the statement. The function main() will be called recursively unless I becomes equal to 0, and since main() is recursively called, so the value of static I ie., 0 will be printed every time the control is returned.

97)

void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); } Answer: Here value is 7 Explanation: The int ret(int ret), ie., the function name and the argument name can be the same. Firstly, the function ret() is called in which the sizeof(float) ie., 4 is passed, after the first expression the value in ret will be 6, as ret is integer hence

23 the value stored in ret will have implicit type conversion from float to int. The ret is returned in main() it is printed after and preincrement. 98) void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); } Answer: here in 3 6 Explanation: The char array 'a' will hold the initialized string, whose length will be counted from 0 till the null character. Hence the 'I' will hold the value equal to 5, after the pre-increment in the printf statement, the 6 will be printed. void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); } Answer: 0 65535

99)

100)

void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } Answer: Ok here Explanation: Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok here" is printed. void main()

101)

24 { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); } Answer: Compiler Error. We cannot apply indirection on type void*. Explanation: Void pointer is a generic pointer type. No pointer arithmetic can be done on it. Void pointers are normally used for, 4. Passing generic pointers to functions and returning such pointers. 5. As a intermediate pointer type. 6. Used when the exact pointer type will be known at a later point of time. 102) void main() { int i=i++,j=j++,k=k++; printf(%d%d%d,i,j,k); } Answer: Garbage values. Explanation: An identifier is available to use in program code from the point of its declaration. So expressions such as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage value. Garbage in is garbage out (GIGO).

103)

void main() { static int i=i++, j=j++, k=k++; printf(i = %d j = %d k = %d, i, j, k); } Answer: i=1j=1k=1 Explanation: Since static variables are initialized to zero by default. void main() { while(1){

104)

24 if(printf("%d",printf("%d"))) break; else continue; } } Answer: Garbage values Explanation: The inner printf executes first to print some garbage value. The printf returns no of characters printed and this value also cannot be predicted. Still the outer printf prints something and so returns a non-zero value. So it encounters the break statement and comes out of the while statement. 104) main() { unsigned int i=10; while(i-->=0) printf("%u ",i); } Answer: 10 9 8 7 6 5 4 3 2 1 0 65535 65534.. Explanation: Since i is an unsigned integer it can never become negative. So the expression i-- >=0 will always be true, leading to an infinite loop. 105) #include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); } Answer: Garbage-value 0 Explanation: The value of y%2 is 0. This value is assigned to x. The condition reduces to if (x) or in other words if(0) and so z goes uninitialized. Thumb Rule: Check all control paths to write bug free code. main() { int a[10]; printf("%d",*a+1-*a+3);

106)

24 } Answer: 4 Explanation: *a and -*a cancels out. The result is as simple as 1 + 3 = 4 ! 107) #define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); } Answer: 10 Explanation: The macro expands and evaluates to as: x+2*y-1 => x+(2*y)-1 => 10 main() { unsigned int i=65000; while(i++!=0); printf("%d",i); } Answer: 1 Explanation: Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to post-increment on i the value of i while printing is 1. 109) main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); } Answer: -1 Explanation: Unary + is the only dummy operator in C. So it has no effect on the expression and now the while loop is, while(i--!=0) which is false and so breaks out of while loop. The value 1 is printed due to the postdecrement operator.

108)

24

113)

main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); } Answer: Line no 5: Error: Lvalue required Line no 6: Cannot apply leftshift to float Line no 7: Cannot apply mod to float Explanation: Enumeration constants cannot be modified, so you cannot apply ++. Bit-wise operators and % operators cannot be applied on float values. fmod() is to find the modulus values for floats as % operator is for ints.

110)

main() { int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); } Answer: Compiler error: unknown type integer Compiler error: undeclared function write Explanation: Pascal keyword doesnt mean that pascal code can be used. It means that the function follows Pascal argument passing mechanism in calling the functions.

111)

void pascal f(int i,int j,int k) { printf(%d %d %d,i, j, k); } void cdecl f(int i,int j,int k) { printf(%d %d %d,i, j, k); } main()

24 { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); } Answer: 10 11 12 13 12 11 10 13 Explanation: Pascal argument passing mechanism forces the arguments to be called from left to right. cdecl is the normal C argument passing mechanism where the arguments are passed from right to left. 112). What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); } Answer -128 Explanation Notice the semicolon at the end of the for loop. THe initial value of the i is set to 0. The inner loop executes to increment the value from 0 to 127 (the positive range of char) and then it rotates to the negative value of -128. The condition in the for loop fails and so comes out of the for loop. It prints the current value of i that is -128. 113) main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); } Answer infinite loop Explanation The difference between the previous question and this one is that the char is declared to be unsigned. So the i++ can never yield negative value and i>=0 never becomes false so that it can come out of the for loop.

24

114) main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); } Answer: Behavior is implementation dependent. Explanation: The detail if the char is signed/unsigned by default is implementation dependent. If the implementation treats the char to be signed by default the program will print 128 and terminate. On the other hand if it considers char to be unsigned by default, it goes to infinite loop. Rule: You can write programs that have implementation dependent behavior. But dont write programs that depend on such behavior. 115) Is the following statement a declaration/definition. Find what does it mean? int (*x)[10]; Answer Definition. x is a pointer to array of(size 10) integers. Apply clock-wise rule to find the meaning of this definition. 116). What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); } Answer Compiler error: Multiple declaration for error Explanation The name error is used in the two meanings. One means that it is a enumerator constant with value 1. The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the compiler

24 cannot distinguish the meaning of error to know in what sense the error is used: error g1; g1=error; // which error it refers in each case? When the compiler can distinguish between usages then it will not issue error (in pure technical terms, names can only be overloaded in different namespaces). Note: the extra comma in the declaration, enum errorType{warning, error, exception,} is not an error. An extra comma is valid and is provided just for programmers convenience. 117) typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }

Answer 1 Explanation The three usages of name errors can be distinguishable by the compiler at any instance, so valid (they are in different namespaces). Typedef struct error{int warning, error, exception;}error; This error can be used only by preceding the error by struct kayword as in: struct error someError; typedef struct error{int warning, error, exception;}error; This can be used only after . (dot) or -> (arrow) operator preceded by the variable name as in : g1.error =1; printf("%d",g1.error); typedef struct error{int warning, error, exception;}error; This can be used to define variables without using the preceding struct keyword as in: error g1; Since the compiler can perfectly distinguish between these three usages, it is perfectly legal and valid. Note This code is given here to just explain the concept behind. In real programming dont use such overloading of names. It reduces the readability of the code. Possible doesnt mean that we should use it!

24

118)

#ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); } Answer: Compiler error : undefined symbol some Explanation: This is a very simple example for conditional compilation. The name something is not already known to the compiler making the declaration int some = 0; effectively removed from the source code.

119)

#if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); } Answer 00 Explanation This code is to show that preprocessor expressions are not the same as the ordinary expressions. If a name is not known the preprocessor treats it to be equal to zero.

120). What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); } Answer

24 1 Explanation This is due to the close relation between the arrays and pointers. N dimensional arrays are made up of (N-1) dimensional arrays. arr2D is made up of a 3 single arrays that contains 3 integers each . arr2D arr2D[1] arr2D[2] arr2D[3]

The name arr2D refers to the beginning of all the 3 arrays. *arr2D refers to the start of the first 1D array (of 3 integers) that is the same address as arr2D. So the expression (arr2D == *arr2D) is true (1). Similarly, *arr2D is nothing but *(arr2D + 0), adding a zero doesnt change the value/meaning. Again arr2D[0] is the another way of telling *(arr2D + 0). So the expression (*(arr2D + 0) == arr2D[0]) is true (1). Since both parts of the expression evaluates to true the result is true(1) and the same is printed. 121) void main() { if(~0 == (unsigned int)-1) printf(You can answer this if you know how values are represented in memory); } Answer You can answer this if you know how values are represented in memory Explanation ~ (tilde operator or bit-wise negation operator) operates on 0 to produce all ones to fill the space for an integer. 1 is represented in unsigned value as all 1s and so both are equal. 122) int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20;

24 swap(&x,&y); printf("x= %d y = %d\n",x,y); } Answer x = 20 y = 10 Explanation This is one way of swapping two values. Simple checking will help understand this. 123) main() { char *p = ayqm; printf(%c,++*(p++)); } Answer: b main() { int i=5; printf("%d",++i++); } Answer: Explanation: lvalue is required. 125) main() { char *p = ayqm; char c; c = ++*p++; printf(%c,c); } Answer: b Explanation: There is no difference between the expression ++*(p++) and + +*p++. Parenthesis just works as a visual clue for the reader to see which expression is first evaluated. 126) int aaa() {printf(Hi);} int bbb(){printf(hello);} iny ccc(){printf(bye);} main() Compiler error: Lvalue required in function main ++i yields an rvalue. For postfix ++ to operate an

124)

25 { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); } Answer: bye Explanation: int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns the type int. By the assignment ptr[0] = aaa; it means that the first function pointer in the array is initialized with the address of the function aaa. Similarly, the other two array elements also get initialized with the addresses of the functions bbb and ccc. Since ptr[2] contains the address of the function ccc, the call to the function ptr[2]() is same as calling ccc(). So it results in printing "bye". 127) main() { int i=5; printf(%d,i=++i ==6); } Answer: 1 Explanation: The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result. 128) main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); } Answer: A Explanation: Due to the assignment p[1] = c the string becomes, %c\n. Since this string becomes the format string for printf and ASCII value of 65 is A, the same gets printed. 129) void ( * abc( int, void ( *def) () ) ) (); Answer::

25 abc is a ptr to a function which takes 2 parameters .(a). an integer variable.(b). a ptrto a funtion which returns void. the return type of the function is void. Explanation: Apply the clock-wise rule to find the result. 130) main() { while (strcmp(some,some\0)) printf(Strings are not equal\n); } Answer: No output Explanation: Ending the string constant with \0 explicitly makes no difference. So some and some\0 are equivalent. So, strcmp returns 0 (false) hence breaking out of the while loop. main() { char str1[] = {s,o,m,e}; char str2[] = {s,o,m,e,\0}; while (strcmp(str1,str2)) printf(Strings are not equal\n); } Answer: Strings are not equal Strings are not equal . Explanation: If a string constant is initialized explicitly with characters, \0 is not appended automatically to the string. Since str1 doesnt have null termination, it treats whatever the values that are in the following positions as part of the string until it randomly reaches a \0. So str1 and str2 are not the same, hence the result. 132) main() { int i = 3; for (;i++=0;) printf(%d,i); } Answer: Explanation: Compiler Error: Lvalue required.

131)

25 As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation. 133) void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(%d,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(%d,*cptr); } Answer: garbage-value 0 Explanation: The memory space allocated by malloc is uninitialized, whereas calloc returns the allocated memory space initialized to zeros. void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(%d, i); } Answer: 32767 Explanation: Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. This continues till the integer value rotates to positive value (32767). The while condition becomes false and hence, comes out of the while loop, printing the i value. main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); } Answer: 10 10 Explanation: The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as: if(i,j) { if(i,j)

134)

135)

25 j = i; else j = j; } else j = j; 136) 1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations. Answer: 1. 'const' applies to char * rather than 'a' ( pointer to a constant char ) *a='F' : illegal a="Hi" : legal 2. 'const' applies to 'a' rather than to the value of a (constant pointer to char ) *a='F' a="Hi" 3. Same as 1. 137) main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); } Answer:1 10 Explanation: The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result. 138) main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); } Answer: : legal : illegal

25 41 Explanation: The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expressions truth value is 1. Because it is followed by || and true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression, when any of the operands become false, the whole expressions truth value becomes false and hence the remaining expression will not be evaluated. false && (anything) => false where (anything) will not be evaluated. 139) main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); } Answer: Compier Error: '&' on register variable Rule to Remember: & (address of ) operator cannot be applied on register variables. 140) main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } Answer: Compiler Error: switch expression not integral Explanation: Switch statements can be applied only to integral types. 141) main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i);

25 } } Answer: Linker Error : Unresolved external symbol i Explanation: The identifier i is available in the inner block and so using extern has no use in resolving it. 142) main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); } Answer: 16 16 16 Explanation: f1 and f2 both refer to the same memory location a. So changes through f1 and f2 ultimately affects only the value of a. 143) main() { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); } Answer: sizeof(p) = 2, sizeof(*p) = 1, strlen(p) = 4 sizeof(a) = 5, strlen(a) = 4 Explanation: sizeof(p) => sizeof(char*) => 2 sizeof(*p) => sizeof(char) => 1 Similarly, sizeof(a) => size of the character array => 5 When sizeof operator is applied to an array it returns the sizeof the array and it is not the same as the sizeof the pointer variable. Here the sizeof(a) where a is the character array and the size of the array is 5 because the space necessary for the terminating NULL character should also be taken into account. 144) #define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10];

25 printf(The dimension of the array is %d, DIM(arr, int)); } Answer: 10 Explanation: The size of integer array of 10 elements is 10 * sizeof(int). The macro expands to sizeof(arr)/sizeof(int) => 10 * sizeof(int) / sizeof(int) => 10. 145) int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(The dimension of the array is %d, DIM(arr)); } Answer: 1 Explanation: Arrays cannot be passed to functions as arguments and only the pointers can be passed. So the argument is equivalent to int * array (this is one of the very few places where [] and * usage are equivalent). The return statement becomes, sizeof(int *)/ sizeof(int) that happens to be equal in this case. main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } } Answer: 1 2 3 4 5 6 1 4 7 2 5 8 1 2 3 4 5 6 1 4 7 2 5 8

146)

25 7 8 9 3 6 9 7 8 9 3 6 9

Explanation: *(*(p+i)+j) is equivalent to p[i][j]. 147) main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; } Answer: x=10 y=8 Explanation: Using ^ like this is a way to swap two variables without using a temporary variable and that too in a single statement. Inside main(), void swap(); means that swap is a function that may take any number of arguments (not no arguments) and returns nothing. So this doesnt issue a compiler error by the call swap(&x,&y); that has two arguments. This convention is historically due to pre-ANSI style (referred to as Kernighan and Ritchie style) style of function declaration. In that style, the swap function will be defined as follows, void swap() int *a, int *b { *a ^= *b, *b ^= *a, *a ^= *b; } where the arguments follow the (). So naturally the declaration for swap will look like, void swap() which means the swap can take any number of arguments. 148) main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); } Answer:

25 11 Explanation: The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes are taken by casting it to char * and get printed. 149) main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); } Answer: 21 Explanation: The integer value 257 can be represented in binary as, 00000001 00000001. Remember that the INTEL machines are small-endian machines. Small-endian means that the lower order bytes are stored in the higher memory addresses and the higher order bytes are stored in lower addresses. The integer value 258 is stored in memory as: 00000001 00000010. 150) main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); } Answer: 556 Explanation: The integer value 300 in binary notation is: 00000001 00101100. It is stored in memory (small-endian) as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory representation as: 00101100 00000010. So the integer corresponding to it is 00000010 00101100 => 556. 151) #include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least);

25 } Answer: 0 Explanation: After ptr reaches the end of the string the value pointed by str is \0. So the value of str is less than that of least. So the value of least finally is 0. 152) Declare an array of N pointers to functions returning pointers to functions returning pointers to characters? Answer: (char*(*)( )) (*ptr[N])( ); main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); } Answer: Compiler Error: Undefined structure date Explanation: Inside the struct definition of student the member of type struct date is given. The compiler doesnt have the definition of date structure (forward reference is not allowed in C in this case) so it issues an error. main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; };

153)

154)

26 scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); } Answer: Compiler Error: Undefined structure date Explanation: Only declaration of struct date is available inside the structure definition of student but to have a variable of type struct date the definition of the structure is required. 155) There were 10 records stored in somefile.dat but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(somefile.dat,r); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } } Explanation: fread reads 10 records and prints the names successfully. It will return EOF only when fread tries to read another record and fails reading EOF (and returning EOF). So it prints the last record again. After this only the condition feof(fp) becomes false, hence comes out of the while loop. Is there any difference between the two declarations, 3. int foo(int *arr[]) and 4. int foo(int *arr[2]) Answer: No Explanation: Functions can only pass pointers and not arrays. The numbers that are allowed inside the [] is just for more readability. So there is no difference between the two declarations. What is the subtle error in the following code segment? void fun(int n, int arr[]) {

156)

157)

26 int *p=0; int i=0; while(i++<n) p = &arr[i]; *p = 0; } Answer & Explanation: If the body of the loop never executes p is assigned no address. So p remains NULL where *p =0 may result in problem (may rise to runtime error NULL pointer assignment and terminate the program). 158) What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; } Answer & Explanation: assert macro should be used for debugging and finding out bugs. The check s != NULL is for error/exception handling and for that assert shouldnt be used. A plain if and the corresponding remedy statement has to be given. What is the hidden bug with the following statement? assert(val++ != 0); Answer & Explanation: Assert macro is used for debugging and removed in release version. In assert, the experssion involves side-effects. So the behavior of the code becomes different in case of debug version and the release version thus leading to a subtle bug. Rule to Remember: Dont use expressions that have side-effects in assert statements.

159)

160)

void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; } Answer: Undefined behavior Explanation:

26 The second statement results in undefined behavior because it points to some location whose value may not be available for modification. This type of pointer in which the non-availability of the implementation of the referenced location is known as 'incomplete type'. 161) #define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); } Answer: No output Explanation: The else part in which the printf is there becomes the else for if in the assert macro. Hence nothing is printed. The solution is to use conditional operator instead of if statement, #define assert(cond) ((cond)?(0): (fprintf (stderr, "assertion failed: \ %s, file %s, line %d \n",#cond, __FILE__,__LINE__), abort())) Note: However this problem of matching with nearest else cannot be solved by the usual method of placing the if statement inside a block like this, #define assert(cond) { \ if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) \ } 162) Is the following code legal? struct a { int x; struct a b; } Answer: No Explanation: Is it not legal for a structure to contain a member that is of the same type as in this case. Because this will cause the structure declaration to be recursive without end.

26 163) Is the following code legal? struct a { int x; struct a *b; } Answer: Yes. Explanation: *b is a pointer to type struct a and so is legal. The compiler knows, the size of the pointer to a structure even before the size of the structure is determined(as you know the pointer to any type is of same size). This type of structures is known as self-referencing structure. Is the following code legal? typedef struct a { int x; aType *b; }aType Answer: No Explanation: The typename aType is not known at the point of declaring the structure (forward references are not made for typedefs). Is the following code legal? typedef struct a aType; struct a { int x; aType *b; }; Answer: Yes Explanation: The typename aType is known at the point of declaring the structure, because it is already typedefined. Is the following code legal? void main() { typedef struct a aType; aType someVariable; struct a {

164)

165)

166)

26 int x; aType *b; }; } Answer: No Explanation: When the declaration, typedef struct a aType; is encountered body of struct a is not known. This is known as incomplete types. 167) void main() { printf(sizeof (void *) = %d \n, sizeof( void *)); printf(sizeof (int *) = %d \n, sizeof(int *)); printf(sizeof (double *) = %d \n, sizeof(double *)); printf(sizeof(struct unknown *) = %d \n, sizeof(struct unknown *)); } Answer : sizeof (void *) = 2 sizeof (int *) = 2 sizeof (double *) = 2 sizeof(struct unknown *) = 2 Explanation: The pointer to any type is of same size. char inputString[100] = {0}; To get string input from the keyboard which one of the following is better? 1) gets(inputString) 2) fgets(inputString, sizeof(inputString), fp) Answer & Explanation: The second one is better because gets(inputString) doesn't know the size of the string passed and so, if a very big input (here, more than 100 chars) the charactes will be written past the input string. When fgets is used with stdin performs the same operation as gets but is safe. Which version do you prefer of the following two, 1) printf(%s,str); // or the more curt one 2) printf(str); Answer & Explanation: Prefer the first one. If the str contains any format characters like %d then it will result in a subtle bug. void main() { int i=10, j=2; int *ip= &i, *jp = &j;

168)

169)

170)

26 int k = *ip/*jp; printf(%d,k); } Answer: Compiler Error: Unexpected end of file in comment started in line 5. Explanation: The programmer intended to divide two integers, but by the maximum munch rule, the compiler treats the operator sequence / and * as /* which happens to be the starting of comment. To force what is intended by the programmer, int k = *ip/ *jp; // give space explicity separating / and * //or int k = *ip/(*jp); // put braces to force the intention will solve the problem. 171) void main() { char ch; for(ch=0;ch<=127;ch++) printf(%c %d \n, ch, ch); } Answer: Implementaion dependent Explanation: The char type may be signed or unsigned by default. If it is signed then ch++ is executed after ch reaches 127 and rotates back to -128. Thus ch is always smaller than 127. Is this code legal? int *ptr; ptr = (int *) 0x400; Answer: Yes Explanation: The pointer ptr will point at the integer in the memory location 0x400. main() { char a[4]="HELLO"; printf("%s",a); } Answer: Compiler error: Too many initializers Explanation:

172)

173)

26 The array a is of size 4 but the string constant requires 6 bytes to get stored. 174) main() { char a[4]="HELL"; printf("%s",a); } Answer: HELL%@!~@!@???@~~! Explanation: The character array has the memory just enough to hold the string HELL and doesnt have enough space to store the terminating null character. So it prints the HELL correctly and continues to print garbage values till it accidentally comes across a NULL character. main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); } Answer: Compiler error: Cannot increment a void pointer Explanation: Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers. 176) main() { { { } printf("%d",i); } printf("%d",i); } int i; 177) Printf can be implemented by using __________ list. extern int i; int i=20; const volatile unsigned i=30; printf("%d",i);

175)

26 Answer: Variable length argument lists 178) char *someFun() { char *temp = string constant"; return temp; } int main() { puts(someFun()); } Answer: string constant Explanation: The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack, so this doesnt lead to dangling pointers. 179) char *someFun1() { char temp[ ] = string"; return temp; } char *someFun2() { char temp[ ] = {s, t,r,i,n,g}; return temp; } int main() { puts(someFun1()); puts(someFun2()); } Answer: Garbage values. Explanation: Both the functions suffer from the problem of dangling pointers. In someFun1() temp is a character array and so the space for it is allocated in heap and is initialized with character string string. This is created dynamically as the function is called, so is also deleted dynamically on exiting the function so the string data is not available in the calling function main() leading to print some garbage values. The function someFun2() also suffers from the same problem but the problem can be easily identified in this case.

26

C++ Aptitude and OOPS C++ Aptitude and OOPS Note : All the programs are tested under Turbo C++ 3.0, 4.5 and Microsoft VC++ 6.0 compilers. It is assumed that, Programs run under Windows environment, The underlying machine is an x86 based system, Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed). 1) class Sample { public: int *ptr; Sample(int i) { ptr = new int(i); } ~Sample() { delete ptr; } void PrintVal() { cout << "The value is " << *ptr; } }; void SomeFunc(Sample x) { cout << "Say i am in someFunc " << endl; } int main() { Sample s1= 10; SomeFunc(s1); s1.PrintVal(); } Answer: Say i am in someFunc Null pointer assignment(Run-time error) Explanation:

26 As the object is passed by value to SomeFunc the destructor of the object is called when the control returns from the function. So when PrintVal is called it meets up with ptr that has been freed.The solution is to pass the Sample object by reference to SomeFunc: void SomeFunc(Sample &x) { cout << "Say i am in someFunc " << endl; } because when we pass objects by refernece that object is not destroyed. while returning from the function. 2) Which is the parameter that is added to every non-static member function when it is called? Answer: this pointer 3) class base { public: int bval; base(){ bval=0;} }; class deri:public base { public: int dval; deri(){ dval=1;} }; void SomeFunc(base *arr,int size) { for(int i=0; i<size; i++,arr++) cout<<arr->bval; cout<<endl; } int main() { base BaseArr[5]; SomeFunc(BaseArr,5); deri DeriArr[5]; SomeFunc(DeriArr,5); } Answer:

27 00000 01010 Explanation: The function SomeFunc expects two arguments.The first one is a pointer to an array of base class objects and the second one is the sizeof the array.The first call of someFunc calls it with an array of bae objects, so it works correctly and prints the bval of all the objects. When Somefunc is called the second time the argument passed is the pointeer to an array of derived class objects and not the array of base class objects. But that is what the function expects to be sent. So the derived class pointer is promoted to base class pointer and the address is sent to the function. SomeFunc() knows nothing about this and just treats the pointer as an array of base class objects. So when arr++ is met, the size of base class object is taken into consideration and is incremented by sizeof(int) bytes for bval (the deri class objects have bval and dval as members and so is of size >= sizeof(int)+sizeof(int) ). 4) class base { public: void baseFun(){ cout<<"from base"<<endl;} }; class deri:public base { public: void baseFun(){ cout<< "from derived"<<endl;} }; void SomeFunc(base *baseObj) { baseObj->baseFun(); } int main() { base baseObject; SomeFunc(&baseObject); deri deriObject; SomeFunc(&deriObject); } Answer: from base from base Explanation: As we have seen in the previous case, SomeFunc expects a pointer to a base class. Since a pointer to a derived class object is passed, it treats the argument only as a base class pointer and the corresponding base function is called. 5) class base {

27 public: virtual void baseFun(){ cout<<"from base"<<endl;} }; class deri:public base { public: void baseFun(){ cout<< "from derived"<<endl;} }; void SomeFunc(base *baseObj) { baseObj->baseFun(); } int main() { base baseObject; SomeFunc(&baseObject); deri deriObject; SomeFunc(&deriObject); } Answer: from base from derived Explanation: Remember that baseFunc is a virtual function. That means that it supports runtime polymorphism. So the function corresponding to the derived class object is called. void main() { int a, *pa, &ra; pa = &a; ra = a; cout <<"a="<<a <<"*pa="<<*pa <<"ra"<<ra ; } /* Answer : Compiler Error: 'ra',reference must be initialized Explanation : Pointers are different from references. One of the main differences is that the pointers can be both initialized and assigned, whereas references can only be initialized. So this code issues an error. */ const int size = 5; void print(int *ptr)

27 { cout<<ptr[0]; } void print(int ptr[size]) { cout<<ptr[0]; } void main() { int a[size] = {1,2,3,4,5}; int *b = new int(size); print(a); print(b); } /* Answer: Compiler Error : function 'void print(int *)' already has a body Explanation: Arrays cannot be passed to functions, only pointers (for arrays, base addresses) can be passed. So the arguments int *ptr and int prt[size] have no difference as function arguments. In other words, both the functoins have the same signature and so cannot be overloaded. */ class some{ public: ~some() { cout<<"some's destructor"<<endl; } }; void main() { some s; s.~some(); } /* Answer: some's destructor some's destructor Explanation: Destructors can be called explicitly. Here 's.~some()' explicitly calls the

27 destructor of 's'. When main() returns, destructor of s is called again, hence the result. */ #include <iostream.h> class fig2d { int dim1; int dim2; public: fig2d() { dim1=5; dim2=6;} virtual void operator<<(ostream & rhs); }; void fig2d::operator<<(ostream &rhs) { rhs <<this->dim1<<" "<<this->dim2<<" "; } /*class fig3d : public fig2d { int dim3; public: fig3d() { dim3=7;} virtual void operator<<(ostream &rhs); }; void fig3d::operator<<(ostream &rhs) { fig2d::operator <<(rhs); rhs<<this->dim3; } */ void main() { fig2d obj1; // fig3d obj2; obj1 << cout; // obj2 << cout; } /* Answer :

27 56 Explanation: In this program, the << operator is overloaded with ostream as argument. This enables the 'cout' to be present at the right-hand-side. Normally, 'cout' is implemented as global function, but it doesn't mean that 'cout' is not possible to be overloaded as member function. Overloading << as virtual member function becomes handy when the class in which it is overloaded is inherited, and this becomes available to be overrided. This is as opposed to global friend functions, where friend's are not inherited. */ class opOverload{ public: bool operator==(opOverload temp); }; bool opOverload::operator==(opOverload temp){ if(*this == temp ){ cout<<"The both are same objects\n"; return true; } else{ cout<<"The both are different\n"; return false; } } void main(){ opOverload a1, a2; a1= =a2; } Answer : Runtime Error: Stack Overflow Explanation : Just like normal functions, operator functions can be called recursively. This program just illustrates that point, by calling the operator == function recursively, leading to an infinite loop. class complex{ double re; double im; public: complex() : re(1),im(0.5) {}

27 bool operator==(complex &rhs); operator int(){} }; bool complex::operator == (complex &rhs){ if((this->re == rhs.re) && (this->im == rhs.im)) return true; else return false; } int main(){ complex c1; cout<< c1; } Answer : Garbage value Explanation: The programmer wishes to print the complex object using output re-direction operator,which he has not defined for his lass.But the compiler instead of giving an error sees the conversion function and converts the user defined object to standard object and prints some garbage value. class complex{ double re; double im; public: complex() : re(0),im(0) {} complex(double n) { re=n,im=n;}; complex(int m,int n) { re=m,im=n;} void print() { cout<<re; cout<<im;} }; void main(){ complex c3; double i=5; c3 = i; c3.print(); } Answer: 5,5 Explanation:

27 Though no operator= function taking complex, double is defined, the double on the rhs is converted into a temporary object using the single argument constructor taking double and assigned to the lvalue. void main() { int a, *pa, &ra; pa = &a; ra = a; cout <<"a="<<a <<"*pa="<<*pa <<"ra"<<ra ; } Answer : Compiler Error: 'ra',reference must be initialized Explanation : Pointers are different from references. One of the main differences is that the pointers can be both initialized and assigned, whereas references can only be initialized. So this code issues an error. Try it Yourself 1) Determine the output of the 'C++' Codelet. class base { public : out() { cout<<"base "; } }; class deri{ public : out() { cout<<"deri "; } }; void main() { deri dp[3]; base *bp = (base*)dp; for (int i=0; i<3;i++) (bp++)->out(); } 2) Justify the use of virtual constructors and destructors in C++.

27 3) Each C++ object possesses the 4 member fns,(which can be declared by the programmer explicitly or by the implementation if they are not available). What are those 4 functions? 4) What is wrong with this class declaration? class something { char *str; public: something(){ st = new char[10]; } ~something() { delete str; } }; 5) Inheritance is also known as -------- relationship. Containership as relationship. ________

6) When is it necessary to use member-wise initialization list (also known as header initialization list) in C++? 7) Which is the only operator in C++ which can be overloaded but NOT inherited. 8) Is there anything wrong with this C++ class declaration? class temp { int value1; mutable int value2; public : void fun(int val) const{ ((temp*) this)->value1 = 10; value2 = 10; } };

27 1. What is a modifier? Answer: A modifier, also called a modifying function is a member function that changes the value of at least one data member. In other words, an operation that modifies the state of an object. Modifiers are also known as mutators. 2. What is an accessor? Answer: An accessor is a class operation that does not modify the state of an object. The accessor functions need to be declared as const operations 3. Differentiate between a template class and class template. Answer: Template class: A generic definition or a parameterized class not instantiated until the client provides the needed information. Its jargon for plain templates. Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed. Its jargon for plain classes. 4. When does a name clash occur? Answer: A name clash occurs when a name is defined in more than one place. For example., two different class libraries could give two different classes the same name. If you try to use many class libraries at the same time, there is a fair chance that you will be unable to compile or link the program because of name clashes. 5. Define namespace. Answer: It is a feature in c++ to minimize name collisions in the global name space. This namespace keyword assigns a distinct name to a library that allows other libraries to use the same identifier names without creating any name collisions. Furthermore, the compiler uses the namespace signature for differentiating the definitions. 6. What is the use of using declaration. Answer: A using declaration makes it possible to use a name from a namespace without the scope operator. 7. What is an Iterator class? Answer: A class that is used to traverse through the objects maintained by a container class. There are five categories of iterators: input iterators, output iterators,

27 forward iterators, bidirectional iterators, random access. An iterator is an entity that gives access to the contents of a container object without violating encapsulation constraints. Access to the contents is granted on a one-ata-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered binary tree). The iterator is a construct, which provides an interface that, when called, yields either the next element in the container, or some value denoting the fact that there are no more elements to examine. Iterators hide the details of access to and update of the elements of a container class. The simplest and safest iterators are those that permit read-only access to the contents of a container class. The following code fragment shows how an iterator might appear in code: cont_iter:=new cont_iterator(); x:=cont_iter.next(); while x/=none do ... s(x); ... x:=cont_iter.next(); end; In this example, cont_iter is the name of the iterator. It is created on the first line by instantiation of cont_iterator class, an iterator class defined to iterate over some container class, cont. Succesive elements from the container are carried to x. The loop terminates when x is bound to some empty value. (Here, none)In the middle of the loop, there is s(x) an operation on x, the current element from the container. The next element of the container is obtained at the bottom of the loop. 9. List out some of the OODBMS available. Answer: GEMSTONE/OPAL of Gemstone systems. ONTOS of Ontos. Objectivity of Objectivity inc. Versant of Versant object technology. Object store of Object Design. ARDENT of ARDENT software. POET of POET software. 10. List out some of the object-oriented methodologies. Answer: Object Oriented Development (OOD) (Booch 1991,1994). Object Oriented Analysis and Design (OOA/D) (Coad and Yourdon 1991). Object Modelling Techniques (OMT) (Rumbaugh 1991). Object Oriented Software Engineering (Objectory) (Jacobson 1992).

28 Object Oriented Analysis (OOA) (Shlaer and Mellor 1992). The Fusion Method (Coleman 1991).

11. What is an incomplete type? Answer: Incomplete types refers to pointers in which there is non availability of the implementation of the referenced location or it points to some location whose value is not available for modification. Example: int *i=0x400 // i points to address 400 *i=0; //set the value of memory location pointed by i. Incomplete types are otherwise called uninitialized pointers. 12. What is a dangling pointer? Answer: A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed. 13. Differentiate between the message and method. Answer: Message Method Objects communicate by sending messages Provides response to a message. to each other. A message is sent to invoke a method. It is an implementation of an operation. 14. What is an adaptor class or Wrapper class? Answer: A class that has no functionality of its own. Its member functions hide the use of a third party software component or an object with the non-compatible interface or a nonobject- oriented implementation. 15. What is a Null object? Answer: It is an object of some class whose purpose is to indicate that a real object of that class does not exist. One common use for a null object is a return value from a member function that is supposed to return an object with some specified properties but cannot find such an object. 16. What is class invariant? Answer: A class invariant is a condition that defines all valid states for an object. It is a logical condition to ensure the correct working of a class. Class invariants must hold when an object is created, and they must be preserved under all operations of the class. In

28 particular all class invariants are both preconditions and post-conditions for all operations or member functions of the class. 17. What do you mean by Stack unwinding? Answer: It is a process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught. 18. Define precondition and post-condition to a member function. Answer: Precondition: A precondition is a condition that must be true on entry to a member function. A class is used correctly if preconditions are never false. An operation is not responsible for doing anything sensible if its precondition fails to hold. For example, the interface invariants of stack class say nothing about pushing yet another element on a stack that is already full. We say that isful() is a precondition of the push operation. Post-condition: A post-condition is a condition that must be true on exit from a member function if the precondition was valid on entry to that function. A class is implemented correctly if post-conditions are never false. For example, after pushing an element on the stack, we know that isempty() must necessarily hold. This is a post-condition of the push operation. 19. What are the conditions that have to be met for a condition to be an invariant of the class? Answer: The condition should hold at the end of every constructor. The condition should hold at the end of every mutator(non-const) operation. 20. What are proxy objects? Answer: Objects that stand for other objects are called proxy objects or surrogates. Example: template<class T> class Array2D { public: class Array1D { public: T& operator[] (int index); const T& operator[] (int index) const; ... };

28 Array1D operator[] (int index); const Array1D operator[] (int index) const; ... }; The following then becomes legal: Array2D<float>data(10,20); ........ cout<<data[3][6]; // fine Here data[3] yields an Array1D object and the operator [] invocation on that object yields the float in position(3,6) of the original two dimensional array. Clients of the Array2D class need not be aware of the presence of the Array1D class. Objects of this latter class stand for one-dimensional array objects that, conceptually, do not exist for clients of Array2D. Such clients program as if they were using real, live, two-dimensional arrays. Each Array1D object stands for a one-dimensional array that is absent from a conceptual model used by the clients of Array2D. In the above example, Array1D is a proxy class. Its instances stand for one-dimensional arrays that, conceptually, do not exist. 21. Name some pure object oriented languages. Answer: Smalltalk, Java, Eiffel, Sather. 22. Name the operators that cannot be overloaded. Answer: sizeof . .* .-> :: ?: 23. What is a node class? Answer: A node class is a class that, relies on the base class for services and implementation, provides a wider interface to te users than its base class, relies primarily on virtual functions in its public interface depends on all its direct and indirect base class can be understood only in the context of the base class can be used as base for further derivation can be used to create objects. A node class is a class that has added new services or functionality beyond the services inherited from its base class. 24. What is an orthogonal base class? Answer:

28 If two base classes have no overlapping methods or data they are said to be independent of, or orthogonal to each other. Orthogonal in the sense means that two classes operate in different dimensions and do not interfere with each other in any way. The same derived class may inherit such classes with no difficulty. 25. What is a container class? What are the types of container classes? Answer: A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container. 26. What is a protocol class? Answer: An abstract class is a protocol class if: it neither contains nor inherits from classes that contain member data, non-virtual functions, or private (or protected) members of any kind. it has a non-inline virtual destructor defined with an empty implementation, all member functions other than the destructor including inherited functions, are declared pure virtual functions and left undefined. 27. What is a mixin class? Answer: A class that provides some but not all of the implementation for a virtual base class is often called mixin. Derivation done just for the purpose of redefining the virtual functions in the base classes is often called mixin inheritance. Mixin classes typically don't share common bases. 28. What is a concrete class? Answer: A concrete class is used to define a useful object that can be instantiated as an automatic variable on the program stack. The implementation of a concrete class is defined. The concrete class is not intended to be a base class and no attempt to minimize dependency on other classes in the implementation or behavior of the class. 29.What is the handle class? Answer: A handle is a class that maintains a pointer to an object that is programmatically accessible through the public interface of the handle class. Explanation: In case of abstract classes, unless one manipulates the objects of these classes through pointers and references, the benefits of the virtual functions are lost. User code may become dependent on details of implementation classes because an abstract type

28 cannot be allocated statistically or on the stack without its size being known. Using pointers or references implies that the burden of memory management falls on the user. Another limitation of abstract class object is of fixed size. Classes however are used to represent concepts that require varying amounts of storage to implement them. A popular technique for dealing with these issues is to separate what is used as a single object in two parts: a handle providing the user interface and a representation holding all or most of the object's state. The connection between the handle and the representation is typically a pointer in the handle. Often, handles have a bit more data than the simple representation pointer, but not much more. Hence the layout of the handle is typically stable, even when the representation changes and also that handles are small enough to move around relatively freely so that the user neednt use the pointers and the references. 30. What is an action class? Answer: The simplest and most obvious way to specify an action in C++ is to write a function. However, if the action has to be delayed, has to be transmitted 'elsewhere' before being performed, requires its own data, has to be combined with other actions, etc then it often becomes attractive to provide the action in the form of a class that can execute the desired action and provide other services as well. Manipulators used with iostreams is an obvious example. Explanation: A common form of action class is a simple class containing just one virtual function. class Action { public: virtual int do_it( int )=0; virtual ~Action( ); } Given this, we can write code say a member that can store actions for later execution without using pointers to functions, without knowing anything about the objects involved, and without even knowing the name of the operation it invokes. For example: class write_file : public Action { File& f; public: int do_it(int) { return fwrite( ).suceed( ); } }; class error_message: public Action { response_box db(message.cstr( ),"Continue","Cancel","Retry"); switch (db.getresponse( ))

28 { case 0: return 0; case 1: abort(); case 2: current_operation.redo( );return 1; } }; A user of the Action class will be completely isolated from any knowledge of derived classes such as write_file and error_message. 31. When can you tell that a memory leak will occur? Answer: A memory leak occurs when a program loses the ability to free a block of dynamically allocated memory. 32.What is a parameterized type? Answer: A template is a parameterized construct or type containing generic code that can use or manipulate any type. It is called parameterized because an actual type is a parameter of the code body. Polymorphism may be achieved through parameterized types. This type of polymorphism is called parameteric polymorphism. Parameteric polymorphism is the mechanism by which the same code is used on different types passed as parameters. 33. Differentiate between a deep copy and a shallow copy? Answer: Deep copy involves using the contents of one object to create another instance of the same class. In a deep copy, the two objects may contain ht same information but the target object will have its own buffers and resources. the destruction of either object will not affect the remaining object. The overloaded assignment operator would create a deep copy of objects. Shallow copy involves copying the contents of one object into another instance of the same class thus creating a mirror image. Owing to straight copying of references and pointers, the two objects will share the same externally contained contents of the other object to be unpredictable. Explanation: Using a copy constructor we simply copy the data values member by member. This method of copying is called shallow copy. If the object is a simple class, comprised of built in types and no pointers this would be acceptable. This function would use the values and the objects and its behavior would not be altered with a shallow copy, only the addresses of pointers that are members are copied and not the value the address is pointing to. The data values of the object would then be inadvertently altered by the function. When the function goes out of scope, the copy of the object with all its data is popped off the stack.

28 If the object has any pointers a deep copy needs to be executed. With the deep copy of an object, memory is allocated for the object in free store and the elements pointed to are copied. A deep copy is used for objects that are returned from a function. 34. What is an opaque pointer? Answer: A pointer is said to be opaque if the definition of the type to which it points to is not included in the current translation unit. A translation unit is the result of merging an implementation file with all its headers and header files. 35. What is a smart pointer? Answer: A smart pointer is an object that acts, looks and feels like a normal pointer but offers more functionality. In C++, smart pointers are implemented as template classes that encapsulate a pointer and override standard pointer operators. They have a number of advantages over regular pointers. They are guaranteed to be initialized as either null pointers or pointers to a heap object. Indirection through a null pointer is checked. No delete is ever necessary. Objects are automatically freed when the last pointer to them has gone away. One significant problem with these smart pointers is that unlike regular pointers, they don't respect inheritance. Smart pointers are unattractive for polymorphic code. Given below is an example for the implementation of smart pointers. Example: template <class X> class smart_pointer { public: smart_pointer(); // makes a null pointer smart_pointer(const X& x) // makes pointer to copy of x X& operator *( ); const X& operator*( ) const; X* operator->() const; smart_pointer(const smart_pointer <X> &); const smart_pointer <X> & operator =(const smart_pointer<X>&); ~smart_pointer(); private: //... }; This class implement a smart pointer to an object of type X. The object itself is located on the heap. Here is how to use it: smart_pointer <employee> p= employee("Harris",1333); Like other overloaded operators, p will behave like a regular pointer, cout<<*p; p->raise_salary(0.5);

28 36. What is reflexive association? Answer: The 'is-a' is called a reflexive association because the reflexive association permits classes to bear the is-a association not only with their super-classes but also with themselves. It differs from a 'specializes-from' as 'specializes-from' is usually used to describe the association between a super-class and a sub-class. For example: Printer is-a printer. 37. What is slicing? Answer: Slicing means that the data added by a subclass are discarded when an object of the subclass is passed or returned by value or from a function expecting a base class object. Explanation: Consider the following class declaration: class base { ... base& operator =(const base&); base (const base&); } void fun( ) { base e=m; e=m; } As base copy functions don't know anything about the derived only the base part of the derived is copied. This is commonly referred to as slicing. One reason to pass objects of classes in a hierarchy is to avoid slicing. Other reasons are to preserve polymorphic behavior and to gain efficiency. 38. What is name mangling? Answer: Name mangling is the process through which your c++ compilers give each function in your program a unique name. In C++, all programs have at-least a few functions with the same name. Name mangling is a concession to the fact that linker always insists on all function names being unique. Example: In general, member names are made unique by concatenating the name of the member with that of the class e.g. given the declaration: class Bar { public: int ival; ... };

28

ival becomes something like: // a possible member name mangling ival__3Bar Consider this derivation: class Foo : public Bar { public: int ival; ... } The internal representation of a Foo object is the concatenation of its base and derived class members. // Pseudo C++ code // Internal representation of Foo class Foo { public: int ival__3Bar; int ival__3Foo; ... }; Unambiguous access of either ival members is achieved through name mangling. Member functions, because they can be overloaded, require an extensive mangling to provide each with a unique name. Here the compiler generates the same name for the two overloaded instances(Their argument lists make their instances unique). 39. What are proxy objects? Answer: Objects that points to other objects are called proxy objects or surrogates. Its an object that provides the same interface as its server object but does not have any functionality. During a method invocation, it routes data to the true server object and sends back the return value to the object. 40. Differentiate between declaration and definition in C++. Answer: A declaration introduces a name into the program; a definition provides a unique description of an entity (e.g. type, instance, and function). Declarations can be repeated in a given scope, it introduces a name in a given scope. There must be exactly one definition of every object, function or class used in a C++ program. A declaration is a definition unless: it declares a function without specifying its body, it contains an extern specifier and no initializer or function body, it is the declaration of a static class data member without a class definition, it is a class name definition, it is a typedef declaration. A definition is a declaration unless:

28 it defines a static class data member, it defines a non-inline member function. 41. What is cloning? Answer: An object can carry out copying in two ways i.e. it can set itself to be a copy of another object, or it can return a copy of itself. The latter process is called cloning. 42. Describe the main characteristics of static functions. Answer: The main characteristics of static functions include, It is without the a this pointer, It can't directly access the non-static members of its class It can't be declared const, volatile or virtual. It doesn't need to be invoked through an object of its class, although for convenience, it may. 43. Will the inline function be compiled as the inline function always? Justify. Answer: An inline function is a request and not a command. Hence it won't be compiled as an inline function always. Explanation: Inline-expansion could fail if the inline function contains loops, the address of an inline function is used, or an inline function is called in a complex expression. The rules for inlining are compiler dependent. 44. Define a way other than using the keyword inline to make a function inline. Answer: The function must be defined inside the class. 45. How can a '::' operator be used as unary operator? Answer: The scope operator can be used to refer to members of the global namespace. Because the global namespace doesnt have a name, the notation :: member-name refers to a member of the global namespace. This can be useful for referring to members of global namespace whose names have been hidden by names declared in nested local scope. Unless we specify to the compiler in which namespace to search for a declaration, the compiler simple searches the current scope, and any scopes in which the current scope is nested, to find the declaration for the name. 46. What is placement new? Answer: When you want to call a constructor directly, you use the placement new. Sometimes you have some raw memory that's already been allocated, and you need to construct an object in the memory you have. Operator new's special version placement new allows you to do it. class Widget {

29 public : Widget(int widgetsize); ... Widget* Construct_widget_int_buffer(void *buffer,int widgetsize) { return new(buffer) Widget(widgetsize); } }; This function returns a pointer to a Widget object that's constructed within the buffer passed to the function. Such a function might be useful for applications using shared memory or memory-mapped I/O, because objects in such applications must be placed at specific addresses or in memory allocated by special routines. OOAD 1. What do you mean by analysis and design? Analysis: Basically, it is the process of determining what needs to be done before how it should be done. In order to accomplish this, the developer refers the existing systems and documents. So, simply it is an art of discovery. Design: It is the process of adopting/choosing the one among the many, which best accomplishes the users needs. So, simply, it is compromising mechanism. 2. What are the steps involved in designing? Before getting into the design the designer should go through the SRS prepared by the System Analyst. The main tasks of design are Architectural Design and Detailed Design. In Architectural Design we find what are the main modules in the problem domain. In Detailed Design we find what should be done within each module. 3. What are the main underlying concepts of object orientation? Objects, messages, class, inheritance and polymorphism are the main concepts of object orientation. 4. What do u meant by "SBI" of an object? SBI stands for State, Behavior and Identity. Since every object has the above three. State: It is just a value to the attribute of an object at a particular time. Behaviour: It describes the actions and their reactions of that object. Identity: An object has an identity that characterizes its own existence. The identity

29 makes it possible to distinguish any object in an unambiguous way, and independently from its state. 5. Differentiate persistent & non-persistent objects? Persistent refers to an object's ability to transcend time or space. A persistent object stores/saves its state in a permanent storage system with out losing the information represented by the object. A non-persistent object is said to be transient or ephemeral. By default objects are considered as non-persistent. 6. What do you meant by active and passive objects? Active objects are one which instigate an interaction which owns a thread and they are responsible for handling control to other objects. In simple words it can be referred as client. Passive objects are one, which passively waits for the message to be processed. It waits for another object that requires its services. In simple words it can be referred as server. Diagram: client server (Active) (Passive) 7. What is meant by software development method? Software development method describes how to model and build software systems in a reliable and reproducible way. To put it simple, methods that are used to represent ones' thinking using graphical notations. 8. What are models and meta models? Model: It is a complete description of something (i.e. system). Meta model: It describes the model elements, syntax and semantics of the notation that allows their manipulation. 9. What do you meant by static and dynamic modeling? Static modeling is used to specify structure of the objects that exist in the problem domain. These are expressed using class, object and USECASE diagrams. But Dynamic modeling refers representing the object interactions during runtime. It is represented by sequence, activity, collaboration and statechart diagrams. 10. How to represent the interaction between the modeling elements? Model element is just a notation to represent (Graphically) the entities that exist in the problem domain. e.g. for modeling element is class notation, object notation etc. Relationships are used to represent the interaction between the modeling elements. The following are the Relationships.

29

Association: Its' just a semantic connection two classes. e.g.: uses class A class B Aggregation: Its' the relationship between two classes which are related in the fashion that master and slave. The master takes full rights than the slave. Since the slave works under the master. It is represented as line with diamond in the master area. ex: car contains wheels, etc. car car wheels

Containment: This relationship is applied when the part contained with in the whole part, dies when the whole part dies. It is represented as darked diamond at the whole part. example: class A{ //some code }; class B { A aa; // an object of class A; // some code for class B; }; In the above example we see that an object of class A is instantiated with in the class B. so the object class A dies when the object class B dies.we can represnt it in diagram like this. class A class B

Generalization: This relationship used when we want represents a class, which captures the common states of objects of different classes. It is represented as arrow line pointed at the class, which has captured the common states. class A

class B

class C

29 Dependency: It is the relationship between dependent and independent classes. Any change in the independent class will affect the states of the dependent class. DIAGRAM: class A class B 11. Why generalization is very strong? Even though Generalization satisfies Structural, Interface, Behaviour properties. It is mathematically very strong, as it is Antisymmetric and Transitive. Antisymmetric: employee is a person, but not all persons are employees. Mathematically all As are B, but all Bs not A. Transitive: A=>B, B=>c then A=>c. A. Salesman. B. Employee. C. Person. Note: All the other relationships satisfy all the properties like Structural properties, Interface properties, Behaviour properties. 12. Differentiate Aggregation and containment? Aggregation is the relationship between the whole and a part. We can add/subtract some properties in the part (slave) side. It won't affect the whole part. Best example is Car, which contains the wheels and some extra parts. Even though the parts are not there we can call it as car. But, in the case of containment the whole part is affected when the part within that got affected. The human body is an apt example for this relationship. When the whole body dies the parts (heart etc) are died. 13. Can link and Association applied interchangeably? No, You cannot apply the link and Association interchangeably. Since link is used represent the relationship between the two objects. But Association is used represent the relationship between the two classes. link :: student:Abhilash course:MCA Association:: student course 14. what is meant by "method-wars"? Before 1994 there were different methodologies like Rumbaugh, Booch, Jacobson, Meyer etc who followed their own notations to model the systems. The developers were in a dilemma to choose the method which best accomplishes their needs. This particular span was called as "method-wars" 15. Whether unified method and unified modeling language are same or different? Unified method is convergence of the Rumbaugh and Booch. Unified modeling lang. is the fusion of Rumbaugh, Booch and Jacobson as well as Betrand Meyer (whose contribution is "sequence diagram"). Its' the superset of all the methodologies. 16. Who were the three famous amigos and what was their contribution to the object

29 community? The Three amigos namely, James Rumbaugh (OMT): A veteran in analysis who came up with an idea about the objects and their Relationships (in particular Associations). Grady Booch: A veteran in design who came up with an idea about partitioning of systems into subsystems. Ivar Jacobson (Objectory): The father of USECASES, who described about the user and system interaction. 17. Differentiate the class representation of Booch, Rumbaugh and UML? If you look at the class representaiton of Rumbaugh and UML, It is some what similar and both are very easy to draw. Representation: OMT UML. Diagram:

Booch: In this method classes are represented as "Clouds" which are not very easy to draw as for as the developer's view is concern. Diagram:

18. What is an USECASE? Why it is needed? A Use Case is a description of a set of sequence of actions that a system performs that yields an observable result of value to a particular action. In SSAD process <=> In OOAD USECASE. It is represented elliptically. Representation:

19. Who is an Actor? An Actor is someone or something that must interact with the system.In addition to that an Actor initiates the process(that is USECASE). It is represented as a stickman like this. Diagram:

29

20. What is guard condition? Guard condition is one, which acts as a firewall. The access from a particular object can be made only when the particular condition is met. For Example, customer check customer number ATM. Here the object on the customer accesses the ATM facility only when the guard condition is met. 21. Differentiate the following notations? I: :obj1 :obj2 II: :obj1 :obj2

In the above representation I, obj1 sends message to obj2. But in the case of II the data is transferred from obj1 to obj2. 22. USECASE is an implementation independent notation. How will the designer give the implementation details of a particular USECASE to the programmer? This can be accomplished by specifying the relationship called "refinement which talks about the two different abstraction of the same thing. Or example, calculate pay calculate class1 class2 class3 23. Suppose a class acts an Actor in the problem domain, how to represent it in the static model? In this scenario you can use stereotype. Since stereotype is just a string that gives extra semantic to the particular entity/model element. It is given with in the << >>. class A << Actor>> attributes methods. 24. Why does the function arguments are called as "signatures"? The arguments distinguish functions with the same name (functional polymorphism). The name alone does not necessarily identify a unique function. However, the name and its arguments (signatures) will uniquely identify a function. In real life we see suppose, in class there are two guys with same name, but they

29 can be easily identified by their signatures. The same concept is applied here. ex: class person { public: char getsex(); void setsex(char); void setsex(int); }; In the above example we see that there is a function setsex() with same name but with different signature.

Playing with scanf function Operators & Expressions _____ _____________________________________

[Q001]. Determine which of the following are VALID identifiers. If invalid, state the reason. (a) sample1 (b) 5sample (c) data_7 (d) return (e) #fine (f) variable (g) 91-080-100 (h) name & age (i) _val(j) name_and_age Ans. (a) VALID (b) Invalid, since an identifier must begin with a letter or an underscore (c) VALID (d) Invalid, since return is a reserved word (e) Invalid, since an identifier must begin with a letter or an underscore (f) VALID (g) Invalid, since an identifier must begin with a letter or an underscore (h) Invalid, since blank spaces are not allowed (i) VALID (j) VALID _______________________________________________________________________ __________________________ [Q002]. Determine which of the following are VALID character constants. If invalid, state the reason. (a) 'y' (b) '\r' (c) 'Y' (d) '@' (e) '/r' (f) 'word' (g) '\0' (h) '\?' (i) '\065' (j) '\'' (k) ' ' Ans. (a) VALID (b) VALID (c) VALID (d) VALID

29 (e) Invalid, since escape sequences must be written with a backward slash (i.e. \) (f) Invalid, since a character constant cannot consist of multiple characters (g) VALID (null-character escape sequence) (h) VALID (i) VALID (Octal escape sequence) (j) VALID (k) VALID _______________________________________________________________________ __________________________ [Q003]. Determine which of the following are VALID string constants. If invalid, state the reason. (a) 'Hi Friends' (b) "abc,def,ghi" (c) "Qualification (d) "4325.76e-8" (e) "Don\'t sleep" (f) "He said, "You\'re great" (g) "" (h) " " (i) "Rs.100/-" Ans. (a) Invalid, since a string constant must be enclosed in double quotation marks (b) VALID (c) Invalid, since trailing quotation mark is missing (d) VALID (e) VALID (single-quote escape sequence) (f) Invalid, since the quotation marks and (optionally) apostrophe within the string cannot be expressed without the escape sequences. (g) VALID (h) VALID (i) VALID _______________________________________________________________________ __________________________ [Q004]. Determine which of the following numerical values are valid constants. If a constant is valid, specify whether it is integer or real. Also, specify the base for each valid integer constant. (a) 10,500 (b) 080 (c) 0.007 (d) 5.6e7 (e) 5.6e-7 (f) 0.2e-0.3 (g) 0.2e 0.3 (h) 0xaf9s82 (i) 0XABCDEFL (j) 0369CF (k) 87654321l (l) 87654321 Ans. (a) Invalid, since illegal character(,) (b) VALID (c) VALID (d) VALID (e) VALID (f) VALID (g) Invalid, since illegal character(blank space) (h) Invalid, since illegal character(s) (i) VALID (j) Invalid, since illegal characters (9, C, F), if intended as an octal constant.

29 (k) VALID (l) VALID _______________________________________________________________________ __________________________ [Q005]. Determine which of the following floating-point constants are VALID for the quantity (5 * 100000). (a) 500000 (b) 0.5e6 (c) 5E5 (d) 5e5 (e) 5e+5 (f) 500E3 (g) .5E6 (h) 50e4 (i) 50.E+4 (j) 5.0E+5 (k) All of the above (l) None of these Ans. (k) _______________________________________________________________________ __________________________ [Q006]. What will be the output of the following program : void main() { printf("%f",123.); } (a)123 (b)Compile-Time Error (c)123.00 (d)123.000000 Ans. (d) _______________________________________________________________________ __________________________ [Q007]. What will be the output of the following program : void main() { printf("%d",sizeof(integer)); } (a)2 (b)Compile-Time Error (c)4 (d)None of these Ans. (b) since there is no such data type called 'integer'. _______________________________________________________________________ __________________________ [Q008]. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("%d",sizeof str); } (a)14 (b)Compile-Time Error (c)15 (d)None of these Ans. (a)--c

29 _______________________________________________________________________ __________________________ [Q009]. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("%d",++(sizeof(str))); } (a)14 (b)Compile-Time Error (c)15 (d)None of these Ans. (b) _______________________________________________________________________ __________________________ [Q010]. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("%d",-sizeof(str)); } (a)14 (b)Compile-Time Error (c)-15 (d)-14 Ans. (c) _______________________________________________________________________ __________________________ [Q011]. What will be the output of the following program : void main() { printf("%d",!(100==100)+1); } (a)100 (b)0 (c)1 (d)2 Ans. (c) _______________________________________________________________________ __________________________ [Q012]. What will be the output of the following program : void main() { int x=5,y=6,z=2; z/=y/z==3?y/z:x*y; printf("%d",z); } (a)Compile-Time Error (b)2 (c)0 Ans. (c)

(d)1

30 _______________________________________________________________________ __________________________ [Q013]. What will be the output of the following program : void main() { printf("%d %d %d",5,!5,25 - !25); } (a)5 10 22 (b)5 5 25 (c)5 0 25 (d)5 1 24 Ans. (c) _______________________________________________________________________ __________________________ [Q014]. What will be the output of the following program : int main() { int a=500,b=100,c=30,d=40,e=19; a+=b-=c*=d/=e%=5; printf("%d %d %d %d %d",a,b,c,d,e); } (a)500 100 30 40 4 (b)Run-Time Error (c)700 200 300 10 4 (d)300 -200 300 10 4 Ans. (d) _______________________________________________________________________ __________________________ [Q015]. What will be the output of the following program : void main() { int a=500,b=100,c=30,d=40,e=19; if ((((a > b) ? c : d) >= e) && !((e <= d) ? ((a / 5) == b) : (c == d))) printf("Success"); else printf("Failure"); } (a)VALID : Success (b)VALID : Failure (c)INVALID (d)None of these Ans. (b) _______________________________________________________________________ __________________________ [Q016]. What will be the output of the following program : void main() { int a=1,b=2,c=3,d=4; printf("%d",!a?b?!c:!d:a); } (a)1 (b)2 (c)3

(d)4

30 Ans. (a) _______________________________________________________________________ __________________________ [Q017]. What will be the output of the following program : void main() { int i=12345,j=-13579,k=-24680; long ix=123456789; short sx=-2222; unsigned ux=5555; printf("\n%d %d %d %ld %d %u",i,j,k,ix,sx,ux); printf("\n\n%3d %3d %3d\n%3ld %3d %3u",i,j,k,ix,sx,ux); printf("\n\n%8d %8d %8d\n%15ld %8d %8u",i,j,k,ix,sx,ux); printf("\n\n%-8d %-8d\n%-8d %-15ld\n%-8d %-8u",i,j,k,ix,sx,ux); printf("\n\n%+8d %+8d\n%+8d %+15ld\n%+8d %8u",i,j,k,ix,sx,ux); printf("\n\n%08d %08d\n%08d %015ld\n%08d %08u",i,j,k,ix,sx,ux); } Ans. 12345 -13579 -24680 123456789 -2222 5555 12345 -13579 -24680 123456789 -2222 5555 12345 -13579 -24680 123456789 -2222 5555 12345 -13579 -24680 123456789 -2222 5555 +12345 -13579 -24680 +123456789 -2222 5555 00012345 -0013579 -0024680 000000123456789 -0002222 00005555 _______________________________________________________________________ __________________________ [Q018]. What will be the output of the following program : void main() { int i=12345,j=0xabcd9,k=077777; printf("%d %x %o",i,j,k); printf("\n%3d %3x %3o",i,j,k);

30 printf("\n%8d %8x %8o"i,j,k); printf("\n%-8d %-8x %-8o",i,j,k); printf("\n%+8d %+8x %+8o",i,j,k); printf("\n%08d %#8x %#8o",i,j,k); } Ans. 12345 abcd9 77777 12345 abcd9 77777 12345 abcd9 77777 12345 abcd9 77777 +12345 abcd9 77777 00012345 0xabcd9 077777 _______________________________________________________________________ __________________________ [Q019]. What will be the output of the following program : void main() { char c1='A', c2='B', c3='C'; printf("%c %c %c",c1,c2,c3); printf("\n%c%c%c",c1,c2,c3); printf("\n%3c %3c %3c",c1,c2,c3); printf("\n%3c%3c%3c",c1,c2,c3); printf("\nc1=%c c2=%c c3=%c",c1,c2,c3); } Ans. A B C ABC A B C A B C c1=A c2=B c3=C _______________________________________________________________________ __________________________ [Q020]. What will be the output of the following program : void main() { float a=2.5, b=0.0005, c=3000.; printf("%f %f %f",a,b,c); printf("\n%3f %3f %3f",a,b,c); printf("\n%8f %8f %8f",a,b,c); printf("\n%8.4f %8.4f %8.4f",a,b,c); printf("\n%8.3f %8.3f %8.3f",a,b,c); printf("\n%e %e %e",a,b,c); printf("\n%3e %3e %3e",a,b,c); printf("\n%12e %12e %12e",a,b,c); printf("\n%8.2e %8.2e %8.2e",a,b,c); printf("\n%-8f %-8f %-8f",a,b,c);

30 printf("\n%+8f %+8f %+8f",a,b,c); printf("\n%08f %08f %08f",a,b,c); printf("\n%#8f %#8f %#8f",a,b,c); printf("\n%g %g %g",a,b,c); printf("\n%#g %#g %#g"a,b,c); } Ans. 2.500000 0.000500 3000.000000 2.500000 0.000500 3000.000000 2.500000 0.000500 3000.000000 2.5000 0.0005 3000.0000 2.500 0.001 3000.000 2.500000e+000 5.000000e-004 3.000000e+003 2.500000e+000 5.000000e-004 3.000000e+003 2.500000e+000 5.000000e-004 3.000000e+003 2.5000e+000 5.0000e-004 3.0000e+003 2.50e+000 5.00e-004 3.00e+003 2.500000 0.000500 3000.000000 +2.500000 +0.000500 +3000.000000 2.500000 0.000500 3000.000000 2.500000 0.000500 3000.000000 2.5 0.0005 3000 2.500000 0.000500 3000.000000 _______________________________________________________________________ __________________________ [Q021]. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("%s",str); printf("\n%.5s",str); printf("\n%8.*s",5,str); printf("\n%-10s %.1s",str+6,str); } Ans. C For Swimmers C For C For Swimmers C _______________________________________________________________________ __________________________ [Q022]. What will be the output of the following program : void main() { int a=1,b=2,c=3;

30 scanf("%d %*d %d",&a,&b,&c); printf("a=%d b=%d c=%d",a,b,c); } [NOTE : 3 values entered by the user are:100 200 300] (a)1 2 3 (b)100 200 300 (c)100 200 3 (d)100 300 3 Ans. (d) _______________________________________________________________________ __________________________ [Q023]. What will be the output of the following program : void main() { char line[80]; // Max. length=80 Chars scanf("%[^,]s",line); printf("\n%s",line); } [NOTE : THE USER INPUT IS:Dear Friends, What is the output?] (a)Compile-Time Error (b)Dear Friends (c)What is the output? (d)None of these Ans. (b) _______________________________________________________________________ __________________________ [Q024]. What will be the output of the following program : void main() { char a,b,c; scanf("%c%c%c",&a,&b,&c); printf("a=%c b=%c c=%c",a,b,c); } [NOTE : THE USER INPUT IS :A B C] (a)a=A b=B c=C (b)a=A b= c=B (c)a=A b= c=C (d)None of these Ans. (b) _______________________________________________________________________ __________________________ [Q025]. What will be the output of the following program : void main() { int i=1; float f=2.25; scanf("%d a %f",&i,&f); printf("%d %.2f",i,f); }

30 [NOTE : THE USER INPUT IS:5 5.75] (a)1 2.25 (b)5 5.75 (c)5 2.25 (d)None of these Ans. (c) _______________________________________________________________________ __________________________ [Q026]. What will be the output of the following program : void main() { char a,b,c; scanf("%c %c %c",&a,&b,&c); printf("a=%c b=%c c=%c",a,b,c); } [NOTE : THE USER INPUT IS :ABC DEF GHI] (a)a=ABC b=DEF c=GHI (b)a=A b=B c=C (c)a=A b=D c=G (d)None of these Ans. (b) _______________________________________________________________________ __________________________ [Q027]. What will be the output of the following program : void main() { char a[80],b[80],c[80]; scanf("%1s %5s %3s",a,b,c); printf("%s %s %s",a,b,c); } [NOTE : THE USER INPUT IS:CMeansSea Ocean Vast] (a)C O V (b)C Means Sea (c)C Ocean Vas (d)None of these Ans. (b) _______________________________________________________________________ __________________________ [Q028]. What will be the output of the following program : void main() { int a,b,c; scanf("%1d %2d %3d",&a,&b,&c); printf("Sum=%d",a+b+c); } [NOTE : THE USER INPUT IS :123456 44 544] (a)Sum=480 (b)Sum=594 (c)Sum=589 these Ans. (a)

(d)None of

30 _______________________________________________________________________ __________________________ [Q029]. What happens when the following program is executed : void main() { char line[80]; scanf("%[^1234567890\n]",line); } (a)Accepts the string that contains DIGITS only. (b)Accepts the string that contains DIGITS and NEWLINE characters. (c)Accepts the string that contains anything other than the DIGITS and NEWLINE characters. (d)None of these Ans. (c) _______________________________________________________________________ __________________________ [Q030]. What happens when the following program is executed : void main() { char line[80]; scanf("%[^*]",line); } (a)Accepts the string that contains DIGITS & ALPHABETS only. (b)Accepts the string that contains * or asterisk characters only. (c)Accepts the string that contains anything other than the * or asterisk character. (d)None of these Ans. (c) _______________________________________________________________________ __________________________ [Q001]. What will be the output of the following program : void main() { printf(); } (a)Run-Time Error (b)Compile-Time Error (c)No Output (d)None of these Ans. (b) Since there must be enough arguments for the format. _____________________________________________________ [Q002]. What will be the output of the following program : void main() { printf(NULL);

30 } (a)Run-Time Error (b)Compile-Time Error (c)No Output (d)None of these Ans. (c) Since NULL is a constant value or NULL pointer value or a NULL string. ____________________________________________________ [Q003]. What will be the output of the following program : void main() { printf("%%",7); } (a)7 (b)Compile-Time Error (c)% (d)%% Ans. (c) Since % is a format specifier & excess arguments (more than required by the format) are merely ignored. __________________________________________________________ [Q004]. What will be the output of the following program : void main() { printf("//",5); } (a)5 (b)Compile-Time Error (c)/ Ans. (d) Since // is taken as string _____________________________________________________________

(d)//

[Q005]. What will be the output of the following program : void main() { printf("d%",8); } (a)8 (b)Compile-Time Error (c)d% (d)None of these Ans. (c) Since excess arguments (more than required by the format) are merely ignored. __________________________________________________________ [Q006]. What will be the output of the following program : void main() { printf("%d"+0,123); } (a)123 (b)Compile-Time Error (c)No Output (d)None of these Ans. (a) since"%d"+0 has no effect on the output operation. __________________________________________________________________

30 [Q007]. What will be the output of the following program : void main()-----------------doubt { printf("%d"+1,123); } (a)123 (b)Compile-Time Error (c)d (d)No Output

Ans. (c) since "%d"+1 (i.e 1 or > 0) affects the program output by considering "%d" as string and ignores 123 Where 1 refers to the index i.e. 2nd character in the array or string "%d". _______________________________________________________________ [Q008]. What will be the output of the following program : void main() { printf("%d",printf("Hi!")+printf("Bye")); } (a)ByeHi!6 (b)Hi!Bye6 (c)Compile-Time Error (d)None of these Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is 3+3=6 ____________________________________________________________ [Q009]. What will be the output of the following program : void main() { printf("%d",printf("Hi!")*printf("Bye")); } (a)ByeHi!6 (b)Hi!Bye9 (c)Hi!Bye (d)None of these Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is 3*3=9 ________________________________________________________ [Q010]. What will be the output of the following program : void main() { printf("%d",printf("")+printf("")); } (a)0 (b)No Output (c)Compile-Time Error (d)None of these Ans. (a) Since L->R priority & the length of the 2 empty strings are : 0+0=0 ____________________________________________________________ [Q011]. What will be the output of the following program : void main() { printf("Hi Friends"+3); }

30 (a)Hi Friends (b)Friends (c)Hi Friends3 (d)None of these

Ans. (b) Since (base adress)+0 points to the value 'H'. Now the NEW (base address) equals (base address)+3 that points to the character 'F'. Thus it prints the string from 'F' onwards. ___________________________________________________________ [Q012]. What will be the output of the following program : void main() { printf("C For ") + printf("Swimmers"); } (a)Compile-Time Error (b)C For Swimmers (c)Run-Time Error (d)None of these Ans. (b) It is a VALID C statement. ____________________________________________________________ [Q013]. What will be the output of the following program : void main() { printf("\/\*\-*\/"); } (a)Run-Time Error (b)\/*-*\/ (c)/*-*/ (d)None of these Ans. (c) Since \ is an escape sequence character. Be careful while analyzing such statements. _______________________________________________________________ [Q014]. What will be the output of the following program : int main() { int main=7; { printf("%d",main); return main; } printf("Bye"); } (a)Compile-Time Error (b)Run-Time Error (c)7Bye

(d)7

Ans. (d) It is a VALID C statement. Prints 7 and returns the same to the OS. NOTE: Last printf statement will not be executed. ________________________________________________________ [Q015]. What will be the output of the following program : void main()

31 { main(); } (a)Compile-Time Error (b)Run-Time Error (c)Infinite Loop (d)None of these Ans. (c) It is a VALID C statement. It is like a recursive function & the statements get executed infinite number of times. (All compilers will not support) _____________________________________________________________ [Q016]. What will be the output of the following program : void main() { printf("Work" "Hard"); } (a)Work (b)Hard (c)No Output (d)WorkHard Ans. (d) Since L->R priority. First it prints the word 'Work' & then 'Hard'. ______________________________________________________________ [Q017]. What will be the output of the following program : void main() { char str[]="%d"; int val=25; printf(str,val); } (a)Compile-Time Error (b)Run-Time Error (c)25 (d)None of these Ans. (c) It is a VALID C statement. First parameter contains the format specifier & the Second parameter contains the actual value 25. ________________________________________________________ [Q018]. What will be the output of the following program : void main() { int val=75; printf("%d",val,.,.); } (a)Compile-Time Error (b)Unpredictable (c)75 (d)None of these Ans. (b) Output is Unpredictable B'coz there are not enough arguments for the format. But it is a VALID C statement. ____________________________________________________ [Q019]. What will be the output of the following program : void main()---------------------doubt { int val=10; printf("%d",val+1,"%d",val--); }

31 (a)10 (b)11 10 (c)11 9 (d)10 9 Ans. (a) Since R->L priority. The second format specifier %d is an excess argument and it is ignored. ______________________________________________________________ [Q020]. What will be the output of the following program : void main() { int val=5; printf("%d %d %d %d",val,--val,++val,val--); } (a)3 4 6 5 (b)5 5 6 5 (c)4 4 5 5 (d)None of these Ans. (c) Since R->L priority. ___________________________________________________________

[Q021]. What will be the output of the following program : void main() { int val=5,num; printf("%d",scanf("%d %d",&val,&num)); } [NOTE : ASSUME 2 values are entered by the user are stored in the variables 'val' & 'num' respectively.] (a)1 (b)2 (c)5 (d)None of these Ans. (b) Since scanf statement returns the number of input fields successfully scanned, converted & stored. _______________________________________________________________________ __________________________ [Q022]. What will be the output of the following program : #define Compute(x,y,z) (x+y-z)---------------doubt

31 void main() { int x=2,y=3,z=4; printf("%d",Compute(y,z,(-x+y)) * Compute(z,x,(-y+z))); } (a)40 (b)30 (c)Compile-Time Error (d)None of these Ans. (b) Since it is macro function. NOTE : Be careful while doing such type of calculations. _______________________________________________________________________ __________________________ [Q023]. What will be the output of the following program : void main() { int m=10,n=20; printf("%d %d %d",m/* m-value */,/* n-value */n,m*/* Compute m*n */n); } (a)Run-Time Error (b)10 20 200 (c)Compile-Time Error (d)None of these Ans. (b) Since comments /*...*/ are ignored by the compiler. _______________________________________________________________________ __________________________ [Q024]. What will be the output of the following program : void main() { int m=10,n=20; /* printf("%d",m*n); } (a)VALID but No Output (b)VALID : Prints 200 (c)Compile-Time Error (d)None of these Ans. (c) Since COMMENT statement not ended properly i.e */ is missing in the above program. _______________________________________________________________________ __________________________ [Q025]. What will be the output of the following program : void main() { int val=97; "Printing..."+printf("%c",val); } (a)Printing...97 (b)97 (c)Compile-Time Error Ans. (d) Since alphabet 'a' is the ASCII equivalent of 97.

(d)a

31 _______________________________________________________________________ __________________________ [Q026]. What will be the output of the following program : void main() { int val=5; val=printf("C") + printf("Skills"); printf("%d",val); } (a)Skills5 (b)C1 (c)Compile-Time Error (d)CSkills7 Ans. (d) VALID Since 'printf' function return the no. of bytes output. _______________________________________________________________________ __________________________ [Q027]. What will be the output of the following program : void main() { char str[]="Test"; if ((printf("%s",str)) == 4) printf("Success"); else printf("Failure"); } (a)TestFailure (b)TestSuccess (c)Compile-Time Error (d)Test Ans. (b) VALID Since 'printf' function return the no. of bytes output. _______________________________________________________________________ __________________________ [Q028]. What will be the output of the following program : void main() { int val=5; printf("%*d",val); } (a) 5 (b)5 (c)Compile-Time Error (d)None of these Ans. (a) VALID Since '*' specifies the precision (i.e. the next argument in the precision). If no precision is specified then the value itself will be the precision value. Thus it prints 5 BLANK SPACES & then the value 5. _______________________________________________________________________ __________________________

31

[Q029]. What will be the output of the following program : void main()--------------------------doubt { int val=5; printf("%d5",val); } (a)Compile-Time Error (b)5 (c)55 (d) 5 Ans. (c) _______________________________________________________________________ __________________________ [Q030]. What will be the output of the following program : void main() } int val=5; printf("%d",5+val++); { (a)Compile-Time Error (b)5 (c)10 (d)11 Ans. (a) Since incorrect usage of pair of braces } and {. Correct usage : Each compound statement should be enclosed within a pair of braces, i.e { and }. _______________________________________________________________________ __________________________ Topic : Decision-making, Branching, Looping & Bit-wise operations [Q001]. What will be the output of the following program : void main() { printf("Hi!"); if (-1) printf("Bye"); } (a)No Output (b)Hi! (c)Bye (d)Hi!Bye Ans. (d) _______________________________________________________________________ __________________________ [Q002]. What will be the output of the following program : void main() { printf("Hi!"); if (0 || -1) printf("Bye");

31 } (a)No Output (b)Hi! (c)Bye (d)Hi!Bye Ans. (d) _______________________________________________________________________ __________________________ [Q003]. What will be the output of the following program : void main() { printf("Hi!"); if (!1) printf("Bye"); } (a)Compile-Time error (b)Hi! (c)Bye (d)Hi! Bye Ans. (b) _______________________________________________________________________ __________________________ [Q004]. What will be the output of the following program : void main() { printf("Hi!"); if !(0) printf("Bye"); } (a)Compile-Time error (b)Hi! (c)Bye (d)Hi! Bye Ans. (a) _______________________________________________________________________ __________________________ [Q005]. What will be the output of the following program : void main() { printf("Hi!"); if (-1+1+1+1-1-1-1+(-1)-(-1)) printf("Bye"); } (a)No Output (b)Hi! (c)Bye (d)Hi!Bye Ans. (d) _______________________________________________________________________ __________________________ [Q006]. What will be the output of the following program : void main()

31 { if (sizeof(int) && sizeof(float) && sizeof(float)/2-sizeof(int)) printf("Testing"); printf("OK"); } (a)No Output (b)OK (c)Testing (d)TestingOK Ans. (b) _______________________________________________________________________ __________________________ [Q007]. What will be the output of the following program : void main() { int a=1,b=2,c=3,d=4,e; if (e=(a & b | c ^ d)) printf("%d",e); } (a)0 (b)7 (c)3 (d)No Output Ans. (b) _______________________________________________________________________ __________________________ [Q008]. What will be the output of the following program : void main() { unsigned val=0xffff; if (~val) printf("%d",val); printf("%d",~val); } (a)Compile-Time error (b)-1 (c)0 (d)-1 0 Ans. (c) _______________________________________________________________________ __________________________ [Q009]. What will be the output of the following program : void main() { unsigned a=0xe75f,b=0x0EF4,c; c=(a|b); if ((c > a) && (c > b)) printf("%x",c); } (a)No Output (b)0xe75f (c)0xefff these Ans. (c)

.(d)None of

31 _______________________________________________________________________ __________________________ [Q010]. What will be the output of the following program : void main() { unsigned val=0xabcd; if (val>>16 | val<<16) { printf("Success"); return; } printf("Failure"); } (a)No Output (b)Success (c)Failure (d)SuccessFailure Ans.(b) _______________________________________________________________________ __________________________ [Q011]. What will be the output of the following program : void main() { unsigned x=0xf880,y=5,z; z=x<<y; printf("%#x %#x",z,x>>y-1); } (a)1000 f87 (b)8800 0xf88 (c)1000 f88 (d)0x1000 0xf88 Ans. (d) _______________________________________________________________________ __________________________ [Q012]. What will be the output of the following program : void main() { register int a=5; int *b=&a; printf("%d %d",a,*b); } (a)Compile-Time error (b)Run-Time error (c)5 5 (d)Unpredictable Ans. (a) _______________________________________________________________________ __________________________

31 [Q013]. What will be the output of the following program : auto int a=5; void main() { printf("%d",a); } (a)Compile-Time error (b)Run-Time error (c)5 (d)Unpredictable Ans. (a) _______________________________________________________________________ __________________________ [Q014]. What will be the output of the following program : void main() { auto int a=5; printf("%d",a); } (a)Compile-Time error (b)Run-Time error (c)5 (d)Unpredictable Ans. (c) _______________________________________________________________________ __________________________ [Q015]. What will be the output of the following program : void main() { int a=1,b=2,c=3,d=4; if (d > c) if (c > b) printf("%d %d",d,c); else if (c > a) printf("%d %d",c,d); if (c > a) if (b < a) printf("%d %d",c,a); else if (b < c) printf("%d %d",b,c); } (a)4 3 3 4 (b)4 3 3 2 (c)4 32 3 (d)4 33 1 Ans. (c) _______________________________________________________________________ __________________________ [Q016]. What will be the output of the following program :

31 void main() { int a=1,b=2,c=3,d=4; if (d > c) if (c > b) printf("%d %d",d,c); if (c > a) printf("%d %d",c,d); if (c > a) if (b < a) printf("%d %d",c,a); if (b < c) printf("%d %d",b,c); } (a)4 32 3 (b)4 33 42 3 (c)4 3 3 4 2 3 (d)None of these Ans. (b) _______________________________________________________________________ __________________________ [Q017]. What will be the output of the following program : void main() { int a=1; if (a == 2); printf("C Program"); } (a)No Output (b)C Program (c)Compile-Time Error Ans. (b) _______________________________________________________________________ __________________________ [Q018]. What will be the output of the following program : void main() { int a=1; if (a) printf("Test"); else; printf("Again"); } (a)Again (b)Test (c)Compile-Time Error (d)TestAgain Ans. (d)

32 _______________________________________________________________________ __________________________ [Q019]. What will be the output of the following program : void main() { int i=1; for (; i<4; i++); printf("%d\n",i); } (a)No Output (b)1 (c)4 (d)None of these 2 3 Ans. (c) _______________________________________________________________________ __________________________ [Q020]. What will be the output of the following program : void main() { int a,b; for (a=0; a<10; a++); for (b=25; b>9; b-=3); printf("%d %d",a,b); } (a)Compile-Time error (b)10 9 (c)10 7 (d)None of these Ans. (c) _______________________________________________________________________ __________________________ [Q021]. What will be the output of the following program : void main() { float i; for (i=0.1; i<0.4; i+=0.1) printf("%.1f",i); } (a)0.10.20.3 (b)Compile-Time Error (c)Run-Time Error (d)No Output Ans. (a) _______________________________________________________________________ __________________________ [Q022]. What will be the output of the following program :

32 void main() { int i; for (i=-10; !i; i++); printf("%d",-i); } (b)Compile-Time Error

(a)0 (c)10 (d)No Output Ans. (c) _______________________________________________________________________ __________________________ [Q023]. What will be the output of the following program : void main() { int i=5; do; printf("%d",i--); while (i>0); } (a)5 (b)54321 (c)Compile-Time Error (d)None of these Ans. (c) _______________________________________________________________________ __________________________ [Q024]. What will be the output of the following program : void main() { int i; for (i=2,i+=2; i<=9; i+=2) printf("%d",i); } (a)Compile-Time error (b)2468 (c)468 (d)None of these Ans. (c) _______________________________________________________________________ __________________________ [Q025]. What will be the output of the following program : void main() { int i=3; for (i--; i<7; i=7) printf("%d",i++); }

32 (a)No Output (b)3456 (c)23456 (d)None of these Ans. (d) _______________________________________________________________________ __________________________ [Q026]. What will be the output of the following program : void main() { int i; for (i=5; --i;) printf("%d",i); } (a)No Output (b)54321 (c)4321 (d)None of these Ans. (c) _______________________________________________________________________ __________________________ [Q027]. What will be the output of the following program : void main() { int choice=3; switch(choice) { default: printf("Default"); case 1: printf("Choice1"); break; case 2: printf("Choice2"); break; } } (a)No Output (b)Default (c)DefaultChoice1 (d)None of these Ans. (c) _______________________________________________________________________ __________________________ [Q028]. What will be the output of the following program : void main() {

32 static int choice; switch(--choice,choice-1,choice-1,choice+=2) { case 1: printf("Choice1"); break; case 2: printf("Choice2"); break; default: printf("Default"); } } (a)Choice1 (b)Choice2 (c)Default (d)None of these Ans. (a) _______________________________________________________________________ __________________________ [Q029]. What will be the output of the following program : void main() { for (;printf("");); } (a)Compile-Time error (b)Executes ONLY once (c)Executes INFINITELY (d)None of these Ans. (b) _______________________________________________________________________ __________________________ [Q030]. What will be the output of the following program : void main() { int i; for (;(i=4)?(i-4):i++;) printf("%d",i); } (a)Compile-Time error (b)4 (c)Infinite Loop (d)No Output Ans. (d) _______________________________________________________________________ __________________________ [Q031]. What will be the output of the following program :

32 void main() { static int j; for (j<5; j<5; j+=j<5) printf("%d",j++); } (a)024 (b)Compile-Time Error (c)01234 (d)No Output Ans. _______________________________________________________________________ __________________________ [Q032]. What will be the output of the following program : void main() { int i=9; for (i--; i--; i--) printf("%d ",i); } (a)9 6 3 (b)Compile-Time Error (c)7 5 3 1 (d)Infinite Loop Ans. _______________________________________________________________________ __________________________ [Q033]. What will be the output of the following program : void main() { int i; for (i=5; ++i; i-=3) printf("%d ",i); } (a)6 4 2 (b)Compile-Time Error (c)6 3 1 (d)Infinite Loop Ans. _______________________________________________________________________ __________________________ [Q034]. Which of the following code causes INFINITE Loop : (a)do while(1); (b)do;while(1); (d)do{}while(1); while(1); (i)Only (a) these (ii)Only (b), (c) & (d) (c)do;

(iii)None of these

(iv)All of

32 Ans. _______________________________________________________________________ __________________________ [Q035]. What will be the output of the following program : #define Loop(i) for (j=0; j<i; j++){ \ sum += i+j; \ } void main() { int i,j,sum=0; for (i=0; i<=3; i++) Loop(i) printf("%d",sum); } (a)Run-Time Error (b)Compile-Time Error (c)18 (d)0 Ans. _______________________________________________________________________ __________________________ (1) What will be output if you will compile and execute the following c code? void main(){ int i=320; char *ptr=(char *)&i; printf("%d",*ptr); }

(a)320 (b)1 (c)64 (d)Compiler error (e)None of above Output: (c)

32 Explanation: As we know size of int data type is two byte while char pointer can pointer one byte at time. Memory representation of int i=320 So char pointer ptr is pointing to only first byte as shown above figure. *ptr i.e. content of first byte is 01000000 and its decimal value is 64.

How to represent char, int and float data in memory? Data type tutorial.

(2) What will be output if you will compile and execute the following c code?

#define x 5+2 void main(){ int i; i=x*x*x; printf("%d",i); } (a)343 (b)27 (c)133 (d)Compiler error (e)None of above Output: (b)

32

Explanation: As we know #define is token pasting preprocessor it only paste the value of micro constant in the program before the actual compilation start. If you will see intermediate file you will find:

test.c 1: test.c 2: void main(){ test.c 3: int i; test.c 4: i=5+2*5+2*5+2; test.c 5: printf("%d",i); test.c 6: } test.c 7:

You can absorb #define only pastes the 5+2 in place of x in program. So, i=5+2*5+2*5+2 =5+10+10+2 =27

What is intermediate file and how to see intermediate file? Preprocessor tutorial.

(3) What will be output if you will compile and execute the following c code? void main(){

32 char c=125; c=c+10; printf("%d",c); } (a)135 (b)+INF (c)-121 (d)-8 (e)Compiler error Output: (c) Explanation: As we know char data type shows cyclic properties i.e. if you will increase or decrease the char variables beyond its maximum or minimum value respectively it will repeat same value according to following cyclic order: So, 125+1= 126 125+2= 127 125+3=-128 125+4=-127 125+5=-126 125+6=-125 125+7=-124 125+8=-123 125+9=-122

32 125+10=-121

What is cyclic nature of data type?

Data type tutorial. (4) What will be output if you will compile and execute the following c code? void main(){ float a=5.2; if(a==5.2) printf("Equal"); else if(a<5.2) printf("Less than"); else printf("Greater than"); } (a)Equal (b)Less than (c)Greater than (d)Compiler error (e)None of above Output: (b) Explanation: 5.2 is double constant in c. In c size of double data is 8 byte while a is float variable. Size of float variable is 4 byte. So double constant 5.2 is stored in memory as:

33

101.00 11001100 11001100 11001100 11001100 11001100 11001101 Content of variable a will store in the memory as: 101.00110 01100110 01100110 It is clear variable a is less than double constant 5.2 Since 5.2 is recurring float number so it different for float and double. Number likes 4.5, 3.25, 5.0 will store same values in float and double data type.

Note: In memory float and double data is stored in completely different way. If you want to see actual memory representation goes to question number (60) and (61). Data type tutorial. (5) What will be output if you will compile and execute the following c code? void main(){ int i=4,x; x=++i + ++i + ++i; printf("%d",x); } (a)21 (b)18 (c)12 (d)Compiler error (e)None of above Output: (a) Explanation:

33 In ++a, ++ is pre increment operator. In any mathematical expression pre increment operator first increment the variable up to break point then starts assigning the final value to all variable.

Step 1: Increment the variable I up to break point. Step 2: Start assigning final value 7 to all variable i in the expression. So, i=7+7+7=21

What is break point?

Operator tutorial.

(6) What will be output if you will compile and execute the following c code? void main(){ int a=2; if(a==2){ a=~a+2<<1; printf("%d",a); } else { break; } } (a)It will print nothing. (b)-3 (c)-2

33

(d)1 (e)Compiler error Output: (e) Explanation: Keyword break is not part of if-else statement. Hence it will show compiler error: Misplaced break Where we can use break keyword? Control statement tutorial (7) What will be output if you will compile and execute the following c code?

void main(){ int a=10; printf("%d %d %d",a,a++,++a); }

(a)12 11 11 (b)12 10 10 (c)11 11 12 (d)10 10 12 (e)Compiler error Output: (a) Explanation: In c printf function follows cdecl parameter passing scheme. In this scheme parameter is passed from right to left direction. So first ++a will pass and value of variable will be a=10 then a++ will pass now

33 value variable will be a=10 and at the end a will pass and value of a will be a=12. What is cedecl and pascal parameter passing convention? Function tutorial. (8) What will be output if you will compile and execute the following c code?

void main(){ char *str="Hello world"; printf("%d",printf("%s",str));

(a) 11Hello world (b) 10Hello world (c) Hello world10 (d) Hello world11 (e) Compiler error Output: (d) Explanation: Return type of printf function is integer and value of this integer is exactly equal to number of character including white space printf function prints. So, printf(Hello world) will return 13.

What is prototype of printf function?

33 Formatted I/O tutorial.

(9) What will be output if you will compile and execute the following c code?

#include "stdio.h" #include "string.h" void main(){ char *str=NULL; strcpy(str,"cquestionbank"); printf("%s",str); }

(a)cquestionbank (b)cquestionbank\0 (c)(null) (d)It will print nothing (e)Compiler error Output: (c) Explanation: We cannot copy any thing using strcpy function to the character pointer pointing to NULL. String tutorial.

More questions of string.

33

(10) What will be output if you will compile and execute the following c code?

#include "stdio.h" #include "string.h" void main(){ int i=0; for(;i<=2;) printf(" %d",++i); } (a)0 1 2 (b)0 1 2 3 (c)1 2 3 (d)Compiler error (e)Infinite loop Output: (c) Explanation: In for loop each part is optional.

Complete tutorial of looping in C.

(11) What will be output if you will compile and execute the following c code?

33

void main(){ int x; for(x=1;x<=5;x++); printf("%d",x); }

(a)4 (b)5 (c)6 (d)Compiler error (e)None of above

Output: (c) Explanation: Body of for loop is optional. In this question for loop will execute until value of variable x became six and condition became false.

Looping tutorial.

33 (12) What will be output if you will compile and execute the following c code?

void main(){ printf("%d",sizeof(5.2)); }

(a)2 (b)4 (c)8 (d)10 (e)Compiler error

Output: (c) Explanation: Default type of floating point constant is double. So 5.2 is double constant and its size is 8 byte.

Detail explanation of all types of constant in C.

(13) What will be output if you will compile and execute the following c code?

33

#include "stdio.h" #include "string.h" void main(){ char c='\08'; printf("%d",c); }

(a)8 (b)8 (c)9 (d)null (e)Compiler error Output: (e) Explanation: In c any character is starting with character \ represents octal number in character. As we know octal digits are: 0, 1, 2, 3, 4, 5, 6, and 7. So 8 is not an octal digit. Hence \08 is invalid octal character constant. Octal character constantan.

Hexadecimal character constant.

(14) What will be output if you will compile and execute the following c code? #define call(x,y) x##y

33 void main(){ int x=5,y=10,xy=20; printf("%d",xy+call(x,y)); } (a)35 (b)510 (c)15 (d)40 (e)None of above Output: (d) Explanation: ## is concatenation c preprocessor operator. It only concatenates the operands i.e. a##b=ab

If you will see intermediate file then you will find code has converted into following intermediate code before the start of actual compilation. Intermediate file:

test.c 1: test.c 2: void main(){ test.c 3: int x=5,y=10,xy=20; test.c 4: printf("%d",xy+xy); test.c 5: }

34 test.c 6:

It is clear call(x, y) has replaced by xy.

What is macro call? Preprocessor tutorial.

(15) What will be output if you will compile and execute the following c code? int * call(); void main(){ int *ptr; ptr=call(); clrscr(); printf("%d",*ptr); } int * call(){ int a=25; a++; return &a; } (a)25 (b)26 (c)Any address (d)Garbage value (e)Compiler error Output: (d) Explanation:

34 In this question variable a is a local variable and its scope and visibility is within the function call. After returning the address of a by function call variable a became dead while pointer ptr is still pointing to address of variable a. This problem is known as dangling pointer problem.

Complete pointer tutorial.

(16) What is error in following declaration?

struct outer{ int a; struct inner{ char c; }; };

(a)Nesting of structure is not allowed in c. (b)It is necessary to initialize the member variable. (c)Inner structure must have name. (d)Outer structure must have name. (e)There is not any error.

Output: (c) Explanation: It is necessary to assign name of inner structure at the time of declaration

34 other wise we cannot access the member of inner structure. So correct declaration is:

struct outer{ int a; struct inner{ char c; }name; };

Structure tutorial.

Union tutorial.

(17) What will be output if you will compile and execute the following c code?

void main(){ int array[]={10,20,30,40}; printf("%d",-2[array]); }

(a)-60 (b)-30 (c)60

34

(d)Garbage value (e)Compiler error

Output: (b) Explanation: In c, array[2]=*(array+2)=*(2+array)=2[array]=30

Array tutorial.

Array of pointer.

How to read complex pointers.

(18) What will be output if you will compile and execute the following c code?

void main(){ int i=10;

34

static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater than"); else printf("Less than"); } (a)Equal (b)Greater than (c)Less than (d)Compiler error (e)None of above Output: (d) Explanation: static variables are load time entity while auto variables are run time entity. We can not initialize any load time variable by the run time variable. In this example i is run time variable while x is load time variable.

What is storage class?

(18) What will be output if you will compile and execute the following c code?

void main(){

34 int i=5,j=2; if(++i>j++||i++>j++) printf("%d",i+j); }

(a)7 (b)11 (c)8 (d)9 (e)Compiler error

Output: (d) Explanation: || is logical OR operator. In C logical OR operator doesnt check second operand if first operand is true. ++i>j++ || i++>j++ First operand: ++i>j++ Second operand: i++>j++ First operand ++i > j++ => 6 > 2 Since first operand is true so it will not check second operand. Hence i= 6 and j=3

34

Properties of && operator.

Operator tutorial with examples.

(19) What will be output if you will compile and execute the following c code?

#define max 5; void main(){ int i=0; i=max++; printf("%d",i++); }

(a)5 (b)6 (c)7 (d)0 (e)Compiler error Output: (e) Explanation: #define is token pasting preprocessor. If you will see intermediate file: test.i

34

test.c 1: test.c 2: void main(){ test.c 3: int i=0; test.c 4: i=5++; test.c 5: printf("%d",i++); test.c 6: } test.c 7:

It is clear macro constant max has replaced by 5. It is illegal to increment the constant number. Hence compiler will show Lvalue required.

What is Lvalue and Rvalue?

How to see intermediate file?

Preprocessor questions and answer.

(20) What will be output if you will compile and execute the following c code?

void main(){ double far* p,q; printf("%d",sizeof(p)+sizeof q);

34 }

(a)12 (b)8 (c)4 (d)1 (e)Compiler error

Output: (a) Explanation: It is clear p is far pointer and size of far pointer is 4 byte while q is double variable and size of double variable is 8 byte.

What is near pointer?

What is far pointer?

What is huge pointer?

Complete pointer tutorial.

34

(21) What will be output if you will compile and execute the following c code?

void main(){ int a=5; float b; printf("%d",sizeof(++a+b)); printf(" %d",a); }

(a)2 6 (b)4 6 (c)2 5 (d)4 5 (e)Compiler error Output: (d) Explanation: ++a +b =6 + Garbage floating point number =Garbage floating point number //From the rule of automatic type conversion Hence sizeof operator will return 4 because size of float data type in c is 4 byte. Value of any variable doesnt modify inside sizeof operator. Hence value of variable a will remain 5.

35

Properties of sizeof operator.

Operators tutorial

(22) What will be output if you will compile and execute the following c code? void main(){ char huge *p=(char *)0XC0563331; char huge *q=(char *)0XC2551341; if(p==q) printf("Equal"); else if(p>q) printf("Greater than"); else printf("Less than"); } (a)Equal (b)Greater than (c)Less than (d)Compiler error (e)None of above

35

Output: (a) Explanation: As we know huge pointers compare its physical address. Physical address of huge pointer p

Huge address: 0XC0563331 Offset address: 0x3331 Segment address: 0XC056

Physical address= Segment address * 0X10 + Offset address =0XC056 * 0X10 +0X3331 =0XC0560 + 0X3331 =0XC3891

Physical address of huge pointer q

Huge address: 0XC2551341 Offset address: 0x1341 Segment address: 0XC255

35 Physical address= Segment address * 0X10 + Offset address =0XC255 * 0X10 +0X1341 =0XC2550 + 0X1341 =0XC3891

Since both huge pointers p and q are pointing same physical address so if condition will true.

What is huge pointer? What is normalization? Pointer tutorial.

(23) What will be output if you will compile and execute the following c code?

void main(){ char *str; scanf("%[^\n]",str); printf("%s",str); } (a)It will accept a word as a string from user. (b)It will accept a sentence as a string from user. (c)It will accept a paragraph as a string from user. (d)Compiler error

35

(e)None of above Output: (b) Explanation: Task of % [^\t] is to take the stream of characters until it doesnt receive new line character \t i.e. enter button of your keyboard.

General meaning of %[^ p]

String tutorial.

(24) What will be output if you will compile and execute the following c code?

void main(){ int a=5,b=10,c=15; int *arr[]={&a,&b,&c}; printf("%d",*arr[1]); }

(a)5 (b)10 (c)15 (d)Compiler error (e)None of above

35

Output: (d) Explanation: Array element cannot be address of auto variable. It can be address of static or extern variables.

What is auto variable?

What is extern variable?

What is static variable?

Array tutorial.

(25) What will be output if you will compile and execute the following c code?

void main(){ int array[3]={5}; int i; for(i=0;i<=2;i++) printf("%d ",array[i]); }

(a)5 garbage garbage

35 (b)5 0 0 (c)5 null null (d)Compiler error (e)None of above tput: (b) Explanation: Storage class of an array which initializes the element of the array at the time of declaration is static. Default initial value of static integer is zero.

Properties of static storage class.

How to read complex array.

(26) What will be output if you will compile and execute the following c code?

void main(){ int array[2][2][3]={0,1,2,3,4,5,6,7,8,9,10,11}; printf("%d",array[1][0][2]); }

(a)4 (b)5 (c)6 (d)7

35 (e)8

Output: 8 Explanation: array[1][0][2] means 1*(2*3)+0*(3)+3=9th element of array starting from zero i.e. 8.

Questions on two dimension array.

Complete tutorial of array.

(27) What will be output if you will compile and execute the following c code?

void main(){ int a[2][4]={3,6,9,12,15,18,21,24}; printf("%d %d %d",*(a[1]+2),*(*(a+1)+2),2[1[a]]); }

35

(a)15 18 21 (b)21 21 21 (c)24 24 24 (d)Compiler error (e)None of above

Output: (b) Explanation: In c, a [1][2] =*(a [1] +2) =*(*(a+1) +2) =2[a [1]] =2[1[a]] Now, a [1] [2] means 1*(4) +2=6th element of an array staring from zero i.e. 21.

35 Concept of complex array.

Concept of complex pointer.

Concept of complex function.

(28) What will be output if you will compile and execute the following c code? void call(int,int,int); void main(){ int a=10; call(a,a++,++a); } void call(int x,int y,int z){ printf("%d %d %d",x,y,z); } (a)10 10 12 (b)12 11 11 (c)12 12 12 (d)10 11 12 (e)Compiler error

35

Output: (b) Explanation: Default parameter passing scheme of c is cdecl i.e. argument of function will pass from right to left direction.

First ++a will pass and a=11 Then a++ will pass and a=11 Then a will pass and a=12

What is pascal and cedecl parameter passing scheme?

Concept of variable numbers of argument.

(29) What will be output if you will compile and execute the following c code?

void main(){ int x=5,y=10,z=15; printf("%d %d %d"); }

36 (a)Garbage Garbage Garbage (b)5 10 15 (c)15 10 5 (d)Compiler error (e)Run time error

Output: (c) Explanation: Auto variables are stored in stack as shown in following figure.

Stack follow LIFO data structure i.e. last come and first out. First %d will print then content of two continuous bytes from the top of the stack and so on.

Memory map tutorial.

More questions based on memory map.

36

(30) What will be output if you will compile and execute the following c code?

void main(){ register int i,x; scanf("%d",&i); x=++i + ++i + ++i; printf("%d",x); }

(a)17 (b)18 (c)21 (d)22 (e)Compiler error

Output: (e) Explanation:

36

In c register variable stores in CPU it doesnt store in RAM. So register variable have not any memory address. So it is illegal to write &a.

Complete tutorial of storage class with examples.

Properties of register storage class.

(31) What will be output if you will compile and execute the following c code? void main(){ int a=5; int b=10; { int a=2; a++; b++; } printf("%d %d",a,b); } (a)5 10 (b)6 11 (c)5 11 (d)6 10 (e)Compiler error

Output: (c)

36

Explanation: Default storage class of local variable is auto. Scope and visibility of auto variable is within the block in which it has declared. In c, if there are two variables of the same name then we can access only local variable. Hence inside the inner block variable a is local variable which has declared and defined inside that block. When control comes out of the inner block local variable a became dead.

Complete tutorial of storage class with examples.

What is auto storage class?

(32) What will be output if you will compile and execute the following c code?

void main(){ float f=3.4e39; printf("%f",f); }

(a)3.4e39 (b)3.40000 (c)+INF (d)Compiler error (e)Run time error

36

Output: (c) Explanation: If you will assign value beyond the range of float data type to the float variable it will not show any compiler error. It will store infinity.

Data type tutorial with examples.

Concept of float data type.

(33) What will be output if you will compile and execute the following c code?

void main(){ enum color{ RED,GREEN=-20,BLUE,YELLOW }; enum color x; x=YELLOW; printf("%d",x); } (a)-22

36 (b)-18 (c)1 (d)Compiler error (e)None of above

Output: (b) Explanation: Default value of enum constant = value of previous enum constant +1 Default value of first enum constant=0 Hence: BLUE=GREEN+1=-20+1=-19 YELLOW=BLUE+1=-19+1=-18

Complete tutorial of enum data type with examples.

(34) What will be output if you will compile and execute the following c code?

void main(){ asm{

36 mov bx,8; mov cx,10 add bx,cx; } printf("%d",_BX); } (a)18 (b)8 (c)0 (d)Compiler error (e)None of above

Output: (a) Explanation: asm keyword is used to write assembly language program in c. mov command stores the constants in the register bx, cx etc. add command stores the content of register and stores in first register i.e. in bx.

36 How to write assembly language program by c?

Advance c tutorial.

(35) What will be output if you will compile and execute the following c code?

void main(){ enum xxx{ a,b,c=32767,d,e }; printf("%d",b); }

(a)0 (b)1 (c)32766 (d)Compiler error (e)None of above

36 Output: (d) Explanation: Size of enum constant is size of sign int. Since value of c=32767. Hence value of d will be 32767+1=32768 which is beyond the range of enum constant.

Tutorial of data type with examples.

(36) What will be output if you will compile and execute the following c code?

void main(){ signed int a=-1; unsigned int b=-1; if(a==b) printf("%d %d",a,b); else printf("Not equal"); }

(a)-1 -1 (b)-1 32767 (c)-1 -32768 (d)Not equal (e)Compiler error

36

Output: (a) Explanation:

What is automatic type conversion?

(37) What will be output if you will compile and execute the following c code?

void main(){ float f=5.5f; float x; x=f%2; printf("%f",x); }

(a)1.500000 (b)1.000000 (c)5.500000 (d)Compiler error (e)None of above

37

Output: (d) Explanation: Modular division is not allowed with floating number.

Properties of modular division.

Operators tutorial with examples.

(38) What will be output if you will compile and execute the following c code?

void main(){ int a=-20; int b=-3; printf("%d",a%b); }

(a)2 (b)-2

37

(c)18 (d)-18 (e)Compiler error

Output: (b) Explanation: Sign of resultant of modular division depends upon only the sign of first operand.

Properties of modular division. Operators tutorial with examples.

(39) What will be output if you will compile and execute the following c code?

void main(){ char c='0'; printf("%d %d",sizeof(c),sizeof('0')); }

(a)1 1

37 (b)2 2 (c)1 2 (d)2 1 (e)None of above

Output: (c) Explanation: Size of char data type is one byte while size of character constant is two byte.

Why character constant is of two byte in c?

(40) What will be output if you will compile and execute the following c code?

void main(){ char *url="c:\tc\bin\rw.c"; printf("%s",url); }

37 (a)c:\tc\bin\rw.c (b)c:/tc/bin/rw.c (c)c: c inw.c (d)c:cinw.c (e)w.c in

Output: (e) Explanation: 1. \t is tab character which moves the cursor 8 space right. 2. \b is back space character which moves the cursor one space back. 3. \r is carriage return character which moves the cursor beginning of the line.

Complete string tutorial with examples.

Properties of escape characters.

37

(41) What will be output if you will compile and execute the following c code?

void main(){ clrscr(); goto abc; printf("main"); getch(); } void dispaly(){ abc : printf("display"); } (a)main (b)display (c)maindisplay (d)displaymain (e)Compiler error

Output: (e)

37

Explanation: Label of goto cannot be in other function because control cannot move from one function to another function directly otherwise it will show compiler error: unreachable label

What is goto keyword.

Complete function tutorial with examples.

(42) What will be output if you will compile and execute the following c code?

void main(){ int i=3; if(3==i) printf("%d",i<<2<<1); else printf("Not equal"); }

(a)1 (b)48 (c)24 (d)Not equal (e)Compiler error

37

Output: (c) Explanation: Associative of bitwise left shifting operator is left to right. In the following expression: i<<2<<1 There are two bitwise operators. From rule of associative leftmost operator will execute first. i <<><<> After execution of leftmost bitwise left shifting operator: so i=i*pow(2,2) =3*

What is associative?

What is precedence?

Tutorial of bitwise operators.

(43) What will be output if you will compile and execute the following c code?

37

void main(){ int x=2,y=3; if(x+y<=5) printf("True"); else printf("False"); }

(a)True (b)False (c)Compiler error: Lvalued required (d)Compiler error: Invalid expression (e)None of above

Output: (a) Explanation: Expression x+y<=5 => 2+3 <=5

37

=> 5<=5 is true because 5 is either greater than 5 or equal to 5.

Operator tutorial with examples.

(44) What will be output if you will compile and execute the following c code?

void main(){ const int i=5; i++; printf("%d",i); }

(a)5 (b)6 (c)0 (d)Compiler error (e)None of above

Output: (d)

37

Explanation: We cannot modify the const variable by using increment operator.

Properties of const keyword.

Properties of volatile keyword.

Data type tutorial with examples.

(45) What will be output if you will compile and execute the following c code?

void main(){ const int x=25; int * const p=&x; *p=2*x; printf("%d",x); }

(a)25 (b)50 (c)0 (d)Compiler error (e)None of above

38

Output: (b) Explanation: const keyword in c doesnt make any variable as constant but it only makes the variable as read only. With the help of pointer we can modify the const variable. In this example pointer p is pointing to address of variable x. In the following line: int * const p=&x; p is constant pointer while content of p i.e. *p is not constant. *p=2*x put the value 50 at the memory location of variable x.

Properties of const keyword.

What is constant pointer?

Data type tutorial with examples.

(46) What will be output if you will compile and execute the following c code?

void main(){

38 int i=11; int const * p=&i; p++; printf("%d",*p); }

(a)11 (b) 12 (c)Garbage value (d)Compiler error (e)None of above

Output: (c) Explanation: In the following line: int const * p=&i; *p i.e. content of p is constant pointer p is not constant pointer. So we can modify the pointer p. After incrementing the pointer it will point next memory location and its content will any garbage value.

38

Note: We have assumed arbitrary memory address.

To make pointer p as constant pointer write: int const * const p=&i;

Properties of const keyword.

Properties of volatile keyword.

(47) What will be output if you will compile and execute the following c code?

void main(){ int a=15,b=10,c=5; if(a>b>c ) printf("Trre"); else printf("False"); }

38 (a)True (b)False (c)Run time error (d)Compiler error (e)None of above

Output: (b) Explanation: Relation operator in c always returns 1 when condition is true and 0 when condition is false. So in the following expression a>b>c Associative of relational operators are left to right order of execution will be following manner:

Hence in this expression first solve bolded condition: a > b > c Since condition a>b is true so result will be 1. Now expression became:

38

1>c Since this condition is false so result will be 0. Thus else part will execute.

What is associative?

What is precedence?

(48) What will be output if you will compile and execute the following c code?

void main(){ float f; f=3/2; printf("%f",f); }

(a)1.5 (b)1.500000 (c)1.000000 (d)Compiler error (e)None of above Output: (c) Explanation:

38 In the following expression: f=3/2 both 3 and 2 are integer constant hence its result will also be an integer constant i.e. 1.

Properties of floating type numbers.

(49) What will be output if you will compile and execute the following c code? void main(){ int a=sizeof(a); a=modify(a); printf("%d",a); } int modify(int x){ int y=3; _AX=x+y; return; } (a)2 (b)3 (c)5 (d)Garbage value (e)None of above

38

Output: (c) Explanation: _AX is register pseudo variable. It stores return type of function.

What is register pseudo variable?

What is global identifier?

(50) What will be output if you will compile and execute the following c code? #define PRINT printf("c");printf("c++"); void main(){ float a=5.5; if(a==5.5) PRINT else printf("Not equal"); } (a)c c++ (b)Not equal (c)c c++

38 (d)Compiler error (e)None of above

Output: (d) Explanation:

First see intermediate file:

try.c 1: try.c 2: void main(){ try.c 3: float a=5.5; try.c 4: if(a==5.5) try.c 5: printf("c");printf("c++"); try.c 6: else try.c 7: printf("Not equal"); try.c 8: } try.c 9: try.c 10:

38 If there are more than one statement in if block then it is necessary to write inside the { } otherwise it will show compiler error: misplaced else

More questions on preprocessors.

Preprocessor tutorial with examples.

Links to this post 2 comments C questions and answer

(51) What will be output if you will compile and execute the following c code? struct marks{ int p:3; int c:3; int m:2; }; void main(){ struct marks s={2,-6,5};

38

printf("%d %d %d",s.p,s.c,s.m); } (a) 2 -6 5 (b) 2 -6 1 (c) 2 2 1 (d) Compiler error (e) None of these Answer: (c) Explanation: Binary value of 2: 00000010 (Select three two bit) Binary value of 6: 00000110 Binary value of -6: 11111001+1=11111010 (Select last three bit) Binary value of 5: 00000101 (Select last two bit)

Complete memory representation: Structure tutorial

More questions

(52) What will be output if you will compile and execute the following c code?

void main(){

39 static char *s[3]={"math","phy","che"}; typedef char *( *ppp)[3]; static ppp p1=&s,p2=&s,p3=&s; char * (*(*array[3]))[3]={&p1,&p2,&p3}; char * (*(*(*ptr)[3]))[3]=&array; p2+=1; p3+=2; printf("%s",(***ptr[0])[2]);

(a) math (b) phy (c) che (d) Compiler error (e) None of these

Answer: (c)

39 Explanation: Here ptr: is pointer to array of pointer to string. P1, p2, p3: are pointers to array of string. array[3]: is array which contain pointer to array of string. Pictorial representation:

Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.

As we know p[i]=*(p+i) (***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2] =(***(&array))[2] //ptr=&array =(**array)[2] //From rule *&p=p =(**(&p1))[2] //array=&p1 =(*p1)[2] =(*&s)[2] //p1=&s =s[2]=che

How to read complex pointer?

39

Pointer tutorial.

(53) What will be output if you will compile and execute the following c code?

#include"conio.h" int display(); int(*array[3])(); int(*(*ptr)[3])(); void main(){ array[0]=display; array[1]=getch; ptr=&array; printf("%d",(**ptr)()); (*(*ptr+1))(); } int display(){ int x=5; return x++; }

(a)5 (b)6 (c)0

39

(d)Compiler error (e)None of these

Answer: (a) Explanation: In this example: array []: It is array of pointer to such function which parameter is void and return type is int data type.

ptr: It is pointer to array which contents are pointer to such function which parameter is void and return type is int type data.

(**ptr)() = (** (&array)) () //ptr=&array = (*array) () // from rule *&p=p =array [0] () //from rule *(p+i)=p[i] =display () //array[0]=display

(*(*ptr+1))() =(*(*&array+1))() //ptr=&array =*(array+1) () // from rule *&p=p =array [1] () //from rule *(p+i)=p[i]

39

=getch () //array[1]=getch

How to read complex array?

Array tutorial.

(54) What will be output if you will compile and execute the following c code?

void main(){ int i; char far *ptr=(char *)0XB8000000; *ptr='A'; *(ptr+1)=1; *(ptr+2)='B'; *(ptr+3)=2; *(ptr+4)='C'; *(ptr+5)=4; }

39

Answer: It output will be A, B and C in blue, green and red color respectively. As shown in following figure:

What is far pointer?

Advance c tutorial?

Working with text video memory.

(55) What will be output if you will compile and execute the following c code?

#include "dos.h" void main(){ int j; union REGS i,o; char far *ptr=(char *)0XA0000000; i.h.ah=0; i.h.al=0x13;

39 int86(0x10,&i,&o); for(j=1;j<=100;j++){ *(ptr+j)=4; }

Answer: One red color line in the graphics console as shown in the following figure

What is union REGS?

Advance c tutorial.

39

Working with graphics video memory.

(56) What will be output if you will compile and execute the following c code?

void main(){ int huge*p=(int huge*)0XC0563331; int huge*q=(int huge*)0xC2551341; *p=200; printf("%d",*q); }

(a)0 (b)Garbage value (c)null (d) 200 (e)Compiler error

39

Answer: (d) Explanation:

Physical address of huge pointer p

Huge address: 0XC0563331 Offset address: 0x3331 Segment address: 0XC056

Physical address= Segment address * 0X10 + Offset address =0XC056 * 0X10 +0X3331 =0XC0560 + 0X3331 =0XC3891

Physical address of huge pointer q

Huge address: 0XC2551341 Offset address: 0x1341 Segment address: 0XC255

Physical address= Segment address * 0X10 + Offset address =0XC255 * 0X10 +0X1341

39 =0XC2550 + 0X1341 =0XC3891

Since both huge pointers p and q are pointing same physical address so content of q will also same as content of q.

What is huge pointer?

Pointer tutorial.

(57) Write c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)?

Answer:

#includedos.h #includestdio.h void main() { union REGS i,o; int x,y,k; //show mouse pointer

40 i.x.ax=1; int86(0x33,&i,&o); while(!kbhit()) //its value will false when we hit key in the key board { i.x.ax=3; //get mouse position x=o.x.cx; y=o.x.dx; clrscr(); printf("(%d , %d)",x,y); delay(250); int86(0x33,&i,&o); } getch(); }

What is int86? Advance c tutorial.

(58) Write a c program to create dos command: dir. Answer:

Step 1: Write following code.

40

#include stdio.h #include dos.h void main(int count,char *argv[]) { struct find_t q ; int a; if(count==1) argv[1]="*.*"; a = _dos_findfirst(argv[1],1,&q); if(a==0) { while (!a) { printf(" %s\n", q.name); a = _dos_findnext(&q); } } else { printf("File not found"); } }

40

Step 2: Save the as list.c (You can give any name) Step 3: Compile and execute the file. Step 4: Write click on My computer of Window XP operating system and select properties. Step 5: Select Advanced -> Environment Variables Step 6: You will find following window: Click on new button (Button inside the red box)

Step 7: Write following: Variable name: path Variable value: c:\tc\bin\list.c (Path where you have saved)

Step 8: Open command prompt and write list and press enter.

Command line argument tutorial.

(59) What will be output if you will compile and execute the following c code?

40

void main(){ int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater than"); else printf("Less than");

} (a) Equal (b) Greater than (c) Less than (d) Compiler error (e) None of above

40 Answer: (d) Explanation: static variables are load time entity while auto variables are run time entity. We can not initialize any load time variable by the run time variable. In this example i is run time variable while x is load time variable.

Properties of static variables.

Properties of auto variables.

(60) What will be output if you will compile and execute the following c code? void main(){ int i; float a=5.2; char *ptr; ptr=(char *)&a; for(i=0;i<=3;i++) printf("%d ",*ptr++); }

(a)0 0 0 0 (b)Garbage Garbage Garbage Garbage (c)102 56 -80 32 (d)102 102 -90 64

40

(e)Compiler error

Answer: (d) Explanation: In c float data type is four byte data type while char pointer ptr can point one byte of memory at a time. Memory representation of float a=5.2

ptr pointer will point first fourth byte then third byte then second byte then first byte. Content of fourth byte: Binary value=01100110 Decimal value= 64+32+4+2=102 Content of third byte: Binary value=01100110 Decimal value=64+32+4+2=102

40

Content of second byte: Binary value=10100110 Decimal value=-128+32+4+2=-90 Content of first byte: Binary value=01000000 Decimal value=64

Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit.

How to represent float data type in memory?

(61) What will be output if you will compile and execute the following c code?

void main(){ int i; double a=5.2; char *ptr; ptr=(char *)&a; for(i=0;i<=7;i++) printf("%d ",*ptr++); }

40 (a) -51 -52 -52 -52 -52 -52 20 64 (b) 51 52 52 52 52 52 20 64 (c) Eight garbage values. (d) Compiler error (e) None of these

Answer: (a) Explanation: In c double data type is eight byte data type while char pointer ptr can point one byte of memory at a time. Memory representation of double a=5.2

ptr pointer will point first eighth byte then seventh byte then sixth byte then fifth byte then fourth byte then third byte then second byte then first byte as shown in above figure.

40

Content of eighth byte: Binary value=11001101 Decimal value= -128+64+8+4+1=-51 Content of seventh byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of sixth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of fifth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of fourth byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of third byte: Binary value=11001100 Decimal value= -128+64+8+4=-52 Content of second byte: Binary value=000010100 Decimal value=16+4=20 Content of first byte: Binary value=01000000

40

Decimal value=64

Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit.

How to represent double data type in memory?

(62) What will be output if you will compile and execute the following c code?

void main(){ printf("%s","c" "question" "bank"); }

(a) c question bank (b) c (c) bank (d) cquestionbank (e) Compiler error

41

Answer: (d) Explanation: In c string constant xy is same as x y

String tutorial.

(63) What will be output if you will compile and execute the following c code?

void main(){ printf("%s",__DATE__); }

(a) Current system date (b) Current system date with time (c) null (d) Compiler error (e) None of these

41

Answer: (a) Explanation: __DATE__ is global identifier which returns current system date.

What is global identifier?

(64) What will be output if you will compile and execute the following c code?

void main(){ char *str="c-pointer"; printf("%*.*s",10,7,str); }

(a) c-pointer (b) c-pointer (c) c-point (d) cpointer null null (e) c-point

41

Answer: (e) Explanation: Meaning of %*.*s in the printf function: First * indicates the width i.e. how many spaces will take to print the string and second * indicates how many characters will print of any string.

Following figure illustrates output of above code:

Properties of printf function.

(65) What will be output if you will compile and execute the following c code?

void start(); void end(); #pragma startup start #pragma exit end int static i; void main(){

41

printf("\nmain function: %d",++i); } void start(){ clrscr(); printf("\nstart function: %d",++i); } void end(){ printf("\nend function: %d",++i); getch(); }

(a) main function: 2 start function: 1 end function:3

(b) start function: 1 main function: 2 end function:3

(c) main function: 2

41

end function:3 start function: 1

(d) Compiler error (e) None of these

Answer: (b) Explanation: Every c program start with main function and terminate with null statement. But #pragma startup can call function just before main function and #pragma exit

What is pragma directive?

Preprocessor tutorial.

(66) What will be output if you will compile and execute the following c code?

41 void main(){ int a=-12; a=a>>3; printf("%d",a); }

(a) -4 (b) -3 (c) -2 (d) -96 (e) Compiler error

Answer :( c) Explanation:

Binary value of 12 is: 00000000 00001100 Binary value of -12 wills 2s complement of 12 i.e.

41

So binary value of -12 is: 11111111 11110100

Right shifting rule: Rule 1: If number is positive the fill vacant spaces in the left side by 0. Rule 2: If number is negative the fill vacant spaces in the left side by 1. In this case number is negative. So right shift all the binary digits by three space and fill vacant space by 1 as shown following figure:

Since it is negative number so output will also a negative number but its 2s complement.

Hence final out put will be:

41

And its decimal value is: 2 Hence output will be:-2

More questions on shifting operator.

Operator tutorial.

(67) What will be output if you will compile and execute the following c code?

#include "string.h" void main(){ clrscr(); printf("%d %d",sizeof("string"),strlen("string")); getch(); }

(a) 6 6 (b) 7 7 (c) 6 7

41 (d) 7 6 (e) None of these

Answer: (d) Explanation: Sizeof operator returns the size of string including null character while strlen function returns length of a string excluding null character.

String tutorial.

Library functions of string.

(68) What will be output if you will compile and execute the following c code?

void main(){ static main; int x; x=call(main); clrscr();

41

printf("%d ",x); getch(); } int call(int address){ address++; return address; } (a) 0 (b) 1 (c) Garbage value (d) Compiler error (e) None of these

Answer: (b) Explanation: As we know main is not keyword of c but is special type of function. Word main can be name variable in the main and other functions.

What is main function in c?

(69) What will be output if you will compile and execute the following c code?

42

void main(){ int a,b; a=1,3,15; b=(2,4,6); clrscr(); printf("%d ",a+b); getch(); }

(a) 3 (b) 21 (c) 17 (d) 7 (e) Compiler error

Answer: (d) Explanation:

42

In c comma behaves as separator as well as operator. a=1, 3, 15; b= (2, 4, 6); In the above two statements comma is working as operator. Comma enjoys least precedence and associative is left to right.

Assigning the priority of each operator in the first statement:

Hence 1 will assign to a. Assigning the priority of each operator in the second statement:

42

Operator tutorial.

(70) What will be output if you will compile and execute the following c code?

int dynamic(int,...); void main(){ int x,y; x=dynamic(2,4,6,8,10,12,14); y=dynamic(3,6,9,12); clrscr(); printf("%d %d ",x,y); getch(); } int dynamic(int s,...){ void *ptr; ptr=...; (int *)ptr+=2; s=*(int *)ptr; return s;

42

(a) 8 12 (b) 14 12 (c) 2 3 (d) Compiler error (e) None of these

Answer: (a) Explanation: In c three continuous dots is known as ellipsis which is variable number of arguments of function. In this example ptr is generic pointer which is pointing to first element of variable number of argument. After incrementing it will point third element.

What is variable number of argument?

(71) What will be output if you will compile and execute the following c code?

int extern x; void main()

42 printf("%d",x); x=2; getch(); } int x=23;

(a) 0 (b) 2 (c) 23 (d) Compiler error (e) None of these

Answer: (c) Explanation: extern variables can search the declaration of variable any where in the program.

Properties of extern storage class.

42

(72) What will be output if you will compile and execute the following c code?

void main(){ int i=0; if(i==0){ i=((5,(i=3)),i=1); printf("%d",i); } else printf("equal"); }

(a) 5 (b) 3 (c) 1 (d) equal (e) None of above

Answer: (c)

42

Explanation:

Comma operator.

Operator tutorial.

(73) What will be output if you will compile and execute the following c code?

void main(){ int a=25; clrscr(); printf("%o %x",a,a); getch(); }

(a) 25 25 (b) 025 0x25 (c) 12 42 (d) 31 19 (e) None of these

42

Answer: (d) Explanation: %o is used to print the number in octal number format. %x is used to print the number in hexadecimal number format.

Note: In c octal number starts with 0 and hexadecimal number starts with 0x.

What is octal number?

What is hexadecimal number?

(74) What will be output if you will compile and execute the following c code?

#define message "union is\ power of c" void main(){ clrscr(); printf("%s",message); getch();

42

(a) union is power of c (b) union ispower of c (c) union is Power of c (d) Compiler error (e) None of these

Answer: (b) Explanation: If you want to write macro constant in new line the end with the character \.

Preprocessor tutorial.

(75) What will be output if you will compile and execute the following c code?

42

#define call(x) #x void main(){ printf("%s",call(c/c++)); }

(a)c (b)c++ (c)#c/c++ (d)c/c++ (e)Compiler error

Answer: (d) Explanation: # is string operator. It converts the macro function call argument in the string. First see the intermediate file:

test.c 1: test.c 2: void main(){

43 test.c 3: printf("%s","c/c++"); test.c 4: } test.c 5:

It is clear macro call is replaced by its argument in the string format.

What is # and ##?

Preprocessor tutorial?

(75) What will be output if you will compile and execute the following c code?

void main(){ if(printf("cquestionbank")) printf("I know c"); else printf("I know c++"); }

(a) I know c (b) I know c++ (c) cquestionbankI know c (d) cquestionbankI know c++

43 (e) Compiler error

Answer: (c) Explanation: Return type of printf function is integer which returns number of character it prints including blank spaces. So printf function inside if condition will return 13. In if condition any non- zero number means true so else part will not execute.

Prototype of printf function.

1. Which of the following does not have an unary operator? 1) -7 3) j 2) ++i 4) all of the above

2. In printf(),the appearance of the output of the output can be affected by 1) field with 3) flag 2) conversion character 4) all of the above

3. Any of the following programs in c has access to three standard files: 1) standard input file, standard output file, standard error file

43 2) stdin,stdout, stderr 3) keyboard,screen,screen 4) all the above 5) A variable can be declared static using the keyword. 1) extern 2) static 3) stat 4) auto 6) A program can be terminated at any time by calling the function 1) fflush() 3) exit() 7) Heap 1) is a region from where memory is allocated 2) lies between you program and the stack 3) is a finite area 4) all of the above 8) A function can 1) perform a task 2) return a value 3) change value of actual arguments in call by reference 4) all of the above 2) ferror() 4) clearerr()

9) Function definition void check(int i ,char*j) is 1) call by value 2)call by reference 3) both (1) and (2) 4)in valid function definition 10) A union consists of a number of elements that 1) all occupy the same space in memory 2) must be structure 3) are grouped next to each other in memory 4) all have the same type 11) Which of the following array is defined in the statements Char name[30]? 1) name is one dimensiona,30-element integer array

43 2) name is one dimensional,30-element floating point array 3) name is one dimensional ,30-element character array 4) name is one dimensional,30-elements string array 12) c program contains the following declaration: Static float table[2][3]={ {1.1,1.2,1.3}, {2.1,2.2,2.3} }; What is the value of *(*(table+1)+1)? 1) 2.2 3) 2.1 2) 1.2 4) 2.3

13) A c program contains the following declarations and initial Assignments: int i=8,j=5; float x=0.0005,y=-0.01; Char c=c,d=d; What would be the value of the following expression? (3*i-2*j)%(2*d-3) 1)14 3) 1 2)18 4) 0

14) The declaration : int f(int); means 1) f accepts an integer argument and returns an integer quantity 2) f accepts two arguments and returns a double precision quantity, and the second is an integer 3) f accepts three arguments and returns nothing. The first arguments is a double-precision quantity, and the second is an integer 4) f does not accepts any arguments but returns a single character 16) The arguments of a function are included between 1) The parenthesis 2) double quotes 3) curly braces 4) # 17) The int type of constraints are whole numbers in the range 1) -23677 to 23678 3) -32767 to 32768 2) -32768 to 32767 4) -32864 to 32864

43

18) If the variables i,j and k are assigned the values 5,3 and 2 respectively, then the expression i=j+(k++ =6)+7; 1) gives an error message 3) assigns a value 18 to i 2) assigns a value 16 to i 4) assigns a value 19 to i

19) In a relational expression involving characters, we actually Compare 1) 2) 3) 4) the ASCII codes of the characters the characters themselves neither of the two binary code and hexadecimal code

21) The word case used in the switch statement represents a 1) 2) 3) 4) function in the c language data type in the c language keyword in the c language global variable in the c language

22)The logical NOT operator represented by ! is a 1) unary operator 3) ternary operator 2) binary operator 4) octal operator

23) The statement : scanf(%d,&i); 1) 2) 3) 4) assigns an integer to the variable i gives an error message; does not assign any value to i assigns an float to the variable i

24) A pointer is declared by using a statement such as 1) int *p; 2) point; 3) pointer *p; 4) int &p;

25)The null character is represented by 1) \n 2)\0 3)\o 4)\t

43 26) The members in the union 1) have different memory locations 2) share the memory with a structure 3) have the same memory location 4) have different memory variable 27) The global variables by default belong to 1) the register type 3) the auto type 2) the static type 4) the dynamic type

28) The bit fields are the members of a/an 1) array 2) structure 3) union 4) both 2 and 3

29) In c, square brackets [ ] are used in 1) functions 2) arrays 3) statements 4) all of the above

30) A fields width specifier in a printf() function 1) specifies the maximum value of a number 2) controls the size of type used to print numbers 3) controls the merging of the program listing 4) specifies how many characters positions will be used for a number 31) The two operators && and || are 1) arithmetic operators 3) logical operators 2) equality operators 4) relational operators

32) The library files that come with c are 1) text editor for program development 2) the compiler and liker 3) program examples 4) files that contain functions which carry out various commonly Used operations and calculations 33) Precedence determines which operator

43

1) is evaluated first 3) is fastest

2) is most important 4) operates on the largest number

37) The malloc() function 1) returns a pointer to the allocated memory 2) returns a pointer to the first byte of region of memory 3) changes the size of the allocated memory 4) deallocates the memory 38) which of the following expressions will return a 1 if both bits have A value of 1; otherwise will return a value of 0? 1) AND 2)OR 3)XOR4)1stderr complement

39) If an error occurs while opening a file the file pointer is assigned a value 1) NULL 2) stdout 3) sstderr 4) not defined

40) Which of the following backslash codes used for bell? 1) \b 2) \a 3) \r 4) \s

41) One is not the valid keywords in the c language is 1) printf 2) CHAR 3) auto 4) scanf

42) The comments in a c language program are placed between 1) \* and /* 2) / and .* 3) /*and*/ 4) # and #

43) If p and q are assigned the values 2 and 3 respectively then the statement p=q++ 1) gives an error message 3) assigns a value 3 to p 2) assigns a value 4 to p 4) assigns a value 5 to p

44) A compound statement is a group of statement included between a pair of 1) double quots 3) parentesis 2) curly braces 4) / and/

43 45) The number of the relational operators in the c language is 1) four 2) six 3) three 4)one

46) In the c language, 3 represents 1) a digit 2)an integer 3)a character 4)a word

47) In the c language, a hexadecimal number is represented by writing 1) x 2) xo 3) 0x 4)h

48) A string in the c language is represented by enclosing a series of characters in 1) single quotes 3) parenthesis 49) One structure can be 1) 2) 3) 4) a member of some other structure a member of the same structure a member of a union all of the above 2) double quotes 4) / and /

51) Almost every c program begins with the statement 1) main() 2) printf() 3) #include<stdio.h> 4) scanf()

52) A single character input from the keyboard can be obtained by using the function 1) printf( ) 2) getchar( ) 53) An expression 1) 2) 3) 4) is a collection of data objects and operators that can be evaluated to a single value is a name that substitutes for a sequence of characters causes the computer to carry out some action all of the above 3) putchar( ) 4) scanf( )

54) The expression c=i++ causes

43

1) 2) 3) 4)

the value of I assigned to c and then I incremented by 1 I to be incremented by 1 and then the value of I assigned to @ Value of I assigned to c I to be incremented by 1

55)The single character input/output functions are 1) scanf( ) and printf( ) 3) scanf( ) and putchar( ) 2) getchar( ) and printf( ) 4) getchar( ) and putchar( )

56) The conversion character I for data output means that the Data item is displayed as 1) 2) 3) 4) a floating point value with an exponent an unsigned decimal integer a signed decimal integer an octal integer

58) In a circular linked list 1) 2) 3) 4) components are all linked together in some sequential manner there is no beginning and no end components are arranged hierarchically forward and backward transversal within the list is permitted

60) A c function contain 1)function body 2)argument declaration 3)a function header 4)all of the above C QUESTIONS ON ARRAYS 1. main() { Int a[5]; a[-2]=10; a[2]=1; printf(%d,-2[a]); } a) compilation error b)10 c)-1 d)none of the above Ans c

43 2. main() { Char a[3][3]={{a,b,c},pqr,xy}; Printf(%s\n,&a[0][0]); } a) a b)compilation error c)abcpqrxy d)abc Ans c 3. main() { char a[3][3]={abc,pqr,xyz}; printf(%c,a[2][2]); } a) q b) r c) z d) compilation error Ans c 4) main() { Char a[100]={abcdef}; a++; printf(%s,&a[1]); } b) bcdef b) abcdef c)compilation error d) none of the above Ans c 5) main() { Char *p=algc; Printf(%c,++*(p++)); Printf(%c,*++p); } a) al b) bg c) lg d) none of the above ans b 6) main() { Int n[25]; n[0]=100; n[24]=200; printf(%d%d,*n,*(n+24)+*(n+0)); }

44

a) 100 200 b) 100 300 c) 0,100 d) 0,200 Ans b 7)main() { Int a[3]={1}; Printf(%d,a[1]); } a) 1 b) 0 c) compillation error d) none of the above Ans b 8) main() { Static int n[3][3]={2,4,3,6,8,5,3,5,1}; Printf(%d%d%d,n[2][1],n[1][1],n[3][1]); } a)5 8 garbage value b) 845 c)623 d)825 ans a) 9) main() { Char a=ab; Printf(%c,a); } a) a b) b c) ab d) error ans a) 10) main() { Char *p; p=%d\n; p[1]=c; printf(p,65); } a) A b) c c) 65 d) error ans a 11) main() { int a=1;

44 switch(a==5) { Case 1: pf(hi); Break; Case 0:pf(hello);break; Default : pf(wipro\n); } } a) Hi b)hello c) hi d)wipro Ans: b 12) main() { Char a[]={\012345\}; Printf(%s %d %d\n,a,sizeof(a),sizeof(*a)); } 13) main() { Char a[]=hell0009; Printf(%s\n,a); } Ans:: hell0009 DATA STRUCTURES ON SINGLE LINKED LIST 1) each structure in a single linked list contains ---------&------------a) data & pointer to hold the address of that node. b) data & pointer to hold the address of next node. c) data & pointer to hold the address of previous node. d) pointer to hold the address of next and previous node. Ans b 2) what does the last node link contain a) address of first node b) address of that node c) null d) address of previous node ans c 3) steps involved in insertion of a node

44 a) Creating a new node. b) Accepting data into the node c) If the list does not exist, assign start to the new node. d) If the list exists insert the new node at the end of the list. a) 1 2 3 4 b) 1 3 2 4 c) 4 3 2 1 d) 3 1 2 4 Ans a) 4) 1) 2) 3) 4) steps involved in traversing the list store the address of the start node in a temporary variable. Repeat steps 2 & 3 until the the temporary pointer points to null. Print data in the node. Move the temporary pointer pointer to the next node.

a) 1 2 3 4 b) 1 4 3 2 c) 1 3 4 2 d) 2 3 1 4 Ans c 5) steps involved in traversing the list 1) Free the memory occupied by the deleted node. 2) if the node to be deleted is the start then start is made to point to the next node. 3) searching for the node to be deleted by comparing the data with the data entered by the user. 4) if the node to be deleted is in the middle of the list the previous node is made to point to the next node. e) f) g) h) Ans 1234 3241 1324 2431 f

6) if a node (q) is to be inserted at the beginning the operations which are performed is 1) q->next =start->next start =q; 2) start=q; q->next=start; 3) q->next=start; q=start;

44

4) q->next =start; start =q; ans 4 7) when start and last points to same node,how many elements are present 1) 0 2)1 3)2 4)3 Ans 2 8)memory is allocated in single linked list a) dynamically b)compile time c)statically d) linking time. Ans : a 1).What would be the output of the following program. main() { int a[5]={2,3}; printf("\n %d %d %d",a[2],a[3],a[4]); } (a) garbage value (b) 2 3 3 (c) 3 2 2 (d) 0 0 0 ans::d 2).What would be the output of the following program. main() { int i=-3,j=2,k=0,m; m=++i&&++j||++k; printf("\n %d %d %d %d",i,j,k,m); } (a) -2 3 0 1 (b) -3 2 0 1 (c) -2 3 1 1 (d) error ans::a 3) main() { Int n[25]; n[0]=100; n[24]=200; printf(%d%d,*n,*(n+24)+*(n+0)); } a)100 200 b) 100 300 c) 0,100 d) 0,200 4) main() { char str[]="I am in wipro";

44 printf("%s\n",&str[5]); } ans::in wipro 5) { main() Char *p=algc; Printf(%c,++*p++); Printf(%c,*++p); } a)al b) mg c) lg d) none ans::none.....bg 6)main() { void fun(int); int n=3; fun(n); } void fun(int n) { if(n>0) { fun(--n); printf("%d",n); fun(--n); } } ans::0120 7).What would be the output of the following program. main() { struct emp { char name[20]; int age; float sal; }; struct emp e = {"tiger"}; printf("\n %d %f",e.age,e.sal); } (a) 0 0.000000 (b) Garbage values (c) Error (d) none of the above

44 ans::a 8).main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(%d ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(%d ,*p); p++; } } Ans::Error Lvalue Required...can't change the base address a++ 9.main() { int c[ ]={2,3,4,6,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*p); ++q; } for(j=0;j<5;j++) { printf(" %d ",*p); ++p; } } ans::222223465 10. #define ABC 20 #define XYZ 10 #define XXX ABC - XYZ void main() { int a; a = XXX * 10;

44 printf("%d\n", a); } ans::-80 11. #define calc(a, b) (a * b) / (a - b) void main() { int a = 20, b = 10; printf("%d\n", calc(a + 4, b -2)); } 12.void main() { int cnt = 5, a; do { a /= cnt; } while (cnt --); printf ("%d\n", a); } ans::Error..divide error 13. void main() { int a, b, c, abc = 0; a = b = c = 40; if (c) { int abc; abc = a*b+c; } printf ("c = %d, abc = %d\n", c, abc); } ans::40 0 14.main() { int k = 5; if (++k < 5 && k++/5 || ++k <= 8) printf("%d\n", k); }

44 ans::7 15.main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); } ans::Error......base address can't be changed names[3]=names[4] 16.main() { int x=20;y=35; x=x++ + y++; y=++y + ++x; printf("x=%d y=%d\n",x,y); } ans::57 94 17.Assume that integer is 4 bytes,pointer as 4 byter and character as 1 byte,then predict the output struct student { int a; char name[10]; int *p; }s1,*s2; printf("%d%d",sizeof(s1),sizeof(*s2)); a) 18,18 b)18,4 c) 4,18 d) 4,4 18.int fun(int x) { int y=55; return((x-y)?y:x); } main() { int a=20;

44 fun(a); printf("%d",y); } ans::20 19.int compute(int n) { if(n>0) { n=compute(n-3)+compute(n-1); return(n); } return(1); } void main() { printf("%d",compute(5)); } 20.int main() { char *ptr="abcdefgh"; char *sptr; sptr=ptr+5; printf("%s",sptr); } ans::fgh

1. printf("%d %d %d",sizeof(25.75),sizeof(123),sizeof(p)) a. 2 2 2 b. 4 2 2 c. 8 4 1 d. 8 2 2 ans::d

44

2.

int i=5; fun( ) { printf("%d\n", i * 3); } main( ) { int i= 2; { int i = 3; printf(" %d", i); fun(); } } b. c. d. e. 3, 15 3, 6 3 0

Ans::a 3. #define xsq(x) x*x main( ) { int i, j; i = 5; j = xsq(i-2); printf(%d\n, j); } a. 7 b. 9 c. 13 d. 29 Ans::-7 4. main( ) { int a=35; printf( %d %d %d\n, a == 35,a=50,a>40); } a. 1 50 1 b. 1 50 0 c. 0 50 0 d. 0 50 1 ans::c 5. void main()

45 { char a[]="123abcd"; clrscr(); printf("%d",strlen(a)); getch(); } a. b. c. d. Ans::7 6. main() { static int var=5; if(var--) { printf("%d",var); main(); } } a. 4 3 2 1 0 b. 4 3 2 1 c. 5 4 3 2 1 d. 5 4 3 2 1 0 ans::a 7. void main() { int i=1,j=2; switch(i) { case 1: printf("One"); break; case j: printf("Two"); break; default: printf(Default); break; } } a. One b. Two c. Default d. Compiler Error ans::d 8. void main() { switch('a') { 6 7 8 5

45 case 'A': printf("Zero"); case 97: printf("One"); default: printf("Error"); } } break; break; break;

a. Zero b. One c. Error d. Compiler Error ans::b 9. void main() { int p=1,sum=0; clrscr(); while(p<20) { p++; sum+=p++; } printf("\nsum=%d",sum); } a. 120 b. 100 c. 110 d. Error ans::A 10. int x; int modifyvalue() { return(x+=10); } int changevalue(int x) { return(x+=1); } void main() { int x=10; x++; changevalue(x); x++; modifyvalue();

45 printf("First output : %d",x); changevalue(x); printf("\nSecond output : %d",x); } a. 12 12 b. 11 12 c. 12 13 d. 13 13 11. main() { static i=3; printf("%d",i--); return i>0?main():0; } a. 3 2 1 0 b. 3 2 1 c. 2 1 0 d. 2 1 12. void main() { char *ptr="Hello World"; *ptr++; printf("%s",ptr); ptr++; printf("%s",ptr); } a. Iello World Iello World b. Hello World ello World c. ello World ello World d. ello World llo World 13. int const *p; *p=5; printf("%d",*p++); a. 5 b. 6 c. Compiler Error d. Garbage Value ans::c 14. void main()

45 { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("ab")); } a. 5 5 3 b. 4 5 3 c. 2 5 3 d. 1 5 3 ans::c 15. P is a character pointer variable then, what will be the output of the following statement. printf("%d %d",sizeof(p),sizeof(*p)); a. 1 2 b. 2 1 c. 2 2 d. 1 1 ans::b 16. void main() { char *s[]={"dharma","hewlet-packard","siemens","ibm"}; char **p; p=s; printf("%s",++*p); printf("\n%s",*p++); printf("\n%s",++*p); } a. harma harma ewlet-packard b. dharma harma ewlet-packard c. harma hewlet-packard siemens d. harma harma hewlet-packard ans::d 17. void main() { char *ptr="Ramco Systems";

45 (*ptr)++; printf("%s\n",ptr); ptr++; printf("%s",ptr); } a. Samco Systems Samco Systems b. Samco Systems amco Systems c. amco Systems amco Systems d. amco Systems mco Systems ans::b 18. #define square(x) x*x void main() { int i=7; clrscr(); i=64/square(4); printf("%d",i); } a. 7 b. 16 c. 64 d. 4 ans::c 19. #define man(x,y) (x)>(y)?(printf("%d",x)):(y) void main() { int i=15,j=10,k=0; k=man(i++,++j); printf(" %d %d %d",i,j,k); } a. 16 17 11 2 b. 17 17 11 2 c. 16 16 11 2 d. 16 17 12 2 ans::a 20. struct one { int no:1; int pl:2; int x:3; };

45 void main() { struct one a; a.no=0; a.pl=1; a.x=3; printf("%d %u",a.no,a.no); printf("\n%d %u",a.pl,a.pl); printf("\n%d %u",a.x,a.x); } a. 0 0 1 1 3 3 b. 0 0 2 2 3 3 c. 1 1 2 2 3 3 d. 1 1 2 2 2 2 Ans:a 21. void main() { struct emp { struct e { int *a; }e1; int a; }; struct emp emp1; printf("%d %d",sizeof(emp1),sizeof(emp1.e1)); } a. 2 4 b. 2 2 c. 4 4 d. 4 2 ans::d 22. struct emp emp1; struct emp

45 { int a; }; main() { printf("Enter 1 values:"); scanf("%d%d",&emp1.a,&emp1.a); //The given input is 10 and 25 printf("a=%d a=%d",emp1.a,emp1.a); } a. 10 25 b. 25 25 c. 10 10 d. Compiler Error ans::b 23. Arrange the code in order to delete a node being pointer by temp. a) b) c) free(temp) temp->prev->next = temp->next temp->next->prev = temp->prev;

a) b c a b) c b a c) a b c d) both a and b ans::d 24. What does below code do, if temp is pointing to a node other than first and last node temp -> prev ->next = temp ->next; temp ->next -> prev = temp -> prev; a) b) c) d) no effect inserts a node deletes a node shuffling of pointers

ans::c 25. which is the faster traversable dynamically growing list a) Binary Search Tree b) Singly Linked List c) Doubly Linked List d) Using Array ans::c

45 1. void main() { int a=1,b=2,c=3; c=(--a, b++)-c; printf("%d %d %d",a,b,c); } (a)0 3 -3 (b)Compile-Time Error (c)0 3 -1 0 Ans::3 2. #define swap(a,b) temp=a; a=b; b=temp; void main() { static int a=5,b=6,temp; if (a > b) swap(a,b); printf("a=%d b=%d",a,b); } (a)a=5 b=6 (b)a=6 b=5 (c)a=6 b=0 these Ans::c 3. void main() { int i=5; printf("%d %d %d %d %d",++i,i++,i++,i++,++i); } (a)Compile-Time Error (b)10 9 8 7 6 (c)9 8 7 6 6 766 Ans:d 4. void main() { int i, n =10; for (i=1; i<n--; i+=2) printf("%d", n-i); } (a)84 (b)840 (c)852 Ans:c 5. What is the output of the program? #include <stdio.h> int main(int argc, char *argv[])

(d)0 3

(d)None

of

(d)10 8

(d)864

45 { printf(" %d", printf("Hello Genesis")); return 0; } A. B. C. D. 6. #include <stdio.h> main() { switch (5) { case 5: printf(" 5 "); default: printf(" 10 "); case 6: printf(" 6 "); } } B. C. D. E. 5 5 10 6 5 10 56 Hello Genesis 13 Hello Genesis Hello Genesis 13 None of the above

Ans::b 7. Which argument of function 'strncmp()' specifies number of characters to be compared? A. B. C. D. ans::d 8. Which of the following is not a storage class in C? first second between 2 strings third

45 B. C. D. E. Ans::a 9. What is the significance of the free() function? A. B. C. D. Ans::a 10. What is the data type of FILE? A. B. C. D. ans::d 11. #include <stdio.h> #define sq(a) a * a void main() { printf("%d", sq(3 + 2)); } A. B. C. D. Ans::b 25 11 10 Compilation error integer union pointer structure It assigns the pointer a NULL value It erases the contents of any type and cleans the pointer It places the memory address with the pointer in free store It disables the memory address with the pointer Stack Register Extern Static

46 12. Which of the following is a non-linear data structure? A. B. C. D. Ans::d 13. Which of the following function does not return an integer value? B. C. D. E. ans:c 14. Which of the following function does not support dynamic memory allocation? A. B. C. D. ans::a 15. What is the output of the program if the input is 103? main() { int p = 234; printf(" %d ", printf("%d", p), scanf("%d", &p)); } A. 3 103 B. 103 C. 103 3 alloc realloc malloc free printf scanf strcpy strlen Stack Queue Linked List Tree

46 D. 103 2 Ans::c 16. Where do we use a 'continue' statement? B. C. D. E. Ans::d 17. 1. Queue is _________________ a) b) c) d) Ans::d 18. What is the postfix expression for A + B * C / D E * F / G e) ABC*D/+EFG*/f) ABC*D/+EF*G/g) ABCD/*+EF*G/h) None of these. Ans::b 19. Write one statement equivalent to the following two statements: x=sqr(a); return(x); Choose from one of the alternatives (a) return(sqr(a)); (b) printf("sqr(a)"); (c) return(a*a); (d) printf("%d",sqr(a)); Ans::b 20. int i=5; int abc(int z) { return i/2; } main() LIFO LILO FIFO Both b & c In 'if' statement In 'switch' statement In 'goto' labels None of the above

46 { int i=4; printf("%d",abc(i=i/4)); } a) error b) 5 c) 2 d) 0 ans::c 21. union U { int x; float y; char s[2]; }; union U ob; what is the size of ob in bytes, a) 4 b) 2 c) 8 d) 7 ans::a 22. The operation for adding and deleting an entry to a stack is traditionally called: a.add , delete b.append , delete c.insert, delete d.push , pop e. front , rear ans::d 23. What is the Infix expression for - + A / * B C D / * E F G a) A + B * C / D E / F * G b) A + B / C * D E * F / G c) A + B * C / D E * F / G d) A - B * C / D + E * F / G Ans:c 24.

46 What would be the root node if we enter the following data set (in the respective order) into a standard program to construct a Binary search tree? 25, 72, 16, 11, 88, 26, 9, 36, 21, 45, 14, 69 a) 69 b) 25 c) 26 d) 9 Ans::b 25. what does the code below do, where head is pointing to first node & temp is a temporary pointer. 10 be the number of nodes temp = head; while (temp->next->next!=NULL) { temp = temp ->next; } temp -> prev -> next = temp -> next; temp -> next -> prev = temp -> prev; free(temp); a) b) c) d) no effect deletes some node deletes 2nd last node deletes last node

ans::c 1. What will be the output of the following program : int main() { int val=5; printf("%d %d %d %d",val,--val,++val,val--); return(0); } (a)3 4 6 5 (b)5 5 6 5 (c)4 4 5 5 (d)None of these Ans::c 2. #define Compute(x,y,z) (x+y-z) int main() { int x=2,y=3,z=4; printf("%d",Compute(y,z,(-x+y)) * Compute(z,x,(-y+z)));

46 return(0); } (a)40 (b)30 (c)Compile-Time Error (d)None of these Ans::b 3. What will be the output of the following program : int main() { int val=5; val=printf("C") + printf("Skills"); printf("%d",val); return(0); } (a)7 (b)C7 (c)Compile-Time Error(d)CSkills7 Ans::d 4. What will be the output of the following program : int main() { char str[]="Test"; if ((printf("%s",str)) == 4) printf("Success"); else printf("Failure"); return(0); } a)Success b)TestSuccess c)Compile-Time Error(d)Failure ans::b 5. What will be the output of the following program: int main() { int val=5; printf("%d",5+val++); return(0); } (a)Compile-Time Error (b)Lvalue required Error (c)10 (d)11 Ans::c 6. void main() { printf("%d",sizeof(int)); return(0); } (a)Data types not allowed (b)Compile-Time Error(c)3 (d)2

46 Ans::2 7. In tree construction which is the suitable efficient data structure? (a) Array (b) Linked list (c) malloc (d) Queue Ans:b 8. Traverse the given tree using Inorder, Preorder and Postorder traversals. Given tree: A

B a)Inorder : DHBEAFCIGJ Preorder: D A B D H E C F G I J E Postorder: HDEBFIJGCA b)Inorder : Preorder: Postorder: c)Inorder : Preorder: Postorder: d)Inorder : Preorder: Postorder: DHBEAFCIGJ DHEABCFGIJ HH E B F I J G C A D DHBEAFCIGJ ABDHECFGIJ HDEBFIJGAC HDBEAFCIGJ ABDHECFGIJ HDEBFIJGCA

Ans:a 9. main() { static int var = 5; printf("%d ",var--); if(var) main(); } a. 4 3 2 1 Ans:c b. 4 3 2 1 0 c. 5 4 3 2 1 d. 0 0 0 0 0

46 10. #include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } b. 3 hello b. Compiler Error Ans::s 11. #include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } a. 5 6.000000 Ans::d 12. void main() { int k=ret(sizeof(float)); printf("\n %d",++k); } int ret(int ret) { ret += 2.5; return(ret); } a. Compiler Error b. 8 Ans::d 13. int swap(int *a,int *b) b. 5 5.000000 c. 6 6.000000 d. compiler error c. Run time error d. use dot (.) operator

c. 6

d. 7

46 { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); } b. 20 10 b. 10 20 c. 20 20 d. 10 10 Ans:a 14. main() { char *p = ayqm; char c; c = ++*p++; printf(%c,c); a. a ans:b 15. main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } b. 0 Ans:d 16. #define MESS junk main() { printf(MESS); } a. Junk b. Error c. MESS Ans:c 17. main () b. 0 1 2 c. 1 2 0 d. Compiler Error e. 2 0 } b. b c. y d. z

d. MESS junk

46 { int i = 5; switch (i) { static int i; i = 3; i = i * i; case 3: i = i + i; case 4: i = i + i; case 5: i = i + i; printf (%d,i); } printf (%d,i); } a. 9 b. 10 10 c. 0 5 d. 18 18 e. 18 5 Ans:c 18. What would be the output of the following program. Int fn(int); main() { int i=10; fn(i); printf("%d",i); } fn(int i) { return ++i; } (a) 10 (b) 11 (c) 12 Compilation error Ans:a 19. main() { FILE *fp1,*fp2; fp1=fopen("one","w"); fp2=fopen("one","w") ; fputc('A',fp1) ; fputc('B',fp2) ;

(d)

46 fclose(fp1) ; fclose(fp2) ; } Find the Error, If Any? a. b. c. d. no error. But It will over writes on same file. no error. But It will create one more file. error. It will not allow. no error. The new content will append in existing file. Ans:a 20. void main() { int a=555,*ptr=&a,b=*ptr; printf("%d %d %d",++a,--b,*ptr++); } (a)Compile-Time Error (b)555 554 555 (d)557 554 555 Ans:c 21. what type of Binary Tree is the following tree below (c)556 554 555

47

a. b. c. d. ans:d 22.

binary tree strictly binary tree complete binary tree not a binary tree

If suppose root to be deleted then which node will be the root node A

47

a. b. c. d. Ans:a 23.

B G Any node Both a and b are correct

When fopen() fails to open a file it returns a) NULL b) 1 c) 1 d) None of the above ans:a 24. Which function is used to detect the end of file a) EOF b) feof( ) c) ferror( ) d) NULL ans:b 25. If the CPU fails to keep the variables in CPU registers, in that case the variables are assumed a) static b) external c) global d) auto ans::d

1. #include<stdio.h> int x=40; main() { int x = 20; printf("\n %d",x); } Predict the output 20 40 20, 40 None of the above

47

2. #include<stdio.h> main() { int x=40; { int x = 20; printf("\n %d",x); } printf("%d",x); } Predict the output 20 40 40 20 40 20 3. #include<stdio.h> main() { extern int a; printf("\n %d",a); } int a=20; Predict the output 20 0 Garbage value Error 4. #include<stdio.h>--------------------------------doubt main() { struct emp { char name[20]; int age; float sal; };

47 struct emp e={"Raja"}; printf("\n %d %f", e.age, e.sal); } 0 0.000000 Garbage values Error None of the above 5. #include<stdio.h> main() { int x=10, y=20, z=5, i; i= x < y < z; printf("%d", i); } o\p: 1 6. Which of the following definition is correct? int length char int int long float double ans::1 7. What will be the output #include<stdio.h> main() { int i=4; switch (i) { default: printf("Default Value \n"); case 1: printf("Value is 1 \n"); break;

47 case 2: printf("Value is 2 \n"); break; case 3: printf("Value is 3 \n"); break; } } o\p: default value 8. #include<stdio.h>------------doubt main() { int i=1; while() { printf("%d", i++); if (i >10) { break; } } } Ans::Error 9. #include<stdio.h>------------------------------doubt main() { int x=30, y =40; if (x==y) printf("X is equal to Y"); else if (x>y) printf("x is greater than Y"); else if (x<y) printf("X is less than Y"); } Ans: X is less than Y

47

10. #include<stdio.h> main() { int i=10, j=15; if(i%2= = j%3) { printf("Same"); } else { printf("Different err"); } } o\p same 11. #include<stdio.h>------------doubt main() { int x=4, y, z; y=--x; z= x--; printf("%d %d %d", x,y,z); } Output: 433 432 332 233 223 Ans::233 12. #include<stdio.h> main() { float a = 0.7; if (a<0.7) {

47 printf("C"); } else { printf("C++"); } } Try with 0.7f o\p c++

13. #include<stdio.h> main() { float fval=7.29; printf("%d" ,fval); } o\p 7 14. #include<stdio.h>-----------------------doubt main() { printf("Welcome"); main(); } How many times the program will execute? Ans:Infinte

15. #include<stdio.h>----------------------doubt void fun(char *); void main()

47 { char a[100]; a[0] = 'A', a[1] = 'B'; a[2] = 'C', a[3] = 'D'; fun(&a[0]); } void fun(char *a) { a++; printf("%c", *a); a++; printf("%c", *a); } o\p b c 16. #include<stdio.h> #define MESS "Junk" main() { printf("MESS"); } o\p mess

18. #include<stdio.h> main() { char str[] = "Sales\0man\0"; printf("%d", strlen(str)); } o\p 5 19. #include<stdio.h> main() { char str[] = "Sales\0man\0"; printf("%d", sizeof(str)); }

47

o\p 11 20 #include<stdio.h>-------------doubt #include<string.h> main() { char sentence[80]; int i; printf("Enter the line of text"); gets(sentence); for(i=strlen(sentence)-1;i>=0;i--) { putchar(sentence[i]); } } 21 #include<stdio.h>-----------------------doubt main() { float a=3.15529; printf("%6.2f \n",a); printf("%6.3f \n",a); printf("%5.4f \n",a); printf("%2.1f \n",a); printf("%0.0f \n",a); } 1. Which of the following does not have an unary operator? 1) -7 3) j 2) ++i 4) all of the above

2. In printf(),the appearance of the output of the output can be affected by 1) field with 3) flag 2) conversion character 4) all of the above

3. Any of the following programs in c has access to three standard files: 1) standard input file, standard output file, standard error file

47 2) stdin,stdout, stderr 3) keyboard,screen,screen 4) all the above 4. The comma operator(,) is used to 1) permit two different expressions to appear in situations where only one expression would ordinarily be used 2) Terminate loops or to exit from switch 3) alter the normal sequence of program execution by transferring control to some other part of the program 4) carry out a logical test and then take one of two possible actions, depending upon the outcome of the test 5) A variable can be declared static using the keyword. 1) extern 2) static 3) stat 4) auto 6) A program can be terminated at any time by calling the function 1) fflush() 3) exit() 7) Heap 1) is a region from where memory is allocated 2) lies between you program and the stack 3) is a finite area 4) all of the above 8) A function can 1) perform a task 2) return a value 3) change value of actual arguments in call by reference 4) all of the above 9) Function definition void check(int i ,char*j) is 1) call by value 2)call by reference 3) both (1) and (2) 4)in valid function definition 10) A union consists of a number of elements that 1) all occupy the same space in memory 2) must be structure 2) ferror() 4) clearerr()

48 3) are grouped next to each other in memory 4) all have the same type 11) Which of the following array is defined in the statements Char name[30]? 5) 6) 7) 8) name is one dimensiona,30-element integer array name is one dimensional,30-element floating point array name is one dimensional ,30-element character array name is one dimensional,30-elements string array

12) c program contains the following declaration: Static float table[2][3]={ {1.1,1.2,1.3}, {2.1,2.2,2.3} }; What is the value of *(*(table+1)+1)? 2) 2.2 4) 2.1 2) 1.2 4) 2.3

13) A c program contains the following declarations and initial Assignments: int i=8,j=5; float x=0.0005,y=-0.01; Char c=c,d=d; What would be the value of the following expression? (3*i-2*j)%(2*d-3) 1)14 3) 1 2)18 4) 0

14) The declaration : int f(int 1); means 1) f accepts an integer argument and returns an integer quantity 2) f accepts two arguments and returns a double precision quantity, and the second is an integer 3) f accepts three arguments and returns nothing. The first arguments is a double-precision quantity, and the second is an integer 4) f does not accepts any arguments but returns a single character 15) The c language was developed by

48

1) Marting Richards 3) Ken Thompson

2) Dennis Ritchie 4) Smith Volt

16) The arguments of a function are included between 1) The parenthesis 2) double quotes 3) curly braces 4) # 17) The int type of constraints are whole numbers in the range 1) -23677 to 23678 3) -32767 to 32768 2) -32768 to 32767 4) -32864 to 32864

18) If the variables i,j and k are assigned the values 5,3 and 2 respectively, then the expression i=j+(k++ =6)+7; 1) gives an error message 3) assigns a value 18 to i 2) assigns a value 16 to i 4) assigns a value 19 to i

19) In a relational expression involving characters, we actually Compare 5) 6) 7) 8) the ASCII codes of the characters the characters themselves neither of the two binary code and hexadecimal code

20) The getchar function is used to 1) print the ASCII code of the character 2) to assign a character typed at the keyboard to a variable 3) to print a character on the screen 4) all of the above 21) The word case used in the switch statement represents a 5) 6) 7) 8) function in the c language data type in the c language keyword in the c language global variable in the c language

22)The logical NOT operator represented by ! is a 1) unary operator 3) ternary operator 2) binary operator 4) octal operator

48

23) The statement : scanf(%d,&i); 5) 6) 7) 8) assigns an integer to the variable i gives an error message; does not assign any value to i assigns an float to the variable i

24) A pointer is declared by using a statement such as 1) int *p; 2) point; 3) pointer *p; 4) int &p;

25)The null character is represented by 1) \n 2)\0 3)\o 4)\t

26) The members in the union 1) have different memory locations 2) share the memory with a structure 3) have the same memory location 4) have different memory variable 27) The global variables by default belong to 1) the register type 3) the auto type 2) the static type 4) the dynamic type

28) The bit fields are the members of a/an 1) array 2) structure 3) union 4) inter section

29) In c, square brackets [ ] are used in 1) functions 2) arrays 3) statements 4) all of the above

30) A fields width specifier in a printf() function 1) specifies the maximum value of a number

48 2) controls the size of type used to print numbers 3) controls the merging of the program listing 4) specifies how many characters positions will be used for a number 31) The two operators && and || are 1) arithmetic operators 3) logical operators 2) equality operators 4) relational operators

32) The library files that come with c are 1) text editor for program development 2) the compiler and liker 3) program examples 4) files that contain functions which carry out various commonly Used operations and calculations 33) precedence determines which operator 1) is evaluated first 3) is fastest 2) is most important 4) operates on the largest number

34) The string containing certain required formatting information is called 1) argument 3) character string 2) character array 4) control string

35) The advantage of a switch statement over an else-if construct is 1) a default condition can be used in the switch 2) the switch is easier to understand 3) several different conditions can cause one set of statements to executed in a switch 4) several different statements can be executed for each case in a switch 36) The header file math.h can be used for 1) providing links to assembly-language for calls 2) providing diagnostic and debugging assistance 3) providing support for the string handling functions 4) none of the above 37) The malloc() function

48 1) returns a pointer to the allocated memory 2) returns a pointer to the first byte of region of memory 3) changes the size of the allocated memory 4) deallocates the memory 38) which of the following expressions will return a 1 if both bits have A value of 1; otherwise will return a value of 0? 1) AND 2)OR 3)XOR4)1stderr complement

39) If an error occurs while opening a file the file pointer is assigned a value 1) NULL 2) stdout 3) sstderr 4) not defined

40) Which of the following backslash codes used for bell? 1) \b 2) \a 3) \r 4) \s

41) One of the valid keywords in the c language is 1) printf 2) CHAR 3) auto 4) scanf

42) The comments in a c language program are placed between 1) \* and /* 2) / and .* 3) /*and*/ 4) # and #

43) If p and q are assigned the values 2 and 3 respectively then the statement p=q++ 1) gives an error message 2) assigns a value 4 to p 3) assigns a value 3 to p 4) assigns a value 5 to p 44) A compound statement is a group of statement included between a pair of 1) double quots 3) parentesis 2) curly braces 4) / and/

45) The number of the relational operators in the c language is 1) four 2) six 3) three 4)one

46) In the c language, 3 represents 1) a digit 2)an integer 3)a character 4)a word

48 47) In the c language, a hexadecimal number is represented by writing 1) x 2) xo 3) 0x 4)h

48) A string in the c language is represented by enclosing a series of characters in 1) single quotes 3) parenthesis 49) One structure cannot be 5) 6) 7) 8) a member of some other structure a member of the same structure a member of a union all of the above 2) double quotes 4) / and /

50) Masking is used 1) to copy a portion of a given bit pattern to a new variable, while the remainder of the new variable is filled with 0s(using the bitwise AND) 2) to copy a portion of a given bit pattern to a new variable, while the reminder of the new variable is filled with 1s (using the bitwise OR) 3) to copy a portion of a given bit pattern to a new variable, while the remainder of the original bit pattern is inverted within the new variable 4) all of the above 51) Almost every c program begins with the statement 1) main() 2) printf() 3) #include<stdio.h> 4) scanf()

52) A single character input from the keyboard can be obtained by using the function 1) printf( ) 2) getchar( ) 53) An expression 5) is a collection of data objects and operators that can be evaluated to a single value 6) is a name that substitutes for a sequence of characters 7) causes the computer to carry out some action 3) putchar( ) 4) scanf( )

48 8) all of the above 54) The expression c=i++ causes 5) 6) 7) 8) the value of I assigned to c and then I incremented by 1 I to be incremented by 1 and then the value of I assigned to @ Value of I assigned to c I to be incremented by 1

55)The single character input/output functions are 1) scanf( ) and printf( ) 3) scanf( ) and putchar( ) 2) getchar( ) and printf( ) 4) getchar( ) and putchar( )

56) The conversion character I for data output means that the Data item is displayed as 5) 6) 7) 8) a floating point value with an exponent an unsigned decimal integer a signed decimal integer an octal integer

57) The header file ctype.h can be used for 1) providing links to assembly language for calls 2) providing diagnostic and debugging assistance 3) providing support for string handling functions 4) providing character type identification (Boolean) and translation 58) In a circular linked list 5) 6) 7) 8) components are all linked together in some sequential manner there is no beginning and no end components are arranged hierarchically forward and backward transversal within the list is permitted

59) The function ftell( ) 1) 2) 3) 4) reads a character from a file reads an integer form a file gives the current position in the file sets the position to the beginning of the file

48 60) A c function contain 1)function body 3)a function header 1.int a=123.12; int b=-123.12; c=a+b; printf("%2f",c); a) 0.00 b) 123.12 c) 246.24 d) None of these 2. struct emp { int a; char *name; }; main() { struct emp *e; printf("%d",sizeof(e)); } ANs: 2 3. which is the best thing for linked list than arrays? i) Insertion ii) Deletion iii) Traversal a) (i) only b) (i),(ii) only c) ii,iii only d) iii only 4. consider the character array of size 10. When a string is more than 10 a) Error b) Overwrites c) Nothing d) garbage value 2)argument declaration 4)all of the above

48

5. main() { char *str; char *fun(); str=fun(); printf("%s",str); } char * fun() { char *buffer; strcpy(buffer,"Hello world"); return buffer; } a) hello world b) Compiler error c) Runtime error d) None of the above 6. main() { char *str; char *fun(); str=fun(); printf("%s",str); } char * fun() { char *buffer; buffer=(char *) malloc(sizeof(char)); strcpy(buffer,"Hello world"); return buffer; } a) hello world b) Compiler error c) Runtime error

48 d) None of the above ans::a

7) what is the prefix expression for the given Infix expression A+B*C/D Ans::+A/*BCD

8) int a; a=65536; printf("%d",a); a) 0 b) Garbage c) 65535 d) -32768 9) main() { char p=65; printf("%c",p); } a) 65 b) p c) error d) none of the above 10) In a function call, _____________ is passed as arguments. a) variables b) constants c) Expressions d) All the above 11) The value of EOF is ___-1______. 12) () is used for __________ a) function body b) Arguments c) Return type d) Declaration of function 13) How does the string ends?

49 a) /n b) /t c) /0 d) none 14) The range of Unsigned integer is a) 127 to -128 b) 0 to 65535 c) Depend up on the compiler d) -32768 to 32767 15) which one of the following is a illegal real constants a) 32.535 b) -1.5E25 to 3.5E65 c) "53682" d) -6.583 16) main() { int i,a[5]; a[1]=5; for(i=0;i<5;i++) { printf("%d",a[i]); a++; } a) 0 0 0 0 0 b) Garbage value c) 0 5 0 0 0 d) Compiler error ans:d..base address cant be changed a++ 17) The size of int is 2 bytes,then what is the size of short int? a) 1 b) 2 c) 4 d) 8 18) In a queue,what condition applies a) First In First Out b) Last in first out c) Elements are inserted and deleted at one end

49 d) Elements can be inserted and deleted in different ends 19) which of the following statements are true in the case of doubly linked list i) Every node is connected to other node ii) We can traverse in both the directions a) i) only b)ii) only c) Both i and ii d) neither i nor ii ans::c 20) main() { char str[]="Wipro Infotech"; char *s; s=&str[13]-13; while(*s) printf("%c",*s++); } Ans:: Wipro Infotech 21) char *a="Malayalam"; char *s; Char *str; str=a; s=str+8; for(;s>=str;s--) printf("%c",*s); a) Malayalam b) malayalaM c) error d) None of the above 22) struct main { int a; int b; struct main *e1; } printf("%d %d",sizeof(e1),sizeof(*e1)); 23) #define wipro Hai void main()

49 { printf("wipro"); } 24) Is allocating a block of memory effectively the same as defining an array? a) True b) false 25) the size of a node od doubly linked list is always greater than the single linked list a) True b) false 26) Queue is used for i) Expression Evaluation ii) Scheluding the job in First come First serve a) i) only b) ii only c) both i & ii d) neither i nor ii 27) what should be replace ????? in the program to get the output 25? ????? void main() { int x=2,y=3,j; j=SQRT(x+y); printf("%d",j); } a) #define SQRT(int) (int * int) b) #define SQRT(int) (int) * (int) c) #define SQRT(int) (int + int) d) #define SQRT(int) (int) + (int) 28) void fun() { static char p[]="Hello"; return p; } main()

49 { printf("%s",fun()); } what will be the output? Ans::Error Void function do not return a value. a) Compiler Error b) Hello c) Garbage value d) None 29) void main() { int *p; p=(int *)malloc(sizeof(int)); for(i=0;i<5;i++) printf("%d ",p[i]); } What is the output? Ans:garbage value 30) main() { int i,j; for(i=1,j=10;i<j;i++,j--); printf("%d %d",i,j); } Ans:6,5 31) Which of these will pass the address of the pointer *ptr to the function demofun()? a) demofun(ptr) b) demofun(&ptr) c) demofun(*ptr) d) demofun(*&*ptr); 32) which is not a valid expression a) x!=y b) x! c) !x d) x!y 33) If max=10,rear=max,front=0,then what will be my queue? a) Queue Empty b) Queue Full c) Queue has one element d) Queue has max-1 elements 34) which is an indefinite loop? a) do while(0); b) for(;0;);

49 c) while(1); d) while(0); 35) int *ptr; ptr=(int *)malloc(10*sizeof(int)); which is correct? i) All Values are intialised to garbage values ii) Creates memory for 10 integer data a) i only b) ii only c) both i and ii d) neither i nor ii 36) int *ptr; ptr=(int *)calloc(10*sizeof(int)); which is correct? i) All Values are intialised to zero ii) Creates memory for 10 integer data a) i only b) ii only c) both i and ii d) neither i nor ii 37) Struct queue { int rear; int front; int a[100]; } struct queue *q; then how will you add a new element called 'item' in the queue? a) q->rear[a]=item; b) q->a[q->rear]=item; c) q->a[rear]=item; d) q->rear[q->a]=item;

38) In which of the following we can sort the data without moving the data a) Array

49 b) Single Linked list c) Doubly linked list d) Binary search trees 39) Char d=128; printf("%c",d); a)128 b)-128 c) error d) Garbage values 40) In the following definition struct node *ptr; ptr=(struct node *)calloc(sizeof(ptr)); a) ptr is allocated 4 bytes b) ptr will be allocated sizeof struct node c) Error d) ptr will have 8 bytes 41) In a doubly linked list ,if the first node is first and the last node is end,what will be the output? traverse(struct node*end) { while(end!=NULL) traverse(end->prev); printf("%d",end->data); } if the input is 1,2,3,4,5 then the output will be a) 1,2,3,4,5 b) 5,4,3,2,1 c) compilation error d) none 42) void main() { int b=20; printf("%d"*&b++); } what will be the output? a) 21 b)20 c) error d) Garbage value

49 ANS::C 43) how will you refer the last node in the doubly linked list which is pointed by the pointer variable 'cursor'? a)cursor==NULL b)cursor->link=NULL c) Cursor->link=0 d) cursor->data=NULL

44) how will you refer the previous node of the pointer 'cursor' in the doubly linked list (cursor is not in the first or in the last)? a)cursor->link++ b)cursor=cursor->left c) Cursor++ d) cursor->left++

1. #include<stdio.h> int x=40; main() { int x = 20; printf("\n %d",x); } Predict the output 20 40 20, 40 None of the above

49

2. #include<stdio.h> main() { int x=40; { int x = 20; printf("\n %d",x); } printf("%d",x); } Predict the output 20 40 40 20 40 20 3. #include<stdio.h> main() { extern int a; printf("\n %d",a); } int a=20; Predict the output 20 0 Garbage value Error 4. #include<stdio.h>--------------------------------doubt main() { struct emp { char name[20]; int age; float sal; };

49 struct emp e={"Raja"}; printf("\n %d %f", e.age, e.sal); } 0 0.000000 Garbage values Error None of the above 5. #include<stdio.h> main() { int x=10, y=20, z=5, i; i= x < y < z; printf("%d", i); }o\p 1 6. Which of the following definition is correct? int length char int int long float double 7. What will be the output #include<stdio.h> main() { int i=4; switch (i) { default: printf("Default Value \n"); case 1: printf("Value is 1 \n"); break; case 2: printf("Value is 2 \n");

49 break; case 3: printf("Value is 3 \n"); break; } }o\p default value 8. #include<stdio.h>------------doubt main() { int i=1; while() { printf("%d", i++); if (i >10) { break; } } } 9. #include<stdio.h>------------------------------doubt main() { int x=30, y =40; if (x==y) printf("X is equal to Y"); elseif (x>y) printf("x is greater than Y"); elseif (x<y) printf("X is less than Y"); } Statement missing Expression Syntax Lvalue required RValue required

50

10. #include<stdio.h> main() { int i=10, j=15; if(i%2 = j%3) { printf("Same"); } else { printf("Different err"); } } o\p same 11. #include<stdio.h>------------doubt main() { int x=4, y, z; y=--x; z= x--; printf("%d %d %d", x,y,z); } Output: 433 432 332 233 223 12. #include<stdio.h> main() { float a = 0.7; if (a<0.7) {

50 printf("C"); } else { printf("C++"); } } Try with 0.7f o\p c++

13. #include<stdio.h> main() { float fval=7.29; printf("%d" ,fval); }o\p 7 14. #include<stdio.h>-----------------------doubt main() { printf("Welcome"); main(); } How many times the program will execute? 32767 65535 Till the stack doesnt overflow 15. #include<stdio.h>----------------------doubt void fun(char *); void main() { char a[100]; a[0] = 'A', a[1] = 'B';

50 a[2] = 'C', fun(&a[0]); } void fun(char *a) { a++; printf("%c", *a); a++; printf("%c", *a); } o\p b c 16. #include<stdio.h> #define MESS "Junk" main() { }o\p printf("MESS"); mess a[3] = 'D';

18. #include<stdio.h> main() { char str[] = "Sales\0man\0"; printf("%d", strlen(str)); }o\p 5 19. #include<stdio.h> main() { char str[] = "Sales\0man\0"; printf("%d", sizeof(str)); }o\p 11 20 #include<stdio.h>-------------doubt #include<string.h>

50 main() { char sentence[80]; int i; printf("Enter the line of text"); gets(sentence); for(i=strlen(sentence)-1;i>=0;i--) { putchar(sentence[i]); } } 21 #include<stdio.h>-----------------------doubt main() { float a=3.15529; printf("%6.2f \n",a); printf("%6.3f \n",a); printf("%5.4f \n",a); printf("%2.1f \n",a); printf("%0.0f \n",a); } 1. printf("%d %d %d",sizeof(25.75),sizeof(123),sizeof(p)) a. 2 2 2 b. 4 2 2 c. 8 4 1 d. 8 2 2 2. int i=5; fun( ) { printf("%d\n", i * 3); } main( ) { int i= 2; { int i = 3; printf(" %d", i); fun(); } } f. 3, 15 g. 3, 6

50 h. 3 i. 0 3. #define xsq(x) x*x main( ) { int i, j; i = 5; j = xsq(i-2); printf(%d\n, j); } e. 7 f. 9 g. 13 h. 29 4. main( ) { int a=35; printf( %d %d %d\n, a == 35,a=50,a>40); } a. b. c. d. 1 50 1 1 50 0 0 50 0 0 50 1

5. void main() { char a[]="123abcd"; clrscr(); printf("%d",strlen(a)); getch(); } a. b. c. d. 6. main() { static int var=5; if(var--) { printf("%d",var); main(); 6 7 8 5

50 } } a. 4 3 2 1 0 b. 4 3 2 1 c. 5 4 3 2 1 d. 5 4 3 2 1 0 7. void main() { int i=1,j=2; switch(i) { case 1: printf("One"); break; case j: printf("Two"); break; default: printf(Default); break; } } a. One b. Two c. Default d. Compiler Error 8. void main() { switch('a') { case 'A': printf("Zero"); case 97: printf("One"); default: printf("Error"); } } a. Zero b. One c. Error d. Compiler Error 9. void main() { int p=1,sum=0; clrscr(); while(p<20) { p++; sum+=p++;

break; break; break;

50 } printf("\nsum=%d",sum); } a. 120 b. 100 c. 110 d. Error 10. int x; int modifyvalue() { return(x+=10); } int changevalue(int x) { return(x+=1); } void main() { int x=10; x++; changevalue(x); x++; modifyvalue(); printf("First output : %d",x); changevalue(x); printf("\nSecond output : %d",x); } a. 12 12 b. 11 12 c. 12 13 d. 13 13 11. main() { static i=3; printf("%d",i--); return i>0?main():0; } a. 3 2 1 0 b. 3 2 1

50 c. 2 1 0 d. 2 1 12. void main() { char *ptr="Hello World"; *ptr++; printf("%s",ptr); ptr++; printf("%s",ptr); } a. Iello World Iello World b. Hello World ello World c. ello World ello World d. ello World llo World 13. int const *p; *p=5; printf("%d",*p++); a. 5 b. 6 c. Compiler Error d. Garbage Value 14. void main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("ab")); } a. 5 5 3 b. 4 5 3 c. 2 5 3 d. 1 5 3 15. P is a character pointer variable then, what will be the output of the following statement. printf("%d %d",sizeof(p),sizeof(*p)); a. 1 b. 2 c. 2 d. 1 2 1 2 1

50

16. void main() { char *s[]={"dharma","hewlet-packard","siemens","ibm"}; char **p; p=s; printf("%s",++*p); printf("\n%s",*p++); printf("\n%s",++*p); } a. harma harma ewlet-packard b. dharma harma ewlet-packard c. harma hewlet-packard siemens d. harma harma hewlet-packard 17. void main() { char *ptr="Ramco Systems"; (*ptr)++; printf("%s\n",ptr); ptr++; printf("%s",ptr); } a. Samco Systems b. Samco Systems c. amco Systems d. amco Systems Samco Systems amco Systems amco Systems mco Systems

18. #define square(x) x*x void main() { int i=7; clrscr(); i=64/square(4); printf("%d",i); }

50

a. 7 b. 16 c. 64 d. 4 19. #define man(x,y) (x)>(y)?(printf("%d",x)):(y) void main() { int i=15,j=10,k=0; k=man(i++,++j); printf(" %d %d %d",i,j,k); } a. 16 b. 17 c. 16 d. 16 17 17 16 17 11 11 11 12 2 2 2 2

20. struct one { int no:1; int pl:2; int x:3; }; void main() { struct one a; a.no=0; a.pl=1; a.x=3; printf("%d %u",a.no,a.no); printf("\n%d %u",a.pl,a.pl); printf("\n%d %u",a.x,a.x); } a. 0 0 1 1 3 3 b. 0 0 2 2 3 3 c. 1 1 2 2

51 3 d. 1 2 2 3 1 2 2

21. void main() { struct emp { struct e { int *a; }e1; int a; }; struct emp emp1; printf("%d %d",sizeof(emp1),sizeof(emp1.e1)); } a. 2 b. 2 c. 4 d. 4 4 2 4 2

22. struct emp emp1; struct emp { int a; }; main() { printf("Enter 1 values:"); scanf("%d%d",&emp1.a,&emp1.a); //The given input is 10 and 25 printf("a=%d a=%d",emp1.a,emp1.a); } a. 10 25 b. 25 25 c. 10 10 d. Compiler Error 23. Arrange the code in order to delete a node being pointer by temp. a) free(temp)

51 b) c) temp->prev->next = temp->next temp->next->prev = temp->prev;

a) b c a b) c b a c) a b c d) both a and b 24. What does below code do, if temp is pointing to a node other than first and last node temp -> prev ->next = temp ->next; temp ->next -> prev = temp -> prev; a) b) c) d) no effect inserts a node deletes a node shuffling of pointers

25. which is the faster traversable dynamically growing list a) Binary Search Tree b) Singly Linked List c) Doubly Linked List d) Using Array

51

1. void main() { int a=1,b=2,c=3; c=(--a, b++)-c; printf("%d %d %d",a,b,c); } (a)0 3 -3 (b)Compile-Time Error 0 2. #define swap(a,b) temp=a; a=b; b=temp; void main() { static int a=5,b=6,temp; if (a > b) swap(a,b); printf("a=%d b=%d",a,b); } (a)a=5 b=6 (b)a=6 b=5 these 3. void main() { int i=5; printf("%d %d %d %d %d",++i,i++,i++,i++,++i); } (a)Compile-Time Error (b)10 9 8 7 6 766 4. void main() { int i, n =10; for (i=1; i<n--; i+=2) printf("%d", n-i); } (a)84 (b)840 5. What is the output of the program? #include <stdio.h> int main(int argc, char *argv[])

(c)0 3 -1

(d)0 3

(c)a=6 b=0

(d)None

of

(c)9 8 7 6 6

(d)10 8

(c)852

(d)864

51 { printf(" %d", printf("Hello Genesis")); return 0; } E. F. G. H. 6. #include <stdio.h> main() { switch (5) { case 5: printf(" 5 "); default: printf(" 10 "); case 6: printf(" 6 "); } } F. G. H. I. 7. Which argument of function 'strncmp()' specifies number of characters to be compared? E. F. G. H. 8. Which of the following is not a storage class in C? F. G. H. I. Stack Register Extern Static first second between 2 strings third 5 5 10 6 5 10 56 Hello Genesis 13 Hello Genesis Hello Genesis 13 None of the above

51 9. What is the significance of the free() function? E. F. G. H. 10. What is the data type of FILE? E. F. G. H. 11. #include <stdio.h> #define sq(a) a * a void main() { printf("%d", sq(3 + 2)); } E. F. G. H. 12. Which of the following is a non-linear data structure? E. F. G. H. 13. Which of the following function does not return an integer value? Stack Queue Linked List Tree 25 11 10 Compilation error integer union pointer structure It assigns the pointer a NULL value It erases the contents of any type and cleans the pointer It places the memory address with the pointer in free store It disables the memory address with the pointer

51 F. G. H. I. 14. Which of the following function does not support dynamic memory allocation? E. F. G. H. 15. What is the output of the program if the input is 103? main() { int p = 234; printf(" %d ", printf("%d", p), scanf("%d", &p)); } E. F. G. H. 16. Where do we use a 'continue' statement? F. G. H. I. 17. 2. Queue is _________________ a) b) c) d) LIFO LILO FIFO Both b & c In 'if' statement In 'switch' statement In 'goto' labels None of the above 3 103 103 103 3 103 2 alloc realloc malloc free printf scanf strcpy strlen

51 18. What is the postfix expression for A + B * C / D E * F / G i) ABC*D/+EFG*/j) ABC*D/+EF*G/k) ABCD/*+EF*G/l) None of these. 19. Write one statement equivalent to the following two statements: x=sqr(a); return(x); Choose from one of the alternatives (a) return(sqr(a)); (b) printf("sqr(a)"); (c) return(a*a); (d) printf("%d",sqr(a)); 20. int i=5; int abc(int z) { return i/2; } main() { int i=4; printf("%d",abc(i=i/4)); } a) b) c) d) error 5 2 0

21. union U { int x; float y; char s[2]; }; union U ob; what is the size of ob in bytes, a) 4 b) 2

51 c) 8 d) 7 22. The operation for adding and deleting an entry to a stack is traditionally called: a.add , delete b.append , delete c.insert, delete d.push , pop e. front , rear 23. What is the Infix expression for - + A / * B C D / * E F G a) A + B * C / D E / F * G b) A + B / C * D E * F / G c) A + B * C / D E * F / G d) A - B * C / D + E * F / G 24. What would be the root node if we enter the following data set (in the respective order) into a standard program to construct a Binary search tree? 25, 72, 16, 11, 88, 26, 9, 36, 21, 45, 14, 69 e) 69 f) 25 g) 26 h) 9 25. what does the code below do, where head is pointing to first node & temp is a temporary pointer. 10 be the number of nodes temp = head; while (temp->next->next!=NULL) { temp = temp ->next; } temp -> prev -> next = temp -> next; temp -> next -> prev = temp -> prev; free(temp); a) no effect

51 b) c) d) deletes some node deletes 2nd last node deletes last node

51

52

1. What will be the output of the following program : int main() { int val=5; printf("%d %d %d %d",val,--val,++val,val--); return(0); } (a)3 4 6 5 (b)5 5 6 5 (c)4 4 5 5 (d)None of these 2. #define Compute(x,y,z) (x+y-z) int main() { int x=2,y=3,z=4; printf("%d",Compute(y,z,(-x+y)) * Compute(z,x,(-y+z))); return(0); } (a)40 (b)30 (c)Compile-Time Error (d)None of these 3. What will be the output of the following program : int main() { int val=5; val=printf("C") + printf("Skills"); printf("%d",val); return(0); } (a)7 (b)C7 (c)Compile-Time Error(d)CSkills7 4. What will be the output of the following program : int main() { char str[]="Test"; if ((printf("%s",str)) == 4) printf("Success"); else printf("Failure"); return(0); } a)Success b)TestSuccess c)Compile-Time Error(d)Failure 5.

52 What will be the output of the following program: int main() { int val=5; printf("%d",5+val++); return(0); } (a)Compile-Time Error (b)Lvalue required Error 6. void main() { printf("%d",sizeof(int)); return(0); } (a)Data types not allowed (b)Compile-Time Error(c)3 (d)2 7. In tree construction which is the suitable efficient data structure? (a) Array (b) Linked list (c) malloc (d) Queue 8. Traverse the given tree using Inorder, Preorder and Postorder traversals. Given tree: A

(c)10 (d)11

B a)Inorder : DHBEAFCIGJ Preorder: D A B D H E C F G I J E Postorder: HDEBFIJGCA b)Inorder : Preorder: Postorder: c)Inorder : Preorder: Postorder: d)Inorder : Preorder: Postorder: DHBEAFCIGJ DHEABCFGIJ HH E B F I J G C A D DHBEAFCIGJ ABDHECFGIJ HDEBFIJGAC HDBEAFCIGJ ABDHECFGIJ HDEBFIJGCA

52

9. main() { static int var = 5; printf("%d ",var--); if(var) main(); } b. 4 3 2 1 10. #include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } c. 3 hello b. Compiler Error 11. #include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } b. 5 6.000000 12. void main() { int k=ret(sizeof(float)); b. 5 5.000000 c. 6 6.000000 d. compiler error c. Run time error d. use dot (.) operator b. 4 3 2 1 0 c. 5 4 3 2 1 d. 0 0 0 0 0

52 printf("\n %d",++k); } int ret(int ret) { ret += 2.5; return(ret); } b. Compiler Error b. 8 13. int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); } c. 20 10 b. 10 20 c. 20 20 d. 10 10 14. main() { char *p = ayqm; char c; c = ++*p++; printf(%c,c); b. a 15. main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } c. 0 b. 0 1 2 c. 1 2 0 d. Compiler Error e. 2 0 } b. b c. y d. z

c. 6

d. 7

52 16. #define MESS junk main() { printf(MESS); } b. Junk b. Error c. MESS 17. main () { int i = 5; switch (i) { static int i; i = 3; i = i * i; case 3: i = i + i; case 4: i = i + i; case 5: i = i + i; printf (%d,i); } printf (%d,i); } b. 9 b. 10 10 c. 0 5 d. 18 18 e. 18 5

d. MESS junk

18. What would be the output of the following program. Int fn(int); main() { int i=10; fn(i); printf("%d",i); } fn(int i) { return ++i; } (a) 10 (b) 11 (c) 12 Compilation error 19.

(d)

52 main() { FILE *fp1,*fp2; fp1=fopen("one","w"); fp2=fopen("one","w") ; fputc('A',fp1) ; fputc('B',fp2) ; fclose(fp1) ; fclose(fp2) ; } Find the Error, If Any? e. f. g. h. 20. void main() { int a=555,*ptr=&a,b=*ptr; printf("%d %d %d",++a,--b,*ptr++); } (a)Compile-Time Error (b)555 554 555 (d)557 554 555 (c)556 554 555 no error. But It will over writes on same file. no error. But It will create one more file. error. It will not allow. no error. The new content will append in existing file.

21. what type of Binary Tree is the following tree below

52

e. f. g. h.

binary tree strictly binary tree complete binary tree not a binary tree

22. If suppose root to be deleted then which node will be the root node A

52

e. f. g. h. 23.

B G Any node Both a and b are correct

When fopen() fails to open a file it returns a) NULL b) 1 c) 1 d) None of the above 24. Which function is used to detect the end of file a) EOF b) feof( ) c) ferror( ) d) NULL

25. If the CPU fails to keep the variables in CPU registers, in that case the variables are assumed a) static b) external c) global d) auto 1.main() { int x,y,z; x=y=z=1; z=++x||++y&&++z; printf("%d %d %d",x,y,z); } 2.13. main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); } ans::1 10 3.2. main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++;

52 printf("%d %d %d %d %d",i,j,k,l,m); } ans::0 0 1 3 1 3.28. main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); } ans::4 1 3.29 main() { int a=500,b=100,c=300,d=40,e=19; a+=b-=c*=d/=e%=5; printf("%d %d %d %d %d",a,b,c,d,e); } 2.main( ) { int k=35; printf( %d %d %d\n, k == 35,k=50,k>40); } ans::0 5 0 3.main( ) { int x=4,y,z; y = --x; z = x--; printf(%d %d %d\n,x,y,z); } ans:2 3 3 1.6. main() { int i=10; i=!i>14; printf ("i=%d",i); } ans::0 1.20. main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

52 ans -1,-1 1.34. #include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); } 1.42. main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); } 3.4. main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } int main() { int x=5,y=6,z=2; clrscr(); z/=y/z==3?y/z:x*y; printf("%d",z); return 0; } int main() { int a=1,b=2,c=3,d=4; clrscr(); printf("%d\n",!a?b?!c:!d:a); printf("%d %d %d %d",a,b,c,d); return(0); } 5.main() { printf("\nab"); printf("\bsi");

53 printf("\rha"); } 1. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); } 2.#include<stdio.h> #include<conio.h> void main() { char a[]="abcdef"; char *p=a; clrscr(); p++; p++; p[2]='z'; printf("%s",p); getch(); } ans::cdzf 2.6. main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); } ans:: 2 5 5 2.1. main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); } ans:: m m m a a a n n n

53 1.7. #define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); } ans::64 1.7.2 #define cube(x) x*x*x main() { int b=3,a; a=cube(b++); printf("%d %d",a,b); } ans::27,6 1.8. #include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); } ans::50 3.3. #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } 3.6. main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t;

53 for (i=0;i<=4;i++) printf("%s",names[i]); } ans::Lvalue Required.....base address can't be changed names[3]=names[4];

1)Allocating a 2 blocks of memory for 'n' intgers is by using a) malloc b) calloc c) both malloc and calloc d) none of the above 2) In queue using linked list, which is correct? a) both insertion and deltionis made at the front end b) insertion is made at the beginning and deletion is made at the last c) insertion is made at the last and deletion is made at the beginning d) both insertion and deltionis made at the back end 3) for(i=0;i<2;i++) { for(j=0;j<5;j++) { if(i==j) break; printf("wipro"); } } how many times would wipro to be printed? 1 time 4) all the local variables are stored in ----------------a) stack b) heap c) queue d) none ans::a 5) Once you call a function,all the return addresses are stored in ---------------a) stack b) heap c) queue d) none 6) Which of the following is true about binary tree i) all nodes except leaf node has exactly two child ii) root node is greater than the left sub tree and lesser than the right sub tree.

53 a) i only b)ii only c) both i and ii d) neither i nor ii 7) Which of the following is true about complete binary tree i) all nodes except leaf node has exactly two child ii) root node is greater than the left sub tree and lesser than the right sub tree. a) i only b)ii only c) both i and ii d) neither i nor ii 8) struct node { int data; struct node *left,*right; }; suppose start and end are the pointers pointing to the beginning and ending node reapectively.then,what will be the output of the following snippet front=start; back=end; while(back!=NULL) { printf("%d",back->data); back=back->left; } Data Structure Questions & Answers 1)Consider the following structure struct node { int info; struct node *link; }; Suppose ptr is a pointer which is not pointing to the first or the last node. Then if we are going to delete a node after ptr, then the code will be a) ptr=ptr->link; b) ptr->link=ptr; c) ptr->link=ptr->link->link; d) ptr=ptr->link->link;

53

2) Consider the following structure struct node { int info; struct node *link; }; Suppose start is a pointer pointing to the first node of the linked list. s1 and ptr are the two pointers (they are not pointing to the first or last node). Then if we are going to execute the following code, i) start->link=s1; ii) s1->link=ptr; iii) ptr->link=start; then the list is a) b) c) d) It is having only 3 nodes with start, s1, ptr in the list, having start as the first node It is a circular linked list It is a doubly linked list None of the above

3) In a queue, if rear=front then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) None of the above 4) In a queue, if rear=0, front=0 then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) None of the above 5) a) b) c) d) In a queue, if rear=0, front=1 then what will be the queue Queue is empty Queue is full Queue has only one element Queue is circular

53

6) In a queue,if rear=-1,front=-1 then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) None of the above 7) In a queue, if rear=max-1, front=0 then what will be the queue a) Queue is empty b) Queue is full c) Queue has only one element d) None of the above 8) The postfix expression is ab+c*d/e-.The values of a, b, c, d, e are 2, 4, 5, 2, 1 respectively. Then the output is a) b) c) d) 14 11 20 15

9) The infix expression is a+b*(c-d)/(e+f)*h then my postfix expression is a) ab+cd-*ef+h*/ b) abcd-ef+*/h* c) abcd-*ef+/h*+ d) abcdef+-*/h*+ 10) In the stack, if top=0 then the stack is a) Stack is empty b) Stack is full c) Stack has only one element d) None of the above

11) Consider the structure struct node { int info; struct node *left; struct node *right; };

53 We have 10 elements in the list. If the following executes what will be the output? for(ptr=start; ptr; ptr=ptr->right) { if(ptr->data%2==0) printf("%d", ptr->data); } a) Only even numbers are printed b) Only odd numbers are printed c) Compiler error d) Only garbage values 12) struct node { int data; struct node *left,*right; };

Suppose nd is a node which is not in the beginning and also not in the end. How will you delete a node after nd? a) nd->right=nd->right->left;nd->right->left=nd->left->right; b) nd->right=nd->right->right;nd->right->left=nd; c) nd->right=nd->right->left;nd->right->left=nd->right; d) nd->right=nd->left->right;nd->left->right=nd;

13)

struct node { int data; struct node *left,*right; };

Suppose nd is a node which is not in the beginning and also not in the end. How will you delete a node before nd? a) nd->left=nd->right->left;nd->right->left=nd->left->right; b) nd->left=nd->right->right;nd->left->right=nd->right; c) nd->left=nd->left->left;nd->left->right=nd; d) nd->left=nd->left->right;nd->left->right=nd; 14) struct node

53 { int data; struct node *left,*right; }; Suppose ptr is a node which is not in the beginning and also not in the end. How will you delete a node ptr? a) ptr->left->right=ptr->right;ptr->right->left=ptr->left;free(ptr); b) ptr->left->right=ptr->right->right;ptr->left->right=ptr->right;free(ptr); c) ptr->left->right=ptr->left->left;ptr->left->right=ptr;free(ptr); d) ptr->left->right=ptr->left;ptr->left->right=ptr->left;free(ptr);

53 15) struct node { int data; struct node *left,*right; };

Suppose ptr is a node which is not in the beginning and also not in the end. nd is the new node. Here is the coding: i) ptr->right->left=nd; ii) nd->left=ptr; iii) ptr->right=nd; iv) nd->right=ptr->right; Then what sequence does it follows for inserting nd after ptr? a) i,ii,iii,iv b) ii,iv,i,iii c)iv,iii,ii,i d) ii,iii,i,iv 16) In the Given Infix expression which is the root node for your expression tree (A+B)(C*D)+G/H*I a) + b) c) * d) /

17) Consider a binary search tree insert(10,root); insert(25,root); insert(5,root); insert(8,root); insert(13,root); insert(45,root); insert(70,root); insert(32,root); delete(13,root); insert(66,root); insert(13,root);

53 insert(36,root); What will be the preorder traversal is a) 5,8,10,13,25,32,36,45,66,70 b) 10,5,8,25,13,45,32,36,70,66 c) 10,8,5,13,32,45,36,66,32,70 d) 8,5,32,36,10,66,45,70,25,13

19) The preorder traversal is 5, 3, 66, 30, 77, 70 .What will be the root node a) 5 b) 66 c) 70 d) 30 20) Which one of the following is true for the binary tree i) root is greater than the left sub tree and lesser than the right sub tree ii) root is lesser than the left sub tree and greater than the right sub tree a) only i b) only ii c) both i and ii d) Neither i nor ii 1. What will be the output of the following program : void main() { printf("%f",123); } (a)123 (b)Compile-Time Error (c)123.00 2. What will be the output of the following program : void main() { int a = -1,b=2,c=3,d=4; d=++a||++b&&++c; printf(%d %d %d %d,a,b,c,d); } (a) 0 3 4 1 (b) 1 3 4 5 (c)1 3 4 0 3. What will be the output of the following program : void main()

(d)123.000000

(d) 1 2 3 4

54 { char line[80]; // Max. length=80 Chars scanf("%[^,]s",line); printf("\n%s",line); } INPUT :Dear,how, are, you (a)Compile Error (b)Dear Friends 4.. main() { scanf(%c%c%c,&I,&j,&k); } Input is : hello India hi What will be the values of i,j,k ? (c) dear (d) runtime error

// VARIABLES NOT DECLARED

a) hello , India, hi b) h , e , l (c ) compile time error (d) run time error 5) What will be the output of the following program : void main() { printf(); } (a)Run-Time Error (b)Compile-Time Error (c)No Output (d)None of these 6) What will be the output of the following program : void main() { printf("d%",8); } (a)8 (b)Compile Error (c)%d (d)None of these 7) void main() { int val=5; printf("%*d",val); } (a) 5 (b)5 (c)Compile-Time Error (d)None of these 8. void main() { printf("Hi!"); if (0 || -1) printf("Bye"); } (a)No Output (b)Hi! (c)Bye 9) void main() { printf("%d %d",sizeof ( 4.56),sizeof(4.56f)); }

(d)Hi!Bye

54 (a)8, 4 (b)4,8 (c)8,8 (d)4,4 10,. main() { for( i=-2;i ; i++) printf(hi\n); } here the loop is performed how many times ? (a) 2 ( b) 1 ( c) 3 d) infinite 11. ) which of the following doesnot change value of variable i . a) i++ (b) i+1 (c) i=i+1 (d) i+=1 *12. union u { Int a; Char ch[2]; }u1; U1.a=5; U1.ch[0]=4 ; u1.ch[1]=2; Printf(%d,a); // IT SHOULD BE u1.a } (a) 5 (b) 42 ( c) 1028 (d) 24 13. void main() { int x, y, z; x = 27 ; y = x % 5; z = x / 5 ; } what value y and z hold currently : (a) 2,5 (b) 2,5.4 (c) 2,5.000000 (d) none of these 14. int i=10,j=25; Double k; K = j/i; Printf(%lf,k); (a) 2 (b) 2.5 (c) 2.499999 (d) invalid type conversion 15. static int choice; switch(++choice,choice+1,choice-2,choice+=1)//choice =2 Currently choice will hold what value in Switch statement: (a) 2 (b) 0 (c) invalid switch statement (d) -1

( ans: 516)

16,. struct s { int rno=10;//cannot declare here }s1; printf(%d, s1.rno);

54

(a) compile error

(b) runtime error (c) 10

(d) 0

16. void main() { int a=1,b=2,c=3; c=(--a,b++)-c; printf("%d %d %d",a,b,c); } (a)0 3 -3 (b)Compile-Time Error (c)0 3 -1 (d)0 3 0 17. #define swap(a,b) a=b ;temp=a; ; b=temp; void main() { static int a=5,b=6,temp; if (a > b) swap(a,b); printf("a=%d b=%d",a,b); } (a)a=5 b=6 (b)a=6 b=5 (c)a=5 b=5 (d)None of these 18. void main() { unsigned int val=5; printf("%u %u",val,val-21); } (a)Compile-Time error (b)5 -6 (c)5 65520 (d)None of these 19. a=5,b=6; (++a<++b) ?a:--b; Printf(%d %d,a,b); } (a) 5 6 (b) 6 7 (c) 5 7 (d) 6 6 20. void main() { int x=4,y=3,z=2; *&z*=*&x**&y; printf("%d",z); } (a)Compile-Time error (b)Run-Time Error (c)24 (d)Unpredictable 21,. char a[2][11]={morning,India) printf(%s, *(a[0]+3)); (a) compile error (b) n (c) i (d) none of these *22. #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }

54 a)compilation error b)runtime error c)100 d)var##12

23,. int I=40; Int *p= i;//there sud be p=&i; I++; Int *q = i; Printf(%d %d, *p, *q); } (a) 40 41 (b) 41 41 (c) 40 40 (d) compile time error 24. The syntax of a function call statement is a) function name (argument list); b) [storage class] function name (parameter list); c) function name (parameter list); d) [return type] function name (argument list); 25. int a[5] = { 1,2,3,4,5}; a++; printf(%d,*a); (a) 1 (b) 2 (c) address of a[0] (d) none of these 26. feof ,NULL , EOF are used for the following commands respectively (a) fgetc,fgets,fread (b) fread, fgetc,fgets (c) fread, fgets,fgetc (d) none of these//ans sud be c 27, . which of the following mode is useful for editing a file ? (a) w+ (b) r+ (c) a+ (d) a 28, the term FILE is a (a) header file (b) Predefined function (c) Predefined structure (d) none of these 29,. int p[3][5] = { { 2, 4, 6, 8, 10}, { 3, 6, 9, 12, 15}, { 5, 10, 15, 20, 25} }; What is the vlalue of *(*(p+1)+1)+1 , *(*(p+1)+2)//ans is 7 9 so none of these (a) 6, 7 (b) 7,12 (c) 6 , 13 (d) none of these 30.to locate the position of file pointer in a file, which of the following function is used (a) ftell() (b) fseek ( ) (c) fwrite( ) (d) fread( ) 31. Which data structures used to perform recursion? A. B. C. D. FIFO Structure LIFO Structure LILO Structure NON Linear Structure

54 32. The operation for adding an entry to a stack is traditionally called: a. add b. append c. insert d. push 33. I have implemented the queue with a linked list, keeping track of a front pointer and a rear pointer. Which of these pointers will change during an insertion into a NONEMPTY queue? a. Neither changes b. Only front_ptr changes. c. Only rear_ptr changes. d. Both change. 34. Suppose we have an array implementation of the Queue, with ten items in the Queue stored at Que[0] through Que[9]. The CAPACITY is 30. From where does the DeleteQue function deletes item in the array? a. Que[0] b. Que[1] c. Que[9] d. Que[10] 35. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? a. ABDC b. DCAB c. DCBA d. ABCD *36.Here is an infix expression: 6+2*(1*5-9). Suppose that we are using the usual stack algorithm to convert the expression from infix to postfix notation. What is the maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression? e. 1 f. 2 g. 3 h. 4 i. 5 36. One difference between a queue and a stack is: a. Queues use two ends of the structure; stacks use only one. b. Queues require dynamic memory, but stacks do not.

54 c. Stacks require dynamic memory, but queues do not. d. Stacks use two ends of the structure, queues use only one. 37. If we draw a binary Tree for the expression : A * B - (C + D) * (P / Q) and traversed in Preorder, then we will get the following output. a. -*AB*+CD/PQ b. AB*CD+PQ/*c. ABCDPQ-*+*/ d. *-+*/ABCDPQ 38. Suppose that p is a pointer variable that contains the NULL pointer. What happens if your program tries to read or write *p? a. A syntax error always occurs at compilation time. b. A run-time error always occurs when the program finishes. c. The results are unpredictable. d. A run-time error always occurs when *p is evaluated. 39. Which of the following stack operations could result in stack Overflow flow? a. is_full b. pop c. push d. Two or more of the above answers 40. Suppose think there is a linked list with 10 nodes and a pointer Ptr pointing to the Second node. Then which of the following statement will cause Ptr to point 5th node data. a. Ptr->next->next->data b. Ptr->next->data c. Ptr->next->next->next->data d. Ptr->next->next->next 41. A Full Binary Search Tree contains, How many Nodes? (where n is height) a. n nodes b. 2^(n-1) nodes c. 2^n nodes d. (2^n)-1 nodes 42. A Structure which can refer an another Structure of same type is called as: a. Structure within structure b. Nested structure c. Self referential structure d. Dynamic structure 43. Suppose think there is a Double linked list with 15 nodes and a pointer Ptr pointing to the Fifth node. Then which of the following statement will cause Ptr to point 3th node data. a. Ptr->next->prior->prior b. Ptr->next->next->next

54 c. Ptr->next->prior->prior->prior d. Ptr->next->prior->next->prior 44. What kind of list is best to access the item at given position n?" a. Doubly-linked lists. b. Lists implemented with an array. c. Singly-linked lists. d. Doubly-linked or singly-linked lists are equally best 45. Suppose Ptr points to a node in a linked list. What Boolean expression will be true when Ptr points to the tail node of the list? a. Ptr == NULL b. Ptr->link == NULL c. Ptr->data == NULL d. Ptr->data == 0.0 e. None of the above. 46. the given below is login for Reverse traversal of a double linked list, replace the XXXXXXX with correct statements. for( XXXXXXX ) { printf(%d, ptr->marks); } a. b. c. d. ptr =first; (ptr); ptr = ptr->next ptr = last; (ptr); ptr = ptr->prior ptr = last; (ptr); ptr = ptr->next ptr = first; (ptr); ptr = ptr->prior

47. In a Binary Search tree a new node is always inserted as: a. Right child b. Left child c. Leaf node d. Non leaf node 48. Suppose Ptr needs to point last but one node in a Double linked list. Which of the statement will give me the result? a. Ptr=first->prior

54 b. Ptr=last->prior c. Ptr=last->next d. Ptr=first->next->prior 49. Which of the following applications may use a stack? a. A parentheses balancing program. b. Evolution of postfix expression. c. Syntax analyzer for a compiler. d. All of the above.

1.Consider the following statement : s= fscanf(infile, %c, &nextChar); what is true about the above statement? A. infile is the name of a file B. infile is the name of a variable of type FILE* C. after the fscanf executes, s contains the next character read from the file (unless there is no data left on the file) a. b. c. d. e. A only B only C only All of A, B and C None of A, B, or C

Answer: b. B only 2. Which of the following can be used across files a. extern b. volatile c. static d. const Answer: a 3. When fopen() fails to open a file it returns _______ a. NULL b. -1 c. 1

54 d. None of the above Answer: a. NULL 4. The EOF is equivalent to a. -1 b. 1 c. 0 d. None of the above Answer: a 5. What will be the output of following code assuming file already exists with some contents #include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF); printf("%c",i); } a. b. c. d. contents of zzz.c error contents of zzz.c followed by an infinite loop prints some data after EOF reaches.

Answer: D 6. what is the output of the following #define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); } a. no output b. error

54 c. else part is printed d. prints assertion failed in the file Answer: c

7. What will be the output of the following code segmet FILE *fp1,*fp2; fp1=fopen("one","w") ; fp2=fopen("one","w") ; fputc('A',fp1) ; fputc('B',fp2) ; fclose(fp1) ; fclose(fp2) ; a. No Error, but it will over writes on same file b. Error in pointer declaration c. Runs successfully d. None Answer: C 8. Considering the code Char x[20]; -------fgets(x,20,fp); printf(%s,x); where fp points to the file which contains the data in the following format wipro innovation program what is the output? a. b. c. d. wipro wiproinnovationprogr ram wipro\ninnovation\npro

Answer: A 9. When file opened in mode ab

55 a. b. c. d. pointer is placed at the beginning of file end of the file error in opening none of the above

Answer: b 10. Consider the following statement fseek(fp,50,4); which among the following is correct a. b. c. d. moves the file pointer to beginning of file moves the file pointer to the 50th byte from beginning of file moves file pointer to the 50th byte from end of file compilation error Answer: d SESSION 1 1. main() { float me = 1.1; double you = 1.1; if(me==you) printf("Hello"); else printf("Welcome"); } 1. Answer: Welcome Explanation: For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double. Rule of Thumb: Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) 2. main() {

55 int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } } 2. Answer : three Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

3. main() { int i=10; i=!i>14; printf ("i=%d",i); } 3. Answer: i=0 Explanation: In the expression !i>14 , NOT (!) operator has more precedence than > symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).

4. #include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD");

55 break; case j: printf("BAD"); break; } } 4. Answer: Compiler Error: Constant expression required in function main. Explanation: The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error). Note: Enumerated types can be used in case statements. 5. main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } 5. Answer: 1 Explanation: Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1. 6. main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } 6. Answer: 1 Explanation: before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).

55 7. main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); } 7. Answer: i = -1, +i = -1 Explanation: Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator).

8. main() { char not; not=!2; printf("%d",not); } 8. Answer: 0 Explanation: ! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0.

9. main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); } 9. Answer: 1==1 is TRUE Explanation: When two strings are placed together (or separated by white-space) they are concatenated (this is called as "stringization" operation). So the string is as if it is given as "%d==1 is %s". The conditional operator( ?: ) evaluates to "TRUE".

55

10. main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); } 10. Answer: 2000 is a leap year Explanation: An ordinary program to check if leap year or not.

11. main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); } 11. Answer: i = -1, -i = 1 Explanation: -i is executed and this execution doesn't affect the value of i. In printf first you just print the value of i. After that the value of the expression -i = -(-1) is printed.

12. int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p 12. Answer: 4--0

55 3--1 2--2 Explanation: Let us assume some x= scanf("%d",&i)-t the values during execution will be, t i x 4 0 -4 3 1 -2 2 2 0

13. main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); } 13. Answer: hello Explanation: The comma operator has associativity from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if becomes true so, "hello" will be printed.

14. main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); } 14. Explanation: i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and control comes out of the loop.

15. void main() { while(1){ if(printf("%d",printf("%d")))

55 break; else continue; } } 15. Answer: Garbage values Explanation: The inner printf executes first to print some garbage value. The printf returns no of characters printed and this value also cannot be predicted. Still the outer printf prints something and so returns a non-zero value. So it encounters the break statement and comes out of the while statement.

16. #include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); } 16. Answer: Garbage-value 0 Explanation: The value of y%2 is 0. This value is assigned to x. The condition reduces to if (x) or in other words if(0) and so z goes uninitialized. Thumb Rule: Check all control paths to write bug free code.

17. main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); } 17. Answer infinite loop Explanation

55 The difference between the previous question and this one is that the char is declared to be unsigned. So the i++ can never yield negative value and i>=0 never becomes false so that it can come out of the for loop. 18. main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); } 18. Answer:128 Behavior is implementation dependent. Explanation: The detail if the char is signed/unsigned by default is implementation dependent. If the implementation treats the char to be signed by default the program will print 128 and terminate. On the other hand if it considers char to be unsigned by default, it goes to infinite loop. Rule: You can write programs that have implementation dependent behavior. But dont write programs that depend on such behavior.

19. main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); } 19. Answer: A Explanation: Due to the assignment p[1] = c the string becomes, %c\n. Since this string becomes the format string for printf and ASCII value of 65 is A, the same gets printed.

20. main() { int i=10,j=20; j = i, j?(i,j)?i:j:j;

55 printf("%d %d",i,j); } 20. Answer: 10 10 Explanation: The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as: if(i,j) { if(i,j) j = i; else j = j; } else j = j;

21. main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } 21. Answer: Compiler Error: switch expression not integral Explanation: Switch statements can be applied only to integral types. 22. Which version do you prefer of the following two, 1) printf(%s,str); // or the more curt one 2) printf(str); 22. Answer & Explanation: Prefer the first one. If the str contains any format characters like %d then it will result in a subtle bug.

55

23. void main() { char ch; for(ch=0;ch<=127;ch++) printf(%c %d \n, ch, ch); } 23. Answer: Infinite Loop Implementaion dependent Explanation: The char type may be signed or unsigned by default. If it is signed then ch++ is executed after ch reaches 127 and rotates back to -128. Thus ch is always smaller than 127.

24. main() { char a[4]="HELLO"; printf("%s",a); } 24. Answer: Compiler error: Too many initializers Explanation: The array a is of size 4 but the string constant requires 6 bytes to get stored. 25. main() { char a[4]="HELL"; printf("%s",a); } 25. Answer: HELL Explanation: The character array has the memory just enough to hold the string HELL and doesnt have enough space to store the terminating null

56 character. So it prints the HELL correctly and continues to print garbage values till it accidentally comes across a NULL character. Session -2 26. main() { int c=- -2; printf("c=%d",c); } 26. Answer: c=2; Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus. Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.

27. void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } 27. Answer: Ok here Explanation: Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok here" is printed.

28. What is the output of the program given below

56 main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); } 28. Answer -128 Explanation Notice the semicolon at the end of the for loop. THe initial value of the i is set to 0. The inner loop executes to increment the value from 0 to 127 (the positive range of char) and then it rotates to the negative value of -128. The condition in the for loop fails and so comes out of the for loop. It prints the current value of i that is -128.

29. main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); } 29. Answer: Behavior is implementation dependent. Explanation: The detail if the char is signed/unsigned by default is implementation dependent. If the implementation treats the char to be signed by default the program will print 128 and terminate. On the other hand if it considers char to be unsigned by default, it goes to infinite loop. Rule: You can write programs that have implementation dependent behavior. But dont write programs that depend on such behavior.

30. main() { int i = 3; for (;i++=0;) printf(%d,i); }

56

30. Answer: Compiler Error: Lvalue required. Explanation: As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation. 31. main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); } 31. Answer: 1 10 Explanation: The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result.

Session 3 32. main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); } 32. Answer: 00131 Explanation : Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression i++ && j++ && k++ is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for 0 || 0 combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.

56

33. main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } 33. Answer: 45545 Explanation: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result.

34. main() { int i=5,j=6,z; printf("%d",i+++j); } 34. Answer: 11 Explanation: the expression i+++j is treated as (i++ + j)

35. main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); } 35. Answer: 0..0 Explanation: The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if

56 statement is not executed. The values of i and j remain unchanged and get printed. 36. void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); } 36. Answer: 0 65535

37. main() { unsigned int i=10; while(i-->=0) printf("%u ",i); } 37. Answer: 10 9 8 7 6 5 4 3 2 1 0 65535 65534.. Explanation: Since i is an unsigned integer it can never become negative. So the expression i-- >=0 will always be true, leading to an infinite loop.

38. main() { unsigned int i=65000; while(i++!=0) ; printf("%d",i); } 38. Answer: 1 Explanation: Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to post-increment on i the value of i while printing is 1.

56

39. main() { int i=5; printf(%d,i=++i ==6); } 39. Answer: 1 Explanation: The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result. 40. main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); } 40. Answer: 41 Explanation: The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expressions truth value is 1. Because it is followed by || and true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression, when any of the operands become false, the whole expressions truth value becomes false and hence the remaining expression will not be evaluated. false && (anything) => false where (anything) will not be evaluated.

SESSION 1 1. main() { float me = 1.1; double you = 1.1;

56 if(me==you) printf("Hello"); else printf("Welcome"); } Ans: welcome 2. main() { extern int i; i=20; printf("%d",i); } Ans: linker error. 3. main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } } Ans: Three 4. main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); } Ans::Hello World 5. #define int char

56 main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); } Ans: 1 6. main() { int i=10; i=!i>14; printf ("i=%d",i); } Ans: 0 7. #define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); } Ans: 64 8. #include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); } Ans: 50 9. #define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); } Ans: 100

56

10.main() { printf("%p",main); } Ans : address of main 11. main() { clrscr(); } clrscr(); ans::error:type mismatch 12. main() { int i=400,j=300; printf("%d..%d"); } Ans::300 400 13. main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } Ans: error::goto here sud be defined and called in same fuction 14. #include<stdio.h> main() { int i=1,j=2; switch(i)

56 { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } Ans: Error::case sud have a constant value 15. main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } Ans: 1 16. main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } Ans: 1 ya since i++ will not increment first time so I is o in for thats why 17 . main() { extern int i; i=20; printf("%d",sizeof(i)); } Ans: Linker error 18. main() { extern out; printf("%d", out); } int out=100;

57 Ans: 100 19. main() { show(); } void show() { printf("I'm the greatest"); } Ans::I m the greatest

20. main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); } Ans: -1 -1 21. What are the files which are automatically opened when a C file is executed? Ans: stdinput,stdoutput,stderror. 22. main() { main(); } Ans: runs until stack overflow. 23. main() { char not; not=!2; printf("%d",not); } Ans: 0 if %c is used then a blank is printed as !2=0 24. #define FALSE -1 #define TRUE 1 #define NULL 0 main() {

57 if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); } Ans: TRUE ::print as it anythng in double quotes 25. main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); } Ans: 1==1 is True. 26. main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year",y); else printf("%d is not a leap year",y); } Ans: 2000 leap year 27. main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); } Ans: i=-1 i=1

28. main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z'))

57 x=convert(c); printf("%c",x); } convert(z) { return z-32; } Ans: Error//1ST convert (char);will do in fuction prtotype+fuc dec convert(char z) sud be used+getc() is not a library function 29. main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; } Ans:Error?? 30. int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p Ans: 40 31 22 //GUD 1 31. main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); } Ans:hello

57 32. main() { unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); } Ans: infinite. 33. void main() { while(1) { if(printf("%d",printf("%d"))) break; else continue; } } Ans: (garbage or 0) 1(compiler dependent) 34. #include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); } Ans: garbage 0 35. main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); } Ans: infinite 36. main() { char i=0; for(;i>=0;i++) ;

57 printf("%d\n",i); } Ans: -128 39. main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); } Ans: A 40. main() { while (strcmp(some,some\0)) printf(Strings are not equal\n); } Ans: no output//strcmp(gives 0 if all letters are equal) 41. main() { char str1[] = {s,o,m,e}; char str2[] = {s,o,m,e,\0}; while (strcmp(str1,str2)) printf(Strings are not equal\n); } Ans: no output. 42. main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); } Ans: 10 10 43. main() { register int a=2; printf("Address of a = %d",&a);

57 printf("Value of a = %d",a); } Ans: error 44. main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } Ans: error 45. main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i); } } Ans: linker error(run time error) 46. char inputString[100] = {0}; To get string input from the keyboard which one of the following is better? 1) gets(inputString) 2) fgets(inputString, sizeof(inputString), fp) Ans: 1. 47. Which version do you prefer of the following two, 1) printf(%s,str); // or the more curt one 2) printf(str); Ans: 1

57 48. void main() { char ch; for(ch=0;ch<=127;ch++) printf(%c %d \n, ch, ch); } Ans: infinite 49. main() { char a[4]="HELLO"; printf("%s",a); } Ans: error//too many intializers 50. main() { char a[4]="HELL"; printf("%s",a); } Ans: HELLgarbage 51. main() { printf("%d", out); } int out=100; Ans: error

SESSION 2 1. main() { char s[ ]="man"; int i;

57 for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); } Ans: mmmm aaaa nnnn 2. main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); } Ans: 1 2 *3. main() { printf("%x",-1<<4); } Ans: 0xfff0 4. main() { int c=- -2; printf("c=%d",c); } Ans: 2 5.. main() { char *p; p="Hello"; printf("%c\n",*&*p); } //here*&p==&*p Ans: H 6. main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));

57 } Ans: 2 5 5 7. #include<stdio.h> main() { register i=5;//valid int can be dropped char j[]= "hello"; printf("%s %d",j,i); } Ans: hello 5 8. void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } Ans: Ok here 9. What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); } Ans : -128 10. main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

57

Ans : -128 *11. void main() { if(~0 == (unsigned int)-1) printf(You can answer this if you know how values are represented in memory); } Ans : you can answer this if you know how values are represented in memory 12. main() { int i = 3; for (;i++=0;) printf(%d,i); } Ans : L value required error. 13. main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); } Ans : 1 10 14. #define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(The dimension of the array is %d, DIM(arr, int)); } Ans: 10 15. int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10];

58 printf(The dimension of the array is %d, DIM(arr)); } Ans: The dimension of the array is 10 16. #define max 5 #define int arr1[max] main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; arr2 name="name"; printf("%d %s",list[0],name); } Ans: Error SESSION 3 1. main() { static int var = 5; printf("%d ",var--); if(var) main(); } Ans : 5 4 3 2 1 2. main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); } Ans: 0 0 1 3 1 3. #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p;

58 str1=s; printf("%d",++*p + ++*str1-32);//asc of \n==10 so 11+98-32==77// } Ans: 77 4. main() { int i=5; printf("%d%d%d%d%d",i++,i--,++i,--i,i); } Ans : 4,5,5,4,5

5. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++;//whever there is a pointer we consider r to l so 1st post printf("%s %s",p,p1); } Ans: ibj!gsjfoet 6. main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); } Ans: Error 7. void main() { int i=5; printf("%d",i++ + ++i);

increment

58 } Ans: 12 8. void main() { int i=5; printf("%d",i++ +++i); }

Ans : Lvalue required error 9. #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } Ans : 77 10. #include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } Ans: error 11. main() { int i=5,j=6,z; printf("%d",i+ + +j); } Ans: 11 12. main() { int i=_l_abc(10);

58 printf("%d\n",--i); } int _l_abc(int i) { return(i++); } Ans : 9. 13. main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); } Ans: 0 0 *14. main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); } //printf(name of string )==will print string.. Ans: 300 15. void main() { static int i=5; if(--i){ main(); printf("%d ",i); } } Ans: 0 0 0 0 16. void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); }

58 int ret(int ret) { ret += 2.5; return(ret); } Ans: 7. 17. void main() { char a[]="12345\0"; int i=strlen(a); printf("%d\n",++i); } Ans: 6 *18. void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); } Ans: 0 65535 19. void main() { int i=i++,j=j++,k=k++; printf(%d%d%d,i,j,k); } Ans: garbage 20. void main() { static int i=i++, j=j++, k=k++; printf(i = %d j = %d k = %d, i, j, k); } Ans: Error 21. main() { unsigned int i=10; while(i-->=0) printf("%u ",i);

58 } Ans: infinite 22. main() { unsigned int i=65000; while(i++!=0); printf("%d",i); } Ans: 1 23. main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); } Ans: -1//ya check while 24. main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k);//cannot increment. printf("%f\n",f<<2);//illegal instruction printf("%lf\n",f%g);//illegal instruction printf("%lf\n",fmod(f,g)); //Legal instruction } Ans: error 25.. main() { int i=5; printf("%d",++i++); } Ans: l value required ++(i++) since (i++)will give an constatnt value and cant perform ++ on a constant value error 26. main() { int i=5; printf(%d,i=++i ==6);

58 } Ans: 1 27. void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(%d, i); } Ans: 32767//ya 28. main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); } Ans: 4 1 29. char *someFun1() { char temp[ ] = string"; return temp; } char *someFun2() { char temp[ ] = {s, t,r,i,n,g}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }

Ans: garbage SESSION 4 1. main() { int a=10,*j; void *k;

58 j=k=&a; j++; k++; printf("\n %u %u ",j,k); } Ans: type of k is unknown hence ERROR 2. Is this code legal? int *ptr; ptr = (int *) 0x400; Ans: Legal 3. void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp;//ya but *ip/(*jp) is valid since /* is used for printf(%d,k); }

comment

Ans : error 4. void main() { printf(sizeof (void *) = %d \n, sizeof( void *)); printf(sizeof (int *) = %d \n, sizeof(int *)); printf(sizeof (double *) = %d \n, sizeof(double *)); printf(sizeof(struct unknown *) = %d \n, sizeof(struct unknown *)); } Ans: 2 2 2 2 5. void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; } Ans : Legal 6. What is the subtle error in the following code segment? void fun(int n, int arr[]) { int *p=0;

58 int i=0; while(i++<n) p = &arr[i]; *p = 0; } Ans: only one stmt executes with in while loop. 7. #include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); } Ans: 0//gud 1 8. main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); } Ans:300 *9. main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); } // Ans: 2 1 *10. main() { int i = 257;// int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

58 Ans: 1 1 11. main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } } Ans : 1 1 1 1 2424 3737 4242 5555 6868 7373 8686 9999 //gud que 12. main() { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); } Ans: 2 1 4 54 13. main() { int a=2,*f1,*f2;//a=2,f1=&a,f2=&a// f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); } sizeof(p),

59 Ans: 16 16 16 Gud1 14. 1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations. Ans: data constant Pointer constant Data constant 15. main() { char *p = ayqm; char c; c = ++*p++; printf(%c,c); } Ans:b 16. main() { char *p = ayqm; printf(%c,++*(p++)); } Ans: b 17. What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); } Ans: 1 18. Is the following statement a declaration/definition. Find what does it mean? int (*x)[10]; Ans: pointer to an array of 10 integer data. 19. main() { int a[10];

59 printf("%d",*a+1-*a+3); } Ans: 4 Gud1 20. void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); } Ans : 2 21. # include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); } //if ptr+=2 then 3 will printed Ans: garbage 22. main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); } Ans : 1 2 4 23. #include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q;

59 p=&a[2][2][2]; *q=***a; printf("%d..%d",*p,*q); } Ans: 10 garbage 24. main() { int *j; { int i=10; j=&i; } printf("%d",*j); } Ans: 10 25. main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); } Ans: error 26. main() { int i, n; char *x = girl; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(%s\n,x); x++; } } Ans: irl rl l

59

27. main ( ) { static char *s[ ] = {black, white, yellow, violet}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(%s,*--*++p + 3); } Ans::ck 28. main( ) { void *vp; char ch = g, *cp = goofy; int j = 20; vp = &ch; printf(%c, *(char *)vp);//g vp = &j; printf(%d,*(int *)vp);//20 vp = cp; printf(%s,(char *)vp + 3);//fy }http://www.orkut.co.in/Main - Home?rl=t Ans: g 20 fy 29. main( ) { char *q; int j; for (j=0; j<3; j++) scanf(%s ,(q+j)); for (j=0; j<3; j++) printf(%c ,*(q+j)); for (j=0; j<3; j++) printf(%s ,(q+j)); } Ans: Error (Null pointer assignment) 30. main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); *ptr++; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr);

59 *++ptr; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); ++*ptr; printf(\n %d %d %d, ptr-p, *ptr-a, **ptr); 31. main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(%d ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(%d ,*p); p++; } } Ans: Error 32. main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(%u %u %u %d \n,a,*a,**a,***a); printf(%u %u %u %d \n,a+1,*a+1,**a+1,***a+1); } Ans: let base address be 1000. 1000 1000 1000 2 1012 1004 1002 3 33. #include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }

59

Ans: garbage garbage 34. main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } } Ans: 2 2 2 2 2 23465 35. void main() { int const * p=5; printf("%d",++(*p)); } Ans: error SESSION 5 1. # include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc;

59 ptr[2](); } Ans: bye. 2. In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ } Ans: *k=&a; 3. main(){ char a[100]; a[0]='a';a[1]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }

Ans: b c 4. func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2)

59 int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); } Ans: error 5. #define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); } Ans: 10 6. int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); } Ans: 20 10 8. Explain the statement: void ( * abc( int, void ( *def) () ) ) ();

10. Declare an array of N pointers to functions returning pointers to functions returning pointers to characters? 11. Is there any difference between the two declarations, int foo(int *arr[]) and int foo(int *arr[2])

59

12. char *someFun() { char *temp = string constant"; return temp; } int main() { puts(someFun()); } Ans: string constant. 13. main() { int i=_l_abc(10); printf(%d\n,i); } int _l_abc(int i) { return(i++); } Ans: -10 14. main() { char a[100]; a[0]=a'; a[1]]=b';a[2]=c';a[4]=d'; abc(a); } abc(char a[]) { a++; printf(%c,*a); a++; printf(%c,*a); } 15. void main() { static int i=5; if(i)

59 { main(); printf(%d ,i); } } Ans:Infinite Loop No Output SESSION 6 1. what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR); Ans: 0 offset 2. What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF) ; Ans : while((fgets(receiving array,50,file_ptr))!=NULL). 3. #include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); } Ans: linker error.fgetch() no such built in function. 4. void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(%d,*mptr); cptr = (int*)calloc(sizeof(int),1); printf(%d,*cptr); } Ans: garbage 0 5. The value of EOF is -1.

60 6. Using pointers to call a function is called as function pointer 7. The variable that contains address of another variable is called as 8. How many values can be returned by a C++ function? Ans: one 9. Which of the following is mandatory for all C++ program? a) main() b) scanf() c) system() d) all the above Ans : main() 10. The variables that can be used only within the function in which it is declared is called as Local variable. Pointer

SESSION 7 1. #include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } Ans: error//declare a data type of structure then add sud be given to struct pointer *2. #include<stdio.h> main() { struct xx { int x;

60 struct yy { char s; struct xx *p; }; struct yy *q; }; } Ans: Error 3. enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); } Ans: 0 1 2 6. struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc; def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x);

60 } Ans : 2 *7. struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }

Ans: 0 0 00 8. What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); } Ans: Error (multiple declaration of error). 9. typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); } Ans : error 10. main() { struct student { char name[30]; struct date dob;

60 }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.month, &student.dob.year); } Ans: error 11. main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); } Ans: error 12. There were 10 records stored in somefile.dat but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(somefile.dat,r); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } }

&student.dob.day,

60 Ans: last record will get printed twice when feof is used. 13. Is the following code legal? struct a { int x; struct a b; }

Ans : no 14. Is the following code legal? struct a { int x; struct a *b; }

Ans: no// 15. Is the following code legal? typedef struct a { int x; aType *b; }aType

Ans: syntax error. 16. Is the following code legal? typedef struct a aType; struct a { int x; aType *b; }; Ans: yes 17. Is the following code legal? void main() { typedef struct a aType; aType someVariable; struct a

60 { int x; aType *b; }; } Ans: no 18. Printf can be implemented by using __________ list.

19. main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf(%d\n,++k); printf(%f\n,f<<2); printf(%lf\n,f%g); printf(%lf\n,fmod(f,g)); } Ans:error.

1)

struct value { int bit1:1; int bit3:4; int bit4:4; }bit={1,2,2}; printf("\n %d %d %d",bit.bit1,bit.bit3,bit.bit4);

ans::-1,2,2 2) ans::2 3) enum days{MON=-1,TUE,WED=55,THU,FRI,SAT}; printf("%d %d %d %d %d %d",MON,TUE,WED,THU,FRI,SAT); ans::-1,0,55,56,57,58 4) void main() enum value{VAL1=0,VAL2,VAL3,VAL4,VAL5}var; printf("%d",sizeof(var));

60 { union var { int a,b; }; union var v; v.a=80; v.b=100; printf("%d",v.a); } ans::100 5) void main() { struct node { int data; struct node *link; }; struct node *p,*q; p=(struct node *)malloc(sizeof(struct node)); q=(struct node *)malloc(sizeof(struct node)); printf("%d %d",sizeof(p),sizeof(q)); } ans::2,2

6)

struct byte { int one:1; }; struct byte var={1}; printf("%d",var.one);

ans::-1 7) enum status{pass,fail,atkt}; enum status stud1,stud2,stud3; stud1=pass; stud2=atkt; stud3=fail; printf("\n %d %d %d",stud1,stud2,stud3);

60 ans::0 1 2 8) int i=4,j=8; printf("%d %d %d ",i|j&j|i,i|j&&j|i,i^j);

9) {

union x int i; char ch[2]; }; union x u; u.ch[0]=3; u.ch[1]=2; printf("%d %d %d ",u.ch[0],u.ch[1],u.i);

10)

struct course { int courseno; char coursename[25]; }; void main() { struct course c[]={ {102,"Thermal"},{103,"Manufacturing"},{104,"Design"} }; printf("%d",c[1].courseno); printf("%s",(*(c+2)).coursename); } ans::103 Design

11) {

void main() union test { int i; float f; char c;

60 }; union test *t; t=(union test *)malloc(sizeof(union test)); t->i=10; printf("%d\n",t->c); t->f=10.10f; printf("%f\n",t->f); t->c='a'; printf("%c\n",t->i); }

12) {

void main() struct address { char phone[15]; char city[25]; int pin; }; struct emp { char name[25]; struct address a; }; struct emp e={"jeru","2344","kerala",55}; printf("\n name=%s phone=%s",e.name,e.a.phone); printf("\n city=%s pin=%d",e.a.city,e.a.pin);

} ans:: name=jeru phone=2344 city=kerala pin=55

13) {

struct book char name[25]; char author[25]; int no;

}; void display(struct book *); void main() { struct book b1={"Let us C","YPK",101}; display(&b1);

60 } void display(struct book *b) { printf("\n %s %s %d",b->name,b->author,b->no); } ans:: Let us C,YPK,101 14) { void main() struct sample { int num; char m1[50]; char m2[50]; }m; m.num=1; strcpy(m.m1,"i love India"); strcpy(m.m2,"We are Indians"); printf("\n %u %u %u",&m.num,m.m1,m.m2); } ans::garbage value will be printed 16) { void main() struct sample { int a:3; int b:2; unsigned int c:3; }; struct sample s; s.a=-2; s.b=1; s.c=3; printf("\nThe value of a is %d",s.a); printf("\nThe value of b is %d",s.b); printf("\nThe value of c is %d",s.c); s.a=2; s.b=0; s.c=5; printf("\nThe value of a is %d",s.a);

61 printf("\nThe value of b is %d",s.b); printf("\nThe value of c is %d",s.c); printf("\n Total size of the structure sample is %d",sizeof(s)); }

17) {

struct sample char name[10]; int no; }; void main() { struct sample s; void passrecord(struct sample); printf("\n Enter the name:"); scanf("%s",s.name); printf("\n Enter the Roll Number:"); scanf("%d",s.no); passrecord(s); printf("\n values after the function:\n"); printf("\n Name : %s",s.name); printf("\n No : %d",s.no); } void passrecord(struct sample x) { x.no=x.no+10; printf("\n Inside the function:\n"); printf("\n Name : %s",x.name); printf("\n No : %s",x.no);

18) Point out if there is any error in the program void main() { struct employee

61 { char name[25]; int age; float bs; }; struct employee e; strcpy(e.name,"Hacker"); age=25; printf("\n %s %d",e.name,e.age); } ans::error

1. can we have more than one data members in a doubly linked list structure a) b) yes no

3. Arrange the code below, to delete a node being pointer by temp. a) free(temp) b) temp->prev->next=temp->next c) temp->next->prev=temp->prev; a) b c a b) c b a c) a b c d) both a and b 4. struct list { int data; struct list* left, struct list* right; }; what are the steps required to insert a new node n at a point pointed by ptr? note: ptr is not the last node. 1. n->left = ptr->left

61 2. 3. 4. n->left->right = n ptr->left = n n->right = ptr b) 4 3 2 1 c) 4 2 1 3 d) 3 1 2 4

a)1 2 3 4 . 5.

where does the pointer temp pointing after the

code execution, where head is startning node in D.L.L of nine nodes,. for( i=1; head!=NULL;head=head ->next, i++) { If(i==1) temp=head; else if (i%2==1) temp= temp->next; } a) middle node b) last but one node c) cannot make out d) second node 6. What does function do, if temp is pointing to a node in DLL and temp1 is a newnode? temp1 -> prev = temp temp1 -> next =temp ->next temp -> next->prev = temp1 temp -> next = temp1 a) inserts node before temp; b) inserts after temp c) deletes node pointed by temp; d) none of the above 7. What does below code do, if temp is pointing to a node other than first and last node temp -> prev ->next = temp ->next; temp ->next -> prev = temp -> prev; free(temp); a) b) c) no effect inserts a node deletes a node

61 d) shuffling of pointers

8. What does the code below do, where head is starting node & temp is temporary pointer temp=head; head= head -> next; head -> prev = null; free(temp); a) b) c) d) no effect NULL data is stored Starting node is deleted First and second node are shuffled

9. what does the code below do, where head is pointing to first node & temp is a temporary pointer. 10 be the number of nodes temp = head; while (temp->next->next!=NULL) { Temp = temp ->next; } temp -> prev -> next = temp -> next; temp -> next -> prev = temp -> prev; free(temp); a) no effect b) deletes some node c) deletes 2nd last node d) deletes last node 10. what does the code do, if there are 100 nodes temp=head; while ( temp -> next -> next -> next != NULL) { Temp = temp -> next; } temp -> prev -> next = temp -> next; temp -> next -> prev = temp -> prev; free ( temp ); a) b) c) d) deletes 3rd last node no effect 3rd node is deleted Middle node is deleted

61 1.main() { printf("%c\n", '1' + 1); } a)ASCII value of '1' is required to find out the answer b)2 c)50 d)Syntax Error ans::b 2. main() { char y[10] = "abcdefghi"; char *p = y; p = p + 9; printf("%c\n",*p); } a)i b)Program will have runtime error c)Unpredictable d)No visible output ans::d 3. main() { int y[2][2] = { {1,2}, {3,4} }; int *p = &y[1]; p = p + 1; printf("%d\n",*p); } a)4 b)3 c)The program doesn't compile d)Output is unpredicatable ans::a

61

4. int y = 10; main() { int x = 10; int y = 20; x = x + y; if (x >= 30) { int y = 30; x = x + y; } else { int y = 40; x = x + y; } printf("%d\n", x); } a)40 b)50 c)60 d)70 ans::b 5. main() { unsigned int i = 5; while (--i >= 0) { printf("Hello World\n"); } } a)5 b)6 c)Infinite d)Program will not compile ans::c

61 6. struct emp { int age; char name[10]; struct emp e; }; void main() { printf(%d.sizeof(struct emp)); } a)24 b) No Output c) Compiler Error d) None Of the above ans::c 7. what is the outpur of the following prog. void main() { insert(root,2); insert(root,1); insert(root,3); insert(root,4); insert(root,5); preorder(root); } assume insert function inserts a node at its correct position in the tree. root is the root of the tree and preorder function prints the nodes as in preorder traversal. a)1 2 3 4 5 b) 1 2 5 4 3 c) 1 3 4 5 2 d) 2 1 3 4 5 8. void main() { int x; push(top,10); push(top,20); push(top,5); push(top,40); push(top,1);

61 push(top,25); x=pop(top); printf(%d, x); } consider push function pushes onto the stack the given value. and pop operation performs pop and returns the poped value. (a) 10 (b) 5 (c) 1 (d) 25 9. if the preorder traversal of a BST tree is 100 50 25 75 200 150 300 then the root of the tree is : ( a) 300 (b) 75 (c) 100 (d) 25 10. The operation for adding an entry to a stack is traditionally called: a.add b.append c.insert d.push

11. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? a.ABCD b.ABDC 14 / \ 2 /\ /\ 1 3 10 30 / / 7 40 \ 50 11 c.DCAB d.DCBA

12. There is a tree in the box at the top of this section. How many leaves does it have? A. 5 B. 6 C. 4 D. 9 13. There is a tree in the box at the top of this section. How many of the nodes have at least one sibling?

61 E. 5 F. 6 G. 7 H. 8 I. 9 14.There is a tree in the box at the top of this section. What is the value stored in the parent node of the node containing 30? J. 10 K. 11 L. 14 M. 40 N. None of the above 15.There is a tree in the box at the top of this section. How many descendants does the root have? O. 0 P. 2 Q. 9 R. 8 16.There is a tree in the box at the top of this section. What is the depth of the tree? S. 2 T. 3 U. 4 V. 8 W. 9 17.There is a tree in the box at the top of this section. How many children does the root have? X. 2 Y. 4 Z. 6 18. What is the value of the postfix expression 6 3 2 4 + - *: a. Something between -15 and -100 b. Something between -5 and -15 c. Something between 5 and -5

61 d. Something between 5 and 15 e. Something between 15 and 100 19. Suppose cursor points to a node in a linked list (using the node definition with member functions called data and link). What statement changes cursor so that it points to the next node? a. cursor++; b. cursor = link; c. cursor += link; d. cursor = cursor->link; 20. Consider the following queue which can be allocated eight integers and five operations.front = 1 rear= 3 Queue = -,2 , 4 ,5, - , - , -,(for notational convenience used to denote an empty cell) The following operations have to be performed. (i) 6 is added to the queue. (ii) Two elements are deleted from the queue. (iii) 10 and 12 are added to the queue. (iv) Two elements are deleted from the queue. (v) 2 and 3 are added to the queue. What are the final front and rear values when the above operations are performed into a circular queue? (a) front = 7 rear=2 (b) front = 2 rear=0 (c) front = 5 rear=8 (d) front = 5 rear=0 21. Consider the following statements: (i) The queue can be implemented by a linked list. (ii) The queue can be implemented only by stack. (iii) There are references kept at both the front and the back of the list. (iv) The Queue can be implemented only by an array-based method. Which of the above statement(s) is/are valid for the queues? (a) (i) only (b) (i),(ii) and (iii) only (c) (i) and (iii) only (d) (i),(iii) and (iv) only (e) (ii) and (iv) only

62

22. main(){ int i,b[]={1,2,3,4,5},*p; p=b; ++*p; Printf(%d,*p); P+=2; Printf(%d,*p); } A. 2 3 B. 2 4 C. 3 4 D. 2 5 Ans::2 3 23. void main() { int a=1,b=2,c=3,d=4,e; e=(a,a)+(b,c)+(c,d)-(d,b); printf("%d",e); } (a)Compile-Time Error (b)10 Ans::6 24. struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); }

(c)6

(d)2

62

25. What would be the output if we enter the following data set (in the respective order) into a standard program to construct a Binary search tree? 25, 72, 16, 11, 88, 26, 9, 36, 21, 45, 14, 69

(a)
25 1 6 1 1 2 1 1 4 2 6 3 6 4 5 6 9 7 2 8 8

25

(b)
1 1

1 6 2 1 1 4 2 6

7 2 8 8 3 6 4 5 6 9

25

(c)
1 6 1 1 2 1 1 4 2 6 3 6 6 9 45 7 2 8 8

62

(d)
1 6 1 1

21

2 5 7 2 1 4 3 6 2 6 4 5 6 9 8 8

2) which of the following data structure is used in hierarchical data modeling a) stacks b) queues c) trees d) structure ans: trees 3) a binary tree with 20 nodes have _____ null branches a) 20 b) 22 c) 21 d) 40 Ans: 21 4) in tree construction which of the following is suitable efficient data structure a) array b) doubly linked list c) stack d) queue e) none ans: doubly linked list 5) find output of the following code. #define min(a,b) (a<b?a:b)

62 main() { int a; a=min(3+4,4+4); printf(%d\n,a); } a) b) c) d) 6) main() { float f=2; switch(f) { default: puts(hi); break; case 2.0 : puts(helloo); } } a) b) c) d) Ans::a 2. which finite set of elements that is either empty or is portioned into 3 disjoint subset. a. single linked list b. double linked list c. stack d. binary tree Answer: D 3. what type of Binary Tree is the following tree below compile time error hi helloo No output 7 8 Compile time error Garbage. Ans::a

62

a. b. c. d.

strictly binary tree complete binary tree almost complete binary tree not a binary tree

Answer: D 4. Which is the preorder of a Binary tree represented below A

a. ABDHIECFJKG b. IHDEBKJFGCA

62 c. IHDBEAKJFCG d. ABCDEFKJGHI Answer: A 5. If suppose root to be deleted then which node will be the root node A

a. b. c. d.

B G Any node Both a and b are correct

Answer B 6. conver this expression in prefix form (A+B * C/D * E /G H + I) ^ ( J /K * L) a. b. c. d. ^-+//*BC*DEGA+HI/J*KL ^+-//*BC*DEGA+HI*J/KL ^-+/*/BC*DEGA+HI*J/KL ^-+/*/BC*DEGA+HI*J/KL

Answer: A 7. postfix form of the following expression is A*B*C*D*E*F/G+H-I^J a. -/*****ABCDEFGH+^IJ b. AB*C*D*E*F*G/H+IJ^-

62 c. BA*D*C*F*GH/+IJ^d. AB*CB*D*E*F/GH*IJ^ 8. which is formula to find the total no. of node in complete B.T a. tn=2d+1 1 where depth(d) !=level b. tn ==2d+1 1 where depth != level(L) c. tn = 2L+1 d. 2n-1 Answer: A 9. what type of B.T is the following tree A

H F G

a. b. c. d.

strictly B.T completely B.T stricktly complete B.T almost complete B.T

Answer: A 10. Considering the following code in which root is the root node in binary tree and temp and temp1 are pointers and key has only 1 leaf node left temp=root; While(tempvalue!=key) { temp1=temp; If(tempvalue<key)

62 { temp=tempright; } else if(tempvalue>key) temp=templeft; } if(temp1value < key) temp1right=templeft; else if(temp1value>key) temp1left=templeft; free(temp); what is the output after execution of this code? a. b. c. d. Deletes the key value node & make leaf node connected to its parent Deletes the leaf node of key node Deletes the key node None of the above

Answer: A