BSc CSIT (TU) Science Object-Oriented Programming (BSc CSIT, CSC161) Question Paper 2078 Nepal
This is the official BSc CSIT (TU) (Science stream) Object-Oriented Programming (BSc CSIT, CSC161) question paper for 2078, as set in the regular annual examination. It carries 60 full marks and a time allowance of 180 minutes, across 12 questions. On Kekkei you can attempt this Object-Oriented Programming (BSc CSIT, CSC161) past paper online with a timer, get instant AI feedback and step-by-step solutions, and track the topics where you lose marks — completely free. Whether you are revising for your BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) exam or solving previous years' question papers, this 2078 paper is a great way to practise under real exam conditions.
Section A: Long Answer Questions
Attempt any TWO questions.
What are templates in C++? Explain function templates and class templates with a program for each.
Templates in C++
A template is a feature of C++ that allows writing generic, type-independent code. Instead of writing separate functions or classes for each data type, a single template is written using a placeholder type parameter, and the compiler generates the appropriate concrete code for each type used. This supports code reusability and is the basis of generic programming (the STL is built on templates).
The general syntax uses the keyword template followed by a type parameter list:
template <class T> // or template <typename T>
1. Function Template
A function template defines a family of functions that can operate on different data types. The compiler deduces T from the arguments and generates a separate version (template instantiation) for each type.
#include <iostream>
using namespace std;
template <class T>
T getMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << getMax(10, 20) << endl; // int -> 20
cout << getMax(3.5, 2.1) << endl; // double -> 3.5
cout << getMax('a', 'z') << endl; // char -> z
return 0;
}
Output:
20
3.5
z
2. Class Template
A class template lets a class work with a generic data type. The type is supplied explicitly when an object is created, e.g. Calculator<int>.
#include <iostream>
using namespace std;
template <class T>
class Calculator {
T a, b;
public:
Calculator(T x, T y) : a(x), b(y) {}
T add() { return a + b; }
T multiply() { return a * b; }
};
int main() {
Calculator<int> c1(10, 5);
cout << "Int add: " << c1.add() << ", mul: " << c1.multiply() << endl;
Calculator<double> c2(2.5, 4.0);
cout << "Double add: " << c2.add() << ", mul: " << c2.multiply() << endl;
return 0;
}
Output:
Int add: 15, mul: 50
Double add: 6.5, mul: 10
Summary
- Function template — generic function; type is deduced from arguments.
- Class template — generic class; type is specified explicitly at object creation.
- Both promote reusability and type safety, generating type-specific code at compile time.
What is a copy constructor? When is it called? Explain the difference between a shallow copy and a deep copy with examples.
Copy Constructor
A copy constructor is a special constructor that creates a new object as an exact copy of an existing object of the same class. It takes a reference to an object of the same class (usually const) as its parameter.
Syntax:
ClassName(const ClassName &obj);
If the programmer does not define one, the compiler provides a default copy constructor that performs a member-by-member (shallow) copy.
When is the Copy Constructor Called?
- When an object is initialized from another object of the same class:
Student s2 = s1;orStudent s2(s1); - When an object is passed by value to a function.
- When an object is returned by value from a function.
- When the compiler creates a temporary object.
Shallow Copy vs Deep Copy
Shallow Copy
The default copy constructor copies member values directly. If the class contains a pointer, only the pointer (address) is copied — both objects point to the same memory. This causes problems: changes through one object affect the other, and the destructor may double-free the same memory (dangling pointer / crash).
class Shallow {
int *p;
public:
Shallow(int v) { p = new int(v); }
// default copy constructor => both objects share the same *p
~Shallow() { delete p; } // double delete if copied!
};
Deep Copy
A user-defined copy constructor allocates new memory and copies the value pointed to, so each object owns an independent copy.
class Deep {
int *p;
public:
Deep(int v) { p = new int(v); }
Deep(const Deep &obj) { // deep copy constructor
p = new int(*obj.p); // allocate fresh memory, copy value
}
~Deep() { delete p; } // safe: each object frees its own memory
};
Difference Table
| Aspect | Shallow Copy | Deep Copy |
|---|---|---|
| Pointer handling | Copies address only | Allocates new memory, copies value |
| Memory sharing | Shared between objects | Independent memory per object |
| Default behavior | Done by default copy constructor | Must be written explicitly |
| Risk | Dangling pointer, double free | Safe |
Conclusion: When a class manages dynamically allocated resources, a deep copy (user-defined copy constructor) is required to avoid shared-memory and double-free bugs.
Explain file handling in C++. Write a program to write records to a file and read them back using ifstream and ofstream.
File Handling in C++
File handling allows a program to store data permanently in secondary storage and retrieve it later, instead of losing it when the program ends. C++ provides file handling through the <fstream> header, which defines three stream classes:
| Class | Purpose |
|---|---|
ofstream | Output file stream — used to write to a file |
ifstream | Input file stream — used to read from a file |
fstream | Both reading and writing |
Steps in File Handling
- Declare a stream object.
- Open the file (via constructor or
open()), choosing a mode such asios::in,ios::out,ios::app,ios::binary. - Read/Write data using
<<,>>,getline(),read(),write(), etc. - Close the file with
close()to flush buffers and release the resource.
Program: Write Records and Read Them Back
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// ---- Writing records to a file ----
ofstream fout("students.txt");
if (!fout) { cout << "Error opening file!"; return 1; }
fout << "Ram 85\n";
fout << "Sita 90\n";
fout << "Hari 78\n";
fout.close();
cout << "Records written successfully.\n";
// ---- Reading records back from the file ----
ifstream fin("students.txt");
if (!fin) { cout << "Error opening file!"; return 1; }
string name;
int marks;
cout << "\nName\tMarks\n";
while (fin >> name >> marks) {
cout << name << "\t" << marks << endl;
}
fin.close();
return 0;
}
Output:
Records written successfully.
Name Marks
Ram 85
Sita 90
Hari 78
Explanation
ofstream fout("students.txt")creates/opens the file for writing;<<writes the records.ifstream fin("students.txt")opens the same file for reading;>>reads each field and thewhileloop continues until end-of-file.close()ensures buffered data is saved and the file is released.
Section B: Short Answer Questions
Attempt any EIGHT questions.
Explain the four pillars of OOP.
The Four Pillars of OOP
-
Encapsulation — Binding data members and the member functions that operate on them into a single unit (a class), and hiding internal data from outside access using access specifiers (
private,protected,public). Data is accessed only through public methods, protecting it from misuse (data hiding). -
Abstraction — Showing only the essential features of an object while hiding the complex implementation details. The user knows what an object does, not how. Example: using
coutwithout knowing how it internally writes to the screen. -
Inheritance — The mechanism by which a new class (derived class) acquires the properties and behaviours of an existing class (base class). It promotes code reusability and supports an is-a relationship (e.g., a
Caris-aVehicle). -
Polymorphism — "One name, many forms." The same function or operator behaves differently in different contexts. It is achieved through:
- Compile-time (static): function overloading, operator overloading.
- Run-time (dynamic): virtual functions and function overriding.
Together these four pillars make programs modular, reusable, extensible and easier to maintain.
What is a copy constructor? Write its syntax.
Copy Constructor
A copy constructor is a special member function that initializes a new object as a copy of an existing object of the same class. It takes a single argument which is a reference (usually const) to an object of the same class, preventing infinite recursion (a value argument would itself require a copy).
If not defined by the programmer, the compiler supplies a default copy constructor that performs a member-wise (shallow) copy.
Syntax
className (const className &obj) {
// copy each data member from obj to this object
}
Example
class Point {
int x, y;
public:
Point(int a, int b) { x = a; y = b; }
Point(const Point &p) { // copy constructor
x = p.x;
y = p.y;
}
};
Point p1(3, 4);
Point p2 = p1; // copy constructor called
It is invoked when an object is initialized from another object, passed by value, or returned by value.
Differentiate between public and private inheritance.
Public vs Private Inheritance
The access specifier used while inheriting decides the access level of the base class members inside the derived class and to the outside world.
class Derived : public Base { ... }; // public inheritance
class Derived : private Base { ... }; // private inheritance
| Basis | Public Inheritance | Private Inheritance |
|---|---|---|
Base public members become | public in derived | private in derived |
Base protected members become | protected in derived | private in derived |
Base private members | Not accessible | Not accessible |
| Relationship modelled | is-a relationship | has-a / implemented-in-terms-of relationship |
| Access by objects of derived | Public base members accessible through derived object | Base members not accessible through derived object |
Default for class | Must be written explicitly | This is the default if no specifier is given |
Note: In both cases, base private members are never directly accessible in the derived class. Public inheritance is the most common form and preserves the interface of the base class, whereas private inheritance hides the base interface and is used for implementation reuse only.
What is the role of the catch block in exception handling?
Role of the catch Block in Exception Handling
C++ handles runtime errors using the try, throw, and catch mechanism. The catch block is the exception handler — its role is to receive and handle the exception thrown from the corresponding try block.
Roles / Functions:
- It catches the exception object thrown by a
throwstatement inside the associatedtryblock. - Its parameter type must match the type of the thrown object; this determines which catch block executes.
- It contains the code to handle the error gracefully (display a message, perform cleanup, recover) so the program does not terminate abnormally.
- Multiple catch blocks can follow one
tryto handle different exception types;catch(...)catches any type.
try {
int a = 10, b = 0;
if (b == 0) throw "Division by zero!";
cout << a / b;
}
catch (const char* msg) { // catch block handles the exception
cout << "Error: " << msg;
}
Output: Error: Division by zero!
If no matching catch block exists, the program calls terminate() and aborts.
Explain the get() and put() functions in file handling.
get() and put() Functions in File Handling
Both are unformatted single-character I/O functions used to read and write one character at a time from/to a stream or file.
get()
- A member function of input stream classes (
istream/ifstream). - Reads a single character (including whitespace such as spaces and newlines) from the file/stream.
char ch;
ifstream fin("data.txt");
fin.get(ch); // reads one character into ch
put()
- A member function of output stream classes (
ostream/ofstream). - Writes a single character to the file/stream.
ofstream fout("data.txt");
fout.put('A'); // writes the character 'A'
Example: Copy a file character by character
ifstream fin("source.txt");
ofstream fout("copy.txt");
char ch;
while (fin.get(ch)) { // read until EOF
fout.put(ch); // write each character
}
Difference: get() is used for reading a character, while put() is used for writing a character. Unlike >>, get() does not skip whitespace, which makes them useful for copying files exactly.
What is an object? How is memory allocated to an object?
Object
An object is an instance of a class — a concrete entity that occupies memory and represents a real-world thing. It bundles together data members (state) and member functions (behaviour) defined by its class. While a class is only a blueprint (a logical entity), an object is the actual usable entity created from it.
class Student {
int roll; // data member
public:
void show(); // member function
};
Student s1; // s1 is an object of class Student
Memory Allocation to an Object
- Data members: When an object is created, separate memory is allocated for the data members of each object. Every object has its own copy of the (non-static) data members.
- Member functions: Memory for member functions is not duplicated per object. The function code is stored only once in memory and is shared by all objects of the class. Each call passes a hidden
thispointer so the function operates on the correct object's data. - Static members: A
staticdata member has only one shared copy for the whole class, regardless of the number of objects. - Stack vs heap: An object declared normally (
Student s1;) is allocated on the stack, whereas an object created withnew(Student *p = new Student;) is allocated on the heap and must be freed withdelete.
Summary: Memory is allocated for data members per object, but member function code is shared by all objects of the class.
Write a short note on virtual destructor.
Virtual Destructor
A virtual destructor is a destructor declared with the keyword virtual in the base class. It ensures that when an object of a derived class is deleted through a base class pointer, the correct (derived) destructor is called first, followed by the base destructor.
Why it is needed
If the base destructor is not virtual and an object is deleted via a base pointer, only the base class destructor runs — the derived class destructor is skipped. This causes a resource/memory leak because resources allocated by the derived class are never released (undefined behaviour).
Example
class Base {
public:
virtual ~Base() { cout << "Base destructor\n"; } // virtual
};
class Derived : public Base {
public:
~Derived() { cout << "Derived destructor\n"; }
};
int main() {
Base *p = new Derived();
delete p; // calls Derived then Base destructor
}
Output:
Derived destructor
Base destructor
If ~Base() were not virtual, only "Base destructor" would print and Derived's cleanup would be lost.
Conclusion: Always declare the base class destructor virtual when a class is meant to be used polymorphically (has virtual functions / is inherited and deleted through base pointers).
What is a static member function?
Static Member Function
A static member function is a member function declared with the keyword static that belongs to the class as a whole rather than to any individual object.
Characteristics
- It can be called without creating an object, using the class name and scope resolution operator:
ClassName::functionName(); - It can access only
staticdata members and other static functions of the class — it cannot access non-static (ordinary) members. - It does not have a
thispointer, because it is not associated with any specific object. - It is commonly used to operate on or report static data (e.g., counting the number of objects created).
Example
class Counter {
static int count; // static data member
public:
Counter() { count++; }
static int getCount() { // static member function
return count;
}
};
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
cout << "Objects created: " << Counter::getCount(); // called via class name
return 0;
}
Output: Objects created: 3
Explain the cin and cout objects.
cin and cout Objects
C++ performs console input/output using predefined stream objects declared in the header <iostream> within the std namespace.
cout (Console Output)
coutis an object of the classostream, connected to the standard output device (the monitor/screen).- It is used with the insertion operator
<<to display data.
cout << "Hello" << endl;
cout << "Sum = " << 25;
cin (Console Input)
cinis an object of the classistream, connected to the standard input device (the keyboard).- It is used with the extraction operator
>>to read data into variables.
int age;
cin >> age; // reads an integer from keyboard
Key Points
| Object | Class | Device | Operator |
|---|---|---|---|
cin | istream | Keyboard (input) | >> (extraction) |
cout | ostream | Screen (output) | << (insertion) |
Both operators can be cascaded (e.g., cin >> a >> b; and cout << a << b;), and >> skips leading whitespace by default.
Frequently asked questions
- Where can I find the BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) question paper 2078?
- The full BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2078 (regular) question paper is available free on Kekkei. You can read every question online and attempt the paper under timed exam conditions.
- Does the Object-Oriented Programming (BSc CSIT, CSC161) 2078 paper come with solutions?
- Yes. Every question on this Object-Oriented Programming (BSc CSIT, CSC161) past paper includes a step-by-step solution, plus instant AI feedback when you attempt it on Kekkei.
- How many marks is the BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2078 paper?
- The BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2078 paper carries 60 full marks and is meant to be completed in 180 minutes, across 12 questions.
- Is practising this Object-Oriented Programming (BSc CSIT, CSC161) past paper free?
- Yes — reading and attempting this Object-Oriented Programming (BSc CSIT, CSC161) past paper on Kekkei is completely free.