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++:

FeatureCC++
ParadigmProceduralMulti-paradigm (Procedural + OOP)
EncapsulationNot supportedSupported (Classes and Objects)
Function OverloadingNot possiblePossible
Standard Input/Outputscanf() / printf()cin / cout
Exception HandlingNot supportedSupported (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 (new and delete).
  • 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++:

  1. Procedural Programming โ€“ Using functions (main(), printf()).
  2. Object-Oriented Programming (OOP) โ€“ Using classes and objects.
  3. Generic Programming โ€“ Using templates (template <typename T>).
  4. 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:

Featurestruct (C-style)class (C++ OOP)
Default Access Modifierpublicprivate
Supports Member Functions?No (C-style)Yes (C++ supports member functions)
EncapsulationNoYes
Example Use CaseData structuresObject-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:

Featurenew (C++)malloc() (C)
Memory InitializationInitializes memoryDoes not initialize memory
ReturnsExact pointer typevoid*, needs typecasting
Exception HandlingThrows an exception if failsReturns 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:

FeatureC++Java
Compilation TypeCompiledInterpreted (JVM)
Memory ManagementManual (new/delete)Automatic (Garbage Collection)
Platform DependencePlatform-dependentPlatform-independent (JVM)
Multiple InheritanceSupportedNot directly supported
PerformanceFasterSlightly 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