C Interview Questions: and Answers
C Interview Questions: and Answers
And Answers
Vwers
C Interview Questions and Answers
What is C language?
The C programming language is a standardized programming language developed in
the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It
has since spread to many other operating systems, and is one of the most widely used
programming languages. C is prized for its efficiency, and is the most popular programming
language for writing system software, though it is also used for writing applications.
printf() Function
What is the output of printf("%d")?
1. When we write printf("%d",x); this means compiler will print the value of x. But as here,
there is nothing after %d so compiler will show in output window garbage value.
2. When we use %d the compiler internally uses it to access the argument in the stack
(argument stack). Ideally compiler determines the offset of the data variable depending on
the format specification string. Now when we write printf("%d",a) then compiler first accesses
the top most element in the argument stack of the printf which is %d and depending on the
format string it calculated to offset to the actual data variable in the memory which is to be
printed. Now when only %d will be present in the printf then compiler will calculate the correct
offset (which will be the offset to access the integer variable) but as the actual data object is
to be printed is not present at that memory location so it will print what ever will be the
contents of that memory location.
3. Some compilers check the format string and will generate an error without the proper
number and type of arguments for things like printf(...) and scanf(...).
Linked Lists -- Can you tell me how to check whether a linked list is circular?
Create two pointers, and set both to the start of the list. Update each as follows:
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular");
}}
If a list is circular, at some point pointer2 will wrap around and be either at the item just
before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.
"union" Data Type What is the output of the following program? Why?
#include
main() {
typedef union {
int a;
char b[10];
float c;
}
Union;
Union x,y = {100};
x.a = 50;
strcpy(x.b,"hello");
x.c = 21.50;
printf("Union x : %d %s %f n",x.a,x.b,x.c);
printf("Union y : %d %s %f n",y.a,y.b,y.c);
}
* the multibyte character sequence, to which we generally call string, is used to initialize an
array of static storage duration. The size of this array is just sufficient to contain these
characters plus the terminating
NUL character.
* it not specified what happens if this array, i.e., string, is modified.
* Two strings of same value[1] may share same memory area. For
example, in the following declarations:
char *s1 = “Calvin and Hobbes”;
char *s2 = “Calvin and Hobbes”;
the strings pointed by s1 and s2 may reside in the same memory
location. But, it is not true for the following:
char ca1[] = “Calvin and Hobbes”;
char ca2[] = “Calvin and Hobbes”;
[1] The value of a string is the sequence of the values of the contained characters, in order.
What is hashing?
To hash means to grind up, and that’s essentially what hashing is all about. The heart of
a hashing algorithm is a hash function that takes your nice, neat data and grinds it into some
random-looking integer.
The idea behind hashing is that some data either has no inherent ordering (such as images) or
is expensive to compare (such as images). If the data has no inherent ordering, you can’t
perform comparison searches.
If the data is expensive to compare, the number of comparisons used even by a binary
search might be too many. So instead of looking at the data themselves, you’ll condense
(hash) the data to an integer (its hash value) and keep all the data with the same hash value
in the same place. This task is carried out by using the hash value as an index into an array.
To search for an item, you simply hash it and look at all the data whose hash values match
that of the data you’re looking for. This technique greatly lessens the number of items you
have to look at. If the parameters are set up with care and enough storage is available for the
hash table, the number of comparisons needed to find an item can be made arbitrarily close
to one. One aspect that affects the efficiency of a hashing implementation is the hash function
itself. It should ideally distribute data randomly throughout the entire hash table, to reduce
the likelihood of collisions. Collisions occur when two different keys have the same hash value.
There are two ways to resolve this problem. In open addressing, the collision is resolved by
the choosing of another position in the hash table for the element inserted later. When the
hash table is searched, if the entry is not found at its hashed position in the table, the search
continues checking until either the element is found or an empty position in the table is found.
The second method of resolving a hash collision is called chaining. In this method, a bucket or
linked list holds all the elements whose keys hash to the same value. When the hash table is
searched, the list must be searched linearly.
When does the compiler not implicitly generate the address of the first element of
an array?
Whenever an array name appears in an expression such as
- array as an operand of the sizeof operator
- array as an operand of & operator
- array as a string literal initializer for a character array
Then the compiler does not implicitly generate the address of the address of the first element
of an array.
What is a method?
Method is a way of doing something, especially a systematic way; implies an orderly
logical arrangement (usually in steps).
What is indirection?
If you declare a variable, its name is a direct reference to its value. If you have a pointer
to a variable, or any other object in memory, you have an indirect reference to its value.
What is an lvalue?
An lvalue is an expression to which a value can be assigned. The lvalue expression is
located on the left side of an assignment statement, whereas an rvalue is located on the right
side of an assignment
statement. Each assignment statement must have an lvalue and an rvalue. The lvalue
expression must reference a storable variable in memory. It cannot be a constant.
What is Polymorphism ?
'Polymorphism' is an object oriented term. Polymorphism may be defined as the ability
of related objects to respond to the same message with different, but appropriate actions. In
other words, polymorphism means taking more than one form. Polymorphism leads to two
important aspects in Object Oriented terminology – Function Overloading and Function
Overriding. Overloading is the practice of supplying more than one definition for a given
function name in the same scope. The compiler is left to pick the appropriate version of the
function or operator based on the arguments with which it is called. Overriding refers to the
modifications made in the sub class to the inherited methods from the base class to change
their behavior.