0% found this document useful (0 votes)
14 views

CPP Interview Questions

Uploaded by

khajulrehaman77
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CPP Interview Questions

Uploaded by

khajulrehaman77
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What is C++?

Answer: C++ is a high-level programming language that supports both procedural


and object-oriented programming. It was developed by Bjarne Stroustrup as an
extension of C.

2. What are the basic concepts of OOP in C++?


Answer: The basic OOP concepts in C++ are:
- Encapsulation: Bundling data and methods into a single unit (class).
- Abstraction: Hiding the implementation details and showing only the essential
features.
- Inheritance: Acquiring properties and behavior from another class.
- Polymorphism: Ability of a function or method to behave differently based on
the context.

3. What is the difference between C and C++?


Answer:
- C is a procedural programming language, while C++ supports both procedural and
object-oriented programming.
- C lacks classes and objects, while C++ introduces them.
- C++ has better memory management with constructors and destructors.

4. What is a class in C++?


Answer: A class in C++ is a user-defined data type that contains data members
(variables) and member functions (methods) to manipulate the data.

5. What is an object in C++?


Answer: An object is an instance of a class. It represents a real-world entity
that can hold data and perform operations defined by the class.

6. What is a constructor in C++?


Answer: A constructor is a special member function that is automatically called
when an object of a class is created. It is used to initialize the object.

7. What is a destructor in C++?


Answer: A destructor is a special member function that is called when an object
goes out of scope or is explicitly deleted. It is used to release resources
allocated by the object.

8. What is the difference between a constructor and a destructor?


Answer:
- A constructor initializes an object when it is created, whereas a destructor
cleans up when the object is destroyed.
- Constructors can be overloaded, but destructors cannot.

9. What is operator overloading in C++?


Answer: Operator overloading allows C++ operators (like +, -, ==) to be used
with user-defined data types (objects) by redefining their behavior.

10. What is function overloading in C++?


Answer: Function overloading allows multiple functions with the same name but
different parameter lists to exist in the same scope, enabling functions to handle
different types or numbers of arguments.

11. What is polymorphism in C++?


Answer: Polymorphism is the ability to process objects differently based on
their data type or class. It can be achieved through function overloading, operator
overloading, and inheritance.
12. What is runtime polymorphism in C++?
Answer: Runtime polymorphism is achieved using virtual functions and
inheritance. The appropriate function is selected at runtime depending on the type
of the object pointed to by the base class pointer.

13. What is compile-time polymorphism in C++?


Answer: Compile-time polymorphism is achieved using function overloading and
operator overloading. It is resolved during compilation rather than at runtime.

14. What is a virtual function in C++?


Answer: A virtual function is a member function in a base class that can be
overridden in derived classes. It enables runtime polymorphism.

15. What is pure virtual function in C++?


Answer: A pure virtual function is a virtual function that has no definition in
the base class and must be implemented by derived classes. It is declared using = 0
syntax. Pure virtual functions make a class abstract.

16. What is an abstract class in C++?


Answer: An abstract class is a class that cannot be instantiated and is designed
to be inherited. It contains at least one pure virtual function.

17. What is inheritance in C++?


Answer: Inheritance is a feature of C++ where one class (child or derived class)
acquires properties and behavior (methods) from another class (parent or base
class).

18. What are the different types of inheritance in C++?


Answer: The types of inheritance are:
- Single Inheritance: One base class and one derived class.
- Multiple Inheritance: One derived class inheriting from multiple base classes.
- Multilevel Inheritance: A derived class inheriting from another derived class.
- Hierarchical Inheritance: Multiple derived classes from a single base class.
- Hybrid Inheritance: A combination of more than one type of inheritance.

19. What is a friend function in C++?


Answer: A friend function is a function that is not a member of a class but can
access the class's private and protected members. It is declared using the friend
keyword.

20. What is encapsulation in C++?


Answer: Encapsulation is the concept of wrapping data (variables) and methods
(functions) that operate on the data into a single unit (class), with access
control to prevent unauthorized access to data.

21. What is data hiding in C++?


Answer: Data hiding is an aspect of encapsulation that restricts direct access
to class members. It is achieved by using access specifiers like private and
protected.

22. What is the difference between `private`, `protected`, and `public` access
specifiers?
Answer:
- Private: Accessible only within the class.
- Protected: Accessible within the class and by derived classes.
- Public: Accessible from anywhere.

23. What is the difference between `struct` and `class` in C++?


Answer: The only difference is the default access modifier:
- In a class, members are private by default.
- In a struct, members are public by default.

24. What is a copy constructor in C++?


Answer: A copy constructor is a constructor that initializes an object by
copying another object of the same class. It is invoked when an object is passed by
value, returned from a function, or explicitly copied.

25. What is a default constructor in C++?


Answer: A default constructor is a constructor that takes no parameters or has
default arguments. It is automatically provided by the compiler if no other
constructors are defined.

26. What is the difference between shallow copy and deep copy in C++?
Answer:
- Shallow copy: Copies the object's values as they are (including pointers).
- Deep copy: Creates a new copy of dynamically allocated objects, ensuring
independent memory allocation.

27. What is a pointer in C++?


Answer: A pointer is a variable that stores the memory address of another
variable. Pointers allow dynamic memory allocation and direct memory manipulation.

28. What is a reference in C++?


Answer: A reference is an alias for another variable, created using the &
symbol. It allows indirect access to a variable without changing its address.

29. What is dynamic memory allocation in C++?


Answer: Dynamic memory allocation refers to allocating memory at runtime using
new and delete operators.

30. What is the difference between `new` and `malloc()`?


Answer:
- new is an operator that allocates memory and calls the constructor for object
initialization.
- malloc() is a function that allocates memory but does not call the
constructor.

31. What is `this` pointer in C++?


Answer: this is a pointer in C++ that holds the address of the current object
and can be used to refer to the object’s members from within a member function.

32. What is the use of the scope resolution operator (`::`)?


Answer: The scope resolution operator is used to define a function outside its
class or to access global variables that are shadowed by local variables.

33. What is a namespace in C++?


Answer: A namespace is a container for identifiers such as variables, functions,
and classes. It helps in avoiding name conflicts, especially in large projects.

34. What is the Standard Template Library (STL)?


Answer: The STL is a collection of template classes and functions in C++ that
provides data structures (like vectors, lists, stacks) and algorithms (like
sorting, searching).

35. What is the difference between a vector and an array in C++?


Answer:
- A vector is a dynamic array that can change its size at runtime, provided by
the STL.
- An array has a fixed size and is allocated statically or dynamically at
compile time.

36. What is function overriding in C++?


Answer: Function overriding occurs when a derived class defines a function with
the same signature as a base class, allowing the derived class's function to

be invoked.

37. What is the difference between function overloading and function overriding?
Answer:
- Function Overloading: Multiple functions with the same name but different
parameters in the same scope.
- Function Overriding: Redefining a base class function in a derived class with
the same signature.

38. What is multiple inheritance in C++?


Answer: Multiple inheritance allows a class to inherit from more than one base
class, thus acquiring properties and behaviors from all parent classes.

39. What are the risks of multiple inheritance in C++?


Answer: It can lead to ambiguity, such as the diamond problem, where a derived
class can inherit the same member multiple times from different paths in the
hierarchy.

40. What is a virtual base class in C++?


Answer: A virtual base class is used to solve the diamond problem in multiple
inheritance by ensuring that only one copy of the base class is inherited by all
derived classes.

---

41. What is a template in C++?


Answer: A template is a blueprint for creating generic functions or classes that
can work with any data type.

42. What is the difference between function templates and class templates in C++?
Answer:
- Function Template: Defines a template for a function to work with any data
type.
- Class Template: Defines a template for a class to work with any data type.

43. What is an exception in C++?


Answer: An exception is a problem that arises during the execution of a program,
such as division by zero or file not found. Exceptions are handled using try,
catch, and throw.

44. What is the use of `try`, `catch`, and `throw` in C++?


Answer:
- try block is used to wrap code that might throw an exception.
- catch block handles the exception thrown in the try block.
- throw is used to signal the occurrence of an exception.

45. What is the difference between `throw` and `throws` in C++?


Answer: C++ uses throw to throw exceptions, while throws does not exist in C++.
(It is a feature in Java.)

46. What is RAII (Resource Acquisition Is Initialization) in C++?


Answer: RAII is a programming technique where resources (like memory, file
handles) are acquired and released using object lifetime. Resources are acquired in
a constructor and released in a destructor.

47. What are smart pointers in C++?


Answer: Smart pointers are objects that manage the lifetime of dynamically
allocated memory. Types of smart pointers include unique_ptr, shared_ptr, and
weak_ptr.

48. What is `unique_ptr` in C++?


Answer: unique_ptr is a smart pointer that owns and manages a resource. It
ensures that there is exactly one unique_ptr to a resource, and when it goes out of
scope, the resource is automatically freed.

49. What is `shared_ptr` in C++?


Answer: shared_ptr is a smart pointer that allows multiple pointers to share
ownership of the same resource. The resource is freed when the last shared_ptr
referencing it goes out of scope.

50. What is `weak_ptr` in C++?


Answer: weak_ptr is a smart pointer that holds a non-owning reference to an
object managed by shared_ptr. It is used to prevent circular references in object
graphs.

51. What is a lambda expression in C++?


Answer: A lambda expression is an anonymous function that can capture variables
from its enclosing scope. It is defined using the [] syntax.

52. What is the significance of the `mutable` keyword in lambda expressions?


Answer: The mutable keyword allows a lambda expression to modify its captured
variables (by value), even though lambdas are const by default.

53. What is type casting in C++?


Answer: Type casting is the process of converting one data type to another. C++
supports four types of casting: static_cast, dynamic_cast, const_cast, and
reinterpret_cast.

54. What is `static_cast` in C++?


Answer: static_cast is used for converting between related types (e.g., from
derived class pointer to base class pointer) and for primitive types.

55. What is `dynamic_cast` in C++?


Answer: dynamic_cast is used for safe downcasting in an inheritance hierarchy.
It checks at runtime whether the cast is valid, and returns nullptr if it is not.

56. What is `const_cast` in C++?


Answer: const_cast is used to add or remove the const qualifier from a variable.
It is typically used when interfacing with legacy code that requires non-const
pointers.

57. What is `reinterpret_cast` in C++?


Answer: reinterpret_cast is used for low-level casting between different types
of pointers or to cast an integral type to a pointer.

58. What is the difference between `static_cast` and `dynamic_cast`?


Answer:
- static_cast performs a compile-time cast and does not check whether the
conversion is valid.
- dynamic_cast performs a runtime check to ensure the validity of the
conversion, making it safer but slower.
59. What are preprocessor directives in C++?
Answer: Preprocessor directives are instructions given to the compiler to
preprocess the code before actual compilation. Examples include #include, #define,
#ifdef, etc.

60. What is `#include` in C++?


Answer: #include is a preprocessor directive used to include the contents of a
file (typically a header file) into the current file.

61. What is the difference between `#include <filename>` and `#include "filename"`?
Answer:
- #include <filename> is used for system or standard library headers, which are
searched in standard directories.
- #include "filename" is used for user-defined headers, which are searched in
the current directory first.

62. What is `#define` in C++?


Answer: #define is a preprocessor directive used to define a macro, which is a
fragment of code that is replaced by its value throughout the program.

63. What is a macro in C++?


Answer: A macro is a fragment of code defined by #define that is replaced by the
preprocessor before compilation.

64. What is the inline function in C++?


Answer: An inline function is a function that is expanded in place where it is
invoked rather than being called as a separate function, which can improve
performance by avoiding function call overhead.

65. What is the difference between a macro and an inline function?


Answer:
- A macro is expanded by the preprocessor, while an inline function is expanded
by the compiler.
- Inline functions have type checking, while macros do not.

66. What is recursion in C++?


Answer: Recursion is a technique where a function calls itself, either directly
or indirectly, to solve a problem by breaking it into smaller sub-problems.

67. What is a pure virtual destructor in C++?


Answer: A pure virtual destructor is a destructor that is declared as virtual
and is set to zero (= 0). It is used in base classes to ensure that derived classes
provide their own destructor implementation.

68. What is the default access specifier for class members in C++?
Answer: The default access specifier for class members in C++ is private.

69. What is the default access specifier for struct members in C++?
Answer: The default access specifier for struct members in C++ is public.

70. What is an enum in C++?


Answer: An enum (enumeration) is a user-defined data type in C++ that consists
of integral constants, and each of them is assigned a name.

71. What is the difference between an enum and a `#define` constant in C++?
Answer:
- An enum is a type-safe way of defining constants, whereas #define is not type-
safe.
- Enum variables can be debugged, whereas #define values are replaced by their
literal values in the code.

72. What is the difference between `new` and `delete` in C++?


Answer:
- new allocates memory dynamically and calls the constructor for object
initialization.
- delete frees the dynamically allocated memory and calls the destructor for
object cleanup.

73. What is the use of `static` keyword in C++?


Answer: The static keyword in C++ serves several purposes:
- Static Variables: Retain their values across function calls and have a local
lifetime but global visibility.
- Static Functions: Can be accessed without creating an instance

of the class, and are shared across all instances.

74. What is the difference between `static` and `const` in C++?


Answer:
- static is used to define variables or methods that belong to the class rather
than an instance.
- const is used to declare variables as unmodifiable after initialization.

75. What is a friend function in C++?


Answer: A friend function is a function that is not a member of a class, but has
access to the class's private and protected members.

You might also like