BSc CSIT (TU) Science Object-Oriented Programming (BSc CSIT, CSC161) Question Paper 2077 Nepal
This is the official BSc CSIT (TU) (Science stream) Object-Oriented Programming (BSc CSIT, CSC161) question paper for 2077, 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 2077 paper is a great way to practise under real exam conditions.
Section A: Long Answer Questions
Attempt any TWO questions.
What is a friend function? Explain its characteristics. Write a C++ program using a friend function to add two objects of a class.
Friend Function
A friend function is a non-member function that is granted special permission to access the private and protected members of a class. It is declared inside the class body using the keyword friend, but it is not a member of that class.
class Sample {
friend void show(Sample s); // declaration
};
Characteristics
- It is not a member of the class, so it is not called using an object (
obj.func()); it is called like an ordinary function. - It can access private and protected members of the class.
- It must be declared with the keyword
friendinside the class, but defined outside withoutfriendand without the scope-resolutionClassName::. - It does not have a
thispointer, so it must receive the object as an argument. - Friendship is not mutual and not inherited unless explicitly granted.
- It can be a friend of more than one class.
C++ Program: Adding Two Objects Using a Friend Function
#include <iostream>
using namespace std;
class Complex {
int real, imag;
public:
Complex(int r = 0, int i = 0) { real = r; imag = i; }
// friend function declaration
friend Complex add(Complex c1, Complex c2);
void display() { cout << real << " + " << imag << "i" << endl; }
};
// friend function definition (no Complex:: , no friend keyword here)
Complex add(Complex c1, Complex c2) {
Complex temp;
temp.real = c1.real + c2.real; // direct access to private members
temp.imag = c1.imag + c2.imag;
return temp;
}
int main() {
Complex a(3, 4), b(5, 6), c;
c = add(a, b); // called like a normal function
cout << "Sum = "; c.display(); // Output: Sum = 8 + 10i
return 0;
}
Output: Sum = 8 + 10i
What is function overloading? Differentiate between function overloading and function overriding with examples.
Function Overloading
Function overloading is a form of compile-time (static) polymorphism in which two or more functions in the same scope share the same name but differ in their parameter list (number, types, or order of arguments). The compiler decides which version to call based on the arguments supplied.
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
Note: functions cannot be overloaded by return type alone.
Function Overriding
Function overriding is run-time (dynamic) polymorphism in which a derived class provides a new definition for a virtual function already defined in its base class, with the same name and same signature.
class Base {
public:
virtual void show() { cout << "Base\n"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived\n"; }
};
Difference Table
| Basis | Function Overloading | Function Overriding |
|---|---|---|
| Binding | Compile-time (early) binding | Run-time (late) binding |
| Scope | Same class/scope | Base and derived classes |
| Signature | Must differ in parameter list | Must be identical |
| Inheritance | Not required | Requires inheritance |
virtual keyword | Not needed | Base function must be virtual |
| Polymorphism type | Static | Dynamic |
Explain exception handling in C++. Write a program using try, catch and throw to handle division by zero.
Exception Handling in C++
Exception handling is a mechanism to detect and respond to run-time errors in a controlled way, separating error-handling code from normal program logic. C++ provides three keywords:
try– encloses the block of code that may generate an exception.throw– signals (raises) an exception when an error condition occurs, passing an object/value describing the error.catch– immediately follows thetryblock and handles the thrown exception of a matching type.
Working: When code inside try throws an exception, normal flow stops, the stack unwinds, and control transfers to the first catch block whose parameter type matches the thrown value. If no handler matches, terminate() is called.
try {
// code that may throw
throw value;
}
catch (type arg) {
// handle exception
}
Program: Handling Division by Zero
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter numerator and denominator: ";
cin >> a >> b;
try {
if (b == 0)
throw "Division by zero error!"; // throw an exception
cout << "Result = " << (float)a / b << endl;
}
catch (const char* msg) { // catch the exception
cout << "Exception: " << msg << endl;
}
return 0;
}
Sample run: Input 10 0 → Output: Exception: Division by zero error!
Input 10 2 → Output: Result = 5
Section B: Short Answer Questions
Attempt any EIGHT questions.
What is a constructor? List its characteristics.
Constructor
A constructor is a special member function of a class that has the same name as the class and is automatically invoked when an object is created. Its main purpose is to initialize the object's data members.
Characteristics
- It has the same name as the class.
- It has no return type, not even
void. - It is called automatically when an object is created.
- It can be overloaded (default, parameterized, copy constructors).
- It is usually declared in the public section.
- It cannot be
virtualand cannot be inherited. - If no constructor is defined, the compiler provides a default constructor.
Differentiate between early binding and late binding.
Early Binding vs Late Binding
Binding means associating a function call with the function body (its address).
| Basis | Early (Static) Binding | Late (Dynamic) Binding |
|---|---|---|
| When resolved | At compile time | At run time |
| Also called | Static / compile-time binding | Dynamic / run-time binding |
| Mechanism | Function call linked directly | Resolved via virtual functions and the vtable |
| Keyword | No special keyword | Uses virtual functions and base-class pointers/references |
| Speed | Faster (no run-time lookup) | Slower (extra indirection) |
| Flexibility | Less flexible | More flexible (supports polymorphism) |
| Example | Normal function call, overloaded functions | Overridden virtual function called through base pointer |
Example (late binding):
Base* p = new Derived();
p->show(); // resolved at run time -> Derived::show() if show() is virtual
What is the 'this' pointer?
The this Pointer
The this pointer is an implicit pointer automatically available inside every non-static member function of a class. It holds the address of the object on which the member function is currently invoked, so each function knows which object's data it is working with.
Key Points
- It is passed implicitly as a hidden argument to all non-static member functions.
- It is not available in
staticmember functions (they belong to the class, not an object). - It is used to:
- Distinguish a data member from a parameter of the same name (e.g.
this->x = x;). - Return the current object by reference to allow method chaining (
return *this;).
- Distinguish a data member from a parameter of the same name (e.g.
class Box {
int x;
public:
Box& setX(int x) {
this->x = x; // 'this' resolves the name clash
return *this; // return current object for chaining
}
};
Explain the friend class concept.
Friend Class
A friend class is a class whose all member functions are granted access to the private and protected members of another class. When class B is declared a friend of class A, every member function of B can access A's private and protected members.
class A {
int secret = 10;
friend class B; // B is a friend of A
};
class B {
public:
void show(A a) {
cout << a.secret; // legal: B can access A's private member
}
};
Key Points
- Declared inside the granting class using
friend class ClassName;. - Gives the entire friend class access to private/protected members.
- Friendship is one-way (if B is friend of A, A is not automatically friend of B).
- Friendship is not inherited and not transitive.
- Useful when two classes are tightly coupled and must share internal data (e.g. linked-list node and list classes).
What are the advantages of inheritance?
Advantages of Inheritance
Inheritance allows a new class (derived/child) to acquire the properties and behaviors of an existing class (base/parent). Its main advantages are:
- Code reusability – existing base-class code is reused, avoiding rewriting common functionality.
- Reduced redundancy – common members are defined once in the base class.
- Extensibility – new features can be added in derived classes without modifying the base class.
- Maintainability – changes made in the base class automatically propagate to derived classes.
- Supports polymorphism – enables run-time polymorphism through virtual functions and overriding.
- Represents real-world hierarchies – models "is-a" relationships (e.g. a Car is a Vehicle), improving readability and design.
- Transitive nature – a derived class can itself be a base for further classes (multilevel inheritance).
Write a note on dynamic memory allocation in C++.
Dynamic Memory Allocation in C++
Dynamic memory allocation is the process of allocating memory at run time from the heap (free store), rather than at compile time. It is useful when the required amount of memory is not known in advance. C++ uses the operators new and delete.
new Operator
Allocates memory on the heap and returns a pointer to it.
int* p = new int; // single int
int* arr = new int[5]; // array of 5 ints
*p = 100;
delete Operator
Frees the dynamically allocated memory to avoid memory leaks.
delete p; // free single object
delete[] arr; // free array
Key Points
- Memory is allocated from the heap, not the stack.
newreturns the address of the allocated memory; if allocation fails it throwsbad_alloc.- Every
newmust be matched by adelete(andnew[]bydelete[]) to prevent memory leaks and dangling pointers. - Unlike C's
malloc()/free(),newalso calls constructors anddeletecalls destructors for objects, andnewis type-safe (no casting needed).
What is type conversion? Mention its types.
Type Conversion
Type conversion is the process of converting a value of one data type into another data type. In C++ it can be implicit (done automatically by the compiler) or explicit (done manually by the programmer using a cast).
Types of Type Conversion
-
Implicit conversion (type promotion): Performed automatically by the compiler when types are compatible, usually from a smaller to a larger type (e.g.
int→float). No data loss for widening; possible loss for narrowing.int i = 5; float f = i; // int automatically converted to float -
Explicit conversion (type casting): Done manually by the programmer.
float x = (float) 7 / 2; // C-style cast double d = static_cast<double>(a); // C++ cast
In the context of user-defined types (objects), the three categories are:
- Basic type to class type – using a constructor.
- Class type to basic type – using an overloaded conversion (cast) operator.
- Class type to another class type – using a constructor or conversion operator.
Differentiate between overloading and overriding.
Overloading vs Overriding
| Basis | Overloading | Overriding |
|---|---|---|
| Definition | Same function name with different parameter lists in the same scope | Derived class redefines a base-class virtual function with the same signature |
| Polymorphism | Compile-time (static) | Run-time (dynamic) |
| Binding | Early binding | Late binding |
| Inheritance | Not required | Required (base and derived classes) |
| Signature | Must differ | Must be identical |
virtual keyword | Not needed | Base function must be virtual |
Overloading example:
int area(int s); // square
int area(int l, int b); // rectangle
Overriding example:
class Shape { public: virtual void draw(); };
class Circle : public Shape { public: void draw() override; };
What is a template function?
Template Function
A template function (function template) is a function written with a generic (parameterized) data type so that the same logic works for many data types without rewriting it for each type. It implements generic programming and is a form of compile-time polymorphism. The compiler generates an actual function for each type used (template instantiation).
Syntax
template <class T>
T functionName(T a, T b) {
// generic body
}
Example
#include <iostream>
using namespace std;
template <class T>
T maximum(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << maximum(10, 20) << endl; // T = int -> 20
cout << maximum(3.5, 2.1) << endl; // T = double -> 3.5
cout << maximum('a', 'z') << endl; // T = char -> z
return 0;
}
Advantages: code reusability, type safety, and a single definition that works for int, float, char, etc.
Frequently asked questions
- Where can I find the BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) question paper 2077?
- The full BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2077 (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) 2077 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) 2077 paper?
- The BSc CSIT (TU) Object-Oriented Programming (BSc CSIT, CSC161) 2077 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.