Thursday, October 2, 2025

C Interview Questions & Answers || Intermediate Level || Set - I

 Question: 22. Why is it not preferred to use gets()

Answer: gets() is used to read a complete line of input until a new line is encountered. This function is generally not preferred because it can lead to buffer overflow and characters overwriting adjacent memory locations. This occurs because gets() does not check the size of the destination buffer, meaning the size of the input doesn't matter, and data can overflow the memory allocated to the buffer.

Question: 23. How to generate random numbers in C? 

Answer: Random numbers in C are generated using the rand() function. Code examples are provided in the source to demonstrate how to generate a random number, as well as how to generate random numbers within a specific range.

Question: 24. What is a memory leak in C? 

Answer: A memory leak occurs when allocated memory is not freed. To avoid a memory leak, the free() function must be used to release the dynamically allocated memory.

Question: 25. What is the difference between call by value and call by reference in C? 

Answer:

  • Call by Value: This occurs when copies of the variables are passed to a function. When using call by value, any changes made to the values within the function do not affect the values of the original variables.

  • Call by Reference: This occurs when the addresses of the variables are passed to the function. Changes made to the variables within the function affect the values of the original variables because the function is working directly with the memory addresses.

Question: 26. Explain the difference between Type Casting and Type Conversion in C with examples? 

Answer:

  • Type Casting is the explicit conversion of one data type into another data type. Since the user performs this conversion, it is an explicit conversion. For example, converting a float variable a = 6.5 to an int variable b using int b = (int)a; results in b being 6.

  • Type Conversion is the conversion of one data type into another data type, but it is an implicit conversion done by the compiler itself. For example, assigning an int x = 5 to a float y (float y = x;) implicitly converts the integer value to a float.

Question: 27. Can we compile a program without a main() function in C? 

Answer: Yes, a program can be compiled without a main() function, but it cannot be executed.

  • The C compiler's job is simply to translate the code into machine language (object code) and will compile the code as long as there are no syntax errors, regardless of whether main() is present.

  • However, the main() function is the entry point of any executable program. When execution is attempted, the operating system looks for the main() function to start the program. If it is missing, the linker will throw an error because it cannot determine where to begin execution.

Question: 28. How to create an Infinite loop in C? Answer: An infinite loop can be created using several methods:

  1. Using a while Loop: Set the condition to 1 (or any non-zero value), which is always true, causing the loop to run indefinitely.

    • Example: while (1) { // Code block }.

  2. Using a for Loop: Do not specify any initialization, condition, or increment in the loop. Without a condition, it is always considered true.

    • Example: for (;;) { // Code block }.

  3. Using a do-while Loop: Similar to the while loop, set the loop condition value to 1, ensuring the loop continues indefinitely.

    • Example: do { // Code block } while (1);.

Question: 29. Write a C program to swap two numbers without using the third variable. Answer: The source indicates that a C program to swap two numbers without a third variable is provided through a code example. This is typically done using arithmetic operations (addition/subtraction) or XOR operations.

Question: 30. Write a C program to check whether a number is prime or not. 

Answer: A program to check if a number is prime or not is available in the source material.

Question: 31. Write a C program to add two numbers without using the addition operator. Answer: A program to add two numbers without using the addition operator is provided in the source material, likely utilizing bitwise operations.

Question: 32. What are the different techniques for making hash function? 

Answer: The techniques for making a hash function include:

  1. Truncation Method: This is the simplest method where only a part of the key is taken as the address.

  2. Midsquare Method: The key is squared, and certain digits from the middle of the squared result are used as the address.

  3. Folding Method: The key is divided into different parts, where the length of each part is generally the same as the required address (except potentially the last part).

  4. Division Method (Modulo-Division): The key ($k$) is divided by the table size ($n$), and the remainder is taken as the address of the hash table. The formula is $H(k) = k \mod n$.

Question: 33. What are the issues that hamper the efficiency in sorting a file? 

Answer: The issues that hamper the efficiency in sorting a file are:

  • The amount of time required by the programmer to code a specific sorting program.

  • The amount of machine time necessary for running the particular program.

  • The amount of space necessary for the particular program.

Question: 34. What is the use of the volatile keyword? 

Answer: The volatile modifier informs the compiler that a variable's value may be changed in ways that are not explicitly specified by the program.

This keyword is important because most C compilers optimize certain expressions by making assumptions that a variable's content is unchanging if it doesn't appear on the left side of an assignment statement. This means the variable might not be reexamined every time it is referenced. Compilers may also change the order of evaluation of an expression during compilation. The volatile modifier prevents these changes.

For instance, if a global variable's address is passed to an operating system's clock routine to hold the system time, the variable's contents are altered without explicit assignment statements in the program. Declaring this variable as volatile ensures the compiler correctly handles these external modifications.

…till next post, bye-bye & take care.

No comments:

Post a Comment