Unit I: Concepts and Basics of C++ Programming
1. What are the key differences between structures, unions, and classes in C++?
o Structures: Members are public by default, used primarily for grouping
related data.
o Unions: All members share the same memory, so only one member can hold a
value at any time.
o Classes: Members are private by default, used for encapsulating data and
functions with access control.
2. Explain the difference between inline and non-inline member functions.
o Inline functions: Defined using the inline keyword or defined inside the
class. They are expanded at compile-time, reducing function call overhead.
o Non-inline functions: Defined outside the class. These are normal function
calls executed at runtime.
3. How does static data differ from static member functions in C++?
o Static data members: Shared among all objects of a class and have a single
memory allocation.
o Static member functions: Can be called without an object and operate only
on static data members.
4. What are the main differences between procedural and object-oriented
programming paradigms?
o Procedural programming: Focuses on functions and sequence of actions,
with data and functions separated. Example: C.
o Object-oriented programming: Focuses on objects encapsulating data and
behavior, using principles like inheritance and polymorphism. Example: C++.
5. Describe the role of cin and cout in C++.
o cin (character input) reads input from the standard input (keyboard).
o cout (character output) prints output to the standard output (console).
6. What are the key features of input/output streams in C++?
o Use of << (insertion) and >> (extraction) operators.
o Support for file and console I/O operations.
o Type safety and ability to format data using manipulators.
7. What is a manipulator function? Give examples.
o A manipulator function is used to format I/O. Examples:
endl: Inserts a newline.
setw: Sets field width for output.
setprecision: Specifies the precision of floating-point numbers.
8. Explain function overloading with an example.
o Function overloading allows multiple functions with the same name but
different parameter lists.
Example:
cpp
Copy code
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
9. How do you define a friend function? What are its applications?
o A friend function is declared using the friend keyword inside a class,
allowing it to access private/protected members.
Example:
cpp
Copy code
class A {
private: int x;
public: friend void show(A obj);
};
void show(A obj) { cout << obj.x; }
10. Compare and contrast call by value, call by address, and call by reference.
• Call by value: Passes a copy of the argument; changes don't reflect back.
• Call by address: Passes the memory address; changes reflect in the original.
• Call by reference: Passes an alias to the variable; changes reflect directly in the
original.
Unit II: Pointers, Reference Variables, Arrays, and String Concepts
1. What is a void pointer? Provide an example of its usage.
o A void pointer is a generic pointer that can hold the address of any data type.
It cannot be dereferenced directly without typecasting.
Example:
cpp
Copy code
void* ptr;
int a = 10;
ptr = &a; // Storing address of an integer
cout << *(int*)ptr; // Typecasting to int* to dereference
2. Explain pointer arithmetic with examples.
o Pointer arithmetic involves incrementing or decrementing pointers based on
the size of the data type they point to.
Example:
cpp
Copy code
int arr[] = {1, 2, 3};
int* ptr = arr;
cout << *ptr; // Output: 1
ptr++;
cout << *ptr; // Output: 2
3. What is a dangling pointer? How can it be avoided?
o A dangling pointer points to memory that has been deallocated or freed.
o To avoid:
Assign nullptr after deleting.
Avoid returning local variables' addresses.
Use smart pointers.
4. What is the significance of the this pointer in C++?
o The this pointer refers to the current object of a class.
o It is implicitly passed to all non-static member functions.
Example:
cpp
Copy code
class A {
int x;
public: void setX(int x) { this->x = x; }
};
5. How does a reference variable differ from a pointer?
o A reference variable is an alias for an existing variable, whereas a pointer
stores the memory address of a variable.
o Pointers can be reassigned, but references cannot.
6. Discuss the concept of an array of objects in C++.
o An array of objects is a collection of objects of the same class.
Example:
cpp
Copy code
class A {
int x;
public: void setX(int x) { this->x = x; }
};
A obj[5]; // Array of 5 objects
7. What are the major functions of the standard C++ string class?
o Key functions:
length(): Returns the size of the string.
substr(): Extracts a substring.
find(): Finds a character or substring.
append(): Appends to the string.
8. Explain the differences between single-dimensional and multi-dimensional
arrays in terms of memory allocation.
o Single-dimensional arrays are a contiguous block of memory for a single list
of elements.
o Multi-dimensional arrays store data in a matrix-like structure.
o Both are stored in row-major order in memory.
9. How is a pointer to a data member used in classes?
o A pointer to a data member allows accessing or modifying a specific class
member using an object.
Example:
cpp
Copy code
class A {
int x;
public: int A::*ptr = &A::x;
};
10. Illustrate the problems caused by null pointer assignments in C++.
o Assigning nullptr to a pointer ensures no memory address is pointed to.
Dereferencing a null pointer causes runtime errors or segmentation faults.
Unit III: Data File Operations and Constructors/Destructors
1. What are the different modes of file opening in C++?
o Modes include:
ios::in: Open for reading.
ios::out: Open for writing.
ios::binary: Open in binary mode.
ios::app: Append to the file.
ios::ate: Move to the end of the file after opening.
2. Differentiate between sequential access and random access file processing.
o Sequential access: Data is read/written in order, from start to end.
o Random access: Access specific positions using functions like seekg() and
seekp().
3. How can binary file operations be performed in C++?
o Binary files store data in raw form. Use ios::binary with file streams.
Example:
cpp
Copy code
ofstream file("data.bin", ios::binary);
int x = 10;
file.write((char*)&x, sizeof(x));
file.close();
4. Explain the purpose of a copy constructor with an example.
o A copy constructor creates a new object as a copy of an existing one.
Example:
cpp
Copy code
class A {
int x;
public: A(const A &obj) { x = obj.x; }
};
5. What is the difference between a parameterized constructor and a default
constructor?
o Default constructor: No parameters, initializes members with default values.
o Parameterized constructor: Takes arguments to initialize members.
6. Describe the lifecycle of a file stream in C++.
o A file stream is created with ifstream/ofstream objects, used for operations,
and closed using the close() method.
7. What are initializer lists? How are they used in constructors?
o An initializer list allows initializing members before the constructor body.
Example:
cpp
Copy code
class A {
int x;
public: A(int a) : x(a) {}
};
8. What is the role of destructors in resource management?
o Destructors release resources (e.g., memory, file handles) allocated during the
object's lifetime.
9. How do you manage file operations using structures in C++?
o File operations with structures involve reading/writing entire structures using
binary I/O.
Example:
cpp
Copy code
struct Data { int id; char name[20]; };
10. Explain the use of classes in handling file operations.
o Classes encapsulate file I/O logic with member functions for better abstraction
and reusability.
Unit IV: Operator Overloading and Inheritance
1. How is a unary operator overloaded? Provide an example.
o Overload using a member function or friend function.
Example:
cpp
Copy code
class A {
int x;
public: void operator++() { ++x; }
};
2. Explain the process of converting a basic type to a class type.
o Define a constructor in the class that accepts the basic type as an argument.
Example:
cpp
Copy code
class A {
int x;
public: A(int val) { x = val; }
};
3. What are the types of inheritance in C++? Provide examples for each.
o Single: One base, one derived class.
o Multi-level: Chain of inheritance.
o Multiple: Multiple base classes.
o Hierarchical: One base, multiple derived classes.
4. What is the significance of a virtual base class in inheritance?
o It prevents multiple copies of a base class when inherited by multiple derived
classes in a hierarchy.
5. How are ambiguities resolved in multiple inheritance?
o Use scope resolution operator (::) to specify the class explicitly.
6. Describe the order of execution of constructors and destructors in inheritance.
o Constructors: Base class constructor is called first.
o Destructors: Derived class destructor is called first.
7. How is function overriding different from overloading?
o Overriding: Redefining a base class function in a derived class.
o Overloading: Same function name with different parameter lists.
8. What is aggregation, and how does it differ from composition?
o Aggregation: Objects have independent lifetimes.
o Composition: Contained objects are destroyed when the container is
destroyed.
9. How can binary operators be overloaded? Provide an example.
Example:
cpp
Copy code
A operator+(const A &obj) {
A temp;
temp.x = this->x + obj.x;
return temp;
}
10. Explain public, private, and protected inheritance modes.
o Public: Base class public/protected remain accessible in derived class.
o Private: All base class members become private in derived class.
o Protected: Public/protected members of base class become protected in
derived class.
Unit V: Dynamic Memory Management and Polymorphism
1. What is dynamic memory allocation in C++? How is it performed?
o Dynamic memory is allocated during runtime using the new operator and freed
using delete.
Example:
cpp
Copy code
int* ptr = new int; // Allocate memory
delete ptr; // Free memory
2. What is a memory leak, and how can it be avoided?
o A memory leak occurs when allocated memory is not freed, causing wastage.
o To avoid:
Always use delete or delete[] after new.
Use smart pointers like std::unique_ptr or std::shared_ptr.
3. What are virtual destructors, and why are they necessary?
o Virtual destructors ensure that destructors in derived classes are correctly
invoked when deleting an object through a base class pointer.
Example:
cpp
Copy code
class Base { virtual ~Base() {} };
4. Explain the concept of runtime polymorphism with an example.
o Achieved using virtual functions and pointers/references.
Example:
cpp
Copy code
class Base {
public: virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public: void show() override { cout << "Derived"; }
};
5. What is the difference between early binding and late binding?
o Early binding: Function calls resolved at compile-time (default behavior).
o Late binding: Function calls resolved at runtime using virtual functions.
6. What is a pure virtual function? How is it declared?
o A pure virtual function has no implementation in the base class and forces
derived classes to override it.
Example:
cpp
Copy code
class Base {
virtual void display() = 0; // Pure virtual
};
7. What is an abstract class? Provide an example.
o A class with at least one pure virtual function is called an abstract class.
Example:
cpp
Copy code
class Abstract {
virtual void func() = 0; // Pure virtual
};
8. What are self-referential classes?
o Classes where objects contain pointers or references to objects of the same
class.
Example:
cpp
Copy code
class Node {
Node* next; // Self-referential pointer
};
9. Explain the use of dynamic constructors with an example.
o Constructors that allocate memory dynamically.
Example:
cpp
Copy code
class A {
int* ptr;
A(int size) { ptr = new int[size]; }
};
10. What is the difference between abstract and concrete classes?
o Abstract class: Contains at least one pure virtual function. Cannot be
instantiated.
o Concrete class: Fully implemented and can be instantiated.
Unit VI: Exception Handling, Templates, and STL
1. What are the key components of exception handling in C++?
o Try block: Code to monitor for exceptions.
o Catch block: Handles exceptions.
o Throw keyword: Used to throw exceptions.
Example:
cpp
Copy code
try { throw "Error"; }
catch (const char* msg) { cout << msg; }
2. How is an exception rethrown in C++?
o Use the throw; statement inside a catch block to propagate the exception.
Example:
cpp
Copy code
try {
try { throw 1; }
catch (...) { throw; }
} catch (int e) { cout << e; }
3. What is a function template? Provide an example.
o A function template allows the creation of generic functions.
Example:
cpp
Copy code
template <typename T>
T add(T a, T b) { return a + b; }
4. Explain the concept of class templates with inheritance.
o A class template can serve as a base class for a derived class.
Example:
cpp
Copy code
template <typename T>
class Base { T x; };
class Derived : public Base<int> {};
5. What are the advantages of using STL in C++?
o Reusability, efficiency, and reduced development time with pre-implemented
data structures and algorithms.
6. What are the major components of the Standard Template Library (STL)?
o Containers: Store collections of data (e.g., vector, list).
o Algorithms: Perform operations like sorting and searching.
o Iterators: Traverse containers.
7. What is the difference between vector and list in STL?
o Vector: Contiguous memory, fast random access, and slow insertion/deletion.
o List: Doubly linked list, slow random access, fast insertion/deletion.
8. What is the throwing mechanism in exception handling?
o Exceptions are thrown using the throw keyword, transferring control to a
catch block.
Example:
cpp
Copy code
throw std::runtime_error("Error occurred");
9. What are the key features of the map container in STL?
o Stores key-value pairs in sorted order. Keys are unique. Provides fast search,
insertion, and deletion.
10. What is the difference between function overloading and templates?
o Function overloading: Requires separate definitions for each type.
o Templates: A single definition works for multiple types.