Question library

C++ interview questions and answers

45 C++ interview questions with answers: 10 junior, 15 mid-level and 20 senior. Junior answers are free to read.

Junior Mid Senior Updated How these answers are created

* View counts were reset on 18 October 2026, when Interviewplus moved to interviewplus.app. Votes were not affected.

Answer

In C++, both structs and classes are used to define user-defined types with data members and member functions. The main difference between the two is the default access level of their members and the default inheritance type.

In a struct, by default, all members are public, and in a class, by default, all members are private. This means that if you define a struct like this:

struct MyStruct {
  int x;
  void printX() {
    std::cout << x << std::endl;
  }
};

Then x and printX() can be accessed from outside the struct without any restrictions:

MyStruct s;
s.x = 10;
s.printX();  // prints 10

If you define a class like this:

class MyClass {
  int x;
  void printX() {
    std::cout << x << std::endl;
  }
};

Then x and printX() are both private, and cannot be accessed from outside the class:

MyClass c;
c.x = 10;    // compile error: 'x' is private
c.printX();  // compile error: 'printX' is private

To access private members of a class, you need to define public member functions or use friend functions.

Another difference is that in a class, the default inheritance type is private, whereas in a struct, it is public. This means that if you derive a class from a struct, the derived class will be a public inheritance, and if you derive a class from a class, the derived class will be a private inheritance. However, it is usually a good practice to explicitly specify the inheritance type in the code.

Answer

Sure, here's an example program in C++ that calculates the factorial of a given number using a recursive function:

#include <iostream>
using namespace std;

// Recursive function to calculate the factorial of a given number
int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    int n;
    cout << "Enter a number to calculate its factorial: ";
    cin >> n;

    // Call the factorial function and display the result
    int result = factorial(n);
    cout << n << "! = " << result << endl;

    return 0;
}

To use this program, simply compile and run it in a C++ compiler. When prompted, enter the number you want to calculate the factorial of, and the program will output the result.

Answer

In C++, a pointer is a variable that holds the memory address of another variable. Pointers are used to manipulate and work with memory locations directly, which can be useful in certain programming scenarios.

Here is an example of how to declare a pointer in C++:

int x = 10;     // Declare and initialize an integer variable 'x'
int* ptr = &x;  // Declare a pointer to an integer variable 'ptr' and initialize it with the address of 'x'

In the example above, the & operator is used to get the memory address of the variable x, which is then assigned to the pointer variable ptr.

Once a pointer is declared, you can use the * operator to access the value at the memory location it points to, or to assign a new value to that memory location. Here is an example:

std::cout << *ptr << std::endl;  // Output: 10

*ptr = 20;  // Assign the value 20 to the memory location pointed to by 'ptr'

std::cout << *ptr << std::endl;  // Output: 20
std::cout << x << std::endl;     // Output: 20 (the value of 'x' has been changed)

In the example above, the * operator is used to access the value stored in the memory location pointed to by ptr. Then, the value of x is changed by assigning a new value to the memory location pointed to by ptr using the * operator.

Pointers can also be used to dynamically allocate memory in C++ using the new operator. For example:

int* arr = new int[10];  // Dynamically allocate an array of 10 integers and assign the pointer to 'arr'

arr[0] = 1;  // Assign the value 1 to the first element of the array
std::cout << arr[0] << std::endl;  // Output: 1

delete[] arr;  // Free the dynamically allocated memory

In the example above, the new operator is used to allocate an array of 10 integers on the heap, and the pointer to the first element of the array is assigned to arr. Then, the first element of the array is assigned a value of 1 using array indexing. Finally, the delete[] operator is used to free the dynamically allocated memory.

Answer

In C++, conditional statements such as if-else statements and switch statements are used to control the flow of the program based on certain conditions. Here's how you can implement them:

If-else statements

An if-else statement is used to execute a block of code if a certain condition is true, and a different block of code if the condition is false. Here's an example:

int x = 10;

if (x > 5) {
  std::cout << "x is greater than 5" << std::endl;
} else {
  std::cout << "x is less than or equal to 5" << std::endl;
}

In the example above, the code inside the curly braces after the if keyword will be executed if the condition x > 5 is true. If the condition is false, the code inside the curly braces after the else keyword will be executed.

Switch statements

A switch statement is used to execute a block of code based on the value of a variable. Here's an example:

int x = 2;

switch (x) {
  case 1:
    std::cout << "x is 1" << std::endl;
    break;
  case 2:
    std::cout << "x is 2" << std::endl;
    break;
  case 3:
    std::cout << "x is 3" << std::endl;
    break;
  default:
    std::cout << "x is not 1, 2, or 3" << std::endl;
    break;
}

In the example above, the value of x is evaluated against each case statement. If x matches the value in a case statement, the code inside the corresponding block will be executed. If x does not match any of the case statements, the code inside the default block will be executed.

Note that the break keyword is used after each block of code inside a case statement to exit the switch block and prevent the execution of subsequent blocks.

 

Answer

In C++, loops are used to repeat a block of code multiple times until a certain condition is met. There are three types of loops in C++: for loops, while loops, and do-while loops.

For loops

A for loop is used to repeat a block of code a specific number of times. Here's an example:

for (int i = 0; i < 10; i++) {
  std::cout << i << std::endl;
}

In the example above, the loop will execute 10 times, with the value of i starting at 0 and incrementing by 1 each time through the loop. The condition i < 10 is checked at the beginning of each iteration, and the loop will terminate when the condition is false.

While loops

A while loop is used to repeat a block of code as long as a certain condition is true. Here's an example:

int i = 0;

while (i < 10) {
  std::cout << i << std::endl;
  i++;
}

In the example above, the loop will execute as long as i is less than 10. The value of i is incremented inside the loop to prevent an infinite loop.

Do-while loops

A do-while loop is similar to a while loop, but the block of code is executed at least once, even if the condition is false. Here's an example:

int i = 0;

do {
  std::cout << i << std::endl;
  i++;
} while (i < 10);

In the example above, the loop will execute at least once, even if i is not less than 10. The value of i is incremented inside the loop to prevent an infinite loop.

In all types of loops, the break keyword can be used to exit the loop prematurely, and the continue keyword can be used to skip the current iteration of the loop and move on to the next iteration.

Answer

In C++, arrays and vectors are used to store a collection of values of the same type. Here's how you can implement them:

Arrays

An array is a collection of elements of the same data type, stored in contiguous memory locations. Here's an example of how to declare and initialize an array:

int myArray[5] = {1, 2, 3, 4, 5};

In the example above, an array myArray is declared with a size of 5, and is initialized with the values 1 through 5. You can access individual elements of the array using the index notation, like this:

int x = myArray[2]; // x will be 3

Arrays are often used when you need to store a fixed number of elements of the same type, such as a list of grades for a class of students or a list of temperatures for a given day.

Vectors

A vector is a dynamic array that can grow or shrink in size at runtime. Here's an example of how to declare and initialize a vector:

std::vector<int> myVector = {1, 2, 3, 4, 5};

In the example above, a vector myVector is declared and initialized with the same values as the previous example. You can access individual elements of the vector using the index notation, like this:

int x = myVector[2]; // x will be 3

Vectors are often used when you need to store a variable number of elements of the same type, such as a list of user IDs or a list of items in a shopping cart. Vectors are also useful when you need to add or remove elements from the collection, since they can automatically adjust their size. To add an element to a vector, you can use the push_back method:

myVector.push_back(6); // adds the value 6 to the end of the vector

To remove an element from a vector, you can use the pop_back method:

myVector.pop_back(); // removes the last element from the vector

Answer

Here's an example of a C++ console app that allows users to create and manage a to-do list:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Todo {
    string task;
    string dueDate;
    bool completed;
};

vector<Todo> todos;

void addTodo() {
    Todo newTodo;
    cout << "Enter task name: ";
    cin >> newTodo.task;
    cout << "Enter due date (MM/DD/YYYY): ";
    cin >> newTodo.dueDate;
    newTodo.completed = false;
    todos.push_back(newTodo);
    cout << "Task added successfully!" << endl;
}

void displayTodos() {
    if (todos.size() == 0) {
        cout << "No tasks found." << endl;
        return;
    }
    cout << "Todo List:" << endl;
    for (int i = 0; i < todos.size(); i++) {
        Todo currentTodo = todos[i];
        cout << i+1 << ". ";
        if (currentTodo.completed) {
            cout << "[X] ";
        } else {
            cout << "[ ] ";
        }
        cout << currentTodo.task << " - Due on " << currentTodo.dueDate << endl;
    }
}

void markTodoCompleted() {
    int index;
    cout << "Enter index of task to mark as completed: ";
    cin >> index;
    if (index < 1 || index > todos.size()) {
        cout << "Invalid index." << endl;
        return;
    }
    Todo& todoToMark = todos[index-1];
    todoToMark.completed = true;
    cout << "Task marked as completed!" << endl;
}

int main() {
    cout << "Welcome to your to-do list!" << endl;
    while (true) {
        cout << endl;
        cout << "Select an option:" << endl;
        cout << "1. Add task" << endl;
        cout << "2. Display tasks" << endl;
        cout << "3. Mark task as completed" << endl;
        cout << "4. Exit" << endl;
        int choice;
        cin >> choice;
        switch (choice) {
            case 1:
                addTodo();
                break;
            case 2:
                displayTodos();
                break;
            case 3:
                markTodoCompleted();
                break;
            case 4:
                cout << "Exiting..." << endl;
                return 0;
            default:
                cout << "Invalid choice." << endl;
                break;
        }
    }
}

This program allows users to add tasks to their to-do list, display all tasks, and mark tasks as completed. The to-do list is stored in a vector of Todo structs, which contain the task name, due date, and completion status. The program uses a simple menu system to allow the user to choose between different options.

Answer

In C++, object-oriented programming (OOP) concepts such as inheritance and polymorphism can be implemented using classes and derived classes. Here's how:

Inheritance

Inheritance allows you to create a new class based on an existing class, inheriting its properties and behaviors. The existing class is called the base class, and the new class is called the derived class. Here's an example of how to create a derived class:

class Animal {
  public:
    void eat() {
      std::cout << "The animal is eating." << std::endl;
    }
};

class Dog : public Animal {
  public:
    void bark() {
      std::cout << "The dog is barking." << std::endl;
    }
};

In the example above, the Dog class is derived from the Animal class using the public access specifier. This means that the public members of the Animal class are accessible from the Dog class. The Dog class also has its own method bark.

Polymorphism

Polymorphism allows you to use a derived class object as if it were an object of its base class. This allows you to write generic code that can be used with different derived classes. Polymorphism can be achieved using virtual functions. Here's an example:

class Animal {
  public:
    virtual void makeSound() {
      std::cout << "The animal makes a sound." << std::endl;
    }
};

class Dog : public Animal {
  public:
    void makeSound() {
      std::cout << "The dog barks." << std::endl;
    }
};

void animalSound(Animal *animal) {
  animal->makeSound();
}

int main() {
  Animal *animal = new Animal();
  Dog *dog = new Dog();
  
  animalSound(animal); // The animal makes a sound.
  animalSound(dog); // The dog barks.
  
  delete animal;
  delete dog;
  
  return 0;
}

In the example above, the Animal class has a virtual method makeSound. This allows derived classes to override the method with their own implementation. The Dog class overrides the method with its own implementation. The animalSound function takes a pointer to an Animal object as an argument, and calls its makeSound method. This allows it to be used with any derived class of Animal.

Answer

In C++, templates are used to create generic classes and functions that can work with different data types. Templates allow you to write code once and use it with different types, rather than having to write separate code for each type. Here's an example of how to create a template class:

template <class T>
class MyArray {
  private:
    T *array;
    int size;
  
  public:
    MyArray(int n) {
      size = n;
      array = new T[size];
    }
    
    T& operator[](int index) {
      return array[index];
    }
    
    ~MyArray() {
      delete[] array;
    }
};

In the example above, the MyArray class is a template class that takes a type parameter T. The class has a private data member array which is a pointer to an array of type T. The constructor creates an array of size n with type T. The operator[] function overloads the subscript operator to allow you to access elements of the array using the [] operator. The destructor deallocates the memory used by the array.

Here's an example of how to use the MyArray template class:

MyArray<int> intArray(10);
MyArray<double> doubleArray(5);

intArray[0] = 1;
intArray[1] = 2;
doubleArray[0] = 1.1;
doubleArray[1] = 2.2;

In the example above, two instances of the MyArray class are created: intArray with type int and size 10, and doubleArray with type double and size 5. Elements of the arrays can be accessed using the subscript operator [].

Templates can also be used to create generic functions that can work with different types. Here's an example of how to create a template function:

template <class T>
T max(T x, T y) {
  return (x > y) ? x : y;
}

In the example above, the max function is a template function that takes two arguments of type T and returns the maximum value. The function can be called with different types:

int a = 5, b = 10;
double c = 2.5, d = 7.3;

std::cout << max(a, b) << std::endl; // Output: 10
std::cout << max(c, d) << std::endl; // Output: 7.3

 

Answer

In C++, input/output operations can be performed using the iostream library for console input/output and the fstream library for file input/output. Here's an example of how to read and write to a file using the fstream library:

#include <iostream>
#include <fstream>

int main() {
  std::ofstream outfile("example.txt"); // create output file stream
  outfile << "Hello, world!" << std::endl; // write to file
  outfile.close(); // close output file stream
  
  std::ifstream infile("example.txt"); // create input file stream
  std::string line;
  while (std::getline(infile, line)) { // read from file line by line
    std::cout << line << std::endl; // print each line to console
  }
  infile.close(); // close input file stream
  
  return 0;
}

In the example above, the ofstream class is used to create an output file stream to write data to the file "example.txt". The << operator is used to write the string "Hello, world!" to the file, followed by a newline character. The close method is then called to close the output file stream.

The ifstream class is then used to create an input file stream to read data from the file "example.txt". The getline method is used to read data from the file line by line into the line string variable. Each line is then printed to the console using the cout statement. Finally, the close method is called to close the input file stream.

This is a simple example of reading and writing to a file in C++. There are many more operations and features available in the iostream and fstream libraries, such as reading and writing binary data, seeking to specific positions in the file, and more.

Answer preview

Here's a sample C++ program that reads a text file and counts the frequency of each word: This program uses an unordered_map to store the frequency of each word. It reads the input file line by line and uses transform and erase functions to convert each word to lowercase and remove any non-alphabetic characters. It…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, dynamic memory allocation can be performed using the new and delete operators. Here's an example of how to use these operators to allocate and deallocate memory for an integer: In this example, the new operator is used to allocate memory for an integer, which is stored in the pointer variable ptr. The *…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, exception handling is a mechanism for handling errors or exceptional events that may occur during program execution. These events are typically unexpected, and can lead to program crashes or other undesirable behavior if not handled properly. The basic idea of exception handling is to "throw" an exception when…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, file handling can be implemented using libraries such as ifstream (for reading from a file) and ofstream (for writing to a file). Here's an example of how to read and write to a file in C++: In this example, we first create an output file stream object (outfile) and open a file named "example.txt" for writing…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, a virtual function is a member function of a class that can be overridden in derived classes. When a virtual function is called through a pointer or reference to a base class, the actual function called is determined at runtime based on the dynamic type of the object. Here's an example: In this example, we…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, operator overloading allows you to define how an operator should behave when used with user-defined types. This means you can define what the + operator does when used with two instances of a custom class, for example. Here's an example of overloading the + operator: In this example, we have a Vector class…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, templates are a powerful feature that allow you to write generic code that can be used with different data types. This means that you can write a single function or class that can work with a variety of data types without having to write separate implementations for each type. Templates are defined using the…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Sure! Here's an example of a simple multithreaded program in C++ using the std::thread library: This program creates two threads using the std::thread constructor, passing in a function and its arguments as parameters. The function printMessage() takes a std::string message and prints it to the console along with the…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Yes, in C++, there are two ways to pass arguments to a function: pass-by-value and pass-by-reference. In pass-by-value, a copy of the argument is made and passed to the function. This means that any changes made to the argument inside the function do not affect the original value of the argument outside the function…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Sure! Here is an example program that calculates the area and perimeter of a rectangle in C++: In this program, we declare four variables: length, width, area, and perimeter. We use the cout object to prompt the user to enter the length and width of the rectangle, and we use the cin object to read the user's input…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, const correctness is a programming technique that is used to ensure data integrity by preventing modifications to data that should not be changed. It is a way to mark certain data as constant or read-only, meaning that it cannot be modified once it has been initialized. The const keyword is used to declare a…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

The Resource Acquisition Is Initialization (RAII) idiom is a C++ programming technique that involves tying the acquisition and release of resources to the lifetime of an object. RAII can be used to manage resource allocation and deallocation, such as memory allocation and deallocation, file or socket handles, locks…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, a smart pointer is a class that behaves like a regular pointer, but with additional memory management features that help manage memory allocation and deallocation automatically. Smart pointers automatically handle the allocation and deallocation of dynamic memory, which can help prevent common memory-related…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, data structures like linked lists, stacks, and queues can be implemented using classes and templates. Here's an example implementation for each: Linked List: This implementation defines two classes: LinkedListNode to represent nodes in the linked list, and LinkedList to represent the list itself. The add…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

C++ is not an ideal language for creating user interfaces, as it is mainly used for system-level programming. However, there are libraries and frameworks available that can be used to create graphical user interfaces in C++. One such popular framework is Qt, which provides a set of cross-platform libraries for…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, move semantics is a feature that allows objects to be moved rather than copied. This can improve performance and reduce memory usage, especially when dealing with large objects or containers. The idea behind move semantics is to transfer ownership of an object's resources (such as memory) from one object to…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Sure! Here are examples of how to implement the Singleton, Factory, and Observer patterns in C++: Singleton Pattern The Singleton pattern is used when we need to ensure that there is only one instance of a class in our program. Here's an example implementation: In this implementation, the instance function returns a…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Sure! Multithreading synchronization in C++ is the process of coordinating the execution of multiple threads to ensure that they access shared resources in a way that maintains data consistency and avoids race conditions. There are several mechanisms available in C++ for achieving synchronization, including mutexes…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Here's an example C++ console app that simulates a simple calculator: The program prompts the user to enter an operator and two numbers, then performs the corresponding arithmetic operation and outputs the result.

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

In C++, the stack and the heap are two different areas of memory that are used for managing data. The stack is a contiguous block of memory that is managed by the compiler and is used to store local variables and function call information. Each time a function is called, a new frame is created on the stack to hold its…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Sure! Here are examples of how to implement the Proxy and Decorator design patterns in C++. Proxy Design Pattern The Proxy design pattern provides a way to control access to an object, by providing a surrogate or placeholder for the object. This can be useful when the object is expensive to create or access, or when…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Here's an example of a simple Hangman game in C++: This implementation randomly selects a word from a collection and displays it as a series of dashes, indicating the number of letters in the word. The user then guesses letters until they have guessed all the letters in the word or have reached the maximum number of…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Type erasure is a technique used in C++ to hide the details of a particular data type and provide a generic interface that can be used with any type. This is useful when you need to write generic code that can work with different types, but you don't want to have to write separate code for each type. One way to…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Function objects, also known as functors, are a way to create objects that can be used as functions. In C++, a functor is an object that acts like a function and can be called using the function call operator (). Functors are implemented as classes or structures that define the function call operator. They can have…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

The Factory Method pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. The pattern defines an abstract factory class, which declares a factory method that returns an object of the abstract…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Move-only types are types that do not support copying but only support moving. Move semantics can be used to transfer ownership of resources from one object to another without incurring a lot of performance overhead. In C++, move semantics are implemented using move constructors and move assignment operators. Move…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

To implement SIMD (Single Instruction Multiple Data) parallel processing in C++, you can use the vectorization support provided by the compiler or use third-party libraries such as Intel's ISPC (Intel SPMD Program Compiler) or Boost.SIMD. Vectorization allows multiple data elements to be processed in parallel using a…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Here's an example of how to create a C++ app that allows users to create and manage their own personal budget: This app defines a structure for expenses and a structure for the budget. It allows the user to add income and expenses to the budget, and view a summary of the budget. The displayMenu() function displays the…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

The Strategy pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one as an object, and make them interchangeable at runtime. It separates the algorithm implementation from the code that uses it, making it easy to add new algorithms without changing the existing…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Metaprogramming is a programming technique where a program is written to manipulate its own code or the code of other programs during compile-time. In C++, this is achieved through the use of templates, which allow the creation of generic classes and functions that can be instantiated with different types or values at…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

To develop a C++ app that allows users to search for and view information about movies, including ratings, reviews, and cast information, we can use an API such as the Open Movie Database (OMDb) API. This API allows us to search for movies and retrieve information such as the title, year, rating, cast, and plot. Here…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Yes, I can explain the concept of CRTP in C++. The Curiously Recurring Template Pattern (CRTP) is a technique used in C++ to implement static polymorphism. In CRTP, a class is defined as a template that takes a derived class as a template argument. The derived class inherits from the base class template, and the base…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

The Bridge pattern is a structural design pattern that decouples an abstraction from its implementation so that the two can vary independently. In C++, the Bridge pattern can be implemented using inheritance and composition. To implement the Bridge pattern using inheritance, you create two separate class hierarchies…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Sure! Here's an example of a basic chatbot written in C++: This chatbot takes user input and responds with a pre-defined set of responses. The toLowercase function is used to convert the user's input to lowercase, so that the chatbot can recognize certain keywords regardless of their case (for example, "Hello" and…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

Answer preview

Optimizing C++ code for performance involves a variety of techniques, including loop unrolling, vectorization, and cache optimization. Here's an example of how these techniques can be used to optimize a simple piece of code: In this example, we're computing the element-wise product of two vectors x and y, storing the…

Sign in to view this answer

Mid and senior level answers are included in every paid plan. Sign in to choose a plan, or browse the junior level answers for free.

More practice

Go further with C++

C++ code challenges

10 challenges with answers

Behavioral Questions

10 questions with answers

Cultural Fit Questions

10 questions with answers

Competency-Based Questions

10 questions with answers

Motivational Questions

10 questions with answers

Communication Skills Questions

10 questions with answers

Scenario-Based Questions

10 questions with answers

Problem-Solving and Analytical Questions

10 questions with answers

Ethical and Integrity Questions

10 questions with answers

Industry-Specific Questions

10 questions with answers

Leadership and Management Questions

10 questions with answers

Personal Questions

10 questions with answers

About this topic

C++ interview questions and answers

This page lists 45 C++ interview questions with answers, ordered from junior to senior level. Commonly asked questions include "Can you explain the difference between a struct and a class in C++?", "Build a C++ console app that calculates the factorial of a given number." and "Can you explain the concept of pointers in C++ and how they are used?".

Other frequently asked questions include "Can you explain the concept of loops in C++ and how they are used?", "How do you implement arrays and vectors in C++ and what are some use cases?" and "Develop a C++ app that reads a file and counts the frequency of each word.". Review each answer and consider how you would adapt it to your own experience.

Practise C++ in a mock interview

Answer questions one at a time and receive a score with written feedback on each answer.

Start a mock interview

Reviews and comments