C++ Developer Freshers Top Interview Questions:
- What is C++?
- C++ is a general-purpose programming language developed as an extension of the C programming language. It supports procedural, object-oriented, and generic programming features.
- Differentiate between C and C++.
- C is a procedural programming language, whereas C++ supports both procedural and object-oriented programming paradigms.
- C does not support classes and objects, whereas C++ does.
- C++ has features like inheritance, polymorphism, encapsulation, and abstraction, which are not present in C.
- What is Object-Oriented Programming (OOP)?
- Object-Oriented Programming is a programming paradigm based on the concept of “objects,” which can contain data, in the form of fields, and code, in the form of procedures. These objects are instances of classes, which define their structure and behavior.
- What is a class in C++?
- A class is a blueprint for creating objects in C++. It defines the data and behavior of objects.
- What is an object in C++?
- An object is an instance of a class. It is a real-world entity that has a state and behavior.
- What is a constructor?
- A constructor is a special member function of a class that is automatically called when an object of that class is created. It is used to initialize the object’s state.
- What is a destructor?
- A destructor is a special member function of a class that is automatically called when an object is destroyed. It is used to release resources acquired by the object.
- What is inheritance?
- Inheritance is a mechanism in which a new class (derived class) is created by inheriting properties and behaviors from an existing class (base class).
- What is polymorphism?
- Polymorphism is the ability of a function to behave differently based on the object that it is operating on. In C++, polymorphism can be achieved through function overloading and function overriding.
- What is function overloading?
- Function overloading is a feature of C++ that allows multiple functions with the same name but different parameters to be defined in the same scope.
- What is function overriding?
- Function overriding is a feature of inheritance in C++ that allows a derived class to provide a specific implementation of a function that is already defined in its base class.
- What is encapsulation?
- Encapsulation is the bundling of data and methods that operate on the data into a single unit (class). It helps in hiding the internal implementation details of a class from the outside world.
- What is abstraction?
- Abstraction is the process of hiding the implementation details of a class and showing only the essential features of the class to the outside world.
- What is a pointer?
- A pointer is a variable that stores the memory address of another variable. It allows indirect access to the memory location of a variable.
- What is a reference?
- A reference is an alias for a variable. It provides an alternative name for an existing variable.
- What is the difference between a pointer and a reference?
- A pointer can be reassigned to point to different objects, whereas a reference always refers to the same object.
- Pointers can be NULL, whereas references cannot.
- Pointers can be uninitialized, whereas references must be initialized when declared.
- What is dynamic memory allocation?
- Dynamic memory allocation is the process of allocating memory at runtime using functions like
new
anddelete
in C++.
- Dynamic memory allocation is the process of allocating memory at runtime using functions like
- What is the
new
operator?- The
new
operator is used to dynamically allocate memory for an object or an array in C++.
- The
- What is the
delete
operator?- The
delete
operator is used to deallocate memory that was allocated dynamically using thenew
operator.
- The
- What are the access specifiers in C++?
- Access specifiers (public, private, protected) are keywords used in C++ to control the access levels of class members.
- What is the
public
access specifier?- Members declared as
public
can be accessed from outside the class.
- Members declared as
- What is the
private
access specifier?- Members declared as
private
cannot be accessed from outside the class. They are only accessible within the class.
- Members declared as
- What is the
protected
access specifier?- Members declared as
protected
can be accessed by derived classes, but not by objects of the class or from outside the class.
- Members declared as
- What is a friend function?
- A friend function is a function that is not a member of a class but has access to the private and protected members of the class.
- What is function template?
- A function template is a mechanism in C++ that allows you to define a function template that can accept any data type.
- What is a class template?
- A class template is a mechanism in C++ that allows you to define a template for a class, with placeholders for the data types of its members.
- What is a virtual function?
- A virtual function is a member function of a base class that can be overridden by derived classes. It allows dynamic binding of function calls at runtime.
- What is pure virtual function?
- A pure virtual function is a virtual function that is declared in a base class but has no implementation. It must be implemented by derived classes.
- What is an abstract class?
- An abstract class is a class that contains at least one pure virtual function. It cannot be instantiated and is meant to be used as a base class.
- What is multiple inheritance?
- Multiple inheritance is a feature of C++ that allows a class to inherit properties and behaviors from multiple base classes.
- What is the difference between
malloc()
andnew
operator?malloc()
is a function from C language used to allocate memory dynamically, whilenew
is an operator in C++ used to allocate memory dynamically and also calls the constructor to initialize the object.
- What is the difference between
delete
andfree()
?delete
is an operator in C++ used to deallocate memory allocated usingnew
, whilefree()
is a function in C used to deallocate memory allocated usingmalloc()
.
- What is a destructor?
- A destructor is a special member function of a class that is called automatically when an object goes out of scope or is explicitly deleted. It is used to release resources held by the object.
- What is the difference between a shallow copy and a deep copy?
- A shallow copy copies the values of the object’s data members to another object, including any pointers. However, the pointers still point to the same memory locations as the original object. In a deep copy, the values of the object’s data members are copied to another object, and separate memory is allocated for any pointers, so the copied object and the original object are independent of each other.
- What is a smart pointer?
- A smart pointer is a class template in C++ that behaves like a pointer but provides automatic memory management. It automatically releases the memory when it is no longer needed.
- What are the different types of smart pointers in C++?
- The different types of smart pointers in C++ are
std::unique_ptr
,std::shared_ptr
, andstd::weak_ptr
.
- The different types of smart pointers in C++ are
- What is RAII (Resource Acquisition Is Initialization)?
- RAII is a programming idiom in C++ where resources are acquired during object creation (in the constructor) and released during object destruction (in the destructor). It ensures that resources are properly managed and released when they are no longer needed.
- What is a const member function?
- A const member function is a member function of a class that promises not to modify the state of the object on which it is called. It is declared using the
const
keyword.
- A const member function is a member function of a class that promises not to modify the state of the object on which it is called. It is declared using the
- What is the difference between
const
andconstexpr
?const
is used to declare variables that cannot be modified after initialization, whereasconstexpr
is used to declare values that can be computed at compile time.
- What is the difference between pass by value and pass by reference?
- Pass by value involves making a copy of the actual parameter’s value and passing it to the function, whereas pass by reference involves passing the memory address of the actual parameter to the function, allowing the function to directly access and modify the parameter.
- What are lambda expressions in C++?
- Lambda expressions are anonymous functions in C++ that can be defined inline. They are used to create function objects in a concise and expressive way.
- What is the difference between a lambda expression and a function object?
- A lambda expression is an anonymous function defined inline, while a function object is a named object of a class that overloads the function call operator
operator()
. Lambdas are more concise and can capture variables from the surrounding scope.
- A lambda expression is an anonymous function defined inline, while a function object is a named object of a class that overloads the function call operator
- What is the difference between
std::vector
andstd::array
?std::vector
is a dynamically resizable array that can change its size at runtime, whilestd::array
is a fixed-size array with a size that is determined at compile time.
- What is the difference between a template class and a template function?
- A template class is a class template that allows you to define a class with placeholders for the data types of its members, while a template function is a function template that allows you to define a function with placeholders for its parameters’ data types.
- What is a mutex?
- A mutex (mutual exclusion) is a synchronization primitive used to protect shared resources from being accessed simultaneously by multiple threads.
- What is a thread in C++?
- A thread is the smallest unit of execution within a process. It allows multiple tasks to be performed concurrently.
- What is the difference between
std::thread
andstd::async
?std::thread
is used to create and manage a new thread of execution, whilestd::async
is used to execute a function asynchronously and potentially concurrently with the calling thread.
- What is an atomic variable?
- An atomic variable is a variable that can be read from and written to in an atomic operation, ensuring that no other thread can access the variable at the same time.
- What is the difference between
std::mutex
andstd::recursive_mutex
?std::mutex
is a basic mutex that does not support recursive locking, whilestd::recursive_mutex
is a mutex that allows the same thread to lock it multiple times recursively.
- What is RTTI (Run-Time Type Identification)?
- RTTI is a mechanism in C++ that allows the type of an object to be determined at runtime. It provides
typeid
operator anddynamic_cast
operator for runtime type checking and casting.
- RTTI is a mechanism in C++ that allows the type of an object to be determined at runtime. It provides