Thursday, May 31, 2012

Some C Interview Questions

Some recent set of questions I found from web, found them unique and interesting. 

The sizeof( ) function doesn’t return the size of the block of memory pointed to by a pointer. Why?
Answer:The sizeof( ) operator does not know that malloc( ) has been used to allocate a pointer. sizeof( ) gives us the size of pointer itself. There is no handy way to find out the size of a block allocated by malloc( ).

How do I write a program to convert a string containing number in a hexadecimal form to its equivalent decimal?
Answer: The following program demonstrates this:
main( )
char str[] = "0AB" ;
int h, hex, i, n ;
n = 0 ; h = 1 ;
for ( i = 0 ; h == 1 ; i++ )
if ( str[i] >= '0' && str[i] <= '9' )
hex = str[i] - '0' ;
if ( str[i] >= 'a' && str[i] <= 'f' )
hex = str[i] - 'a' + 10 ;
if ( str[i] >= 'A' && str[i] <= 'F' )
hex = str[i] - 'A' + 10 ;
h = 0 ;
if ( h == 1 )
n = 16 * n + hex ;
printf ( "\nThe decimal equivalent of %s is %d",
str, n ) ;
The output of this program would be the decimal equivalent of 0AB is 171.

What will be the output of the following code?
void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf (“%d",a[i]) ;
Answer: The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.

How do I know how many elements an array can hold?
Answer: The amount of memory an array can consume depends on the data type of an array. In DOS environment, the amount of memory an array can consume depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume more than 64 kb. Consider following program, which shows the maximum number of elements an array of type int, float and char can have in case of Small memory model.
main( )
int i[32767] ;
float f[16383] ;
char s[65535] ;

Are the following two statements identical?
char str[6] = "Kicit" ;
char *str = "Kicit" ;
Answer: No! Arrays are not pointers. An array is a single, pre-allocated chunk of contiguous elements (all of the same type), fixed in size and location. A pointer on the other hand, is a reference to any data element (of a particular type) located anywhere. A pointer must be assigned to point to space allocated elsewhere, but it can be reassigned any time. The array declaration char str[6] ; requests that space for 6 characters be set aside, to be known by name str. In other words there is a location named str at which six characters are stored. The pointer declaration char *str ; on the other hand, requests a place that holds a pointer, to be known by the name str. This pointer can point almost anywhere to any char, to any contiguous array of chars, or nowhere.

Is the following code fragment correct?
const int x = 10 ;
int arr[x] ;
Answer: No! Here, the variable x is first declared as an int so memory is reserved for it. Then it is qualified by a const qualifier. Hence, const qualified object is not a constant fully. It is an object with read only attribute, and in C, an object associated with memory cannot be used in array dimensions.

How does free( ) know how many bytes to free?
Answer: The malloc( ) / free( ) implementation remembers the size of each block allocated and returned, so it is not necessary to remind it of the size when freeing.

What is a stack ?
Answer: The stack is a region of memory within which our programs temporarily store data as they execute. For example, when a program passes parameters to functions, C places the parameters on the stack. When the function completes, C removes the items from the stack. Similarly, when a function declares local variables, C stores the variable's values on the stack during the function's execution. Depending on the program's use of functions and parameters, the amount of stack space that a program requires will differ.

What's the difference between these two declarations?
struct str1 { ... } ;
typedef struct { ... } str2 ;
A : The first form declares a structure tag whereas the second declares a typedef. The main difference is that the second declaration is of a slightly more abstract type -- its users don't necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it.

How does a C program come to know about command line arguments?
A: When we execute our C program, operating system loads the program into memory. In case of DOS, it first loads 256 bytes into memory, called program segment prefix. This contains file tables,environment segment, and command line information. When we compile the C program the compiler inserts additional code that parses the command, assigning it to the argv array, making the arguments easily accessible within our C program.

When we open a file, how does functions like fread( )/fwrite( ), etc. get to know from where to read or to write the data?
A: When we open a file for read/write operation using function like fopen( ), it returns a pointer to the structure of type FILE. This structure stores the file pointer called position pointer, which keeps track of current location within the file. On opening file for read/write operation, the file pointer is set to the start of the file. Each time we read/write a character, the position pointer advances one character. If we read one line of text at a step from the file, then file pointer advances to the start of the next line. If the file is opened in append mode, the file pointer is placed at the very end of the file. Using fseek( ) function we can set the file pointer to some other place within the file

Why doesn't the following code give the desired result? 
int x = 3000, y = 2000 ;
long int z = x * y ;
Answer: Here the multiplication is carried out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. However, to get the correct output, we should use an explicit cast to force long arithmetic as shown below: 
long int z = ( long int ) x * y ;
Note that ( long int )( x * y ) would not give the desired effect.

Why doesn't the following statement work?
char str[ ] = "Hello" ; 
strcat ( str, '!' ) ;
Answer: The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character. To make the above statement work writes the statement as shown below:
strcat ( str, "!" ) ;

What is the difference between "calloc(...)" and "malloc(...)"?
1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size). 
malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like calloc(...).

2.malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available.
calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to set the new handler mode.

Difference between const char* p and char const* p?
Answer:In const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location. in char const* p, the ptr ‘p’ is constant not the character referenced by it, so u cant make ‘p’ to reference to any other location but u can change the value of the char pointed by ‘p’.

How can you determine the size of an allocated portion of memory?
Answer:You can’t, really. free() can , but there’s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there’s no guarantee the trick won’t change with the next release of the compiler.

Can static variables be declared within a header file? 
A:you can not declare a static variable without defining it as well (this is really because the actual storage class modifiers static and also extern are mutually exclusive). A static variable could be defined in a header file, but this would cause each source file that included the header file to get its very own private copy from the variable, which is most likely not what was meant

Can a variable become both constant and volatile?
A:Yes. The const modifier implies that this particular program code cannot change the value of the actual variable, but that will not imply that the value can not be changed by means outside this code. For instance, within the example in frequently asked questions, the timer structure was accessed through a volatile const pointer.The function by itself did not change the value of the timer, so it had been declared const. However, the value had been changed by hardware on the computer, so it was declared volatile. If a variable is both equally const and volatile, the two modifiers can come in either order.

No comments:

Post a Comment