If you are in search of C and CPP interview questions, then this is the place where your search ends. In this article, we have compiled a list of C and Cpp Interview questions to help you prepare for your interview round.
These questions are carefully selected based on the candidate’s level. The article consists of C and CPP interview questions for freshers as well as working professionals. Read this article to know more.
Top 20 C and CPP Interview Questions for Freshers and Beginners
Check some of the widely asked C and CPP Interview questions for young graduates below.
Q1. What is the C++ language?
Ans: C++ is a powerful, high-level general-purpose language. It was developed as an extension of the C language. It provides additional features like OOPs, STL support, etc.
Q2. What is the use of iostream in C++?
Ans: The iostream header library provides functionality for input and output operations. It allows you to perform input and output operations using streams.
Q3. What is a namespace in C++?
Ans: Namespace is a way to group related code elements together. It prevents naming conflicts, which may occur when different parts of the program use the same identifier.
Q4. What is the major difference between C and C++?
Ans: C is a procedural programming language, while C++ is a multi-paradigm language, which means it supports object-oriented, procedural and generic programming styles.
Q5. What is operator overloading in C++?
Ans: Operator overloading in C++ provides operators with a specific meaning for a datatype. It is a compile-time polymorphism. For example, We can overload an operator ‘+’ in a String class to concate two strings by using +.
Q6. What is the difference between C and C++?
Ans: Let us understand the difference between C and C++ with the table given below.
C and CPP Interview Questions |
|
C language | C++ language |
It is a procedural language. | It is a multi-paradigm programming language. |
It consists of simpler syntax | It consists of more complex syntax. |
It does not support classes and objects. | It supports classes and objects. |
There is no support for polymorphism and inheritance. | Support single and multiple inheritances. |
Q7. What is namespace std in C++?
Ans: Using namespace std is a C++ directive, that allows you to access the elements from the std namespace without explicitly starting their names with std::
Q8. How are function arguments passed in C++?
Ans: There are two methods of passing arguments in a function. A function may be passed by value. Any changes made in the argument of the functions do not affect the original value of the calling function.
While arguments passed via references consist of the memory address of the function, which allows the function to directly access and modify the original values of the argument.
Q9. What is a destructor in C++?
Ans: A destructor in C++ is called when the object passes out of scope or is explicitly deleted. It is a member function with the same name as its class, prefixed with a ~(tilde) syntax.
Q10. What is STL in C++?
Ans: STL or Standard Template Library, is a powerful library in C++ that provides a range of containers, such as vectors, lists, sets, maps, stacks and more, to help us easily manipulate and store data.
Q11. What is the purpose of the volatile keyword in C?
Ans: The volatile keyword in C tells the compiler that the value of the given variable may change at any time without any action being taken in the code, which cannot be determined by the compilers. Objects declared as volatile are prevented from optimization.
Q12. Explain the difference between malloc() and calloc() functions.
Ans: The malloc() function creates a single block of memory of a specific size. calloc() functions assign multiple blocks of memory to a single variable. The maximum number of arguments in malloc() is 1 while in calloc() it is 2. The malloc() function has a higher time efficiency while calloc() has lower efficiency.
Q13. What is typecasting in C++?
Ans: Typecasting in C++ is a way of converting one data type to another. There are two major types of typecasting in C++, implicit and explicit typecasting. Implicit typecasting is done by the compiler itself, while explicit typecasting is done by the programmer.
Q14. What is the difference between structure and Class in C++?
Ans:
C and CPP Interview Questions |
|
Structure | Class |
Structure supports inheritance. | The class supports single and multiple inheritance. |
The default members are public. | The default members are private. |
It consists of no access specifiers. | It consists of access specifiers (public, private, protected) |
It consists of no default constructors. | It consists of an automatic default constructor. |
It does not require the ‘class’ keyword. | It requires the ‘class’ keyword. |
Q15. Give a C++ method to clear the screen.
Ans:
C and CPP Interview Questions |
|
#include <cstlib>
Using namespace std; int main (){ system (“cls”); //for windows System (“clear”); //for macOS or Linux //Code return 0; } |
Q16. Predict the output of the following code in the table below.
include <cstlib>
using namespace std; int main (){ int i = 5; int j = i++; print(“%d”, j) //predict the value of j in the output |
Ans: The output value of j is 5, as it is a post-increment operator used with the variable j.
Q17. Which operator can be overloaded in C++?
Ans: The addition operator ‘+’ can be overloaded in C++ to be used as a string concatenation. Overloading provides flexibility to the operators and enables them to be declared under user-defined types.
Q18. What is exception in C++?
Ans: Exception in C+ is an object condition that is thrown at runtime when any unexpected condition is encountered in a program. It helps to transfer control from one part of the program to another to handle the exceptional error. We use the throw keyword to throw an exception, try-catch to catch the error and handle it in the catch block.
Q19. Why is Conio.h used in C++?
Ans: conio.h is a header file in C++ used to read input and output to the console. It consists of various built-in functions. It takes input from the user and displays the output on the screen.
Q20. What is an enum in C++?
Ans: An enum is a user-defined data type that enables users to create a new data type that consists of a fixed range of possible values. For example, consider days of the week, directions, etc.
Learn C++ Programming with PW Skills
Join our DSA with C++ Course to learn the basics and fundamentals of C++ programming with our industry experts. Learn and practice at the same time using our free online PW Lab Compiler. This course provides tutorials on Data Structures and algorithms, along with programming classes. Enroll in the course to start an exceptional programming journey only at pwskills.com
C and CPP Interview Questions FAQs
Q1. Is malloc() faster than the calloc() function in C?
Ans: Yes malloc() function is faster than the calloc() function in C.
Q2. Does the structure in C++ consist of an access specifier?
Ans: No, structures do not consist of access specifiers. Structures in C++ are public by default.
Q3. What is Cpp in programming?
Ans: CPP stands for C Plus Plus, which is a generic procedural programming language. It supports OOPs. Programs in C++ are saved using the .cpp extension.