Interview Level QA On C
Interview Level QA On C
o In case of macros, the corresponding code is inserted directly into your source code
at the point where macro is called. There is no overhead involved in using a
macro.This makes the Macros more efficient and faster than functions. However,
macros are usually small and cannot handle large, complex coding constructs. So, if
it is a complex situation that the program wants to handle, functions are more
suitable.
o Macros are expanded inline - this means that every time a macro occurs, the code is
replicated. So, the code size in case of usage of macros will be larger in comparison
to functions.
So, the choice of using macros or functions actually depends on your requirement - speed vs
program size.
If your program contains small, repeated code sections, you should use Macros. If the program
requires, large number of unique code lines - you should prefer functions.
How does placing some code lines between the comment symbol help in debugging the code?
- Placing comment symbols /* */ around a code isolates some code that the coder believes
might be causing the errors in the program, without deleting it.
- If the code is correct, you can just remove the comment symbols, without needing to retype
it.
- If it is wrong, you can just remove it.
Are comments included during the compilation stage and placed in the EXE file as well?
- No, comments encountered by the compiler are disregarded.
- Their only purpose is guidance and ease of programming. They have no effect on the
functionality of the program.
Situation - The 'sizeof' operator reported a larger size than the calculated size for a structure type.
What could be the reason?
- The 'sizeof' operator shows the amount of storage needed to store an object of the specified
type of operand.
- The result of applying it to a structure type name is the number of bytes including internal
and trailing padding.
- This may include internal leading & trailing padding used for the alignment of structure on
memory boundaries. This may cause the error.
- When you try to enter a value using "stdin", the system will try to store the value at location
with address "i". "i" might be invalid leading the program to crash and core dump.
- It implies that this code has a bug.
What are the advantages of using linked list for tree construction?
- Unless the memory is full, overflow will never occur.
- Insertions & deletions are easier in comparison to arrays.
- Moving pointer is faster & easier in comparison to moving items when records are large.
- a.) Queue –
o Queue is a FIFO or LIFO data structure.
o It permits two operations - enqueue & dequeue.
o The methods used to check the status of the queue are - empty() and full()
- b.) B+ tree -
o Consists of root, internal nodes and leaves.
o Root may be a leaf or internal node with two or more children.
o For a B+ tree of order v, internal nodes contain between v and 2v keys. A node with
'k' keys has 'k+1' children.
o Leaves exist on same level. They are the only nodes with data pointers.
- The no. of CPU registers is dependent on its architectural design. Mostly this number is 32.
Define recursion in C.
- A programming technique in which a function may call itself. Recursive programming is
especially well-suited to parsing nested markup structures
- The persistence of register variables in CPU register is to optimize the access. Even the
optimization is turned off; the register variables are forced to store in CPU register.
Bitwise Left-Shift is useful when to want to MULTIPLY an integer (not floating point numbers) by a
power of 2.
Expression: a << b
Bitwise Right-Shift does the opposite, and takes away bits on the right.
Expression: a >> b
This expression returns the value of a divided by 2 to the power of b.
...
- This way there is a surety that the function is defined in the source file and working with the
same declarations as it is mentioned in the above files. As it can be seen in here the
declaration is done twice. And the second declaration also consisted of the function as well
as the definition. The linkage provides a way for the functions to be used in the same scope.
a. No linkage: defines the linkage that is having internal functionality and internal functions
with its arguments and variables internal to the application itself.
b. External linkage: external linkage defines the linkage that is located externally of the
program. It is considered as the default linkage for the functions and other parameters that
are defined outside the scope of the program. All the instances are referred as the same
object if they are preceded with the external linkage. For example printf() is declared
externally in as int printf(const char *, …) this is the function that returns an integer.
c. Internal linkage: deals with the names that are internal to the files or the same objects
within the same file. This allows the user to define the linkage internally without shifting to
many other files for the references. The internal linkage can be done by prefixing the
keyword static in front of the object name.
// this is the second file that includes the first file externally
extern int i;
void f ()
{
// Write your own code here
i++;
}
- pragma-string passes the instructions to the compiler with some of the required parameters.
The parameters are as follows:
- Copyright- it specifies the copyright string.
- Copyright_date- it specifies the copyright date for the copyright string. Some version control
can be given as parameter.
- If the expression is true then the result will be expression1. If the expression is false then the
result will be expression2. There is only one evaluation take place for the result. The
expression that is used with arithmetic operators are easy to solve and it is easy to convert
and apply to the expressions.
b) If any of the operand is a null pointer constant then result will be considered of the operand. The
example is shown below:
#include <stdio.h>
#include <stdlib.h>
main(){
int i;
for(i=0; i <= 10; i++){
printf((i&1) ? "odd\n" : "even\n");
}
exit(EXIT_SUCCESS);
- Null pointer: null pointer is just used as a regular pointer that consists of any pointer type
with some special value. It doesn't point to any valid reference or memory address. It is just
used for easy to make a pointer free. The result is the result of the type-casting where the
integer value can have any pointer type.
- The null pointer can be written either by having an integral constant with a value of 0 or this
value can be converted to void* type. This can be done by using the cast function. For
assigning any pointer type to any other pointer type then it first gets converted to other
pointer type and then will appear in the program. The code that is being written for null
pointer is:
int *ip;
ip = (int *)6;
*ip = 0xFF;
- int(a);
- int(char c);
- void(int,float);
- There should not be any passing from one data type to another data type. There is also
invalid declaration on the basis of:
- There should not be any blank spaces in between the two periods. The placing of any other
data type can be done in place of ellipsis.
- Wild pointer is used to point to the object but it is not in initialization phase. Null pointer
points to the base address of a particular segment. Wild pointer consists of the addresses
that are out of scope whereas null pointer consists of the addresses that are accessible and
in the scope. Wild pointer is declared and used in local scope of the segment whereas null is
used in the full scope.
- Defining a variable - means declaring it and reserving a place for it in the memory.
- Defining a variable = Declaration + Space reservation.
Where are auto variables stored? What are the characteristics of an auto variable?
- Auto variables are defined under automatic storage class. They are stored in main memory.
o Main memory
o CPU registers
- Memory is allocated to an automatic variable when the block containing it is called. When
the block execution is completed, it is de-allocated.
- The namespace feature surrounds a library's external declaration with a unique namespace
that eliminates the potential for those collisions.
What do the ‘c’ and ‘v’ in argc and argv stand for?
- The c in argument count, argc stands for the number of the command line argument that
the program is invoked with.
- And v in argument vector, argv is a pointer to an array of the character string that contains
the argument.
What is an lvalue?
- lvalue - an expression to which a value can be assigned.
- It is located on the left side of an assignment statement
- lvalue expression must reference a storable variable in memory.
- It is not a constant.
a.) Function
- When a large program is divided into smaller subprograms - each subprogram specifying the
actions to be performed - these subprograms are called functions.
- Function supports only static and extern storage classes.
b.) Built-in function
- Predefined functions supplied along with the compiler.
- They are also called library functions.
- They allow to pass values to functions using call by reference - especially useful while large
sized arrays are passed as arguments to functions.
- They allow dynamic allocation of memory.
- They help in resizing the data structures.
o Macro can be included with the conditional statements using the external else
clause. You can include simple expressions in macro with no declarations or loops
like:
o #define FUNC(arg1, arg2) (expr1, expr2, expr3)
Which one to choose from "initialization lists" or "assignment", for the use in the constructor?
- Initialization list consists of all the member objects, which a constructor initializes. By doing
this, performance of the execution increases and compiler does faster processing in the case
of initialization list. This is better than the assignment as it is an inefficient way to represent
the objects and the performance also degrades, when it gets used. The code runs faster by
using the initialization list rather than the assignment.