SAMPLE CPP Question Answers
SAMPLE CPP Question Answers
1. What is a class?
A class in C++ is a user-defined type that encapsulates data and functions. It serves as a blueprint
for creating objects. Classes support object-oriented features such as encapsulation, inheritance,
and polymorphism. It organizes related variables and functions into one entity, allowing modular,
reusable, and organized code structures in software development.
2. What is an object?
An object in C++ is an instance of a class. It represents a specific entity with attributes (data) and
behaviors (methods). Objects are created using a class, and each object has its own copy of class
members. They are the core units in object-oriented programming for modeling real-world
concepts.
1. What is a class?
A class in C++ is a user-defined type that encapsulates data and functions. It serves as a
blueprint for creating objects. Classes support object-oriented features such as
encapsulation, inheritance, and polymorphism. It organizes related variables and
functions into one entity, allowing modular, reusable, and organized code structures in
software development.
2. What is an object?
An object in C++ is an instance of a class. It represents a specific entity with attributes
(data) and behaviors (methods). Objects are created using a class, and each object has its
own copy of class members. They are the core units in object-oriented programming for
modeling real-world concepts.
23. What are the main differences between procedure oriented languages and object
oriented languages?
Procedure-oriented languages focus on functions and procedures, with data flowing
freely. Object-oriented languages encapsulate data and functions into objects. OOP
promotes abstraction, inheritance, and polymorphism. Procedural programming is
suitable for simple tasks, while OOP is more scalable and modular, supporting software
development practices and reusable, maintainable code structures.
27. What is the difference between pass by reference and pass by value?
In pass by value, a copy of the variable is passed, and changes don’t affect the original. In
pass by reference, an alias is passed, so changes affect the original variable. Pass by
reference allows more efficient memory usage and is often used when large objects or
modifications are needed.
39. Explain OOPS concept with its features & volatile specifier.
OOP features include encapsulation, abstraction, inheritance, and polymorphism. They
promote modular, reusable, and organized code. Volatile is a keyword indicating a
variable can be changed unexpectedly (e.g., hardware updates), preventing compiler
optimizations. OOP enhances large system design, while volatile ensures correct behavior
in concurrent or low-level programming scenarios.
This macro swaps two integer values using a temporary variable. Macros perform text
substitution and are expanded at compile time. They are faster but lack type checking, so
caution is needed when using them in complex expressions or with functions, as
unexpected behaviors may arise due to side effects.
These patterns solve common design problems, improve code structure, and promote
reusability and maintainability in C++ applications.
These Standard Template Library (STL) containers provide efficient ways to store,
retrieve, and manipulate collections of data with built-in algorithms and iterator
support.
45. A ________ has no type name and can be accessed directly by name.
A namespace has no type name and can be accessed directly by name. It is used to avoid
naming conflicts and organize code elements logically. With a using namespace
statement, its members can be accessed directly without scope resolution. This improves
readability and simplifies referencing variables and functions.
46. Trees, sorting algorithms
Trees are hierarchical data structures with parent-child relationships used in search,
hierarchical storage, and decision making. Sorting algorithms like quicksort, mergesort,
and heapsort organize data in a specific order. Both trees and sorting are essential in
efficient data management, enhancing access time, and ensuring faster retrieval in
programming tasks.
class Sample {
int x;
public:
Sample(int val) : x(val) {}
friend std::ostream& operator<<(std::ostream &out, const Sample &s) {
out << s.x;
return out;
}
};
This overloads << for Sample class. It enables using cout << s; to print the object’s value
cleanly.
54. Write difference between C & C++ structures?
In C, structures can only contain data. In C++, structures can contain data, functions,
constructors, destructors, and access specifiers. The default access is public in structures.
C++ structures support object-oriented features like inheritance and polymorphism,
whereas C structures are used primarily for grouping related variables under a single
name.
55. How to implement Operator overloading with an e.g. a+2 & 2+a, where a is an
object to a class.
class Number {
int value;
public:
Number(int v) : value(v) {}
Number operator+(int n) { return Number(value + n); }
friend Number operator+(int n, Number obj) {
return Number(n + obj.value);
}
};
This allows a+2 and 2+a using member and friend functions for operator overloading.
58. Write a program to simulate a pay scale distribution pgm depending on category
(temporary, contract, permanent).
class Employee {
public:
virtual void displayPay() = 0;
};
class Temporary : public Employee {
void displayPay() { cout << "Pay: 10000"; }
};
class Contract : public Employee {
void displayPay() { cout << "Pay: 20000"; }
};
class Permanent : public Employee {
void displayPay() { cout << "Pay: 30000"; }
};
63. Is passing by reference & passing by address the same? Give example.
No. Passing by address uses pointers and requires dereferencing, while passing by
reference uses aliases. Both modify the original variable.
Example:
67. What is more suitable in Fibonacci series, iterative, recursive or depending upon
the input values?
Iterative is more efficient for large inputs due to constant space and time complexity.
Recursive is elegant but leads to redundant calls unless optimized with memoization.
Choice depends on problem size and performance needs. Iteration is preferred in practice,
while recursion demonstrates divide-and-conquer and conceptual clarity for small inputs.
72. Explain dynamic polymorphism using the concept of V table & V pointer?
Dynamic polymorphism uses virtual functions resolved at runtime. A V-table stores
function pointers for virtual functions. Each object has a V-pointer pointing to its class’s
V-table. When a virtual function is called through a base pointer, the V-pointer accesses
the correct function via the V-table, ensuring runtime binding.
class Base {
public:
virtual void display() { cout << "Base class"; }
};
class Derived : public Base {
public:
void display() override { cout << "Derived class"; }
};
Base* b = new Derived();
b->display(); // Output: Derived class
This demonstrates runtime polymorphism using base class pointers and virtual functions.