Interview Questions And Answers in C

Interview Questions And Answers in C

Q. History of C?
C is a high-level case-sensitive programming language used to write a program. C was developed by a system programmer Dennis Ritchie in 1972, at American Telegraph & Telecommunication (AT & T) Bell Laboratories in New Jersey USA. It was written originally to re-implement the UNIX operating system.

C Programming Language Timeline

: Basic Combined Programing Language.
AT&T: American Telegraph & Telecommunication.
ANSI: American National Standardization Institute.
ASCII: American Standard Code for Information Interchange.

Q. What is Structured Programming?
A. Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures, for and while loops in case of the goto statement, which lead to “spaghetti code” that is difficult to follow and maintain.
Structured programming was first suggested by Corrado Bohm and Guiseppe Jacopini.
C is called a structured programming language because to solve a large problem, C programming language divides the problem into smaller modules called functions or procedures each of which handles a particular responsibility.

Q. What is a Program?
A. A program is a group of instructions typed by the programmer in a high-level language. A compiler translates our program from a high level to machine language.

Q. What is a Compiler?
A. C Uses the special software called a compiler to translate our program from a high level to machine language.

Q. What is Semantics?
A. It is the logic or planning of the program. Semantics can be written in any of the following ways: Flowcharts, Algorithms, Pseudo codes.

Q. What is Syntax?
A. It is the way of writing the program in a particular programming language. Syntax changes from language to language.

Q. What is a Flowchart?
A. Pictorial and graphical representation of program logic. Flowcharts are used to decide the sequence of steps involved in finding a solution.

Q. Difference Between Compiler and Interpreter and Assembler?
A. The main difference between compiler interpreter and assembler is that compiler translates the whole high-level language program to machine language in one go and then executes it, while the interpreter translates high-level language program to machine language line by line and assembler converts assembly language program to machine language.
The execution of the program is faster in compiler than interpreter as in interpreter code is executed line by line.
Compiler generates an error report after the translation of the entire code whereas in the case of an interpreter once an error is encountered it is notified and no further code is scanned.

Compiler and Interpreter and Assembler

Q. What is Looping/Iteration?
A. Looping is a process by which we can repeat a single statement or a group of the statement until some condition for termination of the loop is satisfied.

Q. Few Imp ANCII?
A-Z = 65–90
a-z = 97–122
0–9 = 48–57
Space = 32
Enter = 13
Esc = 27

Q. What is Array in C and it’s limitations?
A. It is a collection of homogeneous data, stored continuously under a single name.
Size is constant which leads to the shortage or wastage of memory.
Insertion and deletion require shifting.
No boundary check.

Q. What is String in C?
A. Strings are actually one-dimensional array of characters terminated by a null character ‘\0’. So, a string with the contents, say, “abc” has four characters: ‘a’, ‘b’, ‘c’, and the terminating null character. The terminating null character has the value zero.
Below is an example to declare a string with name as str and initialize it with “Hello”.
1. char str[] = “Hello”;
2. char str[50] = “Hello”;
3. char str[] = {‘H’,’e’,’l’,’l’,’o’,’\0'};
4. char str[6] = {‘H’,’e’,’l’,’l’,’o’,’\0'};

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

Q. Are 0 and ‘\0’ same? If not, explain.
A. No, 0 and ‘\0’ are different. ASCII Value of ‘\0’ is 0 whereas that of 0 is 48. ‘\0’ is a terminating character normally used in Strings. Its the only way that functions within a string can know where the String ends. ‘\0’ is a single character.

Q. What are C Tokens?
A. The smallest individual units of the program are known as Token. C recognizes five types of tokens.
1. Keywords (Words defined by the language)
2. Identifiers (Variables Name)
3. Literals (Constants)
4. Operators
5. Separators

Q. What are Keywords?
A. Keywords are the reserved words whose meaning has already been explained to the C compiler. These words are defined in the language itself.
C89 has 32 keywords.
C99 reserved 5 more keywords.
C11 reserved 7 more keywords.
So now there is a total of 44 reserved keywords.

Q. What is Primitive Data Type?
A. Primitive data types are those that are pre-defined by language. C language supports four primitive types — char, int, float, and void. Primitive types are also known as basic data types.

Q. What is Derived Data Type?
A. A derived data type is defined using a combination of qualifiers along with the primitive data type. Derived types are created using basic data types with modified behavior and property.

Q. What is Enumerated Data Type?
A. An Enumerated Data Type is a data type with user specified constant values. It is used to define constant. The keyword is enum.

Data types hierarchy

Q. What is the difference between || and |?
A. || is a Logical Operator and it is pronounced as OR Operator, whereas | is a Bitwise OR Operator.

Q. What is the difference between && and &?
A. && is a Logical Operator and it is pronounced as AND Operator, whereas & is a Bitwise AND Operator.

Bitwise Operators

Q. Explain the Conditional Operator.
A. Conditional Operator is also called a Ternary Operator. The Syntax for a Conditional Operator is:
expression 1 ? expression 2 : expression 3
Expression 1 includes the Condition. Depending upon its value, if the Condition evaluates to be True, expression 2 will be executed and if the Expression 1 evaluates to be False, expression 3 will be evaluated.

Q. What is the difference between Do While Loop and While Loop?
A. A While Loop checks the Condition before executing the following set of statements whereas a Do-While Loop first executes the set of statements once and then checks the condition. In this, a statement will be executed at least once even if the Condition evaluates to be False.

Q. What is a break keyword used for?
A. The keyword break allows us to transfer the Control to the very first statement that occurs after the current Loop. It, therefore, takes the Execution Control out of the Loop. A Break is generally used with IF Blocks and Switches.

Q. What is Recursion?
A. A function is called recursive if a statement within the body of function calls the same function. Function calling itself, again and again, is called Recursion.

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

Q. What is Structure?
A. It is a collection of heterogeneous data stored continuously under a single name.
keyword: Struct

Struct Student
int rollNo;
char name[10];
float per;

array of structure:

Struct Student
int rollNo;
char name[10];
float per;

Q. What is Union?
A. A union is much like a structure, the difference is that all the members of the union share the same memory location. The compiler allocates enough memory to hold the largest member of the Union. Save memory.

Q. Scope Resolution Operator (::)?
A. Used to print global variables in the local scope.

Q. What is Binding?
A. The process of linking a particular function with a particular definition is called binding.
Early Binding: Compile time and static
Late Binding: Run time and dynamic

Q. What is Encapsulation?
A. Wrapping up of data and method into a single unit is known as Encapsulation.

Q. What is Abstraction?
A. An act to hide the complexity (unwanted information) from the end user.

Q. What is Inheritance?
A. The process of acquiring properties from one object to another without change is known as inheritance.
extends keyword

Q. What is Polymorphism?
A. Ability to take more than one form. Example: Overloading, Overriding

Read more about OOPs Concepts.

Q. What is Function or Method Overloading?
A. Two or more functions having the same name but different argument/parameter lists is called as function overloading. It can be done in the base as well as derived class.

Q. What is Function or Method Overriding?
A. It is the redefinition of base class function in its derived class with the same signature i.e return type and parameters. It can only be done in the derived class.

Q. Argument passing mechanics in C?
Call By Value:

When the arguments are passed by value then the copy of the actual parameter is transferred from calling function to called function definition in the formal parameters. Now any change made in the formal parameter will not be reflected in the actual parameter of the calling function.

Call By Reference:
When the arguments are passed by reference then the address of the actual parameter is transferred from calling function to called function definition in the formal parameter. Now any change made in the formal parameter will be reflected in the actual parameter of the calling function.

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

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

Q. Memory Allocation in C?
A. Static Memory Allocation (Compile Time)
Dynamic Memory Allocation (Run Time) (malloc, calloc, realloc, free)

There are 4 library functions defined under stdlib.h for dynamic memory allocation.

Malloc(): The name malloc stands for “memory allocation”. Malloc allocates the requested no of bytes, leaves them uninitialized, and returns the starting address to the pointer.
ptr = (cast-type*) malloc(byte*size);
ptr = (int*) malloc(100 * sizeof(int));
This statement will allocate either 200 or 400 according to the size of int 2 or 4 bytes respectively.

Calloc(): The name calloc stands for “contiguous allocation”. Calloc allocates the requested no of bytes, initialized them with zero, and return the starting address to the pointer. It allocates non-continuous memory.
The only difference between malloc() and calloc() is that malloc() allocates a single block of memory whereas calloc() allocates multiple blocks of memory each of the same size and sets all bytes to zero.
ptr = (cast-type*) calloc(n, element-size);
ptr = (int*) calloc(25, sizeof(int));
This statement allocates contiguous space in memory for an array of 25 elements each of the size of float, i.e, 4 bytes.

Realloc(): Realloc function increases or decreases the size of the specified block of memory.
ptr = realloc(ptr, newsize);
Here, ptr is reallocated with the size of the new size.

Free(): Free function releases a specified block of memory to the system.

Q. What is Pointer?
A. A pointer is a special type variable that is used to store the address of some other variable. A pointer can be used to store the address of a single variable, array, structure, union, or even a pointer.
Two pointers compared such as:
P1> P2
P1 == P2
P1 != P2
when only using string or array.
Two pointers cannot be multiplied or added.

Q. What is the Null pointer?
A. A Null pointer is a special pointer value that is known ‘not to point anywhere’.
int *p = NULL;

Q. What is ‘this’ pointer?
A. ‘this’ is a pointer inside the class which stores the address of the object currently in use. ‘this’ pointer is automatically passed to a member function when it is called.

Q. What is the dangling pointer?
A. If any pointer is pointing to the memory address of any variable but after some variable has deleted from that memory location while the pointer is still pointing at such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

Q. What is the wild pointer?
A. A pointer in c which has not been initialized is known as a wild pointer.

Q. What is a pointer on pointer?
A. It’s a pointer variable which 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. What is a constant pointer?
A. A pointer, which is not allowed to be altered to hold another address after it is holding one.

Q. To make pointer generic for which data type needs to be declared?
A. Void

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

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

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

Q. What is the difference between far and near pointers?
A. In the first place, they are non-standard keywords. A near pointer can access only 2¹⁵ memory space and a far pointer can access 2³² memory space. Both keywords are implementation specific and are non-standard.

Q. What is the difference between including the header file with-in angular braces < > and double quotes “ “?
A. 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. What is the meaning of the base address of the array?
A. The starting address of the array is called as the base address of the array.

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

Q. Explain modular programming?
A. Dividing the program into subprograms (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. What is a preprocessor?
A. Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

Q. What are command line arguments?
A. The arguments which we pass to the main() function while executing the program are called as command line arguments.

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

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

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

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

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

“The best way to learn a new programming language is by writing programs in it.” — Dennis Ritchie

Thank you for reading! If you liked this article, please clap to get this article seen by more people.
Please follow me on Medium by clicking Follow.
I’m also active on LinkedIn, Twitter, and GitHub.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Naveen Sharma

Naveen Sharma

iOS Developer [Objective-C] [Swift]