Interview Questions on C and C++ Programming and Answers: In this article, we are going to talk about some most commonly asked interview questions on C and C++ programming and their answers. I think you already familiar with both of these two languages if you are a computer science student. So, you may be interested to know what kind of coding questions are asked in the interviews.
Here today we are not going to discuss these two languages but we will discuss some important and tricky interview questions based on C and C++ that are commonly being asked in viva as well as in the job interviews. This article might become so lengthy, but trust me we are including the most commonly asked interview questions on C and C++ programming and their answers for you.
Interview questions on C and C++ programming and answers:
Basically, an interview consists of two major rounds- programming round and conceptual round. Being a fresher, before moving on to the programming round or say the technical round, you have to go through the preliminary round or the conceptual round. So the first round ideally for the freshers is where you have to explain your understanding of a particular concept in a programming language.
For an experienced person, the interview questions on C or C++ that he/she may face are sometimes different from a fresher. If you are having 2 years or 3 years of experience, then you may appear 2-3 rounds of coding interviews. But for a fresher, the preliminary round is taken almost all interviews. Where the interviewer actually tries to access the awareness or understanding that you have on a particular language.
While in the conceptual round, there are certain interview questions on C language, that are most commonly asked by almost any interviewer to make sure that you are aware of the basic concept of that language. The interviewer actually tries to know that you are aware of what’s currently going on that language. All these interview questions on C and C++ that we are going to discuss, you should be thoroughly prepared with. Because the way you answer these questions give a very good understanding of how well you will be able to do when you code. So, keeping that in mind, let’s discuss some commonly asked interview questions on C and C++ and their answers.
Frequently asked interview questions on C and C++:
Q. What is the purpose of main() function?
Ans: The main function invokes other functions within it and the execution of a program always starts with the main() function.
Q. What are header files and their uses?
Ans: Header files also known as library files contain the function definition and prototypes which are used in a program. For example the stdio.h header file contains the definitions and prototypes of the functions like printf() and scanf().
Q. Name the files that automatically open when a C file is executed.
Ans: Standard input, standard output and standard error.
Q. Why is a semicolon (;) put at the end of every program statement?
Ans: A semicolon after a statement acts as a terminator, Which tells the compiler where each statement is ending. It may proceed to divide the statements into smaller components for checking the syntax.
Q. Difference between ++a and a++?
Ans: In ++a the value of the variable is incremented first then the resulting value is used in the operation. It is called as prefix increment. But in the case of a++, the current value of the variable is used in operation then the value of the variable is incremented. It is known as postfix increment.
Q. What does static variable mean?
Ans: It is the variable which is not seen outside the function in which it is declared but remains available until the program terminates.
Q. What is Bus Error?
Ans: It is a fatal error in the execution of a machine language instruction which occurs because the processor detects an abnormal condition on its bus.
Q. What are macros?
Ans: A macro is a block of statement as a preprocessor directive. Being a preprocessor the block of code is communicated to the compiler before entering into the actual code. It is defined with the preprocessor directive #define.
Q. What is “Segmentation Violation”?
Ans: It usually occurs when a program attempts to access the memory location which is not allowed to exist. This occurs due to invalid page faults.
Q. Difference between pass by reference and pass by value?
Ans: Pass by value always invokes or calls the function or returns a value that is based on the value. This value is passed as a constant or a variable which contains a value. But, pass by reference always invokes or calls the function by passing the address or a pointer to a memory location which contains the actual value. The function can update or change the value available at that memory location by reference to the pointer.
Q. What are register variable and their advantages?
Ans: The variables of register type modifier basically informs the compiler to store the variables in a register of CPU. The advantages of a register variable are excess optimisation and speed of program execution. The operations of these variables are comparably much faster.
Q. What is the memory leak?
Ans: An unwanted increase in programs is referred to as a memory leak. It leads to an unintentional increase in consumption of the memory. Memory leakage may cause the function of the system to stop and violation of operating system files.
Q. Which arithmetic operation can you perform on a void* pointer?
Ans: There are no arithmetic operations that can be performed on a void* pointer. Because the compiler doesn’t know the size of the pointed object.
Q. What is “auto” keyword?
Ans: It is a local variable with a local lifetime. It is declared by auto storage class specifier. This variable is visible only in a block in which it is declared. the value of an uninitialized auto variable is undefined.
Q. What is “extern” keyword?
Ans: When we use extern in the declaration of a function, it means that function is implemented externally.The program doesn’t reserve any memory for a variable declared as extern.
Q. What is a break statement?
Ans: A break statement causes the loop to terminate. Control is then passed to next block of code following the body of the loop.
Q. What is the difference between structure and union?
(This is kind of a very common question that we can see which is being asked in C programming interviews. Because students generally confuse between these two terms.)
Ans: A structure can have different types of data type inside it and all the variables of different data types use a different memory location. Hence all the variables of various data type declared within a structure are active at the same time. But in the case of a union, the member variable declared inside union stores the contents at the exact same memory location. So, only one data member is active at a time.
Q. What is the difference between #include”filename” and #include ?
Ans: In #include”filename” preprocessor looks for the file to be included in the same directory where the current source file resides. But in #include the preprocessor searches for the file in directories pre-designed by the compiler that means the directories where standard library header files reside.
Q. What are dangling pointers and how are they different from memory leaks?
(Questions on pointers are very common for anyone who is appearing for a C++ or C interview. Basically, arrays and pointers are the two concepts from where most of the questions are asked in a C or C++ interview. So, make sure that you have a clear understanding of the concept of pointers.)
Ans: Dangling pointers are those that point to memory locations which have already been freed. Memory leaks happen when memory locations are not freed, but there is no way to refer to them.
Q. What is the difference between pre-increment and post-increment operator?
Ans: Pre-increment operator is used to incrementing the variable value by 1 before assigning the value to the variable. But post-increment operator is used to incrementing the variable value by 1 after assigning the value to the variable.
Q. What is “&” and “*” operators in C?
(I think you are familiar with these two operators if you have written programs in C using pointers. Now the question is why these two operators are actually used?)
Ans: The “&” operator is used to get the address or the memory location of a particular variable. But “*” operator is used to get the value stored inside a specified address. You can read more about these two operators in here.
Q. What is the use of “goto” statement?
Ans: The goto statement is used to transfer the normal flow of a program to the specified label in the program.
Q. What is the “extern” and “static” function in C?
Ans: Extern function can be used in any other source file of the same project which has many other files, but a static function can’t be used in other files of the same project.
Q. Can a variable be both volatile and constant in C?
Ans: The value of a constant variable can’t be changed by the internal program once it is declared. But a volatile variable’s value may change at any time.
Q. What does the following declaration mean? int(*ptr);
Ans: It is a pointer to an array of 10 integers.
Q. What function is used to free the memory allocated by calloc()?
Q. What is the difference between new and malloc?
Ans: The new() initializes the allocated memory by calling the constructor. Memory allocated with new should be released with delete(). But malloc() allocated uninitialized memory and allocated memory has to be released with free().
Q. What is the difference between function overloading and operator overloading?
Ans: Function overloading means you can define many functions with the exact same name, but the parameter passed through the functions are has to be of different types or different numbers. On the other hand, operator overloading means you can use almost all built in operators as a function to do any task that you want rather than what it actually does.
Q. What is dynamic binding (late binding)?
Ans: Dynamic binding is a process where the code associated with a given function call is not known until the time of that function call at runtime.
Q. What is the difference between class and structure?
- A structure contains only data but a class can have both data and member functions.
- A class provides data hiding facility, but the structure doesn’t.
- By default, the members of structures are public, but for a class is private.
Q. What is the difference between implicit and explicit conversion?
Ans: If you are doing a conversion between a smaller to bigger datatype that means a wider conversion, then it is an implicit conversion. For example, if an int variable is to be converted to float then it is an implicit conversion.
int x = 10;
y = x;
If you are converting a bigger data type to a smaller one, then it is called explicit conversion.
float y = 10.5;
x = y;
Q. What is a copy constructor?
Ans: A copy constructor is a method that accepts an object of the same class and copies its members to the object on the left part of the assignment.
Q. What is the difference between the copy constructor and overloaded assignment operator?
Ans: A copy constructor constructs a new object by using the content of the argument object. But, an overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.
Q. What is the difference between message and method?
Ans: Objects communicate with each other by sending messages to each other. A method is invoked by sending a message by the object. On the other hand, the method provides a response to a message and it is an implementation of an operation.
Q. What is the difference between late binding and early binding?
- Early binding is overloading, late binding is overriding.
- Early binding is static, but late binding is dynamic.
- Overloaded functions, Normal functions calls are early binding.
- Late binding: Virtual functions.
Q. How can you reallocate pointers?
Ans: Using realloc().
So far we have discussed some really important most commonly asked interview questions on C and C++ and their answers. I request you to go through all the answers in details since we have kept all the answers in very short. You can find these concepts in various books or on some internet forums. Let us know how did you like this article on interviews questions on C and C++ and answers. If you want more interview questions on C or C++ let us know by typing a comment in the comment section below. We will try to make another article on C interview questions and answers with more tricky questions.