This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Which is better #define or enum? |
Answer»
|
|
| 2. |
Suppose a global variable and local variable have the same name. Is it possible to access a global variable from a block where local variables are defined? |
|
Answer» No. This isn’t possible in C. It’s ALWAYS the most local VARIABLE that GETS preference. |
|
| 3. |
Which structure is used to link the program and the operating system? |
|
Answer» The file structure is used to link the operating system and a PROGRAM. The header file "stdio.h" (STANDARD input/output header file) defines the file. It contains information about the file being used LIKE its CURRENT size and its memory location. It contains a CHARACTER pointer that points to the character which is currently being opened. When you open a file, it establishes a link between the program and the operating system about which file is to be accessed. |
|
| 4. |
What is a near pointer and a far pointer in C? |
Answer»
|
|
| 5. |
What is the difference between ‘g’ and “g” in C? |
|
Answer» In C double-quotes variables are identified as a STRING WHEREAS single-quoted variables are identified as the CHARACTER. Another MAJOR difference being the string (double-quoted) variables end with a null terminator that makes it a 2 character array. |
|
| 6. |
What are dangling pointers? How are dangling pointers different from memory leaks? |
|
Answer» The dangling pointer points to a memory that has already been freed. The storage is no LONGER allocated. Trying to access it might cause a Segmentation fault. A common way to end up with a dangling pointer: #include<stdio.h>#include<string.h>char *func(){ char STR[10]; STRCPY(str,"Hello!"); return(str);}You are returning an address that was a local variable, which would have gone out of scope by the time control was returned to the calling function. (Undefined behavior) *c = malloc(5izeof(INT));free(c);*c = 3; //writing to freed location!In the figure shown above writing to a memory that has been freed is an example of the dangling pointer, which makes the program crash. A memory leak is something where the memory allocated is not freed which causes the program to use an undefined amount of memory from the ram making it unavailable for every other running program(or daemon) which causes the programs to crash. There are various tools like O profile testing which is useful to detect memory leaks on your programs. void function(){ char *leak = malloc (10); //leak assigned but not freed} |
|
| 7. |
What is the difference between #include "..." and #include ? |
|
Answer» In practice, the difference is in the location where the preprocessor searches for the included file. For #include <filename> the C preprocessor LOOKS for the filename in the predefined list of system directories first and then to the directories told by the user(we can USE -I option to ADD directories to the mentioned predefined list). For #include "filename" the preprocessor searches first in the same DIRECTORY as the file CONTAINING the directive, and then follows the search path used for the #include <filename> form. This method is normally used to include programmer-defined header files. |
|
| 8. |
Why is it usually a bad idea to use gets()? Suggest a workaround. |
|
Answer» The standard INPUT library gets() reads USER input till it encounters a new line character. However, it does not check on the size of the variable being provided by the user is under the maximum size of the DATA type which makes the system vulnerable to buffer overflow and the input being WRITTEN into memory where it isn’t supposed to. We, THEREFORE, use gets() to achieve the same with a restricted range of input
|
|
| 9. |
What is typedef? |
|
Answer» typedef is a C keyword, used to define ALIAS/synonyms for an existing type in C language. In most cases, we use typedef's to simplify the existing type declaration syntax. Or to provide specific DESCRIPTIVE names to a type. typedef <existing-type> <new-type-identifiers>;typedef provides an alias name to the existing complex type definition. With typedef, you can simply create an alias for any type. WHETHER it is a simple INTEGER to complex function pointer or structure declaration, typedef will SHORTEN your code. |
|
| 10. |
What is Dynamic memory allocation in C? Name the dynamic allocation functions. |
|
Answer» C is a language known for its low-level control over the memory allocation of variables in DMA there are two major standard library malloc() and FREE. The malloc() function takes a single INPUT parameter which tells the size of the memory requested It returns a pointer to the allocated memory. If the allocation fails, it returns NULL. The prototype for the standard library function is like this: void *malloc(size_t size); void free(void *pointer);
|
|
| 11. |
What is a memory leak? How to avoid it? |
|
Answer» When we assign a variable it takes space of our RAM (either heap or RAM)dependent on the size of data type, however, if a PROGRAMMER uses a memory available on the heap and FORGETS to a delta it, at some point all the memory available on the ram will be occupied with no memory left this can lead to a memory leak. int main(){ char * PTR = malloc(sizeof(int)); /* Do some work */ /*Not freeing the allocated memory*/ RETURN 0;}To avoid memory leaks, you can trace all your memory allocations and think forward, where you WANT to destroy (in a good sense) that memory and place delete there. Another way is to use C++ smart pointer in C linking it to GNU compilers. |
|
| 12. |
What is pass by reference in functions? |
|
Answer» In Pass by reference, the CALLEE receives the address and makes a copy of the address of an ARGUMENT into the formal PARAMETER. Callee FUNCTION uses the address to access the actual argument (to do some manipulation). If the callee function changes the value ADDRESSED at the passed address it will be visible to the caller function as well. Pass By Reference |
|
| 13. |
What is call by reference in functions? |
|
Answer» When we CALLER FUNCTION makes a function call bypassing the addresses of actual parameters being passed, then this is called call by reference. In incall by reference, the operation PERFORMED on formal parameters affects the value of actual parameters because all the operations performed on the value STORED in the ADDRESS of actual parameters. |
|
| 14. |
What is the difference between struct and union in C? |
|
Answer» A struct is a group of complex data structures stored in a block of memory where each MEMBER on the block gets a separate memory location to make them accessible at once |
|
| 15. |
What is the difference between malloc() and calloc()? |
|
Answer» calloc() and malloc() are MEMORY dynamic memory ALLOCATING functions. The main difference is that malloc() only takes one argument, which is the number of BYTES, but calloc() takes TWO arguments, which are the number of blocks and the size of each block. |
|
| 16. |
What is an r-value and l-value? |
Answer»
|
|
| 17. |
Specify different types of decision control statements? |
|
Answer» All statements WRITTEN in a PROGRAM are EXECUTED from top to bottom one by one. Control statements are used to execute/transfer the control from one part of the program to another depending on the condition.
|
|