35
C Interview Questions. Answer: These are the questions usually repeated for C written Test. You can alos use these questions to brushup your skills. Instructions: 1. Please ignore any case-sensitive errors and un- included libraries. 2. You may use the back of this question paper for any rough work. Q1. main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above Ans: d, printf( ) prints address/garbage of i, scanf() dont hav & sign, so scans address for i +1, -1 dont hav any effect on code Q2. main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; }

C Interview Questions12

  • Upload
    grsrik

  • View
    1.410

  • Download
    3

Embed Size (px)

DESCRIPTION

c programs and solved questions

Citation preview

Page 1: C Interview Questions12

C Interview Questions.

Answer: These are the questions usually repeated for C written Test.  You can alos use these questions to brushup your skills.Instructions:1.      Please ignore any case-sensitive errors and un-included libraries.2.      You may use the back of this question paper for any rough work.

Q1.main(){ int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1;}a. Runtime error.b. Runtime error.  Access violation.c. Compile error.  Illegal syntaxd. None of the above   Ans: d, printf( ) prints address/garbage of i,  scanf() dont hav & sign, so scans address for i  +1, -1 dont hav any effect on code

Q2.main(int argc, char *argv[]){       (main && argc) ? main(argc-1, NULL) : return 0;}a. Runtime error.b. Compile error.  Illegal syntaxc. Gets into Infinite loopd. None of the above Ans: b)  illegal syntax for using return

Q3.main(){

Page 2: C Interview Questions12

  int i;  float *pf;  pf = (float *)&i; *pf = 100.00;  printf("n %d", i);}a. Runtime error.b. 100c. Some Integer not 100d. None of the above Ans: d) 0

Q4.main(){  int i = 0xff ;  printf("n%d", i<<2);}a. 4b. 512c. 1020d. 1024 Ans: c) 1020

Q5. #define SQR(x) x * xmain(){  printf("%d", 225/SQR(15));}a. 1b. 225c. 15d. none of the above

 Ans: b) 225

Q6.union u{  struct st {

Page 3: C Interview Questions12

       int i : 4;       int j : 4;       int k : 4;       int l; }st; int i;}u;main(){    u.i = 100;    printf("%d, %d, %d",u.i, u.st.i, u.st.l);}a. 4, 4, 0b. 0, 0, 0c. 100, 4, 0d. 40, 4, 0 Ans: c) 100, 4, 0

Q7.union u{     union u    {        int i;        int j;    }a[10];    int b[10];}u;main(){      printf("n%d", sizeof(u));      printf("  %d", sizeof(u.a));//      printf("%d", sizeof(u.a[4].i));}a. 4, 4, 4b. 40, 4, 4c. 1, 100, 1d. 40 400 4

Ans: 20, 200, error for 3rd printf

Q8.main(){

Page 4: C Interview Questions12

      int (*functable[2])(char *format, ...) ={printf, scanf};      int i = 100;      (*functable[0])("%d", i);      (*functable[1])("%d", i);      (*functable[1])("%d", i);      (*functable[0])("%d", &i);}a. 100, Runtime error.b. 100, Random number, Random number, Random number.c. Compile errord. 100, Random number

 Q9.main(){  int i, j, *p;   i = 25;   j = 100;   p = &i; // Address of i is assigned to pointer p  printf("%f", i/(*p) ); // i is divided by pointer p}a. Runtime error.b. 1.00000c. Compile errord. 0.00000 

Ans: c) Error becoz i/(*p) is 25/25 i.e 1 which is int & printed as a float, So abnormal program termination, runs if (float) i/(*p)   -----> Type Casting

Q10.main(){       int i, j;       scanf("%d %d"+scanf("%d %d", &i, &j));       printf("%d %d", i, j);}a. Runtime error.b. 0, 0c. Compile errord. the first two values entered by the user 

Page 5: C Interview Questions12

Ans: d) two values entered, 3rd will be null pointer assignment

Q11.main(){        char *p = "hello world";        p[0] = 'H';        printf("%s", p);}a. Runtime error.b. “Hello world”c. Compile errord. “hello world” Ans: b) Hello world

Q12.main(){   char * strA;   char * strB = I am OK;   memcpy( strA, strB, 6);}a. Runtime error.b. I am OKc. Compile errord. I am O Ans: c)  I am OK is not in    "   "

Q13. How will you print % character?a. printf(“%”)b. printf(“\%”)c. printf(“%%”)d. printf(“%%”)

Ans: c) printf(" %% ");

Q14.const int perplexed = 2;#define perplexed 3main(){

Page 6: C Interview Questions12

    #ifdef perplexed    #undef perplexed    #define perplexed 4    #endif   printf("%d",perplexed);}a. 0b. 2c. 4d. none of the above Ans: c)

Q15.struct Foo{  char *pName;};main(){    struct Foo *obj = malloc(sizeof(struct Foo));    clrscr();    strcpy(obj->pName,"Your Name");    printf("%s", obj->pName);}a. Your Nameb. compile errorc. Named. Runtime error Ans a)

Q16.struct Foo{  char *pName;  char *pAddress;};main(){    struct Foo *obj = malloc(sizeof(struct Foo));clrscr();    obj->pName = malloc(100);    obj->pAddress = malloc(100);

Page 7: C Interview Questions12

    strcpy(obj->pName,"Your Name");    strcpy(obj->pAddress, "Your Address");    free(obj);    printf("%s", obj->pName);    printf("%s", obj->pAddress);}a. Your Name, Your Addressb. Your Address, Your Addressc. Your Name Your Named. None of the above  Ans: d) printd Nothing, as after free(obj), no memory is there containing

 obj->pName   &   pbj->pAddressQ17.main(){      char *a = "Hello ";      char *b = "World";clrscr();      printf("%s", strcat(a,b));}a. Hellob. Hello Worldc. HelloWorldd. None of the above Ans: b)

Q18.main(){      char *a = "Hello ";      char *b = "World";      clrscr();      printf("%s", strcpy(a,b));}a. “Hello”b. “Hello World”c. “HelloWorld”d. None of the above  Ans: d) World, copies World on a, overwrites Hello in a.

Page 8: C Interview Questions12

Q19.void func1(int (*a)[10]){       printf("Ok it works");}void func2(int a[][10]){   printf("Will this work?");}  main() {    int a[10][10];          func1(a);    func2(a);}a. Ok it worksb. Will this work?c. Ok it worksWill this work?d. None of the above  Ans: c)Q20.main(){  printf("%d, %d", sizeof('c'), sizeof(100));}a. 2, 2b. 2, 100c. 4, 100d. 4, 4  Ans: a) 2, 2

Q21.main(){  int i = 100;  clrscr();  printf("%d", sizeof(sizeof(i)));}a. 2b. 100

Page 9: C Interview Questions12

c. 4d. none of the above Ans: a) 2

Q22. main(){  int c = 5;  printf("%d", main||c);}a. 1b. 5c. 0d. none of the above Ans: a) 1,    if  we use main|c  then error, illegal use of pointer

Q23.main(){   char c;   int i = 456;   clrscr();   c = i;   printf("%d", c);}a. 456b. -456c. random numberd. none of the above  Ans: d) -56

Q24.void main (){    int x = 10;    printf ("x = %d, y = %d", x,--x++);}a. 10, 10b. 10, 9

Page 10: C Interview Questions12

c. 10, 11d. none of the above  

Ans: d) Lvalue requiredQ25.main(){   int i =10, j = 20;   clrscr();   printf("%d, %d, ", j-- , --i);   printf("%d, %d ", j++ , ++i);}a. 20, 10, 20, 10b. 20, 9, 20, 10c. 20, 9, 19, 10d. 19, 9, 20, 10  Ans: c)

Q26.main(){  int x=5;  clrscr();  for(;x==0;x--) {    printf("x=%dn”", x--);  }}a. 4, 3, 2, 1, 0b. 1, 2, 3, 4, 5c. 0, 1, 2, 3, 4d. none of the above  Ans: d) prints nothing, as condition x==0 is False

Q27main(){  int x=5;  for(;x!=0;x--) {    printf("x=%dn", x--);

Page 11: C Interview Questions12

  }}a. 5, 4, 3, 2,1b. 4, 3, 2, 1, 0c. 5, 3, 1d. none of the above  Ans: d) Infinite loop as x is decremented twice, it never be 0 and loop is going on & on

  Q28main(){  int x=5;  clrscr();  for(;x<= 0;x--) {    printf("x=%d ", x--);  }}a. 5,  3, 1b. 5, 2, 1,c. 5, 3, 1, -1, 3d. –3, -1, 1, 3, 5 Ans: prints nothing, as condition in loop is false.

Q29.main(){   {      unsigned int bit=256;      printf("%d", bit);   }   {      unsigned int bit=512;      printf("%d", bit);   }}

Page 12: C Interview Questions12

a. 256, 256b. 512, 512c. 256, 512d. Compile error Ans: 256, 512,     becoz these r different blocks, so declaration allowed

Q30.main(){   int i;   clrscr();   for(i=0;i<5;i++)  {       printf("%dn",  1L << i);  }}a. 5, 4, 3, 2, 1b. 0, 1, 2, 3, 4c. 0, 1, 2, 4, 8d. 1, 2, 4, 8, 16 Ans: d) L does't make any diff.

Q31.main(){  signed int bit=512, i=5;   for(;i;i--)  {       printf("%dn",  bit = (bit >>  (i - (i -1))));  }}a. 512, 256, 128, 64, 32b. 256, 128, 64, 32, 16c. 128, 64, 32, 16, 8d. 64, 32, 16, 8, 4 Ans: b)

Page 13: C Interview Questions12

Q32.main(){  signed int bit=512, i=5;   for(;i;i--)  {       printf("%dn",  bit >>  (i - (i -1)));  }}a. 512, 256, 0, 0, 0b. 256, 256, 0, 0, 0c. 512, 512, 512, 512, 512d. 256, 256, 256, 256, 256 

Ans: d) bit's value is not changed

Q33.main(){     if (!(1&&0))    {  printf("OK I am done.");    }    else   { printf("OK I am gone.");   }}a. OK I am doneb. OK I am gonec. compile errord. none of the above Ans: a)

Q34main(){   if ((1||0) && (0||1))  {  printf("OK I am done.");

Page 14: C Interview Questions12

    }    else   { printf("OK I am gone.");   }}a. OK I am doneb. OK I am gonec. compile errord. none of the above  Ans: a)

Q35main(){   signed int bit=512, mBit;  { mBit = ~bit; bit = bit & ~bit ; printf("%d %d", bit, mBit);   }}a. 0, 0b. 0, 513c. 512, 0d. 0, -513 Ans: d)

1. C Programming

2.  Which of the following about the following two declaration is true       i ) int *F()       ii) int (*F)()       Choice :      a) Both are identical      b) The first is a correct declaration and the second is wrong

Page 15: C Interview Questions12

      c) The first declaraion is a function returning a pointer to an integer and the second is a pointer to function returning         int      d) Both are different ways of declarin pointer to a function     Answer : c) The first de...

3. What are the values printed by the following program?                                                     #define dprint(expr) printf(#expr "=%d\n",expr)         main()            {              int x=7;              int y=3;              dprintf(x/y);          }          Choice:           a) #2 = 2 b) expr=2 c) x/y=2 d) none            Answer: c)x/y=2

4. Which of the following is true of the following program       main()            {             char *c;             int *p;             c =(char *)malloc(100);             ip=(int *)c;             free(ip);           }          ans: The code functions properly releasing all the memory allocated

Page 16: C Interview Questions12

5. .output of the following.                                                                                                    main()                 {                   int i;                   char *p;                   i=0X89;                   p=(char *)i;                   p++;                   printf("%x\n",p);                    }             ans:0X8A

6. which of the following is not a ANSI C language keyword?  ans:Function.

7. When an array is passed as parameter to a function, which of the following statement is correct  choice:       a) The function can change values in the original array       b) In C parameters are passed by value. The funciton cannot change the original value in the array        c) It results in compilation error when the function tries toaccess the elements in the array       d) Results in a run time error when the funtion tries to access the elements in the array        Answer: a) The fu...

8. The type of the controlling expression of a switch statement cannot be of the type 

Page 17: C Interview Questions12

        a) int b) char c) short d)float e) none        Answer : d)float

9. What is the value of the expression (3^6) + (a^a)?     a) 3     b) 5    c) 6     d) a+18       e) None     Answer : 5

10.  What is the value assigned to the variable X if b is 7 ?       X = b>8 ? b <<3 : b>4 ? b>>1:b;       a) 7 b) 28 c) 3 d) 14 e) None        ans: 3;

11. Which is the output produced by the following program                main()                  {                   int n=2;                   printf("%d %d\n", ++n, n*n);                    }       a) 3,6 b) 3,4 c) 2,4 d) cannot determine        Answer : b) 3,4

12.  What is th output of the following program?                                                                      int x= 0x65;         main()

Page 18: C Interview Questions12

            {             char x;             printf("%d\n",x)           }       a) compilation error     b) 'A'     c) 65       d) unidentified

13. What is the output of the following program         main()             {              int a=10;              int b=6;              if(a=3)              b++;              printf("%d %d\n",a,b++);              }          a) 10,6 b)10,7 c) 3,6 d) 3,7 e) none           Answer : a) 10,6

14. What can be said of the following program?          main()             {                enum Months {JAN =1,FEB,MAR,APR};                Months X = JAN;                if(X==1)                   {                     printf("Jan is the first month");                                                                               }             }

Page 19: C Interview Questions12

          a) Does not print anything          b) Prints : Jan is the first month          c) Generates compilation error          d) Results in runtime error           Answer: b) Prints : Jan..

15. What is the output of the following program?           main()                 {                     char *src = "Hello World";                     char dst[100];                     strcpy(src,dst);                     printf("%s",dst);                     }strcpy(char *dst,char *src)                       {while(*src) *dst++ = *src++;                        }                       ) "Hello World" b)"Hello" c)"World" d) NULL e) unidentified                      Answer: d) NULL

16.  What is the output of the following program?                 main()                        {                          int l=6;                          switch(l)                          { default : l+=2;

Page 20: C Interview Questions12

                            case 4: l=4;                            case 5: l++;                            break;                            }                            printf("%d",l);                                                                                   

                              }                    a)8 b)6 c)5 d)4 e)none                    Answer : a)8

17. What is the output of the following program?            main()                   {                    int x=20;                    int y=10;                    swap(x,y);                    printf("%d %d",y,x+2);                  }                     swap(int x,int y)                             {                               int temp;                               temp =x;                               x=y;                                y=temp;                             }                     a)10,20 b) 20,12 c) 22,10 d)10,22 e)none                   Answer:b)20,12

Page 21: C Interview Questions12

18. What is the output of the following problem ?            #define INC(X) X++             main()               {                int X=4;                printf("%d",INC(X++));               }              a)4 b)5 c)6 d)compilation error e) runtime error               Answer : d) compilation error

19. what can be said of the following            struct Node {            char *word;             int count;             struct Node left;              struct Node right;               }                 a) Incorrect definition                 b) structures cannot refer to other structure                 c) Structures can refer to themselves. Hence the statement is  OK                 d) Structures can refer to maximum of one other structure                 Answer :c)

20. What is the size of the following union. Assume that the size of int =2, size of float =4 and size of       char =1.       Union Tag{

Page 22: C Interview Questions12

         int a;         flaot b;        char c;           };        a)2 b)4 c)1 d) 7   

21.  What is the output of the following program? (. has been used to indicate  a space)            main()             {              char s[]="Hello,.world";             printf(%15.10s",s);           }           a )Hello,.World...           b)....Hello,.Wor           c)Hello,.Wor....           d)None of the above

 

1. Given the following statementenum day = { jan = 1 ,feb=4, april, may}What is the value of may?(a) 4 (b) 5 (c) 6 (d) 11(e) None of the above

2. Find the output for the following C program

Page 23: C Interview Questions12

main{int x,j,k;j=k=6;x=2;x=j*k;printf("%d", x);

3. Find the output for the following C programfn f(x) { if(x<=0)return;else f(x-1)+x;}

4. Find the output for the following C programi=20,k=0;for(j=1;j<i;j=1+4*(i/j)){k+=j<10?4:3;}printf("%d", k); Ans. k=4

5. Find the output for the following C programint i =10main(){int i =20,n;for(n=0;n<=i;){int i=10;i++;}printf("%d", i); Ans. i=20

6. Find the output for the following C programint x=5;y= x&y7.Find the output for the following C program

Page 24: C Interview Questions12

Y=10;if( Y++>9 && Y++!=10 && Y++>10){printf("%d", Y);else printf("%d", Y);} Ans. 13

8. Find the output for the following C programf=(x>y)?x:ya) f points to max of x and yb) f points to min of x and yc)errorAns. (a)

9. What is the sizeof(long int)(a) 4 bytes (b) 2 bytes (c) compiler dependent (d) 8 bytes10. Which of the function operator cannot be over loaded(a) <=(b) ?:(c) ==(d) *

11. Find the output for the following C programmain(){intx=2,y=6,z=6;x=y==z;printf(%d",x)}

section C - analysing program segements

Page 25: C Interview Questions12

1)struct dlink{int nodeid;struct dlink *next;struct dline *prev;} dlink_t;A pointer to the head of tha linked list ismaintainedas a global variable whose definition is dlink_t *head;

The function remove_element(dlink_t*rp), needs toremove the node pointed to by rp and adjust the head

The first node's prev and the last node's text areNULLremove_element (dlink_t *rp){rp->prev->next =rp->next;rp->next->prev =rp->prev;if(head ==rp)head =rp->next;}which of the following statement is true about thefunction remove_elementa)it works when head is the same as rp;b)it does not work whe rp is the last element on thelistc)it sets the head of the list correctlyd)it works in all cases

ans:( b)

2.#define NULL 0char *index (sp,c)register char *sp,c;{do {if(*sp==c)return(sp);}while (*sp++);return (NULL);}The first argument sp,is a pointer to a C string.The

Page 26: C Interview Questions12

second argument c is a character. This functionsearches for the character c in the string . If itisfounda pointer to that location is returned ,else NULL isreturned

This function worksa)Alwaysb)always but fails when the first byte contains thecharacter cc)works when c is a non NULL character arrayd)works only when the character c is found in thestring

3.main(){printf("%d\n",f(7));}f(x){if(x<=4)return x;return f(--x);}a)4b)5c)6d)7

4.on a machine where pointers are 4 bytes long,whathappens when the following code is executedmain(){int x=0 ,*p=0;x++;p++;printf("%d and %d\n",p);}a) 1 and 1 is printedb) 1 and 4c) 4 and 4d) causes an exception

5.which is correct?

Page 27: C Interview Questions12

a)strcpy(char *dst,char *src){while (*src)*dst++=*src++;

}

b) strcpy(char *dst,char *src){while (*dst++=*src++);}

c) strcpy(char *dst,char *src){while (*src){*dst=*src;dst++;src++;}}

d) strcpy(char *dst,char *src){while (*++dst=*++src);}

6. main(){int i=20,*j=&i;f1(j);*j+=10;f2(j);printf("%d and %d ',i,*j);}f1(k)int *k;{ *k+=15;}f2(x)int *x;{ int m=*x, *n=&m;*n+=10;}The values printed by the program will bea)20 and 55b)20 and 45

Page 28: C Interview Questions12

c)45 and 45d)55 and 55e)35 and 35ans : ( c )

7.int func(int x){if(x<=0)return (1);return func(x-1)+x;}main(){printf("%d",func(5));}a)12 b)16 c)15 d)11

8.consider the following fragments of c code in twofiles which will be linked together and executeda.cint i;main(){i=30;f1();printf("%d",i);}b.cstatic int f1(){ i+=10;}

which of the following is true?a)a.c will fail in compilation phase because f1() isnot declaredb)b.c will fail in compilation because the variableiis not declaredc)will print 30d)will print 40

Page 29: C Interview Questions12

d)a & b

9. void funca(int *k){*k+=20;}void funcb(int *k){int m=*x,*n=&m;*n+=10;}main(){ int var=25,;*varp=&var;funca(varp)*varp+=10;funcb(varp);printf("%d%d,var,*varp);}(a) 20,55(b) 35,35(c) 25,25(d)55,55ans : (d )

9. #include <stream.h>class x{public :int a;x();};x::x() { a=10;cout<< a ;}class b:public x {public :b(); x();};b::b() { a=20;cout<<a;}main(){b temp;}what will be the output of the following program?a)10 b)20 c)20 10 d)10 20