INTERVIEW BASED THEORY QUESTIONS FOR C
What are different storage class specifiers in C?
Ans: auto, register, static, extern
What is scope of a variable? How are variables scoped in C?
Ans: Scope of a variable is the part of the program
where the variable may directly be accessible. In C, all identifiers are
lexically (or statically) scoped. See this for more details..
When should we use pointers in a C program?
1. To get address of a variable
2. For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.
3. To pass large structures so that complete copy of the structure can be avoided.
4. To implement “linked” data structures like linked lists and binary trees.
What is NULL pointer?
Ans: NULL is used to indicate that the pointer doesn’t
point to a valid location. Ideally, we should initialize pointers as
NULL if we don’t know their value at the time of declaration. Also, we
should make a pointer NULL when memory pointed by it is deallocated in
the middle of a program.
Ans: Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.
Ans: Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
Ans:A local static variable is a variable whose lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value as 0.
Ans:In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files. See this for examples and more details.
C is a procedural language. The main features of C language include low-level access to memory, simple set of keywords, and clean style. These features make it suitable for system programming like operating system or compiler development.
What is difference between i++ and ++i?
1) The expression ‘i++’ returns the old value and then increments i. The
expression ++i increments the value and returns new value.
2) Precedence of postfix ++ is higher than that of prefix ++.
3) Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left.
4) In C++, ++i can be used as l-value, but i++ cannot be. In C, they both cannot be used as l-value.
See Difference between ++*p, *p++ and *++p for more details.
What is l-value?
l-value or location value refers to an expression that can be used on
left side of assignment operator. For example in expression “a = 3”, a
is l-value and 3 is r-value.
l-values are of two types:
“nonmodifiable l-value” represent a l-value that can not be modified. const variables are “nonmodifiable l-value”.
“modifiable l-value” represent a l-value that can be modified.3
Write down the smallest executable code?
Ans. main is necessary for executing the code. Code is
void main()
{
}
What are entry control and exit control loops?
Ans. C support only 2 loops
- Entry Control: This loop is categorized in 2 part
a. while loop
b. for loop - Exit control: In this category, there is one type of loop known as
a. do while loop.
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.
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.
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. |
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.
What are stack and heap areas?
- Heap Area:It is used for the objects allocated dynamically (Using malloc() and calloc()).
- 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.
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 |
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.
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.
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.
Comments
Post a Comment