https://github.com/nomemory/cpp-interview-questions
C++ interview questions
https://github.com/nomemory/cpp-interview-questions
Last synced: about 1 year ago
JSON representation
C++ interview questions
- Host: GitHub
- URL: https://github.com/nomemory/cpp-interview-questions
- Owner: nomemory
- Created: 2023-05-09T04:48:37.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-18T11:27:43.000Z (over 2 years ago)
- Last Synced: 2025-04-04T15:17:30.781Z (over 1 year ago)
- Size: 1.95 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
* What is a reference ?
* What is the difference between *pass-by-reference* and *pass-by-value* ?
* What are the main differences between a `struct` and a `class` ?
* Are there any scenarios where you would normally use a `struct` over a `class` ?
* What are the main differences between a `reference` and a `pointer` ?
* What is *polymorphism* in C ?
* Explain the usage for the `virtual` keyword ?
* What is wrong with the following code:
```cpp
class A
{
public:
A() {}
~A(){}
};
class B: public A
{
public:
B():A(){}
~B(){}
};
int main(void)
{
A* a = new B();
delete a;
}
```
Answer: Undefined behavior because A destructor is not `virtual`.
* What is the output:
```cpp
unsigned char half_limit = 150;
for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
// do something;
}
```
Answer: Infinite Loop (overflow)
* Explain what the keyword `static` is doing ?
* What is the following:
```cpp
#include
#include
using namespace std;
void demo() {
static int count = 0;
cout << count << " ";
count++;
}
int main()
{
for (int i = 0; i < 5; i++)
demo();
return 0;
}
```
* What is the O(n) for the following code ?
```cpp
int main() {
int n = 10; //n can be anything
int sum = 0;
float pie = 3.14;
int var = 1;
while (var < n){
cout << pie << endl;
for (int j=0; j
template
struct Factorial {
static const int value = N * Factorial::value;
};
template <>
struct Factorial<0> {
static const int value = 1;
};
int main() {
constexpr int num = 5; // Change this number to calculate factorial at compile-time
std::cout << "Factorial of " << num << " is: " << Factorial::value << std::endl;
return 0;
}
```
# Code questions