Thursday, December 14, 2017

Interview questions for C language - 1

    1. What does static variable mean? 
    This means a static variable is one that is not seen outside the function in which it is declared but which remains until the program terminates. It also means that the value of the variable persists between successive calls to a function

    2.What is a pointer? 
    A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant,

    3.What is a structure? 
    A structure is a composite data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer, or the struct declared name which returns the same address.

    4.What are the differences between structures and arrays? 
    Array is a collection of related data elements of same type. Structure can have elements of different types

    5. In header files whether functions are declared or defined?
    in header files the function are declared not defined

    6. What are the differences between malloc() and calloc()
    malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

    7. What are macros? what are its advantages and disadvantages
    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.

    8. Difference between pass by reference and pass by value? Passing by reference means the called functions' parameter will be the same as the callers' passed argument (not the value,Pass by value means the called functions' parameter will be a copy of the callers' passed argument.

    9. What is static identifier?
    A static identifier (or static variable) retains its value throughout program execution.

    10. Where are the auto variables stored
    Main memory and CPU registers are the two memory locations where auto variables are stored. Auto variables are defined under automatic storage class.

    11. Difference between arrays and linked list? 
    In array, each element is independent, no connection with previous element or with its location. In Linked list, location or address of elements is stored in the link part of previous element/node. In array, no pointers are used like linked list so no need of extra space in memory for pointer.

    12. What are enumerations
    Enumeration type allows programmer to define their own data type . Keyword enum is used to defined enumerated data type.

    13. What are the different storage classes in C? A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program. There are following storage classes which can be used in a C Program. auto. register. static. extern. auto - Storage Class. auto is the default storage class for all local variables.

    14. What are register variables? What are the advantage of using register variables? 
    If a variable is declared with a register storage class,it is known as register variable.The register variable is stored in the cpu register instead of main memory.Frequently used variables are declared as register variable as it?s access time is faster.

    15. What is the use of typedef? 
    typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.

    16. Can we specify variable field width in a scanf() format string? If possible how? 
     It is possible to specify variable field width in a scanf() format string. By using %s control string. This character reads a string of variable field width up to the first white space character. Ex: scanf(“%s”, name); // where name is a character array.

    17. Out of fgets() and gets() which function is safe to use and why? 
    Out of functions fgets( ) and gets( ), fgets( ) is safer to use. gets( ) receives a string from the keyboard and it is terminated only when the enter key is hit. There is no limit for the input string. The string can be too long and may lead to buffer overflow.

    18. Difference between strdup and strcpy? 
    strdup would automatically allocate required memory for the target string. strcpy, user needs to allocate memory for the target string before he/she calls the function

    19. What is recursion in C?
    Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

    20. Differentiate between a for loop and a while loop? What are it uses?
    The FOR loop is often used when you usually know how many times you would like the program, which means it will run that program until the number of times is complete before it terminates itself. The WHILE loop works in a similar manner but requires a conditional statement.

    21. What are the different storage classes in C? 
    The are four storage classes in C are automatic, register, external, and static. 1 Automatic Variables. 2 Register Variables. 3 External Variables. 4 Variable Definition vs Declaration.

    22. Write down the equivalent pointer expression for referring the same element a[i][j][k][l]? 
    *(*(*(*(a+i)+j)+k)+l)

    23. What is difference between Structure and Unions? 
    A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record. A union is a special data type available in C that allows storing different data types in the same memory location.

    24. What the advantages of using Unions? 
    The basic advantage is that union will use the memory space of the datatype which has the highest memory, hence memory consumption will be less.

    25. What are the advantages of using pointers in a program? 
    Major advantages of pointers are: (i) It allows management of structures which are allocated memory dynamically. (ii) It allows passing of arrays and strings to functions more efficiently. (iii) It makes possible to pass address of structure instead of entire structure to the functions.

No comments:

Post a Comment