Basic C++ Programming Questions
1. What is the difference between C and C++?
o Answer: C is a procedural programming language, while C++ supports
both procedural and object-oriented programming (OOP). C++
includes features like classes, inheritance, polymorphism, and
encapsulation, which are absent in C.
2. What are the key features of C++?
o Answer: Key features include object-oriented programming, classes
and objects, inheritance, polymorphism, encapsulation, data
abstraction, function overloading, operator overloading, and
templates.
3. What is a class in C++?
o Answer: A class is a user-defined data type in C++ that contains
variables (attributes) and functions (methods). It serves as a blueprint
for creating objects.
4. What is an object in C++?
o Answer: An object is an instance of a class. It represents a real-world
entity with both data and functions that operate on that data.
5. What is the difference between a constructor and a destructor in C++?
o Answer: A constructor initializes an object when it is created, while a
destructor is used to clean up resources when an object is destroyed.
6. What is inheritance in C++?
o Answer: Inheritance allows a class (derived class) to inherit
properties and methods from another class (base class). It promotes
code reuse and the creation of hierarchical relationships.
7. What are the types of inheritance in C++?
o Answer: Types of inheritance include:
, Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
8. What is polymorphism in C++?
o Answer: Polymorphism allows objects of different classes to be
treated as objects of a common base class. It can be achieved
through function overloading or method overriding.
9. What is encapsulation in C++?
o Answer: Encapsulation is the concept of bundling data and methods
that operate on the data into a single unit, i.e., a class. It restricts
access to the internal details of an object by using access specifiers
(private, public, protected).
10.What is abstraction in C++?
o Answer: Abstraction is the process of hiding the implementation
details and showing only the essential features of an object.
11.What is function overloading in C++?
o Answer: Function overloading allows multiple functions with the
same name to coexist, as long as they have different parameters
(either in number or type).
12.What is operator overloading in C++?
o Answer: Operator overloading allows you to define how operators
(like +, -, *, etc.) work for user-defined classes.
13.What is a virtual function in C++?
o Answer: A virtual function is a member function of a base class that
you expect to override in derived classes. It ensures that the correct
function is called for an object, regardless of the type of reference
(base or derived).
14.What is the use of the new and delete operators in C++?
, o Answer: The new operator dynamically allocates memory, while the
delete operator deallocates the memory. These are used for
managing memory in dynamic storage.
15.What are templates in C++?
o Answer: Templates are a feature that allows functions and classes to
operate with generic types. They enable code reuse for different data
types.
C++ Coding Challenges
16.Write a C++ program to find the factorial of a number using recursion.
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
17.Write a C++ program to find the greatest common divisor (GCD) of two
numbers using recursion.
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
18.Write a C++ program to find the least common multiple (LCM) of two
numbers.
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
19.Write a C++ program to implement a simple calculator.
#include <iostream>
using namespace std;