Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 65

C++ Interview and Viva Questions

In this lesson: Most-asked C++ interview and viva questions — basics, OOP, STL, memory — with short clear answers. Prepare fast. In Hindi & English.

How to use this page

These are the questions that come up again and again in C++ practical viva exams (Class 11–12, BCA, B.Sc., B.Tech) and fresher interviews. Each answer is short enough to say aloud in 20–30 seconds. Read the linked lesson when you need the full explanation.

Tip for viva: answer in three parts — definition, one-line example, why it matters.

Basics (1–10)

1. What is C++?

A general-purpose, compiled language created by Bjarne Stroustrup as an extension of C. It supports procedural, object-oriented and generic programming. See introduction to C++.

2. Difference between C and C++?

C is procedural; C++ adds classes, inheritance, polymorphism, references, function overloading, templates, exceptions, namespaces and the STL. See C vs C++.

3. What are tokens in C++?

The smallest units of a program: keywords, identifiers, literals (constants), operators and punctuators (separators such as ;, {}).

4. What is the role of main()?

It is the entry point where execution starts. There must be exactly one, and it returns int (0 means success).

5. What is a namespace?

A named scope that groups identifiers to avoid name clashes. The standard library is in std; std::cout selects cout from it. See namespaces.

6. Difference between cin/cout and scanf/printf?

cin/cout are type-safe C++ streams that need no format specifiers and work with user-defined types through operator overloading; scanf/printf are C functions that rely on format strings like %d.

7. What is a reference?

An alias for an existing variable: int& r = x;. It must be initialised, cannot be null and cannot be re-seated to another variable. See references.

8. Pointer vs reference?

A pointer stores an address, can be null, can be reassigned and needs * to access the value. A reference is just another name for an object, must be initialised and cannot change what it refers to.

9. What is const?

A qualifier that makes a value read-only. It can apply to variables, pointers (const int* vs int* const), references and member functions. See constants and literals.

10. What are inline functions?

Functions marked inline (or defined inside a class) whose calls the compiler may replace with the body to avoid call overhead. See inline functions.

OOP (11–22)

11. What is OOP? Name its pillars.

A paradigm that models programs as objects combining data and behaviour. Pillars: encapsulation, abstraction, inheritance, polymorphism. See OOP concepts.

12. Class vs object?

A class is a blueprint (class Student { … };); an object is an instance of it (Student s;) with its own memory.

13. What is encapsulation?

Bundling data with its functions and restricting direct access using private/protected, exposing only a public interface.

14. What is abstraction?

Showing only essential features and hiding implementation details — achieved with classes, access specifiers and abstract classes.

15. What is a constructor? Types?

A special member function with the class name and no return type that runs when an object is created. Types: default, parameterised, copy, and move constructors (plus delegating and converting constructors). See constructors.

16. What is a destructor?

A member function ~ClassName() that runs automatically when an object is destroyed, used to release resources. A class has only one destructor and it takes no arguments. See destructors.

17. What is a copy constructor and when is it called?

ClassName(const ClassName& other) creates an object as a copy of another. It is called when an object is initialised from another object, passed by value or (without elision) returned by value. See copy constructor.

18. Shallow copy vs deep copy?

A shallow copy copies pointer values, so two objects share the same heap memory (double-delete risk). A deep copy allocates new memory and copies the data, so each object owns its own.

19. What is the this pointer?

A hidden pointer available inside non-static member functions that points to the object on which the function was called. See this pointer.

20. What are static members?

Data or functions that belong to the class rather than each object — one shared copy. A static function can be called as Class::func() and has no this. See static members.

21. What is a friend function?

A non-member function granted access to a class's private and protected members by declaring it friend inside the class. See friend functions.

22. Types of inheritance?

Single, multiple, multilevel, hierarchical and hybrid. The diamond problem in multiple inheritance is solved with virtual base classes. See inheritance.

Polymorphism and virtual functions (23–28)

23. Compile-time vs run-time polymorphism?

Compile-time: function overloading, operator overloading and templates — resolved by the compiler. Run-time: virtual functions called through a base pointer/reference — resolved while running. See polymorphism.

24. Overloading vs overriding?

Overloading = same name, different parameters, same scope. Overriding = derived class redefines a base virtual function with the same signature.

25. What is a virtual function?

A base-class member function declared virtual so that a call through a base pointer runs the derived class's version. See virtual functions.

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

virtual void draw() = 0; has no body in the base class. A class with at least one pure virtual function is abstract and cannot be instantiated. See abstract classes.

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

If a derived object is deleted through a base pointer and the destructor is not virtual, only the base destructor runs — the derived part is not cleaned up (undefined behaviour, often a leak).

28. What is a vtable?

A per-class table of function pointers the compiler creates for classes with virtual functions; each object holds a hidden pointer (vptr) to it, which is how run-time dispatch works.

Memory and pointers (29–33)

29. new/delete vs malloc/free?

new is an operator that allocates and calls the constructor, returns the correct type and throws bad_alloc on failure. malloc only allocates raw bytes, returns void* and returns NULL on failure. Never mix them. See dynamic memory.

30. What is a memory leak?

Memory allocated with new that is never deleted, so it cannot be reused until the program ends.

31. What is a dangling pointer?

A pointer that still holds the address of memory that has been freed or of a local variable that no longer exists.

32. What are smart pointers?

Classes that own heap memory and free it automatically: unique_ptr (sole owner), shared_ptr (reference-counted shared owner), weak_ptr (non-owning observer). See smart pointers.

33. What is RAII?

"Resource Acquisition Is Initialisation": tie a resource (memory, file, lock) to an object's lifetime — acquire in the constructor, release in the destructor — so it is freed automatically, even when exceptions occur.

Templates, STL and exceptions (34–38)

34. What is a template?

A blueprint for functions or classes that work with any type: template <typename T> T maxOf(T a, T b);. See templates.

35. What is the STL? Its components?

The Standard Template Library: containers, iterators, algorithms and function objects. See STL introduction.

36. vector vs array?

A built-in array has a fixed size known at compile time and no bounds information. std::vector grows dynamically, knows its size(), manages its own memory and offers at() with bounds checking.

37. map vs unordered_map?

map keeps keys sorted (balanced tree, O(log n) operations). unordered_map uses hashing (average O(1)) and keeps no order.

38. How does exception handling work?

Code that may fail goes in a try block; an error is signalled with throw; a matching catch block handles it. Destructors of local objects run during unwinding. See exception handling.

Modern C++ (39–40)

39. What does auto do?

It lets the compiler deduce a variable's type from its initialiser (auto it = v.begin();). The type is still fixed at compile time. See modern C++.

40. What is a lambda expression?

An inline anonymous function, e.g. [](int a, int b) { return a > b; }, often passed to STL algorithms; the capture list [ ] controls access to outside variables. See lambda.

Bonus: predict the output

C++
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() { cout << "Base\n"; }
    void hello()        { cout << "Hello from Base\n"; }
};
class Derived : public Base {
public:
    void show() override { cout << "Derived\n"; }
    void hello()         { cout << "Hello from Derived\n"; }
};

int main() {
    Base* p = new Derived();
    p->show();
    p->hello();
    delete p;
    return 0;
}
Derived Hello from Base

show() is virtual, so the derived version runs; hello() is not virtual, so the base version runs because p is a Base*. (In real code, give Base a virtual destructor before deleting through a base pointer — see question 27.)

Viva tips

  • Know every line of your own practical programs and why each header is included.
  • Be able to write a class with a constructor, destructor and one member function from memory.
  • Practise tracing loops and pointer code on paper.
  • If you don't know an answer, say what you do know about the topic — examiners reward honest partial answers.
🏋️ Practice

Cover the answers and try to say each one aloud in under 30 seconds. Mark the ones you stumble on and revise the linked lesson for those. Then attempt the C++ Test 2 and the C++ MCQ quiz.

Summary

  • Basics: C vs C++, namespaces, references, const, inline.
  • OOP: class/object, four pillars, constructors, destructors, copy semantics, static and friend.
  • Polymorphism: overloading vs overriding, virtual functions, abstract classes, virtual destructors.
  • Memory: new/delete, leaks, dangling pointers, smart pointers, RAII.
  • Templates, STL, exceptions and modern features such as auto and lambdas.

Frequently Asked Questions

What are the most asked C++ interview questions for freshers?
The most common are: difference between C and C++, class vs object, the four OOP pillars, constructor and destructor types, pointer vs reference, function overloading vs overriding, virtual functions and abstract classes, new/delete vs malloc/free, and basic STL containers like vector and map.
How should I prepare for a C++ practical viva?
Be able to explain every line of your own programs, define the OOP terms with one example each, know why each header is included, and be ready to trace a small loop or pointer program by hand. Examiners often ask you to change one line of your program and predict the new output.
What is the difference between overloading and overriding in C++?
Overloading means several functions with the same name but different parameter lists in the same scope, resolved at compile time. Overriding means a derived class provides its own version of a base class virtual function with the same signature, resolved at run time through a base pointer or reference.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topic50 C++ Practice Programs Your progress: 65 / 81 (80%) · Lesson 66 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free