C Interview Questions

What are different techniques for making hash function?

Techniques for making hash function.
• Truncation Method
This is the simplest method for computing address from a key.In this method we take only a part of the key as address.
• Midsquare Method
In this method the key is squared and some digits from the middle of this square are taken as address.
• Folding Method
In this technique, the key is divided into different part where the length of each part is same as that of the required address, except possibly the last part.
• Division Method (Modulo-Division)
In Modulo-Division method the key is divided by the table size and the remainder is taken as the address of the hash table.
–>Let the table size is n then
H (k) =k mod n

Master C and Data Structure, in this C and Data Structure certification training.

What are the issues that hamper the efficiency in sorting a file?

The issues are:

  • Length of time required by the programmer in coding a particular sorting program.
  • Amount of machine time necessary for running the particular program.
  • The amount of space necessary for the particular program.

What is the use of volatile keyword?

The modifier ‘volatile’ tells the compiler that a variable’s value may be changed in ways not explicitly specified by the program. For example, a global variable’s address may be passed to the operating system’s clock routine and used to hold the system time.
In this situation, the contents of the variable are altered without any explicit assignment statements in the program.

This is important because most C compilers automatically optimize certain expressions by assuming that a variable’s content is unchanging if it does not occur on the left side of an assignment statement. Thus, it may not be reexamined each time it is referenced. Also, some compilers change the order of evaluation of an expression during the compilation process. The volatile modifier prevents these changes.

Write a C program without using semicolon to print ‘Hello world’

void main(){

if(printf(“Hello world”)){

}

}

What are differences between sizeof operator and strlen function?

sizeof is keyword of C that can find size of a String constant including null character, but strlen is function which has been defined string.h and can find number of characters in a string excluding null character.

#include

void main(){

int a,b;

a=strlen(“cquestionbank”);

b=sizeof(“cquestionbank”);

printf(“%d  %d”,a,b);

getch();

}

What is the difference between

sprintf(…) writes data to the character array. The C library function sprintf () is used to store formatted data as a string. You can also say the sprintf () function is used to create strings as output using formatted data. The syntax of the sprintf () function is as follows:

int sprintf (char *string, const char *form, … );

Here, the *string will stand for the name of the array that will store the output obtained by working on the formatted data. The *form parameter will show the format of the output.
printf(…) writes data to the standard output device. The printf function is just a useful function from the standard library of functions that are accessible by C programs.

The behavior of printf is defined in the ANSI standard. If the compiler that you’re using conforms to this standard then all the features and properties should be available to you.

When does the compiler not implicitly generate the address of the first element of an array?

The compiler does not implicitly generate the address of the first element of an array whenever an array name appears:
– as an operand of the sizeof operator
– as an operand of & operator
– as a string literal initialize for a character array

Is using exit() the same as using return?

No, the exit() function is used to exit your program and return() controls the operating system.
The return statement is used to return from a function and return control to the calling function. If you make a return from the main() function, you are essentially returning control(operating system) to the calling function. In this case, the return statement and exit() function are similar.

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 refer a storable variable in memory. It cannot be a constant.

What is the difference between goto, longjmp() and setjmp()?

  • A goto statement implements a local jump of program execution whereas the longjmp() and setjmp() functions implement a nonlocal or far jump of the program execution.
  • Generally, a jump in any execution should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.
  • A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement goto between functions.
    However, when your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.
    There is a major drawback of using these functions: your program, when restored to its previously saved state, it will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be inefficient.
    It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice.

What is XOR linked list?

XOR linked list is a Memory Efficient Doubly Linked List. An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory efficient version of Doubly Linked List can be created using only one space for address field with every node. This memory efficient Doubly Linked List is called XOR Linked List or Memory Efficient as the list uses bitwise XOR operation to save space for one address.
In the XOR linked list, instead of storing actual memory addresses, each node stores the XOR of addresses of previous and next nodes.
XOR List Representation:
Let us call the address variable in XOR representation npx (XOR of next and previous)

Node A:

npx = 0 XOR add(B) // bitwise XOR of zero and address of B

Node B:

npx = add(A) XOR add(C) // bitwise XOR of address of A and address of C

Node C:

npx = add(B) XOR add(D) // bitwise XOR of address of B and address of D

Node D:

npx = add(C) XOR 0 // bitwise XOR of address of C and 0

What is ‘trie’ in data structure?

Trie is efficient information retrieval data structure. Using trie, search complexities can be brought to optimal limit (key length). If we store keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is maximum string length and N is number of keys in tree.
• Using trie, we can search the key in O(M) time. However, the penalty is on trie storage requirements.
• Each node of trie consists of multiple branches. Each branch represents a possible character of keys.
• We need to mark the last node of every key as leaf node.
• A trie node field value will be used to distinguish the node as leaf node (there are other uses of the value field).
• A simple structure to represent nodes of English alphabet can be as follows:

struct trie_node

{

int value; /* Used to mark leaf nodes */

trie_node_t *children[ALPHABET_SIZE];

};

Become Master of C & Data Structure by going through this online training course.

What do you understand by splay tree?

Splay tree is a self-balancing Binary Search Tree (BST). The main idea of splay tree is to bring the recently accessed item to root of the tree. This makes the recently searched item to be accessible in O (1) time if accessed again. The idea is to use locality of reference (In a typical application: 80% of the access are to 20% of the items).
Imagine a situation, where we have millions or billions of keys and only few of them are accessed frequently, which is very likely in many practical applications.
All splay tree operations run in O(log n) time on average, where n is the number of entries in the tree. Any single operation can take Theta(n) time in the worst case.

What is Treap?

Treap is a Balanced Binary Search Tree, but not guaranteed to have height as O(Log n). The idea is to use Randomization and Binary Heap property to maintain balance with high probability. The expected time complexity of search, insert and delete is O(Log n).
Each node of Treap maintains two values.

  1. Key follows standard BST ordering (left is smaller and right is greater)
  2. Priority Randomly assigned value that follows Max-Heap property.
  3. How to implement LRU caching scheme? What data structures should be used?

We are given total possible page numbers that can be referred. We are also given cache (or memory) size (Number of page frames that cache can hold at a time). The LRU caching scheme is to remove the least recently used frame when the cache is full and a new page is referenced which is not there in cache.
We use two data structures to implement an LRU Cache.

  1. A Queue: which is implemented using a doubly linked list. The maximum size of the queue will be equal to the total number of frames available (cache size).
    The most recently used pages will be near front end and least recently pages will be near rear end.
  2. A Hash: with page number as key and address of the corresponding queue node as value. When a page is referenced, the required page may be in the memory. If it is in the memory, we need to detach the node of the list and bring it to the front of the queue.
    If the required page is not in the memory, we bring that in memory. In simple words, we add a new node to the front of the queue and update the corresponding node address in the hash. If the queue is full, i.e. all the frames are full, we remove a node from the rear of queue, and add the new node to the front of queue.
  3. Suppose, there are two linked lists: L1 and L2 (of same lengths) that intersect at a particular node N1, which is a common endpoint to all other nodes. What are the possibilities to find N1?

Linear solution is possible. Have two pointers say P1 pointing to the first node of L1 and P2 to that of L2. Traverse through both the lists. If P1 reaches L1’s last node, point it to the first node of L2 and continue traversing.
Do the same thing for P2 when it reaches L2’s last node. (By doing this, we are balancing the difference in the length between the linked lists. The shorter one will get over soon and by redirecting to longer list’s head, it will traverse the extra nodes also). Finally, they will meet at the Intersection node.

Given two keys K1 & K2, write an algorithm to print all the elements between them with K1<=K2 in a BST.

  • Linear solution is possible without using any extra space.
  • Perform an inorder traversal.
  • Once you find K1, print it and continue traversal now.
  • Print all other traversed elements until you reach K2.

How many stacks are required to implement a Queue.

Two stacks are required to implement a Queue.

  • For Enqueue:Take two stacks S1 and S2 and perform push on S1.
  • For Dequeue:If S2 is empty, pop all the elements from S1 and push it to S2. The last element you popped from S1 is an element to be dequeued. If S2 is not empty, then pop the top element in it.

What are the features of C Langauges?

  •  In C one can write programs like that of high level languages as in COBOL, BASIC, FORTRAN etc. as well as it permits very close interaction with the inner workings of the computer.
  • It is a general purpose programming language. It is usually called system programming language but equally suited to writing a variety of applications.
  •  It supports various data types
  •  It follows the programming style based on fundamental control flow constructions for structured programming 
  • Functions may be predefined or user defined and they may return values of basic types, structures, unions or pointers.

What are the advantages of c language?

  •  Easy to write
  •  Rich set of operators and functions that are built–in
  •  Support for bit–wise operation
  •  Flexible use of pointers
  •  Direct control over the hardware
  •  Ability to access BIOS/DOS routines
  •  Interacting using Interrupts
  •  Ability to write TSR programs
  •  Ability to create .COM files
  •  Ability to create library files (.LIB)
  •  Ability to write interface programs
  •  Incorporating assembly language in C program

What are the disadvantages of c language?

  • C is considered difficult to learn
  •  Because of its conciseness, the code can be difficult to follow
  •  It is not suited to applications that require a lot of report formatting and data file manipulation

What are the salient features of c languages?

 The following are the salient features of C language are :

  •  C is called a middle level language
  •  C supports structured design approach
  •  C is extensible
  •  C is rich in data types and operators
  •  C is portable

What is a header file?

Header files provide the definitions and declarations for the library functions. Thus, each header file contains the library functions along with the necessary definitions and declarations. For example, stdio.h, math.h, stdlib.h, string.h etc.

What is character set?

Character set is the set of characters allowed and supported in the programming language.
Generally a program is a collection of instructions, which contain groups of characters. Only a limited set of characters is allowed to write instructions in the program.

What is C token?

The smallest individual units of a C program are known as tokens.

List the different types of C tokens?

  • Identifiers
  • Keywords
  •  Operators
  • Special symbols
  •  Strings
  • constant

What is a string?

A string is a sequence of characters ending with NUL. It can be treated as a one–dimensional array of characters terminated by a NUL character.

What are qualifiers?

Qualifiers or modifiers are identifiers that may precede the scalar data types (except float) to specify the number of bits used for representing the respective type of data in memory. The qualifiers in C are short, long, signed, and unsigned.

What is a function?

A function is a set of statements to perform a specific task.

What is a constant?

A constant is a value that does not change during the program execution. A constant used in C does not occupy memory.

What are the different types of constants?

There are five types of constants. They are :

  •  Integer constants
  • Floating point constants
  •  Character constants
  •  String literals
  •  Enumeration constants

What is variable?

An identifier is used to identify and store some value. If the value of the identifier is changed during the execution of the program, then the identifier is known as variable.

What are the rules for the identifier?

The first character must be an alphabet or underscore (_)

  •  Digits may be included in the variable
  •  The maximum number of characters in a word are 32 (It may vary depending upon the platform)
  •  No other special characters are allowed.

What are global variables?

Global Variables are those, which are required to be acccessed by all the functions defined after their declaration. So, the variables declared before the main {) can be acccessed by all the functions, which follow their declaration.

What are the different types of c instructions?

  •  There are basically three types of instructions in C are :
  •   Type Declaration Instruction
  •   Arithmetic Instruction
  •   Control Instruction

What is an expression?

Expression is defined as a combination of operands and operators to obtain some computation. Operands represent variables or values and The operator tells is what operation to be performed.

What are the types of data files?

 There are two types of data files :

  •  stream oriented or standard data files
  •  system oriented or low level data files

Why C is called a middle level language?

C combines the features of both Assembly Level Languages (Low Level Languages) and Higher Level Languages. For this reason, C is referred to as a Middle Level Language. The feature of ALLs is that of enabling us to develop system level programs and the features of HLLs are those of higher degree of readability and machine independence.

How can variables be characterized?

The variables can be categorized by storage class as well as by data type. The storage class specifies the portion of the program within which the variables are recognized.

Give the rules for variable declaration?

  •  The rules for variable declaration in C are given below :
  • A variable name consists of alphabets, digits and the underscore (_) character
  •  The length of variable should be kept upto 8 characters though your system may allow upto 40 characters
  •  They must begin with an alphabet
  •  Some systems also recognize an underscore as the first character
  • White space and commas are not allowed
  •  Any reserved word (keyword) cannot be used as a variable name.

What is the purpose of type declarations?

The type declaration allow to create a synonym for other data types. Its syntax is typedef type identifier; The declaration typedef unsigned long int INTEGER.

What is recursion?

C language a function may call another function. When a function calls itself, it is referred to as recursive call and the process is known as recursion. C provides very good facilities for recursion.

What is data types?

Data types refer to the classes of data that can be manipulated by C programs. The three fundamental data types supported by C are character, integer and real type.

What are the types of macro formats?

 There are two types of macro formats. There are

  • Simple
  • Parameterized

What are the different types of errors?

  •  Compile–Time Errors
  •  Linker Errors
  •  Runtime Errors
  •   Logical Errors

What is meant by errors and debugging?

Errors may be made during program creation even by experienced programmers. Such type of errors are detected by the compiler.
Debugging means removing the errors..

What is the purpose of main() function?

  •  The function main() invokes other functions within it.It is the first function to be called when the program starts execution.
  •  It is the starting function.
  •  It returns an int value to the environment that called the program.
  •  Recursive call is allowed for main( ) also.
  •  It is a user-defined function.

What is meant by type casting?

It is the explicit type conversion required for a number before carrying out processing or assigning to another variable.

What are the primitive data types in c?

There are five different kinds of data types in C.

  • Char
  • Int
  • Float
  • Double
  • Void

What is the use of typedef?

The typedef help in easier modification when the programs are ported to another machine. A descriptive new name given to the existing data type may be easier to understand the code.

What is meant by type specifiers?

Type specifiers decide the amount of memory space occupied by a variable. In the ease of integral types; it also explicitly states the range of values that the object can hold..

What are the types of type specifiers?

 The available data type specifiers are :

  •  Short
  •  Long
  •  Signed
  •  Unsigned

What is masking?

Masking is a process in which a given bit pattern is partly extracted into another bit pattern by means of a logical bitwise operation.

What is the difference between single charater constant and string constant?

  •  A single character constant consists of only one character and it is enclosed within a pair of single quotes.
  •  A string constant consists of one or more characters and it is enclosed within a pair of double quotes.

What is signed and unsigned?

A numeric value, may have a positive or a negative sign. In the memory, for a variable, one bit is used exclusively to maintain the sign of the data. If we don’t have sign, the sign bit also may be used for data. If the value is negative, the sign bit is 1, and if it is positive, it will be 0.

What are the different categories of functions in C?

In C, the functions can be divided into the following categories :

  •  Functions with no arguments and no return values
  •  Functions having arguments but no return values
  •  Functions having arguments and return values also

What is this pointer?

It is a pointer that points to the current object. This can be used to access the members of the current object with the help of the arrow operator

What is zero based addressing?

The array subscripts always start at zero. The compiler makes use of subscript values to identify the elements in the array. Since subscripts start at 0, it is said that array uses zerobased addressing.

What is a loop?

A loop is a process to do a job repeatedly with possibly different data each time. The statements executed each time constitute the loop body, and each pass is called iteration. A condition must be present to terminate the loop.

What are the types of data types and explain?

There are five basic Data types in C. These are :

  •  void : means nothing i.e. no data involvement in an action
  •  char : to work with all types of characters used in computer operations
  • int : to work with an integer type of data in any computational work
  • float : to work with the real type of data or scientific numbers in the exponential form
  • double : to work with double precision of numbers when the approximation is very crucial.

What is friend function?

The function declaration should be preceded by the keyword friend.The function definitions does not use either the keyword or the scope operator ::.
The functions that are declared with the keyword friend as friend function.Thus, a friend function is an ordinary function or a member of another class.

What is break statement?

When a break is encountered inside a loop, the loop is terminated and the control passes to the statement following the body of the loop.

What is the use of getchar() function?

It returns a character just entered from the standard input unit, that is, keyboard. The entered character can be either assigned to a character variable or echoed to the computer screen.

What is a pointer?

A pointer is a variable that represents the location (rather than the value) of a data item, such as a variable or an array element. It is a variable that holds a memory address. This address is the location of another variable or an array element in memory.

How to declare pointer variables?

If a variable is going to be a pointer, it must be declared as such. A pointer declaration consists of a base type, an *, and the variable name. The general form for declaring a pointer variable is data _type * var_ name;

What is the difference between fread buffer() and fwrite buffer()?

Fread(), buffer is a pointer to an area of memory that will receive the data from the file. For fwrite(), buffer is a pointer to the information that will be written to the file. The value of count determines how many items are read or written, with each item being num_byte bytes in length.
The size_t in both the formats is defined as some kind of unsigned integer. Finally, fp is a file pointer to a previously opened file.

What is macro?

The second preprocessor function is macro definition. A macro is formal syntax that can be used to generate statements for use in a program. For the C language, the macro generates C statements.

What are the types of I/O functions?

  •  I/O functions are grouped into two categories :
  •  Unformatted I/O functions
  •  Formatted I/O functions

What is the difference b/w formatted&unformatted I/O functions?

The formatted I/O functions allow programmers to specify the type of data and the way in which it should be read in or written out. On the other hand, unformatted I/O functions do not specify the type of data and the way is should be read or written.

How to declare pointer variables?

If a variable is going to be a pointer, it must be declared as such. A pointer declaration consists of a base type, an *, and the variable name. The general form for declaring a pointer variable is data _type * var_ name;

What is the difference between c &c++?

C++ is an object oriented programing but c is a procedure oriented programing.c is super set of c++. c can’t suport inheritance,function overloading, method overloading etc. but c++ can do this.In c-programe the main function could not return a value but in the c++ the main function shuld return a value.

What is the use of putchar function?

The putchar function displays one character on the display monitor. The character to be displayed is of type char. The syntax for putchar function is as given below :putchar (ch_var); Where ch_var is a previously declared character variable.

What is the use of getchar functions?

The getchar function accepts a single character from the keyboard. The function does not require any arguments, though a pair of empty parentheses must follow the word getchar as a syntax. It returns a single character from a standard input device (typically a keyboard ) and it can be assigned to predeclared character variable.

What is character constants?

A character constant is a single character, enclosed within the pair of single quotation mark (apostrophes).

What is string constants?

A string constant or literal contains a sequence of zero or more characters or escape seauences enclosed in double Quotation marks.

What is integer constants?

An integer constant is an integer-valued number. It can represent decimal, octal, or hexadecimal values.

What is floating point constants?

Floating-point constants are numbers with decimal parts. A floating-point constants consists of :

  •  An integral part
  •  A decimal point
  •  A fractional part
  •  An exponent part
  •   An optional suffix

What is the difference between fread and fwrite function?

The fread() function returns the number of items read. This value may be less than count if the end of the file is reached or an error occurs. The fwrite() function returns the number of items written. This value will equal count unless an error occurs.

What are the uses of a pointer?

  •  Pointer is used in the following cases
  •  It is used to access array elements
  •  It is used for dynamic memory allocation
  •  It is used in Call by reference
  •  It is used in data structures like trees, graph, linked list etc.

What are linker error?

The Linker Errors occur during the linking process when the external symbols referred to by the program are not resolved.

What are runtime error?

The Runtime Errors occur while a program is being run and hence the name. They occur due to both program internal and external factors.

When do we get logical errors?

  •  The Logical Errors occur if the solution procedure for the given problem itself is wrong.
  •  In this case, the outputs produced by the programs would be incorrect.
  •  Correcting the solution procedure itself by better understanding of the problem eliminates these errors.
  •  The Logical Errors (if any) are to be figured out by ourselves by verifying the outputs that are produced by the program.

Do character constants represent numerical values?

Yes, each character constant associates an integer value with it.

What is the purpose of scanf() and printf() functions?

  •  The function scanf() is used for formatted input from the standard input and provides many of the conversion facilities.
  •  It is used for formatted output to standard output device, that is, screen. The format specification string and the data to be output, are the arguments (parameters) to the printf() function.

What is type qualifiers?

Type qualifier adds properties to an identifier. Type qualifiers describe the manner in which the object will be modified. The application of qualifiers to an object does not affect the range or the arithmetic properties of the object.

What are the types of type qualifiers in c?

The two type qualifiers provided by C are :

  •  const
  •  volatile

What is meant by inheritance?

Inheritance is the process by which objects of one class acquire properties of objects of another class..

Do string constants represent numerical values?

No, the string constants donot have a corresponding numerical value.

What is meant by operator precedence?

Operator precedence describes the order in which C evaluates different operators in a complex expression.

What is an Operator?

An operator is a symbol, which instructs the computer to perform the specified manipulation over some data. The rich set of operators available in C enable us to write efficient and concise programs and this fact serves to set C apart from any other programming languages.

What are the types of operators in c?

  •  Assignment operator
  •  Arithmetic operators
  •  Relational operators
  •  Logical operators
  •  Increment/Decrement operators
  •  Shorthand arithmetic assignment operators
  •  Conditional operator
  •  Bitwise operators
  •  Sizeof() operator
  •  Comma operator

What is a ternary operator in C?

Perhaps the most unusual operator in C language is one called the conditional expression operator. Unlike all other operators in C which are either unary or binary operators the conditional expression operator is a ternary operator; that is, it takes three operands. The two symbols that are used to denote this operator are the question mark (?) and the colon (:). The first operand is placed before the ?, the second between the ? and the and the third after the :.

What is assignment operator?

An operator is a symbol that operates on a certain data type.In C, the ’=’ symbol is known as the assignment operator. It sots the value of the variable on the left hand side of it to that of the right hand side of it.

What are the types of assignment statements?

 C supports a variety of assignment statements. These are given below :

  •  Simple assignment statement
  •  Multiple assignment statement
  •  Arithmetic assignment statement

What is the sizeof () operator?

Even though it looks like a keyword, sizeof( ) is an operator which is used to know the memory size of the data types and variables. This operator returns the number of bytes allocated for the variable (or) data type. The format of the sizeof() operator is as follows.
sizeof (v);
where v is a variable name / data type / value.

What is the use of bitwise operator?

The bitwise operator performs the operation on bits (i.e. bit by bit). Using the bitwise operators we can set / reset / check any bit in the value of the variable.

What is the Difference between = and = = Operators?

The two operators = and == are used for assignment and checking respectively. If not properly used, it causes many problems. The following program illustrates what will happen if we use = instead of = =.

What is unary operator?

The operators that act upon a single operand to produce a new value are known as unary operators.

What are the types of unary operators?

C support unary operators are :

  • minus operator –
  • increment operator + +
  •  decrement operator –
  •  size operator
  •  (type) operator
Summary
Review Date
Reviewed Item
Very Nice! C Interview Questions and Answers Thank you iteanz, Keep updating more.
Author Rating
51star1star1star1star1star