Freshers C++ Interview Questions
What is the difference between std::string and a char array in C++?
| Point | std::string | char array |
|---|---|---|
| Size | Grows by itself | Fixed |
| Join | a + b | strcat (can overflow) |
| Compare | a == b | strcmp |
| Length | size() | strlen |
| Safety | Safe | Buffer 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
}
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,accumulateand 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';
}
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';
}
1
5 2.5 C++
autoneeds an initial value.autoremovesconstand references: useconst auto&when you need them.
See C++ Variables.
What is the difference between const and constexpr in C++?
| Point | const | constexpr |
|---|---|---|
| Meaning | Read only after the start | Computed at compile time |
| Value known | Can be at run time | Must be at compile time |
| Use for array sizes | Only if the value is a compile time constant | Yes |
#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';
}
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';
}
123
2
See Static Members.
What are access specifiers in C++?
| Specifier | Who can use it |
|---|---|
| public | Everyone |
| protected | The class and derived classes |
| private | Only 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;
}
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;
| ^
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';
}
}
Caught: something went wrong
See C++ Exceptions.
How to compile and run a C++ program?
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
}
$ 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.
- « Previous
- 1
- 2