💻 120+ Questions • Updated 2026

C++ Programming Multiple Choice Questions

Master C++ with 120+ curated MCQs covering basics, OOP, templates, STL, exception handling, and more. Perfect for exams, interviews, and placements.

📚12 Topics
120+ Questions
🎯Exam Ready

📚Explore C++ Topics

📝C++ MCQs(122 questions)

1

Who developed the C++ programming language?

  • A

    Dennis Ritchie

  • B

    Bjarne Stroustrup

  • C

    James Gosling

  • D

    Guido van Rossum

Show Answer
B. Bjarne Stroustrup
2

Which of the following is a valid C++ comment style?

  • A

    // comment

  • B

    /* comment */

  • C

    Both A and B

  • D

    # comment

Show Answer
C. Both A and B
3

Which header file is required for input/output operations in C++?

  • A

    #include <stdio.h>

  • B

    #include <iostream>

  • C

    #include <stdlib.h>

  • D

    #include <cstdio>

Show Answer
B. #include <iostream>
4

What is the extension of a C++ source file?

  • A

    .c

  • B

    .cpp

  • C

    .java

  • D

    .py

Show Answer
B. .cpp
5

Which namespace is used for standard input/output in C++?

  • A

    std

  • B

    iostream

  • C

    c++

  • D

    standard

Show Answer
A. std
6

What is the correct syntax to output 'Hello World' in C++?

  • A

    cout << 'Hello World';

  • B

    cout << 'Hello World';

  • C

    print('Hello World');

  • D

    System.out.println('Hello World');

Show Answer
A. cout << 'Hello World';
7

Which of the following is not a fundamental data type in C++?

  • A

    int

  • B

    float

  • C

    string

  • D

    char

Show Answer
C. string (string is a class, not fundamental)
8

What is the size of 'int' in C++ (on most 32-bit systems)?

  • A

    1 byte

  • B

    2 bytes

  • C

    4 bytes

  • D

    8 bytes

Show Answer
C. 4 bytes
9

Which data type is used to store a single character?

  • A

    char

  • B

    string

  • C

    int

  • D

    bool

Show Answer
A. char
10

What is the size of 'bool' in C++?

  • A

    1 byte

  • B

    2 bytes

  • C

    4 bytes

  • D

    8 bytes

Show Answer
A. 1 byte
11

Which of the following is a user-defined data type in C++?

  • A

    int

  • B

    float

  • C

    struct

  • D

    char

Show Answer
C. struct
12

What is the range of 'signed char' typically?

  • A

    -128 to 127

  • B

    0 to 255

  • C

    -32768 to 32767

  • D

    0 to 65535

Show Answer
A. -128 to 127
13

Which operator is used to allocate memory dynamically in C++?

  • A

    malloc

  • B

    new

  • C

    alloc

  • D

    create

Show Answer
B. new
14

Which operator is used to deallocate memory in C++?

  • A

    free

  • B

    delete

  • C

    dealloc

  • D

    remove

Show Answer
B. delete
15

What is the result of (5 / 2) in C++?

  • A

    2.5

  • B

    2

  • C

    3

  • D

    2.0

Show Answer
B. 2 (integer division)
16

Which operator is used for logical AND in C++?

  • A

    &

  • B

    &&

  • C

    |

  • D

    ||

Show Answer
B. &&
17

What is the purpose of the scope resolution operator (::)?

  • A

    Access global variables

  • B

    Access namespace members

  • C

    Define functions outside class

  • D

    All of the above

Show Answer
D. All of the above
18

Which operator is used to access a member of a pointer to an object?

  • A

    .

  • B

    ->

  • C

    ::

  • D

    *

Show Answer
B. ->
19

Which statement is used to exit a loop prematurely in C++?

  • A

    break

  • B

    continue

  • C

    exit

  • D

    return

Show Answer
A. break
20

Which loop guarantees that the body executes at least once?

  • A

    for

  • B

    while

  • C

    do-while

  • D

    None

Show Answer
C. do-while
21

What is the correct syntax for an if-else statement in C++?

  • A

    if (condition) { } else { }

  • B

    if condition { } else { }

  • C

    if (condition) { } elif { }

  • D

    if (condition) then { } else { }

Show Answer
A. if (condition) { } else { }
22

Which statement is used to skip the current iteration of a loop?

  • A

    break

  • B

    continue

  • C

    goto

  • D

    return

Show Answer
B. continue
23

What is the output of the following? int x = 5; if (x > 3) cout << 'Yes'; else cout << 'No';

  • A

    Yes

  • B

    No

  • C

    YesNo

  • D

    Error

Show Answer
A. Yes
24

Which of the following is a valid switch expression in C++?

  • A

    int

  • B

    char

  • C

    enum

  • D

    All of the above

Show Answer
D. All of the above
25

What is the default return type of a function in C++?

  • A

    void

  • B

    int

  • C

    float

  • D

    None (must specify)

Show Answer
D. None (must specify)
26

Which of the following is used to pass arguments by reference in C++?

  • A

    int &a

  • B

    int *a

  • C

    int a

  • D

    const int a

Show Answer
A. int &a
27

What is function overloading in C++?

  • A

    Multiple functions with the same name but different parameters

  • B

    Multiple functions with same name and same parameters

  • C

    Function that calls itself

  • D

    Function that returns multiple values

Show Answer
A. Multiple functions with the same name but different parameters
28

What is the default value of a parameter in a function if not specified?

  • A

    0

  • B

    NULL

  • C

    Depends on type (undefined)

  • D

    No default; it's an error

Show Answer
C. Depends on type (undefined)
29

Which keyword is used to define a function as inline?

  • A

    inline

  • B

    static

  • C

    virtual

  • D

    extern

Show Answer
A. inline
30

What is a friend function in C++?

  • A

    A non-member function that can access private members of a class

  • B

    A member function of a class

  • C

    A function that is declared in a namespace

  • D

    A function that cannot be overloaded

Show Answer
A. A non-member function that can access private members of a class
31

What is the index of the first element in a C++ array?

  • A

    0

  • B

    1

  • C

    -1

  • D

    Depends on declaration

Show Answer
A. 0
32

Which function is used to get the length of a C-style string?

  • A

    strlen()

  • B

    length()

  • C

    size()

  • D

    strlength()

Show Answer
A. strlen()
33

How to declare an array of 10 integers in C++?

  • A

    int arr[10];

  • B

    array int[10];

  • C

    int[10] arr;

  • D

    arr int[10];

Show Answer
A. int arr[10];
34

Which class in C++ is used for strings?

  • A

    string

  • B

    String

  • C

    std::string

  • D

    Both A and C

Show Answer
D. Both A and C
35

What is the output of the following code? char s[] = 'Hello'; cout << sizeof(s);

  • A

    5

  • B

    6

  • C

    4

  • D

    Error

Show Answer
B. 6 (includes null terminator)
36

Which function concatenates two strings in C++ (C-style)?

  • A

    strcat

  • B

    strcpy

  • C

    strcmp

  • D

    strlen

Show Answer
A. strcat
37

What is a pointer in C++?

  • A

    A variable that stores the address of another variable

  • B

    A variable that stores a value

  • C

    A reference to a function

  • D

    A type of array

Show Answer
A. A variable that stores the address of another variable
38

Which operator is used to get the address of a variable?

  • A

    &

  • B

    *

  • C

    ->

  • D

    ::

Show Answer
A. &
39

Which operator is used to dereference a pointer?

  • A

    &

  • B

    *

  • C

    ->

  • D

    .

Show Answer
B. *
40

What is a null pointer?

  • A

    Pointer that points to address 0

  • B

    Pointer that points to garbage value

  • C

    Pointer that points to a valid memory location

  • D

    Pointer that is not initialized

Show Answer
A. Pointer that points to address 0
41

What is a pointer to a pointer?

  • A

    Pointer that stores the address of another pointer

  • B

    Pointer that stores a value

  • C

    Pointer that is declared as void

  • D

    Pointer that cannot be dereferenced

Show Answer
A. Pointer that stores the address of another pointer
42

What is the size of a pointer on a 64-bit system?

  • A

    4 bytes

  • B

    8 bytes

  • C

    2 bytes

  • D

    16 bytes

Show Answer
B. 8 bytes
43

What is a class in C++?

  • A

    A user-defined data type that encapsulates data and functions

  • B

    A built-in data type

  • C

    A function that operates on data

  • D

    A namespace

Show Answer
A. A user-defined data type that encapsulates data and functions
44

What is an object in C++?

  • A

    An instance of a class

  • B

    A function of a class

  • C

    A data member of a class

  • D

    A type of pointer

Show Answer
A. An instance of a class
45

Which access specifier allows members to be accessible only within the class?

  • A

    public

  • B

    protected

  • C

    private

  • D

    internal

Show Answer
C. private
46

What is a constructor in C++?

  • A

    A special member function that initializes objects

  • B

    A function that destroys objects

  • C

    A function that copies objects

  • D

    A function that assigns objects

Show Answer
A. A special member function that initializes objects
47

What is a destructor in C++?

  • A

    A special member function that cleans up before object destruction

  • B

    A function that creates objects

  • C

    A function that copies objects

  • D

    A function that overloads operators

Show Answer
A. A special member function that cleans up before object destruction
48

What is the 'this' pointer in C++?

  • A

    Pointer to the current object

  • B

    Pointer to the base class

  • C

    Pointer to the derived class

  • D

    Pointer to the class itself

Show Answer
A. Pointer to the current object
49

What is inheritance in C++?

  • A

    Deriving a new class from an existing class

  • B

    Creating multiple objects of a class

  • C

    Combining multiple classes

  • D

    Overloading functions

Show Answer
A. Deriving a new class from an existing class
50

Which type of inheritance leads to the diamond problem?

  • A

    Single inheritance

  • B

    Multiple inheritance

  • C

    Multilevel inheritance

  • D

    Hierarchical inheritance

Show Answer
B. Multiple inheritance
51

What is virtual inheritance used for?

  • A

    To solve the diamond problem

  • B

    To override functions

  • C

    To define abstract classes

  • D

    To create templates

Show Answer
A. To solve the diamond problem
52

Which specifier allows base class members to be accessible in derived classes but not outside?

  • A

    public

  • B

    protected

  • C

    private

  • D

    default

Show Answer
B. protected
53

What is the order of constructor execution in inheritance?

  • A

    Base class constructor first, then derived class

  • B

    Derived class constructor first, then base class

  • C

    Both execute simultaneously

  • D

    Depends on the compiler

Show Answer
A. Base class constructor first, then derived class
54

What is the order of destructor execution in inheritance?

  • A

    Derived class destructor first, then base class

  • B

    Base class destructor first, then derived class

  • C

    Both execute simultaneously

  • D

    Depends on the compiler

Show Answer
A. Derived class destructor first, then base class
55

What is polymorphism in C++?

  • A

    Ability to take many forms

  • B

    Ability to inherit from multiple classes

  • C

    Ability to overload operators

  • D

    Ability to create templates

Show Answer
A. Ability to take many forms
56

Which keyword is used to enable function overriding in C++?

  • A

    virtual

  • B

    override

  • C

    final

  • D

    abstract

Show Answer
A. virtual
57

What is a pure virtual function?

  • A

    A virtual function with no implementation in base class

  • B

    A virtual function that is overridden

  • C

    A function that cannot be overridden

  • D

    A function that is declared as static

Show Answer
A. A virtual function with no implementation in base class
58

What is a abstract class?

  • A

    A class that has at least one pure virtual function

  • B

    A class that cannot be inherited

  • C

    A class that has no data members

  • D

    A class that is declared as final

Show Answer
A. A class that has at least one pure virtual function
59

What is run-time polymorphism?

  • A

    Achieved via virtual functions and inheritance

  • B

    Achieved via function overloading

  • C

    Achieved via templates

  • D

    Achieved via operator overloading

Show Answer
A. Achieved via virtual functions and inheritance
60

What is compile-time polymorphism?

  • A

    Achieved via function overloading and templates

  • B

    Achieved via virtual functions

  • C

    Achieved via inheritance

  • D

    Achieved via abstract classes

Show Answer
A. Achieved via function overloading and templates
61

What is a template in C++?

  • A

    A blueprint for creating generic classes and functions

  • B

    A class that cannot be instantiated

  • C

    A function that cannot be overridden

  • D

    A type of inheritance

Show Answer
A. A blueprint for creating generic classes and functions
62

Which keyword is used to define a template in C++?

  • A

    template

  • B

    typename

  • C

    class

  • D

    Both A and B or C

Show Answer
D. Both A and B or C
63

What is the difference between typename and class in template parameters?

  • A

    No difference; they are interchangeable

  • B

    typename is used for types, class for classes

  • C

    class is used for types, typename for classes

  • D

    typename is used for templates, class for classes

Show Answer
A. No difference; they are interchangeable
64

What is template specialization?

  • A

    Defining a specific implementation for a particular type

  • B

    Defining a template with multiple parameters

  • C

    Defining a template without parameters

  • D

    Defining a template as virtual

Show Answer
A. Defining a specific implementation for a particular type
65

What is a function template?

  • A

    A template that defines a generic function

  • B

    A template that defines a generic class

  • C

    A template that defines a variable

  • D

    A template that defines a namespace

Show Answer
A. A template that defines a generic function
66

What is a class template?

  • A

    A template that defines a generic class

  • B

    A template that defines a generic function

  • C

    A template that defines a variable

  • D

    A template that defines a namespace

Show Answer
A. A template that defines a generic class
67

What does STL stand for?

  • A

    Standard Template Library

  • B

    Standard Type Library

  • C

    System Template Library

  • D

    Standard Tool Library

Show Answer
A. Standard Template Library
68

Which container provides fast random access and is dynamic in size?

  • A

    vector

  • B

    list

  • C

    deque

  • D

    set

Show Answer
A. vector
69

Which container is implemented as a doubly linked list?

  • A

    vector

  • B

    list

  • C

    deque

  • D

    stack

Show Answer
B. list
70

Which algorithm is used to sort elements in a range?

  • A

    std::sort

  • B

    std::find

  • C

    std::reverse

  • D

    std::copy

Show Answer
A. std::sort
71

What is an iterator in STL?

  • A

    An object that points to an element in a container

  • B

    A container type

  • C

    An algorithm

  • D

    A function object

Show Answer
A. An object that points to an element in a container
72

Which container stores unique keys in sorted order?

  • A

    set

  • B

    map

  • C

    unordered_set

  • D

    multiset

Show Answer
A. set
73

Which keyword is used to throw an exception in C++?

  • A

    throw

  • B

    catch

  • C

    try

  • D

    exception

Show Answer
A. throw
74

Which block is used to handle exceptions in C++?

  • A

    catch

  • B

    try

  • C

    throw

  • D

    finally

Show Answer
A. catch
75

What is the purpose of the 'try' block?

  • A

    To enclose code that might throw an exception

  • B

    To handle exceptions

  • C

    To throw exceptions

  • D

    To define custom exceptions

Show Answer
A. To enclose code that might throw an exception
76

Can you catch an exception using 'catch (...)'?

  • A

    Yes, catches any exception

  • B

    No, not allowed

  • C

    Only if no other catch is used

  • D

    Only for standard exceptions

Show Answer
A. Yes, catches any exception
77

What is a standard exception class in C++?

  • A

    std::exception

  • B

    std::error

  • C

    std::runtime_error

  • D

    Both A and C

Show Answer
D. Both A and C
78

What happens if an exception is thrown but not caught?

  • A

    Program terminates with an error

  • B

    Program continues execution

  • C

    Exception is ignored

  • D

    Compiler gives an error

Show Answer
A. Program terminates with an error
79

Which header file is used for file input/output in C++?

  • A

    #include <fstream>

  • B

    #include <iostream>

  • C

    #include <file>

  • D

    #include <stdio.h>

Show Answer
A. #include <fstream>
80

Which class is used for writing to a file in C++?

  • A

    ifstream

  • B

    ofstream

  • C

    fstream

  • D

    filebuf

Show Answer
B. ofstream
81

Which class is used for reading from a file in C++?

  • A

    ifstream

  • B

    ofstream

  • C

    fstream

  • D

    filebuf

Show Answer
A. ifstream
82

Which mode is used to append to a file?

  • A

    ios::app

  • B

    ios::out

  • C

    ios::in

  • D

    ios::binary

Show Answer
A. ios::app
83

How do you check if a file was opened successfully in C++?

  • A

    file.is_open()

  • B

    file.open()

  • C

    file.good()

  • D

    Both A and C

Show Answer
D. Both A and C
84

Which function is used to close a file in C++?

  • A

    file.close()

  • B

    file.end()

  • C

    file.exit()

  • D

    file.stop()

Show Answer
A. file.close()
85

What is the main function in C++?

  • A

    main()

  • B

    Main()

  • C

    int main()

  • D

    void main()

Show Answer
C. int main() (standard)
86

What is the size of 'long double' in C++ (on most systems)?

  • A

    8 bytes

  • B

    10 bytes

  • C

    16 bytes

  • D

    4 bytes

Show Answer
C. 16 bytes (or 10/12 depending on compiler)
87

What is the result of (5 % 2) in C++?

  • A

    1

  • B

    2

  • C

    0

  • D

    2.5

Show Answer
A. 1
88

Which loop is best when the number of iterations is known?

  • A

    for

  • B

    while

  • C

    do-while

  • D

    All are equally suitable

Show Answer
A. for
89

What is a recursive function?

  • A

    A function that calls itself

  • B

    A function that is overloaded

  • C

    A function that is virtual

  • D

    A function that is inline

Show Answer
A. A function that calls itself
90

What is the output? int arr[] = {1,2,3}; cout << arr[2];

  • A

    1

  • B

    2

  • C

    3

  • D

    0

Show Answer
C. 3
91

What is the type of a pointer to a function?

  • A

    int (*func)()

  • B

    int *func()

  • C

    int func*()

  • D

    function pointer

Show Answer
A. int (*func)()
92

Which of the following is not a OOP concept in C++?

  • A

    Encapsulation

  • B

    Inheritance

  • C

    Polymorphism

  • D

    Procedural abstraction

Show Answer
D. Procedural abstraction
93

Which type of inheritance is used to create a relationship like 'is-a'?

  • A

    Composition

  • B

    Aggregation

  • C

    Inheritance

  • D

    Association

Show Answer
C. Inheritance
94

What is the use of the 'final' specifier in C++?

  • A

    Prevents a class from being inherited or function overridden

  • B

    Marks a function as virtual

  • C

    Marks a function as abstract

  • D

    Marks a function as inline

Show Answer
A. Prevents a class from being inherited or function overridden
95

What is the syntax for a template parameter with a default type?

  • A

    template <typename T = int>

  • B

    template <class T = int>

  • C

    Both A and B

  • D

    template <T = int>

Show Answer
C. Both A and B
96

Which container is a stack implemented as?

  • A

    std::stack

  • B

    std::queue

  • C

    std::deque

  • D

    std::list

Show Answer
A. std::stack
97

Can you throw an exception of any type?

  • A

    Yes, any type can be thrown

  • B

    No, only derived from std::exception

  • C

    Only built-in types

  • D

    Only user-defined types

Show Answer
A. Yes, any type can be thrown
98

What is the default open mode for fstream?

  • A

    ios::in | ios::out

  • B

    ios::in

  • C

    ios::out

  • D

    ios::app

Show Answer
A. ios::in | ios::out
99

What is the use of the 'using' directive?

  • A

    To bring a namespace into scope

  • B

    To define a new type

  • C

    To create a pointer

  • D

    To allocate memory

Show Answer
A. To bring a namespace into scope
100

What is the size of 'wchar_t' in C++?

  • A

    2 bytes

  • B

    4 bytes

  • C

    1 byte

  • D

    Depends on compiler

Show Answer
D. Depends on compiler
101

Which operator cannot be overloaded in C++?

  • A

    :: (scope resolution)

  • B

    +

  • C

    ->

  • D

    []

Show Answer
A. :: (scope resolution)
102

What is the output? int i = 0; while (i < 3) { cout << i; i++; }

  • A

    012

  • B

    123

  • C

    0123

  • D

    0 1 2

Show Answer
A. 012
103

What is a static function in C++?

  • A

    A function that belongs to a class but can be called without an object

  • B

    A function that cannot be changed

  • C

    A function that is not accessible outside

  • D

    A function that returns void

Show Answer
A. A function that belongs to a class but can be called without an object
104

What is the difference between C-style string and std::string?

  • A

    std::string is a class with methods; C-style is character array

  • B

    C-style is faster

  • C

    std::string uses null terminator

  • D

    No difference

Show Answer
A. std::string is a class with methods; C-style is character array
105

What is a smart pointer in C++?

  • A

    A pointer that automatically manages memory

  • B

    A pointer that is faster

  • C

    A pointer that is global

  • D

    A pointer that cannot be dereferenced

Show Answer
A. A pointer that automatically manages memory
106

Which of the following is a type of constructor?

  • A

    Default constructor

  • B

    Parameterized constructor

  • C

    Copy constructor

  • D

    All of the above

Show Answer
D. All of the above
107

What is the difference between public, protected, and private inheritance?

  • A

    They change the access levels of base class members in derived class

  • B

    They change the speed of inheritance

  • C

    They change the number of base classes

  • D

    They are all the same

Show Answer
A. They change the access levels of base class members in derived class
108

What is the use of the 'override' keyword (C++11)?

  • A

    To explicitly mark a function as overriding a virtual function

  • B

    To define a new function

  • C

    To prevent overriding

  • D

    To make a function pure virtual

Show Answer
A. To explicitly mark a function as overriding a virtual function
109

Can a template have non-type parameters?

  • A

    Yes, e.g., template <int N>

  • B

    No, only types

  • C

    Only for classes, not functions

  • D

    Only for functions, not classes

Show Answer
A. Yes, e.g., template <int N>
110

Which STL algorithm is used to find an element in a range?

  • A

    std::find

  • B

    std::search

  • C

    std::binary_search

  • D

    All of the above

Show Answer
D. All of the above
111

What is the purpose of the 'noexcept' specifier?

  • A

    Indicates that a function does not throw exceptions

  • B

    Indicates that a function always throws

  • C

    Indicates that a function can throw any exception

  • D

    Indicates that a function is virtual

Show Answer
A. Indicates that a function does not throw exceptions
112

How to read a line from a file in C++?

  • A

    getline(file, line)

  • B

    file >> line

  • C

    file.readline(line)

  • D

    Both A and B

Show Answer
A. getline(file, line)
113

What is the difference between #include <header> and #include 'header'?

  • A

    <> searches system paths, '' searches local directories first

  • B

    '' searches system paths, <> searches local

  • C

    Both are the same

  • D

    <> is for C, '' for C++

Show Answer
A. <> searches system paths, '' searches local directories first
114

What is the size of 'long long' in C++?

  • A

    8 bytes

  • B

    4 bytes

  • C

    16 bytes

  • D

    2 bytes

Show Answer
A. 8 bytes
115

Which operator is used to create a new object on the heap?

  • A

    new

  • B

    malloc

  • C

    alloc

  • D

    create

Show Answer
A. new
116

What is the output? int x = 10; if (x = 5) cout << 'Yes'; else cout << 'No';

  • A

    Yes

  • B

    No

  • C

    Error

  • D

    Undefined

Show Answer
A. Yes (since x=5 returns 5, which is true)
117

What is a lambda expression in C++?

  • A

    An anonymous function

  • B

    A named function

  • C

    A function template

  • D

    A virtual function

Show Answer
A. An anonymous function
118

What is the output? char str[] = 'Hello'; cout << strlen(str);

  • A

    5

  • B

    6

  • C

    4

  • D

    Error

Show Answer
A. 5
119

What is a function pointer?

  • A

    A pointer that stores the address of a function

  • B

    A pointer that points to a variable

  • C

    A pointer that points to a class

  • D

    A pointer that cannot be dereferenced

Show Answer
A. A pointer that stores the address of a function
120

What is the order of initialization of static members?

  • A

    In the order they are defined

  • B

    In reverse order

  • C

    Random order

  • D

    Depends on compiler

Show Answer
A. In the order they are defined
121

What is a virtual base class?

  • A

    A base class that is inherited virtually to avoid duplication

  • B

    A base class with virtual functions

  • C

    A base class that is abstract

  • D

    A base class that cannot be inherited

Show Answer
A. A base class that is inherited virtually to avoid duplication
122

Which container uses a binary tree for storage?

  • A

    set

  • B

    vector

  • C

    list

  • D

    deque

Show Answer
A. set

Frequently Asked Questions

What are the key features of C++?
C++ is a general-purpose programming language that supports object-oriented programming, generic programming, and low-level memory manipulation. Key features include classes, inheritance, polymorphism, templates, exception handling, and the Standard Template Library (STL).
What is the difference between C and C++?
C++ is an extension of C that adds object-oriented features like classes, inheritance, polymorphism, encapsulation, and abstraction. C++ also supports function overloading, references, and the Standard Template Library (STL), while C is a procedural language.
What is the Standard Template Library (STL)?
STL is a collection of template classes and functions that provide common data structures (e.g., vector, list, map, set) and algorithms (e.g., sort, find, binary_search). It is a powerful library that simplifies programming by providing reusable components.
What is polymorphism in C++?
Polymorphism means 'many forms'. In C++, it allows objects of different classes to be treated as objects of a common base class. It is achieved through virtual functions and function overloading. There are two types: compile-time (overloading) and runtime (virtual functions).
What is the difference between pass by value and pass by reference?
Pass by value creates a copy of the argument, so changes inside the function do not affect the original. Pass by reference (using &) passes a reference to the original variable, allowing modifications to affect the original. Pass by reference is more efficient for large objects.
What is the role of the 'virtual' keyword?
The 'virtual' keyword is used to declare a function as virtual, enabling runtime polymorphism. When a virtual function is overridden in a derived class, the correct function is called based on the actual object type, not the pointer type. It is essential for dynamic binding.
What are smart pointers in C++?
Smart pointers are template classes that manage dynamically allocated memory automatically. They include unique_ptr, shared_ptr, and weak_ptr. They help prevent memory leaks by automatically deallocating memory when the pointer goes out of scope.
What is the difference between a class and a struct in C++?
In C++, the only difference between a class and a struct is the default access specifier. Members of a class are private by default, while members of a struct are public by default. Both can have member functions, constructors, and inheritance.