Commonly Asked C Programming Interview Questions | Set 3

Q.1 Write down the smallest executable code?
Ans. main is necessary for executing the code. Code is

filter_none

edit
close

play_arrow

link
brightness_4
code

void main()
{
}

chevron_right


Output:


Q.2 What are entry control and exit control loops?
Ans. C support only 2 loops:



  1. Entry Control: This loop is categorized in 2 part
    a. while loop
    b. for loop
  2. Exit control: In this category, there is one type of loop known as
    a. do while loop.

Q.3 Why pre-processor directive does not have a semi-colon at last?
Ans. Semi-colon is needed by the compiler and as the name suggests Preprocessors are programs that process our source code before compilation. Therefore the semi-colon is not required.

Q.4 What is the difference between including the header file with-in angular braces < > and double quotes ” “?
Ans. If a header file is included within < > then the compiler searches for the particular header file only within the built-in include path. If a header file is included within ” “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built-in include path.

Q.5 What is the difference between near, far and huge pointers?
Ans. These are some old concepts used in 16 bit Intel architectures in the days of MS DOS, not much useful anymore.
Near pointer is used to store 16 bit addresses means within current segment on a 16 bit machine. The limitation is that we can only access 64kb of data at a time.
A far pointer is typically 32 bit that can access memory outside current segment. To use this, compiler allocates a segment register to store segment address, then another register to store offset within current segment.
Like far pointer, huge pointer is also typically 32 bit and can access outside segment. In case of far pointers, a segment is fixed. In far pointer, the segment part cannot be modified, but in Huge it can be

Near Pointer Far Pointer
It’s size is 2 bytes Its size is 4 bytes
They have the address in between 0-65535(i.e in user area) They have the address more than 65535(i.e out of user area)
For Example:

simple pointers, which we normally studied in C and C++

Pointers which are used in devices, running program, i.e to attack on other computers via this far pointers.

Q.6 Why does “type demotion” does not exist instead of “type promotion”? Also, it would consume less space resource than by doing it from type promotion.?
Ans. Let’s take an example to understand it.
Suppose
double a=1.5; int b=10 and we want to calculate a+b
By type demotion, float type a will convert to int. Therefore a=1 and a+b=1+10=11 but we know that correct answer is 11.5 which will only get by type promotion. So the conclusion is that by type demotion we will not get the correct answer.

Q.7 What are stack and heap areas?

  1. Heap Area:It is used for the objects allocated dynamically (Using malloc() and calloc()).
  2. Stack Area:It is used to store local variables and arguments of a method. This stays in memory only till the termination of that particular method.

Please refer stack vs heap memory for details.

Q.8 Difference between #include in C and import in Java?
Ans.

#include import
#include is a statement not a keyword/td>

While import is a keyword.
It is processed by pre-processor software. It is processed by compiler.
It increases the size of the code. It doesn’t increases the size of the code. Here, even if we write
import java.lang.*;
it will not attach all the class. Rather it will give permission to access the class of java.lang

Q.9 Difference between ++*p, *p++ and *++p?
1) Precedence of prefix ++ and * is same. Associativity of both is right to left.
2) Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to right.


(Refer: Precedence Table)

The expression ++*p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). Therefore the output of first program is “arr[0] = 10, arr[1] = 20, *p = 11“.

The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. Therefore the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.

The expression *++p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p). Therefore the output of third program is “arr[0] = 10, arr[1] = 20, *p = 20

Please refer Difference between ++*p, *p++ and *++p for details.

Q.10 Explain Deep Copy and Shallow Copy with examples?
In the following C program, struct variable st1 contains pointer to dynamically allocated memory. When we assign st1 to st2, str pointer of st2 also start pointing to same memory location. This kind of copying is called Shallow Copy.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate shallow copy
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
  
struct test
{
  char *str;
};
  
int main()
{
  struct test st1, st2;
  st1.str = (char *)malloc(sizeof(char) * 20);
  
  strcpy(st1.str, "GeeksforGeeks");
  
  st2 = st1;
  st1.str[0] = 'X';
  st1.str[1] = 'Y';
  
  /* Since copy was shallow, both strings are same */
  printf("st1's str = %s\n", st1.str); 
  printf("st2's str = %s\n", st2.str);
  
  return 0;
}

chevron_right


Output:

st1's str = XYeksforGeeks
st2's str = XYeksforGeeks

To do Deep Copy, we allocate new memory for dynamically allocated members and explicitly copy them.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate deep copy
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
  
struct test
{
  char *str;
};
  
int main()
{
  struct test st1, st2;
  st1.str = (char *)malloc(sizeof(char) * 20);
  
  strcpy(st1.str, "GeeksforGeeks");
  
  st2 = st1;
  
  // We add extra statements to do deep copy
  st2.str = (char *)malloc(sizeof(char) * 20);
  strcpy(st2.str, st1.str);
  
  st1.str[0] = 'X';
  st1.str[1] = 'Y';
  
  /* Since copy was deep, both strings are different */
  printf("st1's str = %s\n", st1.str); 
  printf("st2's str = %s\n", st2.str);
  
  return 0;
}

chevron_right


Output:

st1's str = XYeksforGeeks
st2's str = GeeksforGeeks

Related Articles:

1. https://www.geeksforgeeks.org/commonly-asked-c-programming-interview-questions-set-1/
2. https://www.geeksforgeeks.org/commonly-asked-c-programming-interview-questions-set-2/



My Personal Notes arrow_drop_up

Hello friends, I have a deep knowledge on programming language like C,C++,Java,especially in C and Java I like to do comparative study and likes to ask question ie if , why and why not we In future, i want to be an entrepreneur and really focus on how to improve Education System

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : talmoramit, srinam