IE II Key
IE II Key
MCQ’s
Q.1 b Q.2 b Q.3 a Q.4 b Q.5 a Q.6 b Q.7 a Q.8 a Q.9 a Q.10 a
Detailed Explanation:
1. Which of the following is NOT a type of constructor in C++?
Answer: (b) Static Constructor
Explanation:
• (a) Default Constructor: A constructor that takes no parameters and initializes an object
with default values.
• (b) Static Constructor (Incorrect): C++ does not have a concept of a "static constructor."
Constructors in C++ are called when an object is created, and there is no mechanism to
create a constructor that is executed only once like in C#.
• (c) Copy Constructor: A constructor that initializes an object using another object of the
same class.
• (d) Parameterized Constructor: A constructor that accepts parameters to initialize an
object with specific values.
4. A UML diagram which is used to show the interaction between objects over time?
Answer: (b) Sequence Diagram
Explanation:
• (a) Activity Diagram (Incorrect): Used to model workflows and processes, not object
interactions over time.
• (b) Sequence Diagram (Correct): Shows the order of interactions between objects over
time.
• (c) Collaboration Diagram (Incorrect): Focuses on the relationships and interactions
between objects but not in a time-sequenced manner.
• (d) State Chart Diagram (Incorrect): Shows the different states of an object and transitions
between them.
6. A type of inheritance that allows a single derived class to inherit from multiple base classes
is?
Answer: (b) Multiple Inheritance
Explanation:
• (a) Single Inheritance (Incorrect): Involves only one base and one derived class.
• (b) Multiple Inheritance (Correct): Allows a class to inherit from more than one base class.
• (c) Hierarchical Inheritance (Incorrect): One base class is inherited by multiple derived
classes.
• (d) Hybrid Inheritance (Incorrect): A combination of two or more types of inheritance, but
not necessarily multiple inheritance alone.
SECTION B
1. Analyze the impact of constructor overloading and method overloading in C++. How do they
contribute to code reusability and flexibility in software design?
Impact of Constructor Overloading:
• Constructor overloading allows multiple constructors in a class with different parameter
lists, enabling object initialization in various ways.
• It enhances code flexibility, as objects can be created with different initial values without
modifying the class definition.
• It improves code reusability, as a single class can accommodate different initialization
scenarios.
Example of Constructor Overloading:
#include <iostream>
using namespace std;
class Rectangle {
int length, width;
public:
// Default Constructor
Rectangle() {
length = width = 1;
}
// Parameterized Constructor
Rectangle(int l, int w) {
length = l;
width = w;
}
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
}
};
int main() {
Rectangle r1; // Calls default constructor
Rectangle r2(10, 5); // Calls parameterized constructor
r1.display();
r2.display();
return 0;
}
Impact of Method Overloading:
• Method overloading allows multiple functions with the same name but different parameter
types or numbers.
• It improves code readability and maintainability, as related functionalities are grouped
under the same function name.
• It contributes to flexibility in function usage, as the appropriate version is selected based
on parameters.
Example of Method Overloading:
#include <iostream>
using namespace std;
class Math {
public:
int add(int a, int b) {
return a + b;
}
int main() {
Math obj;
cout << "Sum (int): " << obj.add(3, 4) << endl;
cout << "Sum (double): " << obj.add(2.5, 3.8) << endl;
return 0;
}
Conclusion:
Both constructor and method overloading significantly enhance code reusability by allowing
different behaviors without rewriting the logic. They also increase flexibility in object instantiation
and function calls.
2. Explain the differences between operator overloading and method overloading in C++. How
does operator overloading enhance code readability and usability?
Feature Operator Overloading Method Overloading
Redefining operators for user-defined Defining multiple methods with the same name
Definition
types. but different parameters.
Enhances usability of objects with Improves flexibility by allowing different
Purpose
standard operators. behaviors for a method.
Syntax Uses operator keyword. Uses function overloading rules.
Allows custom behavior for operators Allows multiple functions with the same name
Flexibility
like +, -, etc. but different signatures.
Example of Operator Overloading:
#include <iostream>
using namespace std;
class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3, 4), c2(1, 2);
Complex c3 = c1 + c2; // Using overloaded operator
c3.display();
return 0;
}
How Operator Overloading Enhances Code Readability and Usability:
• Natural Syntax: Users can use +, -, *, etc., instead of complex function calls like add(c1,
c2).
• Improves Intuitiveness: Makes user-defined classes work like primitive data types.
• Enhances Maintainability: Reduces redundant function names and improves readability.
Example Without Operator Overloading (Less Readable Code):
Complex c3 = c1.add(c2); // Less intuitive
Example With Operator Overloading (More Readable Code):
Complex c3 = c1 + c2; // More intuitive
3. Compare and contrast UML Sequence Diagram and UML Collaboration Diagram. Provide an
example where these diagrams are useful.
Feature UML Sequence Diagram UML Collaboration Diagram
Represents the sequence of interactions Represents object interactions with a
Purpose
between objects over time. focus on relationships.
Focus Time-ordering of messages. Structural organization of objects.
Uses vertical lifelines and horizontal Uses numbered arrows to represent
Representation
messages. messages.
Useful for understanding message flow Useful for understanding object
Usage
over time. relationships.
Example Use Case:
• Sequence Diagram: Useful in modeling login authentication, where each step (user input,
validation, database check) happens sequentially.
• Collaboration Diagram: Useful in designing ATM transactions, where multiple objects
(User, ATM, Bank Server) interact to complete a process.
Conclusion:
• Sequence Diagrams are better for analyzing message flow over time.
• Collaboration Diagrams are better for understanding structural interactions among
objects.
• Both are important for software design and system modeling.
SECTION C
3. Explain with suitable examples the concept of Inline, Friend, and Virtual Functions.
(a) Inline Functions
Definition: Inline functions are expanded at compile-time instead of executing a function call,
reducing function call overhead.
Usage: Suitable for small, frequently used functions.
Syntax:
#include <iostream>
using namespace std;
class Demo {
public:
inline void show() { cout << "Inline Function\n"; } // Compiler expands this directly
};
int main() {
Demo d;
d.show();
return 0;
}
Advantages: Faster execution for small functions.
Disadvantages: Increases code size if overused.
Conclusion:
• Inline functions improve execution speed by eliminating function calls.
• Friend functions allow access to private members but should be used cautiously.
• Virtual functions enable polymorphism, allowing behavior to be determined at
runtime.
Each function type serves a unique purpose in C++ Object-Oriented Programming.
SECTION D
Here are detailed 12-mark answers for both sets of questions, ensuring clarity, depth, and
proper examples with code where required.
public:
Complex(int r = 0, int i = 0) { real = r; imag = i; }
// Overloading the + operator
Complex operator+(const Complex &c) {
return Complex(real + c.real, imag + c.imag);
}
void display() { cout << real << " + " << imag << "i" << endl; }
};
int main() {
Complex c1(3, 4), c2(1, 2);
Complex c3 = c1 + c2; // Calls overloaded operator
cout << "Sum: ";
c3.display();
return 0;
}
Explanation:
• The + operator is overloaded using operator+ to add two complex numbers.
• The function returns a new Complex object containing the sum.
• This enhances code readability and usability by allowing natural arithmetic
expressions.
2. Multilevel Inheritance:
• A derived class is inherited from another derived class.
• Increases complexity if the hierarchy is deep.
• Example: ElectricCar inherits from Car, which inherits from Vehicle.
class ElectricCar : public Car {
public:
void showBattery() { cout << "Battery Powered" << endl; }
};
3. Multiple Inheritance:
• A derived class inherits from two or more base classes.
• More powerful but introduces complexity (e.g., diamond problem).
• Example: StudentAthlete inherits from Student and Athlete.
class Student {
public:
void study() { cout << "Studying" << endl; }
};
class Athlete {
public:
void train() { cout << "Training" << endl; }
};
(B) C++ Program to Display a Student’s Mark Sheet Using Multiple Inheritance
#include <iostream>
using namespace std;
class Student {
protected:
string name;
int rollNumber;
public:
void setStudent(string n, int r) {
name = n;
rollNumber = r;
}
void displayStudent() {
cout << "Name: " << name << "\nRoll Number: " << rollNumber << endl;
}
};
class Marks {
protected:
int math, science, english;
public:
void setMarks(int m, int s, int e) {
math = m;
science = s;
english = e;
}
void displayMarks() {
cout << "Math: " << math << "\nScience: " << science << "\nEnglish: " << english << endl;
}
};
class Result : public Student, public Marks {
public:
void displayResult() {
displayStudent();
displayMarks();
int total = math + science + english;
float percentage = total / 3.0;
cout << "Total: " << total << "\nPercentage: " << percentage << "%" << endl;
}
};
int main() {
Result student1;
student1.setStudent("John Doe", 101);
student1.setMarks(85, 90, 88);
student1.displayResult();
return 0;
}
Explanation:
1. Class Student stores student details.
2. Class Marks stores subject marks.
3. Class Result inherits from both Student and Marks using multiple inheritance.
4. Displays complete mark sheet.
Output:
Name: John Doe
Roll Number: 101
Math: 85
Science: 90
English: 88
Total: 263
Percentage: 87.67%