LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 9 of 27

Classes and Objects

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Classes and Objects · Study material

C++ Classes and Objects – Interview Questions

1. What is a class in C++?

A user-defined type that groups data members and member functions, with access control over who may use them.

2. How do you define a member function outside the class?

Declare it inside the class, then define it with the class name and scope resolution operator: ReturnType ClassName::name(params) { … }.

3. Are member functions defined inside the class inline?

Yes, implicitly – which allows the class definition to live in a header included by many files.

4. What is a const member function?

A member function marked const after its parameter list; it cannot modify non-mutable data members and is the only kind callable on const objects.

5. What is the mutable keyword?

It allows a data member to be modified even in const member functions – used for caches, counters and mutexes.

6. What is the this pointer and what is its type?

A pointer to the object the member function was called on. In a member of class X it is X* (const X* in a const member function).

7. Why return *this by reference?

To allow method chaining (obj.a().b().c()) on the same object; returning by value would chain on copies.

8. What is a static data member?

A member shared by all objects of the class, with one storage location for the whole program.

9. How do you initialize a static data member?

In C++17 and later, static inline int count = 0; inside the class; constants as static constexpr. Before C++17, define it once in a .cpp file: int X::count = 0;.

10. Can a static member function access non-static members?

Not directly, because it has no this pointer. It can access them through an object passed to it.

11. What is the size of an empty class and why?

At least 1 byte, so that two distinct objects have different addresses.

12. Do member functions take space in each object?

No. Code exists once; only non-static data members, padding and a possible vptr occupy object storage.

13. What is a nested class?

A class declared inside another class. It is a member of the enclosing class, follows its access specifiers, and can access the enclosing class's private members.

14. What is a friend class?

A class granted access to another class's private and protected members by a friend class declaration. Friendship is neither inherited nor transitive.

15. What is an aggregate?

An array or a class with no user-declared constructors, no private or protected non-static data members, no virtual functions and no virtual or private base classes; it can be brace-initialized member by member.

16. What are designated initializers?

A C++20 syntax for aggregates that names the members: Point p{.x = 1, .y = 2};. Designators must follow declaration order.

17. What does auto operator<=>(const T&) const = default; do?

Generates three-way comparison (and with it ==, !=, <, <=, >, >=) comparing members in declaration order.

18. What is the difference between an object on the stack and on the heap?

A stack (automatic) object is destroyed automatically at the end of its scope; a heap object created with new lives until deleted – ideally owned by a smart pointer.

19. When is a static local object constructed?

The first time control passes through its declaration (thread-safely since C++11), and it is destroyed after main returns.

20. What is the difference between a class declaration and a definition?

class X; is a declaration (forward declaration) that allows pointers and references to X; the definition with the member list is needed to create objects or access members.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links