C++Cpp Basicsbeginner

C++ Interview Questions and Answers for Freshers

25 C++ questions for campus rounds and vivas: classes, constructors, destructors, inheritance, polymorphism, references, pointers and memory, with short answers and outputs.

Published
TL;DR – Quick Answer

C++ fresher interviews focus on OOP: classes and objects, constructors and destructors, inheritance, virtual functions, and the difference between references and pointers. Prepare a short definition and a small program for each, and be able to explain the order in which constructors and destructors run.

On This Page

C++ interview questions for freshers mostly test object-oriented basics. You will be asked about classes, constructors, inheritance and virtual functions, and almost always about references vs pointers. This page has 25 questions with short answers, and the code samples show their output.

Programs use standard C++17 and include <iostream> and using namespace std; unless shown otherwise. Small snippets show only the lines that matter.

How to answer C++ interview questions

Answer in three parts: definition, small example, one reason it matters. "A virtual function is resolved at run time. A base pointer to a Circle calls Circle's draw. This lets one loop handle many shapes."

Pro tip: Most C++ follow-ups are "what is the output?" Practise tracing constructor and destructor order on paper.

C++ basics questions

Q1. What is C++?

30-second answer: C++ is a compiled, general-purpose language created by Bjarne Stroustrup. He started it in 1979 as "C with Classes", and it was renamed C++ in 1983. It supports procedural, object-oriented and generic programming.

It is used in game engines, browsers, databases, trading systems and embedded software where speed and control over memory matter.

Q2. What are the main differences between C and C++?

Point C C++
Style Procedural Procedural, object-oriented, generic
Classes No Yes
Function overloading No Yes
References No Yes
Memory malloc, free new, delete (malloc still works)
Standard library C library C library plus STL

30-second answer: C++ keeps most of C and adds classes, overloading, references, templates and the STL. For a fuller comparison, read C vs C++.

Q3. What are the four pillars of OOP?

30-second answer: Encapsulation, abstraction, inheritance and polymorphism. Encapsulation keeps data and functions together and hides details. Abstraction shows only what the user needs. Inheritance reuses a class in another. Polymorphism lets one interface behave differently for different types.

The general OOPs interview questions page covers these ideas in more depth.

Q4. What is the difference between a class and an object?

30-second answer: A class is a blueprint that describes data and functions. An object is a real instance of that class in memory.

class Car {
public:
    string model;
    void start() { cout << model << " started\n"; }
};

Car c;
c.model = "Nexon";
c.start();   // prints: Nexon started

Q5. What is the difference between struct and class in C++?

30-second answer: Only the defaults differ. Members of a struct are public by default, and members of a class are private by default. Default inheritance follows the same rule.

By habit, many teams use struct for plain data and class for types with rules and hidden data.

Q6. What are access specifiers?

30-second answer: public members can be used anywhere. private members can be used only inside the class (and by friends). protected members are like private, but derived classes can also use them.

Constructor and destructor questions

Q7. What is a constructor, and what types are there?

30-second answer: A constructor is a special function with the class name and no return type. It runs automatically when an object is created. Common types are default, parameterised and copy constructors.

#include <iostream>
#include <string>
using namespace std;

class Student {
    string name;
    int roll;
public:
    Student() : name("Unknown"), roll(0) {}
    Student(string n, int r) : name(n), roll(r) {}
    Student(const Student& other) : name(other.name), roll(other.roll) {
        cout << "Copy constructor\n";
    }
    void show() const { cout << name << " " << roll << "\n"; }
};

int main() {
    Student a;
    Student b("Asha", 101);
    Student c = b;
    a.show();
    c.show();
    return 0;
}

Output:

Copy constructor
Unknown 0
Asha 101

Student c = b; creates a new object from an existing one, so the copy constructor runs.

Q8. In what order do constructors and destructors run with inheritance?

#include <iostream>
using namespace std;

class Base {
public:
    Base()  { cout << "Base constructor\n"; }
    ~Base() { cout << "Base destructor\n"; }
};

class Derived : public Base {
public:
    Derived()  { cout << "Derived constructor\n"; }
    ~Derived() { cout << "Derived destructor\n"; }
};

int main() {
    Derived d;
    return 0;
}

Output:

Base constructor
Derived constructor
Derived destructor
Base destructor

30-second answer: Construction goes from base to derived. Destruction goes in reverse, from derived to base. The base part must exist before the derived part is built on top of it.

Q9. What is an initializer list, and when must you use it?

30-second answer: An initializer list sets members before the constructor body runs, as in Student() : name("Unknown"), roll(0) {}. You must use it for const members, reference members and base classes or members that have no default constructor.

It is also more efficient for class-type members, because they are built once instead of built and then assigned.

Q10. What is the difference between a shallow copy and a deep copy?

30-second answer: A shallow copy copies pointer values, so both objects point to the same memory. A deep copy allocates new memory and copies the data.

The default copy constructor copies each member, which is a shallow copy for raw pointers. If two objects then delete the same memory, the program has a double delete. Classes that own memory need their own copy constructor, copy assignment and destructor. This is called the rule of three.

Q11. What is the this pointer?

30-second answer: this is a pointer, available inside non-static member functions, that points to the object the function was called on. It is often used when a parameter has the same name as a member.

class Box {
    int width;
public:
    void setWidth(int width) { this->width = width; }
};

Inheritance questions

Q12. What types of inheritance does C++ support?

30-second answer: Single, multiple, multilevel, hierarchical and hybrid.

Type Shape
Single B inherits A
Multiple C inherits A and B
Multilevel C inherits B, B inherits A
Hierarchical B and C both inherit A
Hybrid A mix of the above

The mode (public, protected or private) decides how base members appear in the derived class. Public inheritance is used for "is-a" relationships.

Q13. What is the diamond problem?

30-second answer: If B and C both inherit A, and D inherits B and C, then D gets two copies of A. A call to an A member through D becomes ambiguous. Virtual inheritance fixes this by sharing one A.

class A { public: int value = 1; };
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};

D d;
cout << d.value;   // works: only one A inside D

Q14. What is the difference between overloading and overriding?

30-second answer: Overloading means functions with the same name but different parameter lists in the same scope, resolved at compile time. Overriding means a derived class gives a new version of a base class virtual function with the same signature, resolved at run time.

int add(int a, int b)          { return a + b; }
double add(double a, double b) { return a + b; }   // overloading

Polymorphism and virtual function questions

Q15. What is a virtual function?

30-second answer: A virtual function is a member function that a derived class can override, and the call is decided at run time by the actual object type. This is runtime polymorphism.

#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() const { cout << "Shape\n"; }
    virtual ~Shape() = default;
};

class Circle : public Shape {
public:
    void draw() const override { cout << "Circle\n"; }
};

int main() {
    Circle c;
    Shape& s = c;
    Shape* p = &c;
    s.draw();
    p->draw();
    return 0;
}

Output:

Circle
Circle

If draw were not virtual, both calls would print Shape. The override keyword asks the compiler to check that a base function is really being overridden.

Q16. Why should a base class have a virtual destructor?

30-second answer: If you delete a derived object through a base pointer and the base destructor is not virtual, the behaviour is undefined. In practice the derived destructor usually does not run, so its resources leak. Make the base destructor virtual whenever a class is meant to be used through base pointers.

Q17. What is a pure virtual function and an abstract class?

30-second answer: A pure virtual function has no body in the base class and is written with = 0. A class with at least one pure virtual function is abstract, so you cannot create its objects. Derived classes must override it to become concrete.

class Payment {
public:
    virtual void pay(double amount) = 0;
    virtual ~Payment() = default;
};

References, pointers and memory questions

Q18. What is the difference between a reference and a pointer?

Point Reference Pointer
Must be initialised Yes No
Can be null No Yes
Can change what it refers to No Yes
Syntax to use Like the variable Needs * and ->
Arithmetic No Yes
int x = 10;
int& r = x;
int* p = &x;
r = 20;
cout << x << "\n";   // 20
*p = 30;
cout << x << "\n";   // 30

30-second answer: A reference is another name for an existing variable. A pointer is a separate variable that holds an address. Use a reference when there is always an object, and a pointer when "no object" is a valid state.

Q19. How do you pass by reference?

30-second answer: Add & to the parameter type. The function then works on the caller's variable, not a copy.

void swapValues(int& a, int& b) {
    int t = a;
    a = b;
    b = t;
}

int x = 3, y = 7;
swapValues(x, y);
cout << x << " " << y;   // prints: 7 3

Use const T& to pass large objects such as strings without copying and without allowing changes.

Q20. What is the difference between new/delete and malloc/free?

30-second answer: new allocates memory and calls the constructor. delete calls the destructor and frees the memory. malloc and free only handle raw bytes and do not call constructors or destructors. Use delete[] for memory from new[], and never mix new with free.

In modern C++, prefer vector or smart pointers such as unique_ptr, which free memory automatically.

Other common C++ questions

Q21. What is a static data member?

30-second answer: A static data member belongs to the class, not to each object. All objects share one copy.

class Counter {
public:
    static int count;
    Counter() { count++; }
};
int Counter::count = 0;

Counter a, b, c;
cout << Counter::count;   // prints 3

Q22. What is a friend function?

30-second answer: A friend function is not a member of the class but is allowed to use its private and protected members. It is declared inside the class with the friend keyword. Use it sparingly, because it weakens encapsulation.

Q23. What is operator overloading?

30-second answer: It lets you define what an operator means for your own type.

struct Point {
    int x, y;
    Point operator+(const Point& o) const { return {x + o.x, y + o.y}; }
};

Point a{1, 2}, b{3, 4};
Point c = a + b;
cout << c.x << " " << c.y;   // prints: 4 6

Some operators, such as ::, . and sizeof, cannot be overloaded.

Q24. What is an inline function?

30-second answer: inline was first a hint to replace a call with the function body. Today compilers decide that on their own. The main effect of inline now is that the function can be defined in a header included by many files without a linker error.

Q25. What is the STL?

30-second answer: The Standard Template Library is the part of the C++ standard library with containers, algorithms and iterators. Common containers are vector, list, map, set, unordered_map, stack and queue.

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> marks = {72, 95, 60};
    sort(marks.begin(), marks.end());
    for (int m : marks) cout << m << " ";
    cout << "\n";
    return 0;
}

Output:

60 72 95

How to prepare in one week

Day Focus
1 Classes, objects, access specifiers
2 Constructors, initializer lists, destructors
3 Copy constructor, shallow vs deep copy
4 Inheritance types, diamond problem
5 Virtual functions, abstract classes
6 References, pointers, new and delete
7 STL basics and output questions

Many C++ interviews start with C. Revise C programming interview questions for pointers and memory. If the company uses Java, read Java vs C++ so you can explain the switch. Coding rounds then test DSA interview questions, where vector, map and sort save time.

Frequently Asked Questions

Which C++ topics should a fresher prepare first?
Start with classes and objects, constructors, destructors, inheritance and virtual functions. Then cover references vs pointers, new and delete, static members and basic STL containers such as vector. These cover most first-round questions.
Should I write using namespace std in an interview?
It is fine in short interview programs and online tests, and it keeps code readable. In larger projects many teams avoid it in header files because it can cause name clashes. Saying this shows you know the trade-off.
Is C++ good for placement coding rounds?
Yes. C++ with the STL is widely used in coding tests because vector, map, set and sort save time. Use the language you are fastest and most accurate in, and make sure the test platform supports it.
Do interviewers ask modern C++ features to freshers?
Some do, at a basic level. Knowing auto, range-based for loops, nullptr, override and smart pointers such as unique_ptr is enough for most fresher rounds.
How is a C++ interview different from a C interview?
A C interview leans on pointers, memory and storage classes. A C++ interview adds object-oriented design, constructors, inheritance, polymorphism and the standard library. Pointer questions still appear in both.
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 4 December 2026 LinkedIn
Chat with us