Freshers C++ Interview Questions

What is the difference between std::string and a char array in C++?

Pointstd::stringchar array
SizeGrows by itselfFixed
Joina + bstrcat (can overflow)
Comparea == bstrcmp
Lengthsize()strlen
SafetySafeBuffer overflow risk
#include <cstring>
#include <iostream>
#include <string>

int main() {
    std::string s = "Hello";
    s += " World";
    std::cout << s << ' ' << s.size() << '\n';

    char c[] = "Hello";
    std::cout << sizeof(c) << ' ' << std::strlen(c) << '\n';      // 6 bytes (with \0), 5 characters
}
Output

Hello World 11
6 5

See C++ Strings.

What is the STL in C++?

The STL has three main parts:

  • Containers: vector, list, map, set, unordered_map, stack, queue.
  • Iterators: objects which point to the items of a container.
  • Algorithms: sort, find, count, transform, accumulate and many more.
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {4, 1, 3};
    std::sort(v.begin(), v.end());
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
}
Output

1 3 4

See STL Containers and Algorithms.

What is the auto keyword in C++?

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

int main() {
    auto n = 5;                                  // int
    auto d = 2.5;                                // double
    auto name = std::string("C++");
    std::vector<std::map<std::string, int>> data = {{{"a", 1}}};
    for (const auto& m : data) {                 // no long type name
        std::cout << m.size() << '\n';
    }
    std::cout << n << ' ' << d << ' ' << name << '\n';
}
Output

1
5 2.5 C++

  • auto needs an initial value.
  • auto removes const and references: use const auto& when you need them.

See C++ Variables.

What is the difference between const and constexpr in C++?

Pointconstconstexpr
MeaningRead only after the startComputed at compile time
Value knownCan be at run timeMust be at compile time
Use for array sizesOnly if the value is a compile time constantYes
#include <array>
#include <iostream>

constexpr int square(int n) { return n * n; }

int main() {
    constexpr int size = square(3);               // calculated while compiling
    std::array<int, size> a{};
    const int input = 5;                          // const, set at the start
    std::cout << a.size() << ' ' << input << '\n';
}
Output

9 5

See C++ Variables.

What is the static keyword in C++?

  • Static local variable: keeps its value between calls.
  • Static data member: one copy for the whole class.
  • Static member function: can be called without an object, and has no this.
  • Static at file level: the name is visible only in that file.
#include <iostream>

int next_id() {
    static int id = 0;           // set only once
    return ++id;
}

struct Counter {
    static inline int total = 0;
    Counter() { total++; }
};

int main() {
    std::cout << next_id() << next_id() << next_id() << '\n';
    Counter a, b;
    std::cout << Counter::total << '\n';
}
Output

123
2

See Static Members.

What are access specifiers in C++?

SpecifierWho can use it
publicEveryone
protectedThe class and derived classes
privateOnly the class (and friends)
class A {
private:
    int secret = 1;
public:
    int open = 2;
};

int main() {
    A a;
    int x = a.open;
    int y = a.secret;
}
Output

Private.cpp: In function 'int main()':
Private.cpp:11:15: error: 'int A::secret' is private within this context
11 | int y = a.secret;
| ^~~~~~
Private.cpp:3:9: note: declared private here
3 | int secret = 1;
| ^~~~~~
Private.cpp:10:9: warning: unused variable 'x' [-Wunused-variable]
10 | int x = a.open;
| ^
Private.cpp:11:9: warning: unused variable 'y' [-Wunused-variable]
11 | int y = a.secret;
| ^

See Access Specifiers and Encapsulation.

What is exception handling in C++?

Use throw to report an error and try ... catch to handle it.

#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::runtime_error("something went wrong");
    } catch (const std::exception& e) {
        std::cout << "Caught: " << e.what() << '\n';
    }
}
Output

Caught: something went wrong

See C++ Exceptions.

How to compile and run a C++ program?

#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
}
Terminal

$ g++ -std=c++23 -Wall -Wextra hello.cpp -o hello
$ ./hello
Hello, C++!

The steps inside are: preprocessing, compiling, assembling and linking. See C++ Installation and Setup.