BSc CSIT Second Semester OOP: Important Chapter-Wise Questions
📘 BSc CSIT – Object-Oriented Programming (OOP) Chapter-wise Past Questions
This post contains the past 5 years' questions of BSc CSIT OOP subject, organized chapter-wise according to the official syllabus.
📘 Unit 1: Introduction to Object-Oriented Programming
📘 Unit 2: Basics of C++ Programming
1. Explain the significance of type conversion. How do we achieve dynamic memory allocation in C++? Explain with an example.
Solution:
Type conversion is the process of changing one data type to another, such as converting an int to a float or vice versa.
Importance / Significance:
- Ensures compatibility between different data types during operations.
- Prevents data loss or errors during calculations.
- Helps in type-safe operations in mixed expressions.
- Improves code readability and maintainability by making type intentions clear.
Types of Type Conversion in C++:
a) Implicit Conversion: Done automatically by the compiler. (e.g., int to float)
b) Explicit Conversion (Casting): Done manually by the programmer using cast syntax. (e.g., (float)num)
Dynamic Memory Allocation: The process of allocating memory during runtime using pointers.
Keyword use in Dynamic Memory Allocation.
new: Allocates memory on the heap
delete: Deallocates memory from the heap
Example
#include<iostream>
using namespace std;
int main()
{
int size;
cout << "Enter how many numbers you want to enter \n ";
cin>>size;
int *ptr;
ptr=new int[size];
for(int i=0;i<size;i++)
{
cout<<"enter"<<i+1<<"number\n";
cin>>ptr[i];
}
for(int i=0;i<size;i++)
{
cout<<"entered"<<i+1<<"number\n";
cout<<ptr[i]<<endl;
}
delete (ptr);
}
Why Use Dynamic Memory?
-When the size of the data is unknown at compile time
-To create flexible data structures like linked lists, trees, etc.
-More efficient memory use in large program
2. What is meant by type conversion? Define two ways of converting one user-defined data type (object) to another.
Type conversion is the process of changing one data type to another, such as when operations involve different data types or when custom objects (user-defined types) need to interact.
Two Ways of Converting One User-Defined Data Type (Object) to Another in C++:
1. Conversion through Constructor (Single-Argument Constructor):
This allows a class to accept an object of another class and convert it.
🔹 Use Case: Target class has a constructor that accepts the source class object.
Example:
#include <iostream>
using namespace std;
class A {
public:
int x;
A(int val) { x = val; }
};
class B {
public:
int y;
// Constructor that accepts an object of A
B(A obj) {
y = obj.x;
}
void show() {
cout << "Value in B: " << y << endl;
}
};
int main() {
A a1(5);
B b1 = a1; // A --> B conversion using constructor
b1.show();
return 0;
}
2. Conversion through Operator Overloading (Type Conversion Operator):
We define a type-cast operator inside the class to convert one object type to another.
🔹 Use Case: Source class defines a conversion function to return the target type.
Example:
#include <iostream>
using namespace std;
class B; // Forward declaration
class A {
public:
int x;
A(int val) { x = val; }
// Type conversion function from A to B
operator B();
};
class B {
public:
int y;
void show() {
cout << "Value in B: " << y << endl;
}
};
// Define the conversion
A::operator B() {
B temp;
temp.y = x;
return temp;
}
int main() {
A a1(10);
B b1 = a1; // A --> B conversion using operator overloading
b1.show();
return 0;
}
3. How dynamic memory allocation is done using new and delete? Write a program illustrating the use of new and delete.
Solution:
Dynamic Memory Allocation: The process of allocating memory during runtime using pointers.
Keyword use in Dynamic Memory Allocation.
new: Allocates memory on the heap
delete: Deallocates memory from the heap
Example
#include<iostream>
using namespace std;
int main()
{ int size;
cout << "Enter how many numbers you want to enter \n ";
cin>>size;
int *ptr;
ptr=new int[size];
for(int i=0;i<size;i++){
cout<<"enter"<<i+1<<"number\n";
cin>>ptr[i];
}for(int i=0;i<size;i++)
{cout<<"entered"<<i+1<<"number\n";
cout<<ptr[i]<<endl;
}delete (ptr);}
Why Use Dynamic Memory?
-When the size of the data is unknown at compile time
-To create flexible data structures like linked lists, trees, etc.
-More efficient memory use in a large program.
4. What is meant by pass-by-reference? How can we pass arguments by reference using reference variables? Illustrate with an example.
Solution:
Pass-by-reference means passing the actual address (reference) of a variable to a function, so that any changes made inside the function affect the original variable.
We can pass arguments using reference variables by using the '&' symbol in the function parameters.
Example
#include <iostream>
using namespace std;
void swap(int &a, int &b) { // Pass-by-reference using &
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swap(x, y); // Actual variables are passed
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}
Solution:
Class:
- A class is a user-defined data type in C++ that acts as a blueprint for creating objects.
- A class represents a set of related objects.
Object:
- An object can be defined as a data field that has unique attributes and behaviors.
- An object is a real-world instance of a class.
#include<iostream>
using namespace std;
class Student { // Class definition
public:
int roll; // Data member
string name;
void display() { // Member function
cout << "Roll: " << roll << ", Name: " << name << endl;
}
};
int main() {
Student s1; // Object creation
// Accessing members using dot operator
s1.roll = 101;
s1.name = "Madhav";
s1.display(); // Calling member function
return 0;
}
Members of the Class Can Be Accessed by using
Using the dot (.) operator with the object.
Syntax: objectName.memberName
6. What is an inline function? Why is it used? Write a program to illustrate the inline function.
Solution:
-An inline function is a function in C++ where the code is expanded at the point of function call, instead of jumping to the function definition.
-It is declared using the keyword inline.
It is used for the following reason:
-To reduce function call overhead (especially for small functions).
-To increase execution speed.
-Useful when a function is called many times.
Example:
#include<iostream>
using namespace std;
inline product(int a,int b)
{
return a*b;
}
int main()
{
int a;
int b;
cout<<"enter first number\n";
cin>>a;
cout<<"enter second number\n";
cin>>b;
cout<<"the product is "<<product(a,b)<<endl;
}
7. What is the principal reason for using default arguments? Explain how missing and default arguments are handled.
Solution:
Default arguments in C++ allow us to assign default values to function parameters so that a function can be called with fewer arguments.
Example:
void greet(string name = "User") {
cout << "Hello, " << name << "!" << endl;
}
The principal reason for using default arguments is
-To make functions more flexible and easier to use by allowing optional arguments.
-To reduce function overloading.
Missing and Default Arguments Are Handled:
-If an argument is provided, it is used.
-If an argument is missing, the default value is used.
-Default arguments are always filled from right to left.
Example:
#include<iostream>
using namespace std;
void display(string name = "Guest", int age = 18) {
cout << "Name: " << name << ", Age: " << age << endl;
}
int main() {
display(); // Uses both default arguments
display("Madhav"); // Uses default age
display("Madhav", 20); // Uses provided name and age
return 0;
}
8. Define and write syntax for default arguments. Write a program to display N number of characters using default arguments.
Solution:
Default arguments in C++ allow us to assign default values to function parameters so that a function can be called with fewer arguments.
Syntax:
return_type function_name(parameter1 = default_value1, parameter2 = default_value2, ...);
#include<iostream>
using namespace std;
// Function with default arguments
void displayChars(char ch = '*', int n = 5) {
for(int i = 0; i < n; i++) {
cout << ch << " ";
}
cout << endl;
}
int main() {
displayChars(); // Uses default: * 5 times
displayChars('#'); // Uses default count: # 5 times
displayChars('$', 3); // Uses both provided values: $ 3 times
return 0;
}
9. Create a function called swaps()
that interchanges values using a function template. Write a main to test with multiple types.
Solution:
#include<iostream>
using namespace std;
// Function template
template <typename T>
void swaps(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int x = 10, y = 20;
float f1 = 1.5, f2 = 2.5;
char c1 = 'A', c2 = 'B';
cout << "Before swap - Integers: x = " << x << ", y = " << y << endl;
swaps(x, y);
cout << "After swap - Integers: x = " << x << ", y = " << y << endl;
cout << "\nBefore swap - Floats: f1 = " << f1 << ", f2 = " << f2 << endl;
swaps(f1, f2);
cout << "After swap - Floats: f1 = " << f1 << ", f2 = " << f2 << endl;
cout << "\nBefore swap - Characters: c1 = " << c1 << ", c2 = " << c2 << endl;
swaps(c1, c2);
cout << "After swap - Characters: c1 = " << c1 << ", c2 = " << c2 << endl;
return 0;
}
10. Write a member function reverseit()
That reverses a string.
Solution:
#include<iostream>
using namespace std;
class MyString {
public:
string str;
void reverseit() {
int n = str.length();
for(int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
}
};
int main() {
MyString s;
cout << "Enter a string: ";
cin >> s.str;
s.reverseit();
cout << "Reversed string: " << s.str << endl;
return 0;
}
Some other topics:
Manipulator: It is a special function that can be used to control the format of the input and output stream.
Reference variable: A reference variable provides an alternative name for a variable that is previously defined.
📘 Unit 3: Classes and Objects
this
pointer, Static data members.📘 Unit 4: Operator Overloading
+
operator using the friend function to add two heights.📘 Unit 5: Inheritance
- What are the various class access specifiers? How does public inheritance differ from private inheritance?
- Depict the difference between private and public derivation. Explain the derived class constructor with the program.
- Briefly explain the types of inheritance used in OOP.
- Describe the chain of constructors and destructors in inheritance.
- How does ambiguity arise in multipath inheritance? How can you remove it?
📘 Unit 6: Virtual Functions, Polymorphism & Miscellaneous
- Difference between compile time and run time polymorphism.
- Explain function template overloading with examples.
- Explain how exceptions are used with a design that includes multiple exceptions.
- What is the use of reinterpret cast operator? Why do we need a virtual function?
- Differentiate between concrete class and abstract class. Define class and function templates with syntax.
- Explain the reason for member function overriding when using virtual function.
- Explain types of polymorphism briefly. Write the roles of polymorphism and how to achieve dynamic polymorphism.
- What is an exception? Why use exception handling? Explain try…catch with an example.
- Explain the default action of the copy constructor. 🔁
- How can you define a catch statement that can catch any type of exception? Use multiple catches with examples.
📘 Unit 7: Function Templates and Exception Handling
- Write a program to implement a function template with multiple arguments.
- What is an exception? Why use exception handling? Explain with try…catch using suitable examples. 🔁
- When are class templates useful? How to implement a stack with int and string? Illustrate with example.
- Create a function called
swaps()
a template for multiple types. 🔁 - Explain how exceptions are used in C++ with multiple exception design. 🔁
📘 Unit 8: File Handling
- Write a program that stores employee info in a file and displays it in ascending salary order.
- Write a program to illustrate the use of
seekg()
andtellg()
. - Write a program that reads the content of the file
data.txt
and displays it. - What do you mean by the stream? Explain different stream classes for file I/O.
- Write a program that reads and displays data for 2 student objects.
- Write a program:
- Class
Account
with acc no, balance, min_balance (static) - Use an array to store and display 5 accounts
- Class
- Write a program to overload
<<
and>>
operators. 🔁 - What is the use of
get
andgetline
? Explain with an example.
Hope this helps you in your exam preparation! 🔁 Questions indicate a high chance of repetition in future exams. Best of luck!