Sei sulla pagina 1di 9

C interview questions with answers for experienced

c interview questions and answers:

1) How to find size of an array say int array[] = {1,2,3,4,5,6,7,8,9,0}; Answer: int arrSize = (sizeof(array)/sizeof(array[0])); 2) What is void pointer?
Answer : Refer Here:

Void pointers 3) write a program to delete an item from a particular location of an linear array?
Answer : int deleteArrItem(void) { int loc,i,ArrSize; int Arr[] = {1,2,3,4,5,6,7,8,9,0}; ArrSize = (sizeof(Arr)/sizeof(Arr[0])); printf("Enter location between 1 to %d :",ArrSize); scanf("%d",&loc); for(i = loc-1; i < ArrSize ; i++) { Arr[i] = Arr[i+1]; } Arr[ArrSize-1] = 0; for(i = 0; i < ArrSize ; i++) { printf("Arr[%d] = %d \n",i,Arr[i]); } } return 0;

4)Assume an array a[5][5] defined as following:


int a[5][5] = { {2,3,6,32,66}, {33,6,36,8,86}, {78,33,2,71,47}, {17,48,3,7,22}, {85,2,6,7,2} };

Can one access elements a[0][6] or a[0][5]? Justify your answer.


Answer : It gives index out of bound error because the the last element can be accesible is a[4][4],after that we can not able to access

5)Given two integers A and B determine how many bits required to convert A to B

Answer : /*Write a functon bitSwapReqd(int A,int B)*/ // Straight method(some what not efficient) #define BYTE 8 #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE (!TRUE) #endif int bitSwapReqd(int A,int B) { int BitsinInt = sizeof(int) * BYTE; int count ; int countRqd = 0; short intA,intB; for (count = 1; count <= BitsinInt; count++) { intA = testBit(A,count); intB = testBit(B,count); /*printf(" intA = %d \n", intA); printf(" intb = %d \n", intB);*/ if(intA != intB) { countRqd++; } } return countRqd; } //Mehtod2 (More efficient) int bitSwapReqd2(int A,int B) { int BitsinInt = sizeof(int) * BYTE; int count,intA = 0 ; int countRqd = 0; int out = A ^ B; //Xor operation done here for (count = 1; count <= BitsinInt; count++) { intA = testBit(out,count); if(intA) { countRqd++; } } return countRqd; } /* test a bit if it is zero or one */ int testBit(int value,int whichBit) { int mask = 1<<whichBit; if (value&mask) { return TRUE; } else return FALSE;

6) Write a program to decorate the input Srtring in console like below: intput : String output : ****** *String* ******
Answer : #define SPOS (3) int decorateConsole(char *var) { int x,y,i; x = strlen(var); for (i=0 ; i <= x+SPOS; i++) { printf("*"); } printf("\n* "); printf("%s ",var); printf("* \n"); for (i = 0; i <= x+SPOS ; i++) { printf("*"); } return 0; }

7)Without using /,% and * operators. write a function to divide a number by 3.itoa() function is available
Answer : int divisionWithoutArithOprts2(int number) { do { number = number >> 3; } while(number >= 0); return number;

8) Can you write a similar function like printf ?


Answer : The following code is the basic version, i will update soon the later versions.. /* Can you write function similar to printf()? */ #include <stdio.h> int uprintf(char *args[]) { int count = 0; char ch[1024]; sscanf(args[0],"%s",ch); puts(ch); } int main() { char *args[] = {"welcome",NULL}; uprintf(args)); }

9) Shift the bits as illustrated above In the picture


unsigned char A = 171; unsigned char B = 223; unsigned char C = 0; //Solution

C = (A >> 3) | ((B >> 5) << 5); printf("A = %d , B = %d , C = %d ",A,B,C);

10) Find out the two logical Errors in the following code...
#include <stdio.h> #include <stdlib.h> #include <string.h> //Find out two logical Errors in the code typedef struct dbTable { char *key; int value; } dbTable; /* prototypes to get data from Hash table */ int getData(dbTable *pdbTable , char *key); int getSize(dbTable *pdbTable); int main() { dbTable dbTableHash[] = {{"test1",1},{"test2",2},{"test3",3}, {"test4",4}, {"test5",5},{"test6",6}}; int value = getData(dbTableHash,"test6"); printf("value = %d \n",value); } int getData(dbTable *pdbTable , char *key) { int i = 0; for(i = 0 ; i < sizeof(pdbTable) ; i++) { if(strcmp(pdbTable[i].key , key) == 0) break; } return pdbTable[i].value; }

C interview questions and answers


By admin | December 23, 2003 1. What will print out?

main() { char *p1=name; char *p2; p2=(char*)malloc(20); memset (p2, 0, 20); while(*p2++ = *p1++); printf(%sn,p2); } Answer:empty string.
2. What will be printed as the result of the operation below: main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(%d%dn,x,y);

} Answer : 5794
3. What will be printed as the result of the operation below: main() { int x=5; printf(%d,%d,%dn,x,x< <2,x>>2);

} Answer: 5,20,1
4. What will be printed as the result of the operation below: #define swap(a,b) a=a+b;b=a-b;a=a-b;

void main() { int x=5, y=10; swap (x,y); printf(%d %dn,x,y);

swap2(x,y); printf(%d %dn,x,y); } int swap2(int a, int b) { int temp; temp=a; b=a; a=temp; return 0; } Answer: 10, 5 10, 5
5. What will be printed as the result of the operation below: main() { char *ptr = Cisco Systems; *ptr++; printf(%sn,ptr); ptr++; printf(%sn,ptr);

} Answer:Cisco Systems isco systems


6. What will be printed as the result of the operation below: main() { char s1[]=Cisco; char s2[]= systems; printf(%s,s1); }

Answer: Cisco
7. What will be printed as the result of the operation below: main() {

char *p1; char *p2;

p1=(char *)malloc(25); p2=(char *)malloc(25); strcpy(p1,Cisco); strcpy(p2,systems); strcat(p1,p2); printf(%s,p1); } Answer: Ciscosystems
8. The following variable is available in file1.c, who can access it?:
9. static int average;

Answer: all the functions in the file1.c can access the variable.
10. WHat will be the result of the following code?

#define TRUE 0 // some code

while(TRUE) { // some code } Answer: This will not go into the loop as TRUE is defined as 0.
11. What will be printed as the result of the operation below:

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:%dn",x); x++; changevalue(x); printf("Second output:%dn",x); modifyvalue(); printf("Third output:%dn",x); } Answer: 12 , 13 , 13
12. What will be printed as the result of the operation below:

main() { int x=10, y=15; x = x++; y = ++y; printf(%d %dn,x,y);

} Answer: 11, 16
13. What will be printed as the result of the operation below:

main() { int a=0; if(a==0) printf(Cisco Systemsn); printf(Cisco Systemsn);

} Answer: Two lines with Cisco Systems will be printed.


http://www.c.happycodings.com/code_snippets/index.html

Potrebbero piacerti anche