0% found this document useful (0 votes)
41 views6 pages

Quiz 2-1

Uploaded by

rockeyraja9090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views6 pages

Quiz 2-1

Uploaded by

rockeyraja9090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

The University of Lahore, Lahore Campus

Quiz 2
Credit
Course Name: Object Oriented Programming Course Code: 4
Hours:
Course Instructor/s: Mr. Najaf Ali Program Name BS Software Engineering
Semester : BSSE 3rd Section S
Submission Deadline: Maximum Marks: 10

1. What will be the output of the following code?


#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
friend void display(const Person &p);
};
void display(const Person &p) {
cout << "Name: " << p.name << ", Age: " << p.age << endl;
}
int main() {
Person p("Alice", 30);
display(p);
return 0;
}
 A) Name: Alice, Age: 30
 B) Compilation error
 C) Runtime error
 D) Undefined behavior
2. In the following code, which line would cause a compilation error?
class Vehicle {
public:
virtual void honk() = 0;
};
class Car : public Vehicle {
public:
void honk() override { cout << "Car honks" << endl; }
};
int main() {
Vehicle v;
Car c;
v.honk(); c.honk(); return 0;
}
 A) Vehicle v;
 B) Car c;
 C) v.honk();
 D) c.honk();
3. What is the purpose of a copy constructor?
 A) To assign one object to another
 B) To create a deep copy of an object
 C) To initialize an object with another object of the same class
 D) All of the above
4. Given this code, what will be the output?
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { cout << "Some sound" << endl; }
}; class Dog : public Animal {
public:
void sound() override { cout << "Bark" << endl; }
};
int main() {
Animal *a = new Dog();
a->sound(); delete a; return 0;}
 A) Some sound
 B) Bark
 C) Compilation error
 D) Undefined behavior
5. In the following code, identify the error:
class Person {
public:
static int count;
Person() { count++; }
};
int Person::count = 0;
int main() {
Person p1, p2;
cout << "Count: " << p1.count << endl;
return 0;}
 A) Static variable not initialized
 B) Compilation error due to count access
 C) Correct code, outputs Count: 2
 D) Undefined behavior
6. Which of the following correctly demonstrates hybrid inheritance?
 A) Inheriting from a single base class
 B) Inheriting multiple classes with multilevel inheritance
 C) Combining multiple inheritance and single inheritance
 D) Using virtual base classes only
7. In the following code, what is the reason for using const in the Person parameter?
void display(const Person &p);
 A) To allow modification of p
 B) To prevent p from being modified
 C) For efficiency, making a copy of p
 D) To trigger the copy constructor
8. What would be the output of the following code?
class Counter { public:
static int count;
Counter() { count++; }
~Counter() { count--; } };
int Counter::count = 0;
int main() { Counter c1, c2;
{ Counter c3; cout << "Count: " << Counter::count << endl; }
cout << "Count: " << Counter::count << endl; return 0;}
 A) Count: 3 followed by Count: 3
 B) Count: 3 followed by Count: 2
 C) Count: 2 followed by Count: 1
 D) Compilation error
9. Which access specifier would make a class member accessible only to derived classes?
 A) Private
 B) Protected
 C) Public
 D) Friend
10. Which of the following outputs "Animal eats"?
Bat bat;
bat.eat();
 A) The Mammal class
 B) The Animal class
 C) The Bird class
 D) The Bat class
11. What would be the output of the following polymorphism example?
class Base {public:
void display() { cout << "Base class display" << endl; }
virtual void show() { cout << "Base class show" << endl; }
};
class Derived : public Base {public:
void display() { cout << "Derived class display" << endl; }
void show() override { cout << "Derived class show" << endl; }
};
int main() { Base *b; Derived d; b = &d; b->display(); b->show(); return 0;}
 A) Derived class display and Derived class show
 B) Base class display and Base class show
 C) Base class display and Derived class show
 D) Compilation error
12. Which of the following defines a pure virtual function?
 A) virtual void fun() = 0;
 B) void fun() override;
 C) friend void fun();
 D) virtual void fun();
13. What is the result of this friend function example?
class Test {private:
int data;
public:
Test(int d) : data(d) {}
friend void display(const Test &t);};
void display(const Test &t) { cout << "Data: " << t.data << endl;}
int main() { Test t(100); display(t); return 0; }
 A) Data: 100
 B) Compilation error
 C) Data: 0 > D) Undefined behavior
14. In hybrid inheritance, the Bat class can access:
 A) Only the methods in Mammal
 B) Only the methods in Bird
 C) Methods in both Mammal and Bird
 D) Only the methods in Bat
Short Questions
1. Define a friend function and explain its purpose.
2. What is a pure virtual function, and why is it used in C++?
3. Describe the main difference between a regular constructor and a copy constructor.
4. Explain the purpose of the virtual keyword in C++ functions.
5. Why is the & symbol used in a copy constructor parameter?
6. What is the main advantage of using static members in a class?
7. How does polymorphism enhance flexibility in object-oriented programming?
8. In what scenarios would you use a destructor in a class?
9. Briefly explain the concept of hybrid inheritance with an example.
10. Why would you declare a function as const in a C++ class?
11. What is the role of the override keyword in C++?
12. How does public inheritance differ from protected inheritance in terms of member access?
13. What does the friend class concept allow in C++?
14. List two benefits of using object-oriented programming.
15. Explain how the new and delete keywords are used in C++ for dynamic memory
management.
16. Describe a situation where you would use an abstract class instead of a regular class.
17. What is the purpose of using a virtual destructor in a base class?
18. Explain the significance of encapsulation in object-oriented programming.
19. What is function overloading, and how does it differ from function overriding?
20. How does inheritance contribute to code reusability?
MCQ Answers:
1. A) Name: Alice, Age: 30
2. A) Vehicle v;
3. D) All of the above
4. B) Bark
5. C) Correct code, outputs Count: 2
6. C) Combining multiple inheritance and single inheritance
7. B) To prevent p from being modified
8. B) Count: 3 followed by Count: 2
9. B) Protected
10. B) The Animal class
11. C) Base class display and Derived class show
12. A) virtual void fun() = 0;
13. A) Data: 100
14. C) Methods in both Mammal and Bird

Short Answer Questions:


1. Define a friend function and explain its purpose.
A friend function allows external access to private and protected members of a class, useful
when an external function needs access to these members without being a member itself.
2. What is a pure virtual function, and why is it used in C++?
A pure virtual function is a function with = 0 in the base class, making it abstract; it ensures
derived classes must implement this function.
3. Describe the main difference between a regular constructor and a copy constructor.
A regular constructor initializes an object, while a copy constructor initializes an object by
copying another object of the same class.
4. Explain the purpose of the virtual keyword in C++ functions.
The virtual keyword allows derived classes to override base class functions, supporting
polymorphism.
5. Why is the & symbol used in a copy constructor parameter?
It passes the object by reference, avoiding unnecessary copying and potential infinite
recursion.
6. What is the main advantage of using static members in a class?
Static members are shared across all instances of a class, making them useful for keeping
track of common data like object count.
7. How does polymorphism enhance flexibility in object-oriented programming?
Polymorphism allows different classes to define unique behaviors while sharing a common
interface, enabling dynamic function calls.
8. In what scenarios would you use a destructor in a class?
Destructors are used to release resources or perform cleanup tasks when an object goes out
of scope or is deleted.
9. Briefly explain the concept of hybrid inheritance with an example.
Hybrid inheritance combines multiple types of inheritance, like a class inheriting both single
and multiple inheritances, e.g., a Bat class inheriting from Mammal and Bird.
10. Why would you declare a function as const in a C++ class?
Declaring a function as const ensures it doesn’t modify the object’s state, allowing safe calls
on constant objects.
11. What is the role of the override keyword in C++?
The override keyword ensures a function in a derived class is correctly overriding a virtual
function in the base class.
12. How does public inheritance differ from protected inheritance in terms of member access?
Public inheritance maintains the base class’s public members as public, while protected
inheritance makes them protected in the derived class.
13. What does the friend class concept allow in C++?
It permits one class to access another class's private and protected members, facilitating
close cooperation between the two.
14. List two benefits of using object-oriented programming.
OOP enhances code reusability through inheritance and promotes modularity, making code
easier to maintain and extend.
15. Explain how the new and delete keywords are used in C++ for dynamic memory
management.
new allocates memory for objects on the heap, while delete frees it, helping manage
memory usage efficiently.
16. Describe a situation where you would use an abstract class instead of a regular class.
Use an abstract class when you want to define a common interface but enforce that derived
classes implement specific functions.
17. What is the purpose of using a virtual destructor in a base class?
A virtual destructor ensures that the derived class destructor is called, preventing resource
leaks in polymorphic deletions.
18. Explain the significance of encapsulation in object-oriented programming.
Encapsulation restricts access to an object’s internal data, promoting data integrity and
modularity.
19. What is function overloading, and how does it differ from function overriding?
Function overloading allows multiple functions with the same name but different
parameters, while overriding redefines a base class function in a derived class.
20. How does inheritance contribute to code reusability?
Inheritance allows new classes to reuse and extend the functionality of existing classes,
reducing redundancy and improving maintainability.

You might also like