C++ Interview Questions – Introduction
1. [Asked in Infosys] What is C++ and how is it different from C?
Answer:
C++ is a general-purpose programming language developed by Bjarne Stroustrup as an extension of C. It adds object-oriented programming (OOP) features while maintaining performance and flexibility like C.
Key Differences between C and C++:
| Feature | C | C++ |
|---|---|---|
| Paradigm | Procedural | Multi-paradigm (Procedural + OOP) |
| Encapsulation | Not supported | Supported (Classes and Objects) |
| Function Overloading | Not possible | Possible |
| Standard Input/Output | scanf() / printf() | cin / cout |
| Exception Handling | Not supported | Supported (try-catch) |
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++";
return 0;
}
Output:
Hello, C++
2. [Asked in TCS] What are the key features of C++?
Answer:
C++ provides powerful features that make it suitable for system programming, game development, and application development.
Key Features:
- Object-Oriented Programming (OOP) โ Supports classes, objects, encapsulation, inheritance, and polymorphism.
- Fast and Efficient โ Close to hardware, making it highly optimized.
- Memory Management โ Supports manual memory allocation (
newanddelete). - Multi-paradigm โ Supports procedural, OOP, and generic programming.
- Rich Standard Library โ Includes STL (Standard Template Library) with containers, algorithms, and iterators.
3. [Asked in Cognizant] How is C++ a multi-paradigm programming language?
Answer:
C++ supports multiple programming paradigms, making it flexible and versatile.
Paradigms Supported by C++:
- Procedural Programming โ Using functions (
main(),printf()). - Object-Oriented Programming (OOP) โ Using classes and objects.
- Generic Programming โ Using templates (
template <typename T>). - Functional Programming โ Using lambda expressions (
[]() {}).
Example: Procedural vs. OOP in C++
// Procedural Programming
int add(int a, int b) {
return a + b;
}
// OOP
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
};
4. [Asked in Wipro] What are classes and objects in C++?
Answer:
A class is a blueprint for creating objects, and an object is an instance of a class.
Example of Class and Object in C++:
#include <iostream>
using namespace std;
class Car {
public:
string brand;
void show() {
cout << "Car brand: " << brand;
}
};
int main() {
Car c1;
c1.brand = "Toyota";
c1.show();
return 0;
}
Output:
Car brand: Toyota
5. [Asked in IBM] What is the difference between struct and class in C++?
Answer:
| Feature | struct (C-style) | class (C++ OOP) |
|---|---|---|
| Default Access Modifier | public | private |
| Supports Member Functions? | No (C-style) | Yes (C++ supports member functions) |
| Encapsulation | No | Yes |
| Example Use Case | Data structures | Object-oriented programming |
Example:
struct Student { // All members are public by default
string name;
};
class Teacher { // Members are private by default
string subject;
};
6. [Asked in Deloitte] What is the difference between cin and cout in C++?
Answer:
cin(Standard Input Stream) โ Used to take input from the user.cout(Standard Output Stream) โ Used to print output.
Example:
#include <iostream>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Hello, " << name;
return 0;
}
Output:
Enter your name: John
Hello, John
Note: cin does not accept spaces. Use getline(cin, variable); to read full strings.
7. [Asked in Amazon] What is the difference between new and malloc() in C++?
Answer:
| Feature | new (C++) | malloc() (C) |
|---|---|---|
| Memory Initialization | Initializes memory | Does not initialize memory |
| Returns | Exact pointer type | void*, needs typecasting |
| Exception Handling | Throws an exception if fails | Returns NULL if fails |
Example Using new:
int *ptr = new int(10); // Allocates memory and initializes it
delete ptr; // Frees memory
Example Using malloc():
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
Recommendation: Use new and delete in C++ for type safety.
8. [Asked in Microsoft] What is the difference between C++ and Java?
Answer:
| Feature | C++ | Java |
|---|---|---|
| Compilation Type | Compiled | Interpreted (JVM) |
| Memory Management | Manual (new/delete) | Automatic (Garbage Collection) |
| Platform Dependence | Platform-dependent | Platform-independent (JVM) |
| Multiple Inheritance | Supported | Not directly supported |
| Performance | Faster | Slightly slower |
9. [Asked in Google] What are constructors and destructors in C++?
Answer:
- Constructor โ Special function called automatically when an object is created.
- Destructor โ Special function called automatically when an object is destroyed.
Example:
#include <iostream>
using namespace std;
class Car {
public:
Car() { cout << "Constructor called!"; }
~Car() { cout << "Destructor called!"; }
};
int main() {
Car c1;
return 0;
}
Output:
Constructor called!
Destructor called!
10. [Asked in Flipkart] What is the role of namespace in C++?
Answer:
A namespace is used to avoid name conflicts in large programs.
Example:
#include <iostream>
namespace A {
int num = 10;
}
int main() {
cout << A::num; // Accessing namespace variable
return 0;
}
Output:
10
