0% found this document useful (0 votes)
11 views2 pages

C++ Interview QA Sample

Uploaded by

himanshujii1997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

C++ Interview QA Sample

Uploaded by

himanshujii1997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ Interview Questions & Answers (Sample)

Q1. What is C++?


Answer: C++ is a general-purpose programming language developed by Bjarne Stroustrup. It is an extension of C
language with Object-Oriented Programming features like classes, inheritance, polymorphism, encapsulation, and
abstraction.

Q2. What are the main features of C++?


Answer: Key features include: Object-Oriented Programming, templates, Standard Template Library (STL),
operator overloading, function overloading, exception handling, and memory management using new/delete.

Q3. Difference between C and C++?


Answer: C is a procedural language, while C++ is a multi-paradigm language supporting both procedural and
object-oriented programming. C++ also supports templates, function overloading, and stronger type checking.

Q4. What is Object-Oriented Programming (OOP)?


Answer: OOP is a paradigm that uses objects and classes. It focuses on code reusability, scalability, and
modularity. Core concepts include Encapsulation, Inheritance, Polymorphism, and Abstraction.

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


Answer: A class is a blueprint or template, whereas an object is an instance of a class. For example, 'Car' can be a
class, and 'HondaCity' is an object of that class.

Q6. What are access specifiers in C++?


Answer: Access specifiers define the scope of class members. Types: public (accessible everywhere), private
(accessible only within the class), protected (accessible within the class and derived classes).

Q7. What is inheritance in C++?


Answer: Inheritance allows one class (derived class) to acquire properties and methods of another class (base
class). Example: A 'Dog' class can inherit from an 'Animal' class.

Q8. Types of inheritance in C++?


Answer: Single, Multiple, Multilevel, Hierarchical, Hybrid.

Q9. What is polymorphism?


Answer: Polymorphism means 'many forms'. In C++, it is achieved using function overloading (compile-time) and
virtual functions (runtime).

Q10. What is the difference between function overloading and function overriding?
Answer: Overloading: Same function name, different parameter list (compile-time). Overriding: Redefining a base
class function in a derived class using virtual functions (runtime).

Q11. What is a constructor?


Answer: A constructor is a special member function with the same name as the class. It is automatically called
when an object is created. Used for initializing objects.

Q12. What is a destructor?


Answer: A destructor is a special member function with the same name as the class, preceded by ~. It is
automatically invoked when an object goes out of scope, to release resources.

Q13. Can constructors be overloaded?


Answer: Yes, constructors can be overloaded with different parameter lists.

Q14. Can constructors be virtual?


Answer: No, constructors cannot be virtual. However, destructors can be virtual.
Q15. What is operator overloading?
Answer: Operator overloading allows redefining operators (like +, -, *) to work with user-defined types (classes).
Example: Adding two complex numbers using + operator.

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


Answer: new/delete are C++ operators, whereas malloc/free are C functions. new calls constructors and delete
calls destructors, while malloc/free do not.

Q17. What are pointers in C++?


Answer: A pointer is a variable that stores the address of another variable. Syntax: int *ptr; ptr = &a;

Q18. What is a reference in C++?


Answer: A reference is an alias for another variable. Must be initialized at declaration and cannot be null. Syntax:
int a=10; int &ref;=a;

Q19. Difference between pointers and references?


Answer: Pointers can be reassigned, can be NULL, and require dereferencing (*). References must be initialized
when declared, cannot be NULL, and act as aliases.

Q20. What is a virtual function?


Answer: A virtual function is a member function declared with the keyword 'virtual' in the base class. It allows
runtime polymorphism by enabling function overriding.

Q21. What is a pure virtual function?


Answer: A pure virtual function is declared with '=0'. It makes the class abstract. Example: virtual void draw() = 0;

Q22. What is an abstract class?


Answer: A class containing at least one pure virtual function is abstract. Objects of abstract classes cannot be
created.

Q23. What is a friend function?


Answer: A friend function is not a member of a class but has access to its private and protected members. Declared
using 'friend' keyword.

Q24. What is a namespace in C++?


Answer: A namespace is used to avoid name conflicts by grouping entities like classes, functions, and objects
under a name. Example: namespace MySpace { int a; }

Q25. What is the Standard Template Library (STL)?


Answer: STL is a library in C++ providing common data structures (vector, list, map, set) and algorithms (sort,
search) implemented as templates.

You might also like