Docsity

C language important 2marks, Exams of Computer Science

Partial preview of the text

Download C language important 2marks and more Exams Computer Science in PDF only on Docsity!

C Language

Questions and

Answers

Abstract This PDF contains 100+ Q&A on C Language to help you

remind yourself of basic concepts and definitions. Especially helpful for Interviews and quick references.

1. What does static variable mean? 2. What is a pointer?

3. What are the uses of a pointer? 4. What is a structure?

5. What is a union? 6. What are the differences between structures and union?

7. What are the differences between structures and arrays? 8. In header files whether functions are declared or defined?

9. What are the differences between malloc () and calloc ()? 10. What are macros? What are its advantages and disadvantages?

11. Difference between pass by reference and pass by 12. What is static identifier?

13. Where is the auto variables stored? 14. Where does global, static, and local, register

15. Difference between arrays and linked list? 16. What are enumerations?

17. Describe about storage allocation and scope of 18. What are register variables? What are the advantages

19. What is the use of typedef? 20. Can we specify variable field width in a scanf()

21. Out of fgets() and gets() which function is safe to use and why? 22. Difference between strdup and strcpy?

23. What is recursion?

47. What modular programming? 48. What is a function?

49. What is an argument? 50. What are built in functions?

51. Difference between formal argument and actual argument? 52. Is it possible to have more than one main() function in a C program?

53. What is the difference between an enumeration and a set of pre-processor# defines?

54. How are Structure passing and returning implemented by the complier? 55. What is the similarity between a Structure, Union and enumeration?

56. Can a Structure contain a Pointer to itself? 57. How can we read/write Structures from/to data files?

58. Write a program which employs Recursion? 59. Write a program which uses Command Line Arguments?

60. Difference between array and pointer? 61. What do the ‘c’ and ‘v’ in argc and argv stand for?

62. What are C tokens? 63. What are C identifiers?

64. Difference between syntax vs logical error? 65. What is preincrement and post increment?

66. Write a program to interchange 2 variables without using the third one. 67. What is the maximum combined length of command line arguments

including the space between adjacent arguments?

68. What are bit fields? What is the use of bit fields in a Structure declaration? 69. What is a preprocessor, What are the advantages of preprocessor?

70. What are the facilities provided by preprocessor? 71. What are the two forms of #include directive?

72. How would you use the functions randomize() and random()? 73. What do the functions atoi(), itoa() and gcvt() do?

74. How would you use the functions fseek(), freed(), fwrite() and ftell()? 75. What is the difference between the functions memmove() and memcpy()?

76. What is a file? 77. What are the types of file?

78. What is a stream? 79. What is meant by file opening?

81. What is a file pointer? 82. How is fopen()used?

83. How is a file closed? 84. What is a random access file?

85. What is the purpose of ftell? 86. What is the purpose of rewind()?

87. Difference between a array name and a pointer variable? 88. Represent a two-dimensional array using pointer?

89. Difference between an array of pointers and a pointer to an array? 90. Can we use any name in place of argv and argc as command line arguments

? 91. What are the pointer declarations used in C?

ANS: 1. What does static variable mean? Ans: Static variables are the variables which retain their values between the function calls. They are initialized only once their scope is within the function in which they are defined. 2. What is a pointer? Ans: Pointers are variables which stores the address of another variable. That variable may be a scalar (including another pointer), or an aggregate (array or structure). The pointed-to object may be part of a larger object, such as a field of a structure or an element in an array. 3. What are the uses of a pointer? Ans: Pointer is used in the following cases i) It is used to access array elements ii) It is used for dynamic memory allocation. iii) It is used in Call by reference iv) It is used in data structures like trees, graph, linked list etc. 4. What is a structure? Ans: Structure constitutes a super data type which represents several different data types in a single unit. A structure can be initialized if it is static or global. 5. What is a union? Ans: Union is a collection of heterogeneous data type but it uses efficient memory utilization technique by allocating enough memory to hold the largest member. Here a single area of memory contains values of different types at different time. A union can never be initialized.

6. What are the differences between structures and union? Ans: A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size. A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes. 7. What are the differences between structures and arrays? Ans: Structure is a collection of heterogeneous data type but array is a collection of homogeneous data types. Array 1-It is a collection of data items of same data type. 2-It has declaration only 3-.There is no keyword. 4- array name represent the address of the starting element. Structure 1-It is a collection of data items of different data type. 2- It has declaration and definition 3- keyword struct is used 4-Structure name is known as tag it is the short hand notation of the declaration. 8. In header files whether functions are declared or defined? Ans: Functions are declared within header file. That is function prototypes exist in a header file,not function bodies. They are defined in library (lib). 9. What are the differences between malloc () and calloc ()? Ans: Malloc Calloc 1-Malloc takes one argument Malloc(a);where a number of bytes 2-memory allocated contains garbage values 1-Calloc takes two arguments Calloc(b,c) where b no of object and c size of object

2-It initializes the contains of block of memory to zerosMalloc takes one argument, memory allocated contains garbage values. It allocates contiguous memory locations. Calloc takes two arguments, memory allocated contains all zeros, and the memory allocated is not contiguous. 10. What are macros? What are its advantages and disadvantages? Ans: Macros are abbreviations for lengthy and frequently used statements. When a macro is called the entire code is substituted by a single line though the macro definition is of several lines. The advantage of macro is that it reduces the time taken for control transfer as in case of function. The disadvantage of it is here the entire code is substituted so the program becomes lengthy if a macro is called several times. Read more: c interview questions for freshers | FreeJobAlert.com http://www.freejobalert.com/clanguage- interview-questions-part1/6125/#ixzz1O1xitnOp 11. Difference between pass by reference and pass by value? Ans: variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value without modifying the variable. (In other words, the callee simply cannot Pass by reference passes a pointer to the value. This allows the callee to modify the modify the variable, since it lacks a reference to it.) 12. What is static identifier? Ans: A file-scope variable that is declared static is visible only to functions within that file. A function-scope or block-scope variable that is declared as static is visible only within that scope. Furthermore, static variables only have a single instance. In the case of function- or block-scope variables, this means that the variable is not ―automatic‖ and thus retains its value across function invocations. 13. Where is the auto variables stored? Ans: Auto variables can be stored anywhere, so long as recursion works. Practically, they‘re stored on the stack. It is not necessary that always a stack exist. You could theoretically allocate function invocation records from the heap.

14. Where does global, static, and local, register variables, free memory and C Program instructions get stored? Ans: Global: Wherever the linker puts them. Typically the ―BSS segment‖ on many platforms. Static: Again, wherever the linker puts them. Often, they only difference between globals and statics is whether the linker will resolve the symbols across compilation units.Local: Typically on the stack, unless the variable gets register allocated and‘re intermixed with the globals. The never spills.Register: Nowadays, these are equivalent to unless they get register-allocated. 15. Difference between arrays and linked list? ―Local‖ variables. They live on the stack Ans: structures scattered through memory, held together by pointers in each element that point to the next element. With an array, we can (on most architectures) move from one element to the next An array is a repeated pattern of variables in contiguous storage. A linked list is a set of by adding a fixed constant to the integer value of the pointer. With a linked list, there is a pointer in each structure which says what element comes next. 16. What are enumerations? ―next‖ Ans: They are a list of named integer-valued constants. Example:enum color { black , orange=4,

iterations to be performed is not known in advance we use while loop. 25. What is storage class? What are the different storage classes in C? Ans: Storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. The storage classes in c are auto, register, and extern, static, typedef. 26. What the advantages of using Unions? Ans: When the C compiler is allocating memory for unions it will always reserve enough room for the largest member. 27. What is the difference between Strings and Arrays? Ans: array of characters terminated by a NULL character. String is a sequence of characters ending with NULL .it can be treated as a one dimensional

28. What is a far pointer? Where we use it? Ans: these In large data model (compact, large, huge) the address B0008000 is acceptable because in model all pointers to data are 32bits long. If we use small data model(tiny, small, medium) the above address won a small data model and want to access the address B0008000 then we use far pointer. Far pointer‘t work since in these model each pointer is 16bits long. If we are working in is always treated as a 32bit pointer and contains a segment address and offset address both of 16bits each. Thus the address is represented using segment : offset format B000h:8000h. For any given memory address there are many possible far address segment : offset pair. The segment register contains the address where the segment begins and offset register contains the offset of data/code from where segment begins. 29. What is a huge pointer? Ans: are normalized pointers so for any given memory address there is only one possible huge address Huge pointer is 32bit long containing segment address and offset address. Huge pointers segment: offset pair. Huge pointer arithmetic is doe with calls to special subroutines so its arithmetic slower than any other pointers. 30. What is a normalized pointer, how do we normalize a pointer? Ans: Since a segment can start every 16bytes so the offset will have a value from 0 to F. for normalization It is a 32bit pointer, which has as much of its value in the segment register as possible. convert the address into 20bit address then use the 16bit for segment address and 4bit for the offset address. Given a pointer 500D: 9407,we convert it to a 20bitabsolute address 549D7,Which then normalized to 549D:0007. 31. What is near pointer? Ans: (if A near pointer is 16 bits long. It uses the current content of the CS (code segment) register the pointer is pointing to code) or current contents of DS (data segment) register (if the pointer is pointing to data) for the segment part, the offset part is stored in a 16 bit near pointer. Using near pointer limits the data/code to 64kb segment. 32. In C, why is the void pointer useful? When would you use it? Ans: and The void pointer is useful because it is a generic pointer that any pointer can be cast into back again without loss of information.

33. What is a NULL Pointer? Whether it is same as an uninitialized pointer? Ans: anywhere. Null pointer is a pointer which points to nothing but uninitialized pointer may point to 34. Are pointers integer? Ans: 35. What does the error No, pointers are not integers. A pointer is an address. It is a positive number. ‘Null Pointer Assignment’ means and what causes this error? Ans: may cause an error. 36. What is generic pointer in C? As null pointer points to nothing so accessing a uninitialized pointer or invalid location Ans: pointer, conversions are applied automatically (implicit conversion). In C void* acts as a generic pointer. When other pointer types are assigned to generic 37. Are the expressions arr and &arr same for an array of integers? Ans: 38. IMP>How pointer variables are initialized? Yes for array of integers they are same. Ans: I. Static memory allocation II. Dynamic memory allocation Pointer variables are initialized by one of the following ways. 39. What is static memory allocation? Ans: the Compiler allocates memory space for a declared variable. By using the address of operator, reserved address is obtained and this address is assigned to a pointer variable. This way of assigning pointer value to a pointer variable at compilation time is known as static memory allocation. 40. What is dynamic memory allocation? Ans: dynamically. If these functions are used to get memory dynamically and the values returned by A dynamic memory allocation uses functions such as malloc() or calloc() to get memory these function are assigned to pointer variables, such a way of allocating memory at run time is known as dynamic memory allocation. 41. What is the purpose of realloc? Ans: (ptr,n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument specifies the new size. The size may be increased It increases or decreases the size of dynamically allocated array. The function realloc or decreased. If sufficient space is not available to the old region the function may create a new region. 42. What is pointer to a pointer? Ans: pointer. Example: If a pointer variable points another pointer value. Such a situation is known as a pointer to a int p1,*p2,v=10; P1=&v; p2=&p1; Here p2 is a pointer to a pointer. 43. What is an array of pointers? Ans: 44. Difference between linker and linkage? if the elements of an array are addresses, such an array is called an array of pointers. Ans: Linker converts an object code into an executable code by linking together the necessary

by an extra, compiler-supported 55. IMP>what is the similarity between a Structure, Union and enumeration? Ans: All of them let the programmer to define new data type. ‗hidden‘ argument to the function.

56. Can a Structure contain a Pointer to itself? Ans: 57. How can we read/write Structures from/to data files? Yes such structures are called self-referential structures. Ans: structure variable. A corresponding fread() invocation can read the structure back from file. calling To write out a structure we can use fwrite() as Fwrite( &e, sizeof(e),1,fp);Where e is a fwrite() it writes out sizeof(e) bytes from the address &e. Data files written as memory images with fwrite(),however ,will not be portable, particularly if they contain floating point fields or Pointers. This is because memory layout of structures is machine and compiler dependent. Therefore, structures written as memory images cannot necessarily be read back by programs running on other machine, and this is the important concern if the data files you writing will ever be interchanged between machines. ‘re 58. Write a program which employs Recursion? Ans: 59.Write a program which uses Command Line Arguments? int fact(int n) { return n > 1? n * fact(n – 1) : 1; } Ans: #include void main(int argc,char *argv[]) { int i; clrscr(); for(i=0;i printf( } ―\n%d‖,argv[i]); 60. Difference between array and pointer? Ans: Array 1- Array allocates space automatically 2- It cannot be resized 3- It cannot be reassigned 4- sizeof (arrayname) gives the number of bytes occupied by the array. Pointer 1-Explicitly assigned to point to an allocated space. 2-It can be sized using realloc() 3-pointer can be reassigned. 4-sizeof (p) returns the number of bytes used to store the pointer variable p. 61. What do the Ans: program is The c in argc(argument count) stands for the number of command line argument the ‘c’ and ‘v’ in argc and argv stand for? invoked with and v in argv(argument vector) is a pointer to an array of character string that contain the arguments. 62. IMP>what are C tokens? Ans: There are six classes of tokens: identifier, keywords, constants, string literals, operators and

other separators. 63. What are C identifiers? Ans: These are names given to various programming element such as variables, function, arrays.It is a combination of letter, digit and underscore.It should begin with letter. Backspace is not allowed. 64. Difference between syntax vs logical error? Ans: Syntax Error 1-These involves validation of syntax of language. 2-compiler prints diagnostic message. Logical Error 1-logical error are caused by an incorrect algorithm or by a statement mistyped in such a way that it doesn 2-difficult to find. 65. What is preincrement and post increment? ‘t violet syntax of language. Ans: expression containing it. n++ (post increment) does increment after the value of n is used. 66. Write a program to interchange 2 variables without using the third one. ++n (pre increment) increments n before its value is used in an assignment operation or any Ans: a ^= b; ie a=a^b b ^= a; ie b=b^a; a ^= b ie a=a^b; here the numbers are converted into binary and then xor operation is performed. You know, you‘re just asking ―have you seen this overly clever trick that‘s not worth applying on modern architectures and only really applies to integer variables? 67. What is the maximum combined length of command line arguments including the space ‖ between adjacent arguments? Ans: 68. What are bit fields? What is the use of bit fields in a Structure declaration? It depends on the operating system. Ans: will call a The syntax of field definition and access is based on structure. A bit field is a set of adjacent bits within a single implementation based storage unit that we ―word‖. Struct { unsigned int k :1; unsigned int l :1; unsigned int m :1; }flags; the number following the colon represents the field width in bits.Flag is a variable that contains three bit fields. 69. What is a preprocessor, what are the advantages of preprocessor? Ans: A preprocessor processes the source code program before it passes through the compiler. 1- a preprocessor involves the readability of program 2- It facilitates easier modification 3- It helps in writing portable programs 4- It enables easier debugging

Ans: creating an I/O stream before reading or writing the data. The action of connecting a program to a file is called opening of a file. This requires

80. What is FILE? Ans: 81. What is a file pointer? FILE is a predefined data type. It is defined in stdio.h file. Ans: points to the block of information of the stream that had just been opened. 82. How is fopen()used? The pointer to a FILE data type is called as a stream pointer or a file pointer. A file pointer Ans: assigned as The function fopen() returns a file pointer. Hence a file pointer is declared and it is FILE *fp; fp= fopen(filename,mode); filename is a string representing the name of the file and the mode represents: ― ― ―rwa‖‖‖ for read operation (^) for append operationfor write operation ― 83How is a file closed? Ans: r+‖,‖ A file is closed using fclose() functionw+‖,‖a+‖ for update operation Eg. fclose(fp); Where fp is a file pointer. 84. What is a random access file? Ans: A file can be accessed at random using fseek() function fseek(fp,position,origin); fp file pointer position number of bytes offset from origin origin 0,1 or 2 denote the beginning ,current position or end of file respectively. 85. What is the purpose of ftell? Ans: ftell(fp); The function ftell() is used to get the current file represented by the file pointer. returns a long integer value representing the current file position of the file pointed by the file pointer fp.If an error occurs ,-1 is returned. 86. What is the purpose of rewind()? Ans: Rewind(fp); Where fp is a file pointer.Also we can get the same effect by The function rewind is used to bring the file pointer to the beginning of the file. feek(fp,0,0); 87. Difference between a array name and a pointer variable? Ans: A pointer variable is a variable where as an array name is a fixed address and is not a variable. A pointer variable must be initialized but an array name cannot be initialized. An array name being a constant value , ++ and — operators cannot be applied to it. 88. Represent a two-dimensional array using pointer?

Ans: Address of a[I][j] Value of a[I][j] &a[I][j] or a[I] + j or *(a+I) + j *&a[I][j] or a[I][j] or *(a[I] + j ) or *( * ( a+I) +j )

89. Difference between an array of pointers and a pointer to an array? Ans: Array of pointers 1- Declaration is: data_type *array_name[size]; 2-Size represents the row size. 3- The space for columns may be dynamically Pointers to an array 1-Declaration is data_type ( *array_name)[size]; 2-Size represents the column size. 90. Can we use any name in place of argv and argc as command line arguments? Ans: 91. What are the pointer declarations used in C? yes we can use any user defined name in place of argc and argv; Ans: 1- Array of pointers, e.g , int a[10]; Array of pointers to integer 2-Pointers to an array,e.g , int (a)[10]; Pointer to an array of into 3-Function returning a pointer,e.g, float *f( ) ; Function returning a pointer to float 4-Pointer to a pointer ,e.g, int **x; Pointer to apointer to int 5-pointer to a data type ,e.g, char *p; pointer to char 92. Differentiate between a constant pointer and pointer to a constant? Ans: const char *p; //pointer to a const character. char const *p; //pointer to a const character. char * const p; //const pointer to a char variable. const char * const p; // const pointer to a const character. 93. Is the allocated space within a function automatically deallocated when the function returns? Ans: No pointer is different from what it points to .Local variables including local pointers variables in a function are deallocated automatically when function returns.,But in case of a local pointer variable ,deallocation means that the pointer is deallocated and not the block of memory allocated to it. Memory dynamically allocated always persists until the allocation is freed or the program terminates. 94. Discuss on pointer arithmetic? Ans:

Ans: C is not C++. Typedef names are not automatically generated for structure tags. 105. Why can’t we compare structures? Ans: There is no single, good way for a compiler to implement structure comparison which is consistent with C‘s low-level flavor. A simple byte-by-byte comparison could founder on random bits present in unused alignment of later fields correct). A field-by-field comparison might require unacceptable amounts of repetitive code for large structures. ―holes‖ in the structure (such padding is used to keep the

106. How are structure passing and returning implemented? Ans: pushed on When structures are passed as arguments to functions, the entire structure is typically the stack, using as many words as are required. Some compilers merely pass a pointer to the structure, though they may have to make a local copy to preserve pass-by-value semantics. Structures are often returned from functions in a location pointed to by an extra,compilersupplied ― for structure returns, although this made structure-valued functions non-reentrant, which ANSI C disallows.hidden‖ argument to the function. Some older compilers used a special,static location