Top 40 C++ Interview Questions
Top 40 C++ Interview Questions
Interview Questions
Q 1. What is C++?
Ans: As an extension of the C language, C++ was
developed by Bjarne Stroustrup as a general
purpose cross-platform language which gives
programmers a high level of control over system
resources and memory.
01
Q 4. What is namespace in C++?
C++
namespace MyNamespace {
int x = 10;
void display() {
std::cout << "x = " << x <<
std::endl;
}
}
#include <iostream>
using namespace std;
int main() {
string name;
cin >> name;
cout << "You entered: " << name << endl;
return 0;
}
02
Q 6. . How to reverse a string in C++?
Ans:
C++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string str = "hello";
reverse(str.begin(), str.end());
cout << "Reversed: " << str << endl;
return 0;
}
C++
int a = 10;
int* ptr = &a; // pointer stores the address of a
03
Q 8. What is a reference variable in C++?
Ans: A reference variable acts as an alias for an
existing variable. When a variable is declared as
a reference, it becomes another name for the
original variable, meaning any operations
performed on the reference are actually
performed on the original variable.
04
Q 10. What is function in C++?
Ans: A function in C++ is a reusable block of code that
performs a specific task. It has a return type, a
name, optional parameters, and a body. Functions
help in organizing code, improving readability, and
avoiding repetition. For example, a function can
take inputs, process them, and return a result. C++
supports various types of functions including user-
defined, inline, recursive, and overloaded functions.
05
Q 12. What is function overloading in C++
Ans: Function Overloading happens in C++ when two or
more functions share the same name. They can be
differentiated on the basis of the type of data they
are passing as parameters or even the number of
paramters they are passing. eg. int fun(char a); &
int fun(int b); & void fun(int a, int b)
06
Q 13. What are Classes and Objects in C++?
Ans: Class:
Blueprint: A class is a blueprint or a template for
creating objects.
Data and Functions:The encapsulation of data
(attributes) and functions (methods) that
operate on the data.
Abstraction: Provides a way to represent real-
world entities with properties, behaviors, and
expressions.
Object:
Instance: An object is an instance of a class
representing a specific entity.
Attributes: Objects store data defined in the
class's attributes.
Methods: Objects can invoke the methods
defined in the class, performing operations
specific to that object's type.
07
Q 14. What is exception in C++
Ans: Runtime abnormal conditions that occur in the
program are called exceptions.
08
Q 16. What is stack in C++?
Ans: A linear data structure which implements all the
operations (push, pop) in LIFO (Last In First Out)
order. Stack can be implemented using either
arrays or linked list.
12
Q 26. Can Java support multiple inheritance?
Ans: Java does not support multiple inheritance with
classes to avoid ambiguity.
13
Q 28. What is Function Overriding in C++?
Ans: Function overriding in C++ occurs when a
derived class defines a function with the same
name, return type, and parameters as one in
its base class. This lets the derived class
provide its own behavior, which is called
instead of the base class version, enabling
polymorphism.
14
Q 32. What is the difference between new and
malloc() in C++?
Ans: new is an operator that allocates memory
and also calls the constructor to initialize
objects. It returns a pointer of the appropriate
type and throws an exception if allocation
fails.
malloc() is a C library function that only
allocates raw memory but does not call
constructors. It returns a void* that needs to
be cast to the appropriate type and returns
nullptr (or NULL) if allocation fails.
Q 33. Can you compile a program without the
main function?
Ans: A program may be compiled without a main()
call. For instance, Use Macros that define the
main
15
Q 35. What is the purpose of a virtual function
in C++?
Ans: A virtual function allows derived classes to
override it so that the correct function is called
through a base class pointer or reference,
enabling runtime polymorphism.
16
Q 37. What are the four main principles of
Object-Oriented Programming (OOP) in C++?
Ans: The four main OOP principles in C++ are:
1.Encapsulation – Bundling data and functions
that operate on that data within a class,
restricting direct access to some of the object’s
components.
2.Abstraction – Hiding complex implementation
details and exposing only the necessary
features.
3.Inheritance – Creating new classes from
existing ones to promote code reuse and
establish relationships.
4.Polymorphism – Allowing functions or
operators to behave differently based on the
object’s actual type, mainly through function
overloading (compile-time) and virtual
functions (runtime).
17
Q 39. What is a mutable storage class specifier?
How can they be used?
Ans: The mutable keyword allows a class member
to be modified even if the object is declared
const. This is useful because normally const
objects can’t change their members. However,
mutable can’t be used with reference, static, or
const members. In a const member function,
only mutable members can be modified since
the this pointer is treated as a pointer to a
const object.
Syntax :-
storage_class var_data_type var_name;
18
Our students have gone on to work at renowned companies,
innovative startups, and leading unicorns.
Courses Blogs
E - Books
www.thelearnnova.com Follow us -