Showing posts with label arrays interview questions. Show all posts
Showing posts with label arrays interview questions. Show all posts

Friday, 19 August 2011

Arrays in C laguage


section 1.6: Arrays


Note carefully that arrays in C are 0-based, not 1-based as they are in some languages. (As we'll see, 0-based arrays turn out to be more convenient than 1-based arrays more of the time, but they may take a bit of getting used to at first.)
When they say ``as reflected in the for loops that initialize and print the array,'' they're referring to the fact that the vast majority of for loops in C look like this:
        for(i = 0; i < 10; ++i)
and count from 0 to 9. The loop
        for(i = 1; i <= 10; ++i)
would count from 1 to 10, but loops like this are comparatively rare. (In fact, whenever you see either ``= 1'' or ``<='' in a for loop, it's an indication that something unusual is going on which you'll want to be aware of, and it may even be a bug.)

They've started going a little fast here, so read up if they're losing you. What's this magic expression c-'0' that they're using as an array subscript? Remember, as we saw first on page 19, that characters in C are represented by small integers corresponding to their values in the machine's character set. In ASCII, which most machines use, 'A' is character code 65, '0' (zero) is code 48, '9' is code 57, and all the other characters have their own values which I won't bother to list. If we've just read the character '9' from the file, it has value 57, so c-'0' is 57 - 48 which is 9, and we'll increment cell number 9 in the array, just like we want to. Furthermore, even if we're not using a machine which uses ASCII, by subtracting '0', we'll always subtract whatever the right value is to map the characters from '0' to '9' down to the array cell range 0 to 9.

Thursday, 18 August 2011

C Programming interview questions latest for all


                                        C Programming
1. 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
      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...
2. 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
3. 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
4.output of the following.                                                                                        
            main()
                 {
                   int i;
                   char *p;
                   i=0X89;
                   p=(char *)i;
                   p++;
                   printf("%x\n",p);
                    }
             ans:0X8A
5  which of the following is not a ANSI C language keyword?  ans:Function.
6. 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...
7. The type of the controlling expression of a switch statement cannot be of the type
        a) int b) char c) short d)float e) none
        Answer : d)float
8. What is the value of the expression (3^6) + (a^a)?
     a) 3     b) 5    c) 6     d) a+18       e) None
     Answer : 5
9. 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;
10. 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
11. What is th output of the following program?                                                             
         int x= 0x65;
         main()
            {
             char x;
             printf("%d\n",x)
           }
       a) compilation error     b) 'A'     c) 65       d) unidentified
12. 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 : d) 3,7
13. 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");                                                              
                  }
             }
          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..
14. 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
15. What is the output of the following program?
                 main()
                        {
                          int l=6;
                          switch(l)
                          { default : l+=2;
                            case 4: l=4;
                            case 5: l++;
                            break;
                            }
                            printf("%d",l);                                                                                      
                              }
                    a)8 b)6 c)5 d)4 e)none
                    Answer : c)5
16. 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:d)10,22
17. 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
18. 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)
19. 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{
         int a;
         flaot b;
        char c;
           };
        a)2 b)4 c)1 d) 7                                                                                                            
20.  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 abov

C Programming interview questions


C Programming
                                      
  1. 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
      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    
2. 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
           
3. What is th output of the following program?                                                                  
    int x= 0x65;
         main()
            {
             char x;
             printf("%d\n",x)
           }
       a) compilation error     b) 'A'     c) 65       d) unidentified
4. 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
         
5. 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");                                                              
                  }
             }
          a) Does not print anything
          b) Prints : Jan is the first month
          c) Generates compilation error
          d) Results in runtime error
          
6. What is the output of the following program?
                 main()
                        {
                          int l=6;
                          switch(l)
                          { default : l+=2;
                            case 4: l=4;
                            case 5: l++;
                            break;
                            }
                            printf("%d",l);                                                                                      
                              }
                    a)8 b)6 c)5 d)4 e)none
                   
7. 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
                  

8. 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{
         int a;
         flaot b;
        char c;
           };
        a)2 b)4 c)1 d) 7