0% found this document useful (0 votes)
391 views7 pages

Object-Oriented Programming Q&A Guide

Uploaded by

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

Object-Oriented Programming Q&A Guide

Uploaded by

bipinbudha369
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented Programming

- Complete Question Bank


(Chapter-wise)
Unit 1: Introduction to Object Oriented
Programming
Long Answer Questions (10 marks)
1. What is object oriented programming? Explain objects, class, encapsulation, data hiding, inheritance, and
polymorphism.
2. Why do we need object oriented programming? How can we use inheritance to reuse already written and tested
code in programs? Discuss with suitable example.
3. What are the main features of the Object-Oriented Programming? Explain with suitable practical examples.
4. Discuss the features of Object-Oriented Programming. Differentiate between Object Oriented Programming and
Procedural Oriented Programming.
5. Explain in detail the following principles of Object-Oriented Programming:

i. Data encapsulation and data hiding

ii. Inheritance and Polymorphism

iii. Abstraction

Short Answer Questions (5 marks)


1. Write any four features of object-oriented programming. Differentiate between operator overloading and function
overloading.
2. Discuss polymorphism with example.
3. Explain the role of polymorphism in Object Oriented Programming.
4. What is abstraction?
5. What are the advantages and applications of OOP?
6. How does OOP model real-world problems better than procedural programming?
Unit 2: Basics of C++ Programming
Long Answer Questions (10 marks)
1. Why do we need the preprocessor directive #include ? Describe the major parts of a C++ program.
2. What do you mean by overloading of a function? When do we use this concept? Explain with example.

Short Answer Questions (5 marks)


1. What is namespace in C++? Explain with example.
2. Explain the purpose of a namespace with suitable example.
3. How can we define our functions inside the namespace and use them outside?
4. What is library function? How is it different from user defined function?
5. What is looping? Differentiate while loop with do-while loop with example.
6. Discuss the use of inline function with example.
7. What is inline function? Why is it used? Explain with example.
8. What is the principle reason for passing arguments by reference? Explain with suitable code.
9. What do you mean by passing arguments by reference? What is the importance of passing arguments by
reference? Explain with example code.
10. What is function overloading? Explain with suitable example.
11. Explain the reason for using default arguments in the function by using suitable example.
12. What do you mean by dynamic initialization of variables?
13. Explain dynamic memory allocation in C++ using new and delete keyword.
14. Write the syntax and use of getline() and write() functions.
15. Explain with examples various manipulators used in C++.
16. What is type casting? Explain with suitable example.
17. Why type conversion is necessary in OOP? Explain with example, the type conversion routine.
18. Explain about keywords, identifiers, data types in C++, enum, operators, scope resolution operator.

Programming Questions (5 marks)


1. Write a program that shows an example of function overloading.
2. Write a program to find the square of given integer using inline function.
3. Write a program to find the cube of given integer using inline function.
4. Write a program to find the square root of given integer using inline function.
5. Write a program to convert centigrade into Fahrenheit temperature.
6. Write a program to convert feet into meter.
7. Write a program to convert inch into centimeter.
8. Write a program in C++ to count a number of words in a line of text.

Unit 3: Classes & Objects


Long Answer Questions (10 marks)
1. Discuss features of class and object. Design a class to represent a bank account with data members name,
account-number, account-type, and balance and functions to assign initial values, to deposit an amount, to
withdraw an amount after checking balance, and to display the name and balance.
2. Why constructor and destructor are required in Object Oriented Programming? Explain with suitable example.
3. Explain the role of constructor and destructor in Object Oriented Programming. Discuss user defined
parameterized constructor with suitable example.
4. What is constructor? Explain their types? Discuss user defined parameterized constructor with suitable example.
5. Define a student class (with necessary constructors and member functions) in Object Oriented Programming.
Derive a Computer Science and Mathematics class from student class adding necessary attributes. Use these
classes in a main function and display the average marks of computer science and mathematics students.

Short Answer Questions (5 marks)


1. What is class and object? Explain class and object with suitable examples.
2. What is constructor? Differentiate it with destructor.
3. Why constructor is needed? Explain different types of constructors with example.
4. What are the characteristics of constructor?
5. What is destructor?
6. Explain about accessing a member function outside a class with example.
7. How can you define member function outside of a class? Explain with example.
8. Why dynamic object is needed? Explain with suitable example.
9. How can you classify objects? Why dynamic objects are needed?
10. What is static variable and static methods? Why is it important to use static data members?
11. Example showing the real use of static data members with brief explanation.
12. What is the use of "this" pointer? Explain with example.
13. Explain about "this" pointer with suitable example.
14. Explain encapsulation in details with example program.
15. What are the various access specifiers used in C++? Explain.
16. Explain the different types of class access specifiers.
17. Differentiate between structure and class in terms of access modifier.
18. Differentiate between private, public and protected variables with suitable example.

Unit 4: Operator Overloading


Long Answer Questions (10 marks)
1. What is operator overloading? What are the benefits of overloading? How is operator overloading different from
function overloading? Write a program that shows an example of function overloading.
2. Explain operator overloading. Write a program that overloads insertion and extraction operators.
Short Answer Questions (5 marks)
1. What is operator overloading? Explain their types with suitable examples.
2. Write a program that illustrates the conversions between objects of different classes having conversion function in
source object.
3. Write a program that decreases an integer value by 1 (one) by overloading – operator.
4. Write a program to compute subtraction of two complex numbers using operator overloading.
5. Unary operator overloading using member function (concept with program)
6. Unary operator overloading using friend function (concept with program)
7. Binary operator overloading using member function (concept with program)
8. Binary operator overloading using friend function (concept with program)
9. Extraction operator overloading (concept with program)
10. Insertion operator overloading (concept with program)
11. Basic to user-defined data conversion (concept with program)
12. User-defined to basic data conversion (concept with program)
13. User-defined to user-defined data conversion (implicit conversion/conversion on destination class)
14. User-defined to user-defined data conversion (explicit conversion/conversion on source class)

Unit 5: Inheritance
Long Answer Questions (10 marks)
1. What is inheritance? Explain the ambiguities associated with multiple inheritance with suitable example programs.
2. What is inheritance? Why is it important?
3. What is Inheritance? Explain their types with their suitable examples.

Short Answer Questions (5 marks)


1. What are the types of inheritance? Explain with examples.
2. Explain the difference between private and public inheritance with suitable diagram.
3. Differentiate Public and Private Inheritance.
4. Differentiate between base class and derived class with suitable examples.
5. Differentiate Derived Class and Base Class.
6. Differentiate between super class and sub class with suitable examples.
7. Why ambiguity occurs in multiple inheritance? What are the ways to solve it? (Explain in detail with program)
8. Ambiguity in multiple inheritance and way to solve it.
9. Explain about constructor and destructor in derived class.
10. Derived class constructor, Default and parameterized constructor.
11. What is aggregation? Explain with example.
12. Differentiate container class from inheritance. Explain with suitable example.
13. What is container class? Differentiate container class from inheritance.
Unit 6: Virtual Function, Polymorphism,
and Miscellaneous C++ Features
Long Answer Questions (10 marks)
1. How late binding is different from early binding. Write a program that explains late binding using virtual function.

Short Answer Questions (5 marks)


1. What is virtual function? Explain.
2. Explain the concept of virtual functions.
3. Explain the role of virtual function in Object Oriented Programming.
4. What is polymorphism? Explain compile-time and run-time polymorphism with examples.
5. Differentiate between early binding and late binding / static binding and dynamic binding / compile-time
polymorphism and run-time polymorphism.
6. Write a program to illustrate late binding using virtual function.
7. Differentiate between abstract base classes and concrete classes.
8. What is abstract class? Explain with example.
9. Explain the concept of virtual destructor.
10. Differentiate between virtual and pure virtual function with suitable example.
11. Why friend function is required? Discuss with example.
12. What is friend function? Why it is used in OOP? Explain with an example.
13. What is friend function? Why friend function is used? Explain with example. Why it is against the philosophy of
encapsulation or data hiding?
14. Explain the friend function with its syntax.
15. Differentiate between overriding vs overloading.
16. Differentiate between function overriding and function overloading. Explain with suitable example.

Unit 7: Function Templates and Exception


Handling
Long Answer Questions (10 marks)
1. Why do we need exceptions? Explain "exceptions with arguments" with suitable program.

Short Answer Questions (5 marks)


1. What is function template? Differentiate it with class template.
2. Explain the function templates with example.
3. Why exception handling is required? Explain with suitable example.
4. Write a program to show exception handling.
5. Write a C++ program containing a possible exception. Use a try block to throw it and a catch block to handle it
properly.

Unit 8: File Handling and Streams


Short Answer Questions (5 marks)
1. What are the advantages of using the stream classes for I/O? Write a program that writes object to a file.
2. Write the syntax and use of getline() and write() functions.

Additional Important Programming


Questions
Class Design Programs
1. Define a Shape class (with necessary constructors and member functions). Derive Triangle and Rectangle
classes from Shape class adding necessary attributes. Use these classes in a main function and display the area
of triangle and rectangle.

2. Define a Clock class (with necessary constructors and member functions). Derive a wall_clock class from clock
class adding necessary attributes. Create two objects of wall_clock class with all initial state to 0 or NULL.

3. Design a class to represent a bank account with appropriate data members and member functions.

Conversion Programs
1. Temperature conversion programs (Celsius to Fahrenheit, etc.)
2. Unit conversion programs (feet to meter, inch to centimeter)
3. Mathematical operation programs using inline functions

Operator Overloading Programs


1. Complex number arithmetic using operator overloading
2. Unary operator overloading (++, --, etc.)
3. Binary operator overloading (+, -, *, /, etc.)
4. Stream operator overloading (<< and >>)
Exam Strategy Tips
For Long Answer Questions (10 marks):
Always include definition, explanation, and example/program
Include advantages and applications where relevant

For Short Answer Questions (5 marks):


Include small code examples where asked

Common Topics to Focus On:


1. OOP principles and features
2. Constructor and destructor concepts
3. Inheritance types and access specifiers
4. Operator overloading (all types)
5. Virtual functions and polymorphism
6. Exception handling basics
7. Friend functions and their usage
8. Type conversion mechanisms

Common questions

Powered by AI

The 'this' pointer is an implicit pointer accessible within a class's non-static member functions, pointing to the object invoking the method, which enables object-specific manipulation. It is useful for resolving shadowed variable names within member functions . Static data members, shared across all instances of a class, maintain constant values and storage shared by all objects. They provide a mechanism for class-level utility or counters, e.g., `static int count;`, incremented in the constructor to track instance counts. Combined, these features enhance encapsulation, reducing redundancy and supporting resource management .

Operator overloading allows C++ operators to be redefined and used with user-defined data types, enhancing the language expressiveness by enabling intuitive object interactions similar to built-in types . For instance, by overloading the '+' operator for a 'Complex' class, both adding two complex numbers and combining their similar components can be implemented as `Complex operator+(const Complex &c) { return Complex(real + c.real, imag + c.imag); }`. This mechanism simplifies code readability and allows operations using operator syntax over method calls, making the code more intuitive .

Polymorphism in Object-Oriented Programming allows objects to be treated as instances of their parent class, thereby enabling one interface to be used for a general class of actions. Compile-time polymorphism occurs via method overloading or operator overloading, allowing methods to be invoked based on the signature of the function . Run-time polymorphism is achieved through method overriding using virtual functions, where the call to an overridden method is resolved during runtime . An example of compile-time polymorphism could be using overloaded functions: `void add(int a, int b) { return a+b; }` and `void add(double a, double b) { return a+b; }`. An example of run-time polymorphism is using a base pointer referencing derived class objects and invoking overridden methods: `Base* obj = new Derived(); obj->display();` where `display()` is a virtual function .

Multiple inheritance can lead to ambiguities, such as the 'diamond problem', where a derived class inherits from two classes that both have a common base . This can result in duplicated base class properties, leading to potential confusion and inefficiencies. Challenges are addressed using virtual inheritance, where the base class is declared virtual, ensuring a single instance of the base class properties in the inheritance hierarchy. In C++, this is addressed by using `class Derived : virtual public Base1, virtual public Base2`. This solution prevents duplicate ambiguity and maintains coherent data management .

Dynamic memory allocation in C++ is performed using the `new` and `delete` operators, allowing allocation and deallocation of memory during the runtime. Using `new` returns a pointer to the beginning of the memory block, while `delete` frees the allocated space . Improper memory management, like memory leaks from not deallocating memory, can lead to exhausted memory resources and application inefficiency. For example, `int *p = new int(5); delete p;` manages memory properly, whereas omitting `delete p;` causes a leak. Proper management ensures efficient memory use and program stability .

Encapsulation and data hiding are fundamental OOP concepts that contribute to better software design by controlling access to data through access specifiers (private, protected, public). Encapsulation bundles the data (variables) and the methods (functions) that operate on the data into a single unit or class, which restricts unauthorized access and modification . Data hiding ensures that internal object details are hidden from external access, reducing complexity and increasing robustness. This differs from procedural programming, where a module-based approach could lead to scattered and less secure code due to lack of inherent access control . Encapsulation improves code maintainability and reusability, facilitating easier updates without affecting interconnected modules .

Constructors in C++ are special member functions executed automatically to initialize objects when they are created, establishing initial state. Destructors are used to clean up resources, such as dynamic memory or open files, when objects are destroyed at the end of their lifecycle . The significance lies in resource management and ensuring proper allocation and deallocation. For example, consider a class `FileHandler` that allocates memory or opens a file in its constructor and releases it in its destructor. `FileHandler() { file = fopen('file.txt', 'r'); } ~FileHandler() { fclose(file); }`. This mechanism prevents resource leaks by automatically managing resource lifecycles .

Virtual destructors ensure that the destructor of the derived class is invoked correctly when an object is deleted through a base class pointer, preventing resource leaks . In a polymorphic class hierarchy, using `virtual ~Base(){}` ensures derived class resources are released properly when a `Derived` object is pointed to by a `Base` pointer: `Base* obj = new Derived; delete obj;`. Without virtual destructors, only the base class destructor is called, potentially leaving derived class resources uncleaned .

Function templates in C++ allow functions to operate with generic types, enabling code reusability for any data type without redefining for each type, using syntax such as `template <typename T> T add(T a, T b) { return a + b; }` . Class templates provide similar functionality for classes, allowing classes to operate on generic types, which is significant in collections or containers like `std::vector`. With `template <class T> class MyContainer { T value; };`, they facilitate generic programming by supporting type-independent code, promoting extensive reusability and flexibility .

Inheritance allows new classes to inherit properties and behaviors from existing classes, promoting code reusability by eliminating redundancy. It enables developers to create a generic class and extend its functionality without the need to rewrite common functionalities . A typical scenario is when a base class 'Vehicle' has common attributes like 'wheels' and 'engine'. Derived classes such as 'Car' and 'Bike' can inherit these common attributes and add specific properties like 'number_of_doors' for 'Car' and 'handle_type' for 'Bike'. This approach prevents duplicating the base class logic in each derived class, which would constitute redundancy .

You might also like