C Interview Questions

#include<stdio.h>
void func(int arg[5], int len)
{
  int n;
  for (n=0; n<len; n++)
    printf("%d ",arg[n]);
}

void func1(int *arg, int len)
{
  int n;
  for (n=0; n<len; n++)
    printf("%d ",*arg++);
}

void func2(int arg[], int len)
{
  int n;
  for (n=0; n<len; n++)
    printf("%d ",arg[n]);
}

void main ()
{
  int arr[] = {2,4,6};
  func(arr,3);
  func1(arr,3);
  func2(arr,3);
}

O/P:2 4 6 2 4 6 2 4 6

#include<stdio.h>
void main()
{
    if(5+"StringTest"-2 == 7+"StringTest"-4)
    {
        printf("PASS ");
        printf(5+"StringTest"-2,7+"StringTest"-4);
    }
    printf(5+"StringTest"-2,7+"StringTest"-4);
}

O/P:PASS ingTestingTest


What will be the output of the following program?
#include<stdio.h>

static int x[10];
void main()
{
int a=0;
x[a]=a++|a;
x[a]=a++&a;
x[a]=a++|a;
printf("%d %d %d %d",a, x[0],x[1],x[2]);
}
O/P : 3 1 2 3




Frequently Asked Questions - C
  1. what are different storage class types in C? Ans: Register,Auto,Static,extern
  2. what are different data types in C? Ans:int,char,float,double,long
  3. what are different types specifiers in C? Ans: void,char,int,float,double,long,short, signed, unsigned,struct,union,enum.
  4. what are different types of qualifiers in C?Ans: Const and Volatile
  5. What does static variable mean? Ans: static declaration limits the variable to file or function scope,Even though its existence is through life time of process only the file or function can modify the variable
  6. What is a pointer? Ans : pointer is address to a location pointers can store address of predefined data types or structures (derived data types) or functions.
  7. What is a structure? Ans : Structure is a derived data type which can be group of similar data types or different data types
  8. What are the differences between structures and arrays? Ans :Arrays is a group of similar data types but Structures can be group of different data types
  9. In header files whether functions are declared or defined? Ans : Generally declared.
  10. What are the differences between malloc() and realloc() and calloc()? Ans :malloc allocates required amount of memory where as realloc allocates required amount of memory but with values retained and calloc allocates memory with requested amount of memory and sets all values to zeoros. Prototype : void * malloc (size) void * realloc(old ptr,size) void*calloc(size_t noofelements,size_t size of each element)
  11. What are macros? what are its advantages and disadvantages? Ans:.Macros are processor directive which will be replaced at compile time
    the disadvantage with macros is that they just replace the code they are not function calls. similarly the advantage is they can reduce time for replacing the same values.
  12. Difference between pass by reference and pass by value? Ans : Pass by value just passes the value from caller to calling function so the called function cannot modify the values in caller function.But Pass by reference will pass the address to the caller function instead of value if called function requires to modify any value it can directly modify.
  13. What is static identifier? Ans :
  14. Where are the auto variables stored? Ans : Auto variable are stored in stack
  15. Where does global, static, local, register variables, free memory and C Program instructions get stored? Ans : local - stack, free memory - heap,c Program instructions - Text or code segment,register variables - registers,static - bss, global - Data segment refer the following document - segment is C
  16. Difference between arrays and linked list? Ans: Arrays are created at statically but linked list can be crated dynamically. Arrays will be in stack but linked list are heap.
  17. What are enumerations? Ans:In mathematics and theoretical computer science, the broadest and most abstract definition of an enumeration of a set is an exact listing of all of its elements (perhaps with repetition).
  18. Describe about storage allocation and scope of global, extern, static, local and register variables? Ans : global - visible to all files can be used across files by using keyword extern, scope is through out program - location Data segment. static - similar to global variable(only regarding lifetime of the variable) but scope limited to function or file - location BSS .local - scope with in the function located in stack.register - scope with in the function - register.
  19. What are register variables? What are the advantage of using register variables? Ans :If any variable is declared as register is called register variable. Advantage is fast access.
  20. What is the use of typedef? Ans : typedef - The intent is to make it easier for programmers to comprehend the source code
  21. Can we specify variable field width in a scanf() format string? If possible how? Ans : goto wikianswers
  22. Out of fgets() and gets() which function is safe to use and why? Ans : goto wikianswers
  23. Difference between strdup and strcpy? Ans : goto wikianswer
  24. What is recursion? Ans : recursion means calling the same function it self.
  25. Differentiate between a for loop and a while loop? What are it uses? Ans : for loop and while loop are similar but in for loop you can initialize the value which will be done only at entry to for loop and you can do other functions like increment or decrement(we often do) or printing etc.
  26. What are the different storage classes in C? Ans : auto,register,static,extern
  27. Write down the equivalent pointer expression for referring the same element a[i][j][k][l]? Ans : p = a, &a[i][j][k][l] is p+i+j+k+l;
  28. What is difference between Structure and Unions? Ans : in case of structure each variables have their own storage location whereas for unions all the variables share a common memory location. In structure all the variables can be handled at a time but for union only a member can be handled at a time.
  29. What the advantages of using Unions? Ans : unions share common memory location
  30. What are the advantages of using pointers in a program? Ans : refer wikianswers
  31. What is the difference between Strings and Arrays? Ans : refer wikianswers
  32. In a header file whether functions are declared or defined? Ans : declared usually.
  33. What is a far pointer? where we use it? Ans : refer wikianswers
  34. How will you declare an array of three function pointers where each function receives two ints and returns a float? Ans : float (*fptr[3])(int,int);
  35. what is a NULL Pointer? Whether it is same as an uninitialized pointer? Ans : pointer with value 0 is called null pointer.It is different from uninitialized pointer
  36. What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro? Ans : for questions 31 and 32 refer wikianswers
  37. What does the error 'Null Pointer Assignment' mean and what causes this error? Ans : it is run time error when ever our program tries to access 0th location i.e. write or read form that location.
  38. What is near, far and huge pointers? How many bytes are occupied by them? Ans : refer wikianswers
  39. How would you obtain segment and offset addresses from a far address of a memory location? Ans : refer wikianswers
  40. Are the expressions arr and &arr same for an array of integers? Ans : not same wikianswers
  41. Does mentioning the array name gives the base address in all the contexts? Ans : It will always treat.
  42. Explain one method to process an entire string as one unit? Ans : refer wikianswers
  43. What is the similarity between a Structure, Union and enumeration? Ans : as far as I know they are user defined data types.
  44. Can a Structure contain a Pointer to itself? Ans : yes
  45. How can we check whether the contents of two structure variables are same or not? Ans :
  46. How are Structure passing and returning implemented by the complier? Ans : it is compiler dependent. Some compilers push structures to stack and other will operate with passing address.
  47. How can we read/write Structures from/to data files?
  48. What is the difference between an enumeration and a set of pre-processor # defines? Ans : enumerations will have continuous values if values are not assigned but #defines should have values predefines
  49. what do the 'c' and 'v' in argc and argv stand for? Ans : "argc" stands for "argument count". It is the number of elements in "argv", "argument vector". (While "vector" and "array" mean different things in java, traditionally they are synonyms.) reference
  50. Are the variables argc and argv are local to main? Ans : yes
  51. What is the maximum combined length of command line arguments including the space between adjacent arguments? Ans : will be compiler dependent.
  52. If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?
  53. Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function? Ans: refer wikianswers
  54. What are bit fields? What is the use of bit fields in a Structure declaration? Ans : refer wikianswers
  55. To which numbering system can the binary number 1101100100111100 be easily converted to? Ans : Hexa decimal or octal
  56. Which bit wise operator is suitable for checking whether a particular bit is on or off? Ans : bit wise and
  57. Which bit wise operator is suitable for turning off a particular bit in a number? Ans : bit wise and.
  58. Which bit wise operator is suitable for turning on a particular bit in a number? Ans : bit wise or.
  59. which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an unsigned int or char by 1? Ans : Left shifting by one for unsigned number for signed left shift by 1 and restore the most significant bit.
  60. Write a program to compare two strings without using the strcmp() function.
    Ans :
    signed int funs32strcmp(const char * ps8string1, const char * ps8string2)
    {
    unsigned int u32stringlen1 = 0,u32stringlen2= 0;
    if((ps8string1 == NULL) ||(ps8string2 = NULL))
    return(-1);
    while(*ps8string1 != '')
    u32stringlen1 ++;
    u32stringlen1 ++;
    while(*ps8string2 != '')
    u32stringlen2 ++;
    u32stringlen2 ++;
    if(u32stringlen2 != u32stringlen1)
    return(-1);
    while(u32stringlen1 != 0)
    {
    u32stringlen1--;
    if( (*(ps8string1+u32stringlen1)) != (*(ps8string2+u32stringlen2)))
    return(-1);
    }
    return(0);
    }
  61. Write a program to concatenate two strings.
    Ans :
    signed int funs32strcat(char * ps8dststr,const char * ps8secstr)
    {
    char * ps8TmpDstString = ps8dststr;
    const char * ps8TmpSrcString = ps8secstr;
    if( (ps8dststr == NULL) || (ps8secstr == NULL))
    {
    return(-1);
    }
    while((*ps8TmpDstString) != '')
    ps8TmpDstString ++;do
    {
    *ps8TmpDstString = *ps8TmpSrcString;
    ps8TmpSrcString ++;
    ps8TmpDstString ++;
    }while((*ps8TmpSrcString) != '');
    }
  62. Write a program to interchange 2 variables without using the third one.
    Ans :
    int main()
    {
    int var1,var2;
    scanf("%d %d",&var1,&var2);
    var1^=var2^=var1^=var2;
    return(0);
    }
  63. Write programs for String Reversal & Palindrome check
    Ans : refer wikianswers and wikianswers
  64. Write a program to find the Factorial of a number
    Ans : refer wikianswers
  65. Write a program to generate the Fibinocci Series
    Ans : refer wikianswers
  66. Write a program which employs Recursion
    Ans : refer wikianswers
  67. Write a program which uses Command Line Arguments
    Ans : refer wikianswers
  68. Write a program which uses functions like strcmp(), strcpy()? etc
  69. What are the advantages of using typedef in a program?
    Ans : refer wikianswers
  70. How would you dynamically allocate a one-dimensional and two-dimensional array of integers?
    Ans : refer wikianswers
  71. How can you increase the size of a dynamically allocated array?
    Ans : by using realloc;
  72. How can you increase the size of a statically allocated array?
    Ans : refer wikianswers
  73. When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?
  74. Which function should be used to free the memory allocated by calloc()?
    Ans : free(pointer returned by calloc);
  75. How much maximum can you allocate in a single call to malloc()?
    Ans :
  76. Can you dynamically allocate arrays in expanded memory?
    Ans :
  77. What is object file? How can you access object file?
    Ans : object file is a pre-compiled file,but not linked.If you want to access object file you need to access it by using header file to the object file, and at the time of linking you need to provide the object file.
  78. Which header file should you include if you are to develop a function which can accept variable number of arguments?
    Ans : stdarg.h
  79. Can you write a function similar to printf()?
    Ans : refer wikianswers
  80. How can a called function determine the number of arguments that have been passed to it?
    Ans : no such function.
  81. Can there be at least some solution to determine the number of arguments passed to a variable argument list function?
    Ans : no
  82. How do you declare the following:
    • An array of three pointers to chars -> char *(ps8arr[3]);
    • An array of three char pointers -> char *(ps8arr[3]);
    • A pointer to array of three chars char * p;
    • A pointer to function which receives an int pointer and returns a float pointer -> float *(*function)(int *);
    • A pointer to a function which receives nothing and returns nothing void (*function)();
  83. What do the functions atoi(), itoa() and gcvt() do?
  84. Does there exist any other function which can be used to convert an integer or a float to a string?
  85. How would you use qsort() function to sort an array of structures?
    Ans:
    //write in ascending order
    #include <stdio.h>
    #include <stdlib.h>
    int compare(const void * var1,const void * var2)
    {
    if(*(int *)var1 > *(int *)var2)
    {
    return(1);
    }
    else{
    return(0);
    }
    }
    void main()
    {
    unsigned int au32Nos[10] = {32,44.,55,66,11,8,9,7,9,10};
    qsort(au32Nos,10,4,compare);printf("%d %d %d %d %d %d %d %d %d %d",au32Nos[0],au32Nos[1],au32Nos[2],au32Nos[3],au32Nos[4],au32Nos[5],au32Nos[6],au32Nos[7],au32Nos[8],au32Nos[9]);
    }//program to write in descending order
    #include <stdio.h>
    #include <stdlib.h>
    int compare(const void * var1,const void * var2)
    {
    if(*(int *)var1 > *(int *)var2)
    {
    return(0);
    }
    else{
    return(1);
    }
    }
    void main()
    {
    unsigned int au32Nos[10] = {32,44.,55,66,11,8,9,7,9,10};
    qsort(au32Nos,10,4,compare);

    printf("%d %d %d %d %d %d %d %d %d %d",au32Nos[0],au32Nos[1],au32Nos[2],au32Nos[3],au32Nos[4],au32Nos[5],au32Nos[6],au32Nos[7],au32Nos[8],au32Nos[9]);
    }
  86. How would you use qsort() function to sort the name stored in an array of pointers to string?
  87. How would you use bsearch() function to search a name stored in array of pointers to string?
  88. How would you use the functions sin(), pow(), sqrt()?
  89. How would you use the functions memcpy(), memset(), memmove()?
  90. How would you use the functions fseek(), freed(), fwrite() and ftell()?
  91. How would you obtain the current time and difference between two times?
  92. How would you use the functions randomize() and random()?
  93. How would you implement a substr() function that extracts a sub string from a given string?
  94. What is the difference between the functions rand(), random(), srand() and randomize()?
  95. What is the difference between the functions memmove() and memcpy()?
    Ans : refer wikianswers
  96. How do you print a string on the printer?
  97. Can you use the function fprintf() to display the output on the screen?

No comments:

Post a Comment

తెలుగులో వ్రాయడానికి http://www.google.com/ime/transliteration/ ఉపయోగించండి.