Commonly Asked C PROGRAMMING Interview Questions and Answers for Experienced and Freshers

By HH Team | Views: 303

 

Dear readers, these C PROGRAMMING Interview Questions have been outlined specially to get you familiar with the nature of questions you'll experience during your interview for the subject of C PROGRAMMING. As per my experience, great interviewers barely plan to ask any specific question during your interview, normally questions start with some essential concept of the subject, and afterward, they proceed based on advance discussion and what you reply.

Q.1.) What is the maximum length of an identifier?
A.1.) Ideally, it is 32 characters and also implementation-dependent.

Q.2.) What is the default function call method?
A.2.) By default, the functions are called by value.

Q.3.) Functions must and should be declared. Comment on this.
A.3.) A function declaration is optional if the same is invoked after its definition.

Q.4.) When the macros get expanded?
A.4.) At the time of preprocessing.

Q.5.) Can a function return multiple values to the caller using the return reserved word?
A.5.) No, only one value can be returned to the caller.

Q.6.) What is a constant pointer?
A.6.) A pointer that is not allowed to be altered to hold another address after it is holding one.

Q.7.) To make the pointer generic for which date type it needs to be declared?
A.7.) Void

Q.8.) Can the structure variable be initialized as soon as it is declared?
A.8.) Yes, w.r.t the order of structure elements only.

Q.9.) Is there a way to compare two structure variables?
A.9.) There is no such. We need to compare element by element of the structure variables.

Q.10.) Which built-in library function can be used to match a pattern from the string?
A.10.) strstr()

Q.11.) What is the difference between far and near pointers?
A.11.) In the first place, they are non-standard keywords. A near pointer can access only 2^15 memory space and a far pointer can access 2^32 memory space. Both the keywords are implementation-specific and are non-standard.

Q.12.) Can we nest comments in a C code?
A.12.) No, we cannot.

Q.13.) Which control loop is recommended if you have to execute a set of statements a fixed number of times?
A.13.) for – Loop.

Q.14.) Can we use just the tag name of structures to declare the variables for the same?
A.14.) No, we need to use both the keyword ‘struct’ and the tag name.

Q.15.) Can the main() function left empty?
A.15.) Yes, possibly the program doing nothing.

Q.16.) Can one function call another?
A.16.) Yes, any user-defined function can call any function.

Q.17.) Apart from Dennis Ritchie who the other person who contributed to the design of the C language.
A.17.) Brain Kernighan

Q.18.) What is a constant?
A.18.) A value that cannot be modified is called so. Such variables are qualified with the keyword const.

Q.19.) What is a pointer on pointer?
A.19.) It’s a pointer variable that can hold the address of another pointer variable. It de-refers twice to point to the data held by the designated pointer variable.

Eg: int x = 5, *p=&x, **q=&p;
Therefore ‘x’ can be accessed by **q.

Q.20.) Distinguish between malloc() & calloc() memory allocation.
A.20.) Both allocate memory from heap area/dynamic memory. By default, calloc fills the allocated memory with 0’s.

--------------------------------------------------------------------------------------------------------------------------------------------

Practice more questions

--------------------------------------------------------------------------------------------------------------------------------------------

Q.21.) What is keyword auto for?
A.21.) By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables.

void f() {
   int i;
   auto int j;
}

NOTE − A global variable can’t be an automatic variable.

Q.22.) What are the valid places for the keyword break to appear?
A.22.) A break can appear only within the looping control and switch statement. The purpose of the break is to bring the control out from the said blocks.

Q.23.) Explain the syntax for for loop.
A.23.) for(expression-1;expression-2;expression-3) {
   //set of statements
}

When control reaches for expression-1 is executed first. Then following expression-2, and if expression-2 evaluates to non-zero ‘set of statements’ and expression-3 is executed, follows expression-2.

Q.24.) What is the difference between including the header file with-in angular braces < > and double quotes “ “
A.24.) 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.25.) What is a static variable?
A.25.) A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.

void f() { 
   static int i; 
   ++i; 
   printf(“%d “,i); 
}

If a global variable is static then its visibility is limited to the same source code.

Q.26.) What is a NULL pointer?
A.26.) A pointer pointing to nothing is called so. Eg: char *p=NULL;

Q.27.) What is the purpose of the extern storage specifier?
A.27.) Used to resolve the scope of a global symbol.
Eg:  
main() {
   extern int i;
   Printf(“%d”,i);
}

int i = 20;

Q.28.) Explain the purpose of the function sprintf().
A.28.) Prints the formatted output onto the character array.

Q.29.) What is the meaning of the base address of the array?
A.29.) The starting address of the array is called the base address of the array.

Q.30.) When should we use the register storage specifier?
A.30.) If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the lookup of the variable.

Q.31.) S++ or S = S+1, which can be recommended to increment the value by 1 and why?
A.31.) S++, as it is single machine instruction (INC) internally.

Q.32.) What is a dangling pointer?
A.32.) A pointer initially holding a valid address, but later the held address is released or freed. Then such a pointer is called a dangling pointer.

Q.33.) What is the purpose of the keyword typedef?
A.33.) It is used to alias the existing type. Also used to simplify the complex declaration of the type.

Q.34.) What are lvalue and rvalue?
A.34.) The expression appearing on the right side of the assignment operator is called an rvalue. Rvalue is assigned to lvalue, which appears on the left side of the assignment operator. The lvalue should designate to a variable not a constant.

Q.35.) What is the difference between actual and formal parameters?
A.35.) The parameters sent to the function at the calling end are called actual parameters while at the receiving of the function definition called formal parameters.

Q.36.) Can a program be compiled without the main() function?
A.36.) Yes, it can be but cannot be executed, as the execution requires a main() function definition.

Q.37.) What is the advantage of declaring void pointers?
A.37.) When we do not know what type of memory address the pointer variable is going to hold, then we declare a void pointer for such.

Q.38.) Where an automatic variable is stored?
A.38.) Every local variable by default being an auto variable is stored in stack memory.

Q.39.) What is a nested structure?
A.39.) A structure containing an element of another structure as its member is referred to so.

Q.40.) What is the difference between variable declaration and variable definition?
A.40.) Declaration associates type to the variable whereas definition gives the value to the variable.

Q.41.) What is a self-referential structure?
A.41.) A structure containing the same structure pointer variable as its element is called a self-referential structure.

Q.42.) Does a built-in header file contain a built-in function definition?
A.42.) No, the header file only declares function. The definition is in the library which is linked by the linker.

Q.43.) Explain modular programming.
A.43.) Dividing the program into sub-programs (modules/function) to achieve the given task is a modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions.

Q.44.) What is a token?
A.44.) A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.

Q.45.) What is a preprocessor?
A.45.) The preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

Q.46.) Explain the use of %i format specifier w.r.t scanf().
A.46.) Can be used to input integer in all the supported format.

Q.47.) How can you print a \ (backslash) using any of the printf() family of functions?
A.47.) Escape it using \ (backslash).

Q.48.) Does a break is required by default case in the switch statement?
A.48.) Yes, if it is not appearing as the last case and if we do not want the control to flow to the following case after default if any.

Q.49.) When to user -> (arrow) operator.
A.49.) If the structure/union variable is a pointer variable, to access structure/union elements the arrow operator is used.

Q.50.) What are bit fields?
A.50.) We can create integer structure members of differing sizes apart from non-standard size using bit fields. Such structure size is automatically adjusted with the multiple integer size of the machine.

Q.51.) What are command-line arguments?
A.51.) The arguments which we pass to the main() function while executing the program are called command-line arguments. The parameters are always strings held in the second argument (below in args) of the function which is an array of character pointers. The first argument represents the count of arguments (below in count) and updated automatically by the operating system.

main( int count, char *args[]) {
}

Q.52.) What are the different ways of passing parameters to the functions? Which to use when?
A.52.) Call by value − We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.
Call by reference − We send the address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.

Q.53.) What is the purpose of the built-in stricmp() function?
A.53.) It compares two strings by ignoring the case.

Q.54.) Describe the file opening mode “w+”.
A.54.) Opens a file both for reading and writing. If a file is not existing it creates one, else if the file is existing it will be over-written.

Q.55.) Where the address-of operator (&) cannot be used?
A.55.) It cannot be used on constants.
It cannot be used on a variable that is declared using the register storage class.

Q.56.) Is FILE a built-in data type?
A.56.) No, it is a structure defined in stdio.h.

Q.57.) What is a reminder for 5.0 % 2?
A.57.) Error, It is invalid that either of the operands for the modulus operator (%) is a real number.

Q.58.) How many operators are there under the category of ternary operators?
A.58.) There is only one operator and is a conditional operator (? : ).

Q.59.) Which keyword is used to perform unconditional branching?
A.59.) goto

Q.60.) What is a pointer to a function? Give the general syntax for the same.
A.60.) A pointer holding the reference of the function is called a pointer to a function. In general, it is declared as follows.

T (*fun_ptr) (T1, T2…); Where T is any data type.
Once fun_ptr refers to a function the same can be invoked using the pointer as follows.
fun_ptr();
[Or]
(*fun_ptr)();

Q.61.) Explain the use of comma operator (,).
A.61.) Comma operator can be used to separate two or more expressions.
Eg: printf(“hi”) , printf(“Hello”);

Q.62.) What is a NULL statement?
A.62.) A null statement is no executable statement such as ; (semicolon).
Eg: int count = 0; 
while( ++count<=10 ) ;
Above does nothing 10 times.

Q.63.) What is a static function?
A.63.) A function’s definition prefixed with a static keyword is called a static function. You would make a function static if it should be called only within the same source code.

Q.64.) Which compiler switches to be used for compiling the programs using the math library with GCC compiler?
A.64.) Option –lm to be used as > gcc –lm <file.c>

Q.65.) Which operator is used to continue the definition of macro in the next line?
A.65.) Backward slash (\) is used.

E.g. #define MESSAGE "Hi, \
Welcome to C"

Q.66.) Which operator is used to receive the variable number of arguments for a function?
A.66.) Ellipses (…) are used for the same. A general function definition looks as follows
void f(int k,…)  {
}

Q.67.) What is the problem with the following coding snippet?
A.67.) char *s1 = "hello",*s2 = "welcome";
strcat(s1,s2);
s1 points to a string constant and cannot be altered.

Q.68.) Which built-in library function can be used to re-size the allocated dynamic memory?
A.68.) realloc().

Q.69.) Define an array.
A.69.) An array is a collection of similar data items under a common name.

Q.70.) What are enumerations?
A.70.) Enumerations are a list of integer constants with names. Enumerators are defined with the keyword enum.

Q.71.) Which built-in function can be used to move the file pointer internally?
A.71.) fseek()

Q.72.) What is a variable?
A.72.) A variable is the name storage.

Q.73.) Who designed the C programming language?
A.73.) Dennis M Ritchie.

Q.74.) C is the successor of which programming language?
A.74.) B

Q.75.) What is the full form of ANSI?
A.75.) American National Standards Institute.

Q.76.) Which operator can be used to determine the size of a data type or variable?
A.76.) sizeof

Q.77.) Can we assign a float variable to a long integer variable?
A.77.) Yes, with loss of fractional part.

Q.78.) Is 068 a valid octal number?
A.78.) No, it contains invalid octal digits.

Q.79.) What is the return value of a relational operator if it returns any?
A.79.) Return a value 1 if the relation between the expressions is true, else 0.

Q.80.) How does bitwise operator XOR work?
A.80.) If both the corresponding bits are the same it gives 0 else 1.

Q.81.) What is an infinite loop?
A.81.) A loop executing repeatedly as the loop-expression always evaluates to true such as
while(0 == 0) {
}

Q.82.) Can variables belong to the different scopes have the same name? If so show an example.
A.82.) Variables belonging to different scope can have the same name as in the following code snippet.

int var;
void f() { 
   int var; 
}

main() { 
   int var; 
}

Q.83.) What is the default value of local and global variables?
A.83.) Local variables get garbage value and global variables get a value 0 by default.

Q.84.) Can a pointer access the array?
A.84.) Pointer by holding the array’s base address can access the array.

Q.85.) What are valid operations on pointers?
A.85.) The only two permitted operations on pointers are
Comparision ii) Addition/Subtraction (excluding void pointers)

Q.86.) What is a string length?
A.86.) It is the count of characters excluding the ‘\0’ character.

Q.87.) What is the built-in function to append one string to another?
A.87.) strcat() form the header string.h

Q.88.) Which operator can be used to access union elements if the union variable is a pointer variable?
A.88.) Arrow (->) operator.

Q.89.) Explain about ‘stdin’.
A.89.) stdin in a pointer variable which is by default opened for the standard input device.

Q.90.) Name a function that can be used to close the file stream.
A.90.) fclose().

Q.91.) Define a structure.
A.91.) A structure can be defined as a collection of heterogeneous data items.

Q.92.) Name the predefined macro which is used to determine whether your compiler is ANSI standard or not?
A.92.) __STDC__

Q.93.) What is typecasting?
A.93.) Typecasting is a way to convert a variable/constant from one type to another type.

Q.94.) What is recursion?
A.94.) Function calling itself is called recursion.

Q.95.) Which function can be used to release the dynamically allocated memory?
A.95.) free().

Q.96.) What is the first string in the argument vector w.r.t command-line arguments?
A.96.) Program name.

Q.97.) How can we determine whether a file is successfully opened or not using fopen() function?
A.97.) On failure fopen() returns NULL, otherwise opened successfully.

Q.98.) What is the output file generated by the linker
A.98.) The linker generates the executable file.

Q.99.) What is the purpose of #undef preprocessor?
A.99.) It is used to undefine an existing macro definition.

Q.100.) How a negative integer is stored.
A.100.) Get the two’s complement of the same positive integer. Eg: 1011 (-5)

Step-1 − One’s complement of 5: 1010
Step-2 − Add 1 to above, giving 1011, which is -5

 

Also Read:

Thank you for your feedback!