24 basic level C++ programming interview questions and answers
Interviewing is an important step for both employees and businesses. For employees, you need to answer interview questions well to get hired. Meanwhile, businesses wishing to recruit must create a suitable set of questions to most accurately assess a candidate's capabilities.
In this article, TipsMake.com will summarize the 24 most common C++ programming interview questions at the basic level (Fresher). TipsMake.com hopes that this article will be useful to both new programmers looking for jobs and employers.
Question 1: What is the difference between C and C++?
Answer : For this question, you should draw a comparison table to present everything more clearly.
C | C++ |
C is a procedural-oriented programming language | C++ is a partially object-oriented programming language |
C follows a top-down approach | C++ follows a bottom-up approach |
C does not support function or operator overloading | C++ supports function or operator overloading |
C does not support virtual functions and friend functions | C++ supports virtual functions and friend functions |
C only has 32 keywords | C++ has 52 keywords |
Question 2: What are classes and objects in C++?
Answer : A class is like a blueprint of an object. It is a user-defined data type that contains component data or component functions and is defined with the class keyword.
You define objects as an instance of a class. When it creates an object it can operate on either member data or member functions.
Question 3: What are access modifiers?
Answer : You use the access modifier to define accessibility for members of the class. It defines how to access class members outside the class scope.
There are three types of access modifiers:
- Private
- Public
- Protected
Question 4: How are the equality (==) and assignment (=) operators different?
Answer : The == operator checks whether two values are equal. If they are equal, the return result is true; if not, the return result is false.
The assignment operator = assigns the value of the expression on the right to the operand on the left.
Question 5: What is the difference between while and do-while loops?
Reply:
while | do-while |
The while loop verifies the condition, if it is true, the loop will be repeated until the condition becomes false | First, the do-while loop iterates its body once after which it checks the condition |
Syntax: while (condition) { statements. statements } | Syntax: do{ statements. statements } while(condition); |
If the condition is false in the while loop then no statements are executed in the loop | If the condition in the do-while loop is false then the body of the loop will be executed once |
Question 6: What is the size of the int data type?
A: 4 bytes
B: 1 byte
C: 8 bytes
D: 2 bytes
Answer : The answer is A, 4 bytes. Integer data type in C++ is 4 bytes.
Question 7: Which of the following operators cannot be overloaded?
A: -
B: +
C: ?:
D: %
Answer : The answer is C, the ?: operator cannot be overloaded.
Question 8: Which of the following is used to return the number of characters in a string?
A: Size
B: Length
C: Both size and length (Both size and length)
D: Name (Name)
Answer : The answer is C, both size and length are used to return the number of characters in a string.
Question 9: How are prefixes and suffixes different?
Answer :
In the prefix (++i), first it increments the variable value, then it assigns the variable to the expression.
In the suffix (i++), it assigns a value to the expression and then increments the value of the variable.
Question 10: Can you compile a program without main function?
Answer : Yes, we can compile a program without main function but cannot run or execute that program because main() function is the entry point, from where all the execution starts. When there is no access point, you cannot execute the program.
Question 11: What is std in C++?
A: std is a standard class in C++
B: std header reads standard files
C: std is a header file
D: std is a standard namespace
Answer : The answer is D, std is a standard namespace.
Question 12: How are the four data types in C++ different?
Answer :
- Primitive/Basic: Char, int, short, float, double, long, bool,.
- Derivative: Array, pointer,.
- Enumeration: Enum
- User defined: Structure, class,.
Question 13: How is structure different from class?
Answer :
Structure | Class |
By default, its components are public | By default, its components are private |
The default access specifiers are public when retrieving a struct from a class/struct | The default access specifiers are private when deriving a class |
Question 14: How do you understand polymorphism in C++?
Answer : The term polymorphism refers to the presence of more than one form. Polymorphism often occurs when there is a hierarchy of classes linked together by inheritance.
Polymorphism in C++ means that depending on the type of object calling the function, a different function will be executed.
Question 15: Compare Compile-time polymorphism and runtime polymorphism.
Answer :
Compile-time polymorphism | Runtime polymorphism |
The method to be executed is known at compile time. And the command has been resolved by the compiler. | The method to be executed is known at runtime. The compiler does not resolve the command. |
Provides faster execution because it is known at compile time | Execution time is slower because it is known at run time |
Achieved by operating or overloading the function | Achieved by overriding the function |
Question 16: What is a constructor in C++?
Answer : In C++, an Object function is a specific "MEMBER FUNCTION" that has the same title as the class to which it belongs and is used to initialize specific values for the object's member data.
#include using namespace std; class student { int no; public: student() { cout << "Enter the RollNo:"; cin >> rno; } void display() { cout << endl << rno << "t"; } }; int main() { student s; // constructor gets called automatically when // we create the object of the class s.display(); return 0; }
Question 17: What is a virtual function?
Answer : A virtual function is a member function in the base class that is redefined in the derived class. It is declared using the virtual keyword. It ensures that the correct function is called for an object, regardless of the reference/pointer type used for the function call. Virtual functions are mostly used for runtime polymorphism.
Question 18: What do you understand about friend functions and friend classes?
Answer : Like a friend function, a friend class can access private and protected variables of the type in which it is declared. All member functions of classes designated as friends of another class are friend functions of your class.
class Node { private: int key; Node* next; /* Other members of Node Class */ // Now class LinkedList can // access private members of Node friend class LinkedList; };
Question 19: What are the differences between the three types of access specifiers in C++?
Answer :
- Public: All member functions and member data are accessible outside the class.
- Protected: All member functions and member data are accessible in the class and derived classes.
- Private: All member functions and member data are inaccessible outside the class.
Question 20: What is abstraction in C++?
Answer : Abstraction means showing details to the user while hiding irrelevant or specific details that you don't want to show to the user. It has two types:
- Control abstraction
- Data abstraction
Question 21: What is the destructor in C++?
Answer : A destructor is a member function that is called immediately when an object exits its scope or is deleted by a delete statement.
class X { public: // Constructor for class X X(); // Destructor for class X ~X(); };
Question 22: Is it possible to overload destructors? Give reasons for your answer.
Answer : No, this is not possible because the destructor does not take arguments or return anything. There is only one empty destructor per class. It must have a void parameter list.
Question 23: What is an abstract class? When is it used?
Answer : An abstract class is a class whose objects cannot be created. It acts as the parent of derived classes. Placing a pure virtual function in a class turns it into an abstract class.
Question 24: What do you understand about static members and static member functions?
Answer : A variable in a class declared as static has space allocated for the lifetime of the program. There is only one single copy of a static member, regardless of how many objects of that class are created. All objects of that class can access the same static member.
Static member functions can be called even when no class objects exist. It is accessed using only the class name and the scope resolution operator (::).
You should read it
- Questions to interview IT people need to understand
- 15 questions to interview for an extremely 'bad brain' job
- 49 most asked questions that Apple asked in the job interview
- The secret to answering the 15 most frequently asked interview questions
- 15 questions interview the 'hottest' programmers and answer suggestions
- 200 common Java interview questions, with reference answers
- Smart answers when asked in an interview: Do you want to ask any questions?
- What do employers want to know about you when asking this question in an interview?
- 18 'extremely difficult' recruitment questions from Apple
- Ask yourself these 10 questions before the interview to gain more confidence
- 8 employers 'psychological game' to assess the competency of the candidate in the interview
- Three classic questions make the candidate suffer the most when interviewing
Maybe you are interested
Why can't I Group in PowerPoint? Find the answer
ChatGPT answered incorrectly 52% of the time in the field of programming
Truecaller allows the use of voice-cloning AI to answer calls
Nvidia develops 'AI police' that prevents ChatGPT from fabricating answers
Learn about Phind: The app that answers questions about coding with the power of AI
Why do Stories fade after so long? Find answers