Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

C++ 2016 1.

(a)Explain dynamic initialization of variables ?


(b)Define naming space ?
(c)Diffrentiate between early binding and late binding ?
(d)Define instance .
(e)Diffrentiate between reference variablea nd pointer ?
(f)Define Visibility mode ?
(g)What are template ?
(h)Diffrentiate between static data structure and dynamic data structure ?
Answer :
(a) Dynamic Initialization of Variables:
Dynamic initialization refers to the process of assigning values to variables during
runtime rather than at compile-time. In languages that support dynamic initialization,
variables can be assigned values based on user input, calculations, or other runtime
conditions. This contrasts with static initialization, where values are assigned at
compile-time. Dynamic initialization is often associated with dynamic memory
allocation, where memory is allocated and values are assigned during program
execution.

(b) Naming Space:

A naming space is a container that holds a set of identifiers (names) and ensures that
each identifier is unique within that container. It provides a way to organize and manage
the names of variables, functions, classes, or other entities within a program. Naming
spaces help avoid naming conflicts and improve code modularity by allowing the same
identifier to be used in different contexts without collision. In programming languages,
naming spaces can be hierarchical, and they facilitate the creation of distinct scopes for
identifiers.

(c) Early Binding vs. Late Binding:

● Early Binding (Static Binding):


● The process of associating a method or function call with its
implementation during compile-time.
● It leads to faster execution but lacks flexibility at runtime.
● Commonly associated with compiled languages like C++.
● Late Binding (Dynamic Binding):
● The process of associating a method or function call with its
implementation during runtime.
● Offers greater flexibility but may result in a performance overhead.
C++ 2016 1. A

● Commonly associated with interpreted or dynamically-typed languages


like Python.

(d) Instance

An i:nstance refers to a concrete occurrence or realization of a class in object-oriented


programming. A class is a blueprint or template that defines the properties and
behaviors of objects. When an object is created based on a class, it is an instance of
that class. Instances represent individual entities with specific values for their attributes
and the ability to perform actions defined by the class.

(e) Reference Variable vs. Pointer:

● Reference Variable:
● Provides an alternative name for an existing variable.
● Cannot be reassigned to refer to a different variable.
● Introduced in languages like C++.
● Pointer:
● Holds the memory address of a variable.
● Can be reassigned to point to different variables.
● Commonly used in languages like C and C++.

(f) Visibility Mode:

Visibility mode refers to the level of access or visibility that elements (such as variables,
methods, or classes) have within a program. Common visibility modes include:

● Public: Accessible from any part of the program.


● Private: Accessible only within the class or code block where it is defined.
● Protected: Accessible within the class and its subclasses.

(g) Template:

A template is a feature in programming languages that allows the creation of generic


classes or functions. It enables the definition of algorithms or data structures without
specifying the exact types they operate on. Templates provide code reusability and
flexibility by allowing the same template to work with different data types.

(h) Static Data Structure vs. Dynamic Data Structure:


C++ 2016 1. A

● Static Data Structure:


● Fixed in size at compile-time.
● Memory allocation is done during compilation.
● Examples include arrays and static arrays.
● Dynamic Data Structure:
● Size can change during runtime.
● Memory allocation and deallocation can be performed dynamically.
● Examples include linked lists, dynamic arrays, and trees.

(a) Diffrence between procedure Oriented programming & Object oriented


programming.
Answer :
Procedure-Oriented Programming (POP):

​ Focus:
● POP focuses on procedures or routines that operate on data.
​ Data and Functions:
● Data and functions are separate entities, and functions typically operate
on data that is passed to them.
​ Global Data:
● Uses a lot of global data, which can lead to potential issues with data
integrity and security.
​ Modularity:
● Divides the program into functions or procedures to promote code reuse
and modularity.
​ Examples:
● C, Pascal, and Fortran are examples of procedure-oriented programming
languages.

Object-Oriented Programming (OOP):

​ Focus:
● OOP focuses on objects that encapsulate data and behavior.
​ Data and Functions:
● Data and functions are bundled together into objects. Objects interact with
each other through defined interfaces.
​ Global Data:
● Emphasizes encapsulation, and data is often hidden within objects.
Access to data is controlled through methods.
C++ 2016 1. A

​ Modularity:
● Promotes code organization through classes and objects, facilitating
better modularity and reusability.
​ Examples:
● Java, C++, Python, and Ruby are examples of object-oriented programming
languages.

Key Differences:

​ Data and Functions:


● POP: Data and functions are separate.
● OOP: Data and functions are encapsulated within objects.
​ Focus:
● POP: Focuses on procedures or routines.
● OOP: Focuses on objects that combine data and behavior.
​ Global Data:
● POP: May use a lot of global data.
● OOP: Emphasizes encapsulation, and data is often hidden within objects.
​ Modularity:
● POP: Achieves modularity through functions or procedures.
● OOP: Achieves modularity through classes and objects.
​ Examples:
● POP: C, Pascal, Fortran.
● OOP: Java, C++, Python, Ruby.
​ Inheritance:
● POP: Generally does not support inheritance.
● OOP: Supports inheritance, allowing for the creation of hierarchical
relationships between classes.
​ Polymorphism:
● POP: Achieved through functions or procedures.
● OOP: Achieved through method overloading and overriding, enabling
objects of different classes to be treated as objects of a common base
class.

(b). Explain characteristics or features of object oriented programming.

Answer : Object-oriented programming (OOP) is a programming paradigm


that uses objects, which are instances of classes, to design and organize
code. OOP is based on several key characteristics or features that
C++ 2016 1. A

contribute to its flexibility, modularity, and reusability. Here are the main
characteristics of object-oriented programming:

​ Class and Object:


● Class: A blueprint or template that defines the attributes (data members)
and behaviors (methods) common to all objects of a certain type.
● Object: An instance of a class, representing a specific entity with its own
unique data and behavior.
​ Encapsulation:
● Encapsulation is the bundling of data (attributes) and the methods
(functions) that operate on that data into a single unit, i.e., a class.
● Access to the data is controlled through public, private, and protected
access modifiers.
​ Inheritance:
● Inheritance allows a class (subclass or derived class) to inherit attributes
and behaviors from another class (superclass or base class).
● Facilitates code reuse, extensibility, and the creation of hierarchical
relationships.
​ Polymorphism:
● Polymorphism allows objects of different types to be treated as objects of
a common base type.
● Achieved through method overloading and overriding.
​ Abstraction:
● Abstraction involves simplifying complex systems by modeling classes
based on their essential characteristics, while ignoring unnecessary
details.
● Focuses on relevant features and hides implementation details.
​ Modularity:
● Modularity is the organization of code into manageable and reusable units
(classes).
● Classes encapsulate related data and behaviors, promoting code
modularity.
​ Message Passing:
● Objects communicate and interact by sending messages to each other.
● Objects invoke methods on other objects to request actions or exchange
information.
​ Dynamic Binding:
● Dynamic binding (or late binding) allows the association of method calls
with their implementations to occur at runtime.
C++ 2016 1. A

● Enhances flexibility and adaptability.


​ Association:
● Association represents the relationships between classes.
● Defines how objects of one class are related to objects of another class.
​ Composition:

Composition is a way to combine simple or smaller objects to create more


complex objects.

Helps in building complex structures and relationships.

3. (a) What is constructor ? Explain its types with suitable examples.

Answer :

A constructor is a special member function of a class that gets executed


when an object is created. Its purpose is to initialize the object's data
members and prepare the object for use. Constructors have the same
name as the class and do not have a return type. They can be overloaded,
allowing multiple constructors within a class.

Types of Constructors in C++:

​ Default Constructor:
● A constructor with no parameters is called a default constructor.
● It is automatically invoked when an object is created if no other
constructor is called explicitly.
● Example:

class Example {

public:

// Default Constructor

Example() {

// Initialization code here

cout << "Default Constructor called." << endl;


C++ 2016 1. A

};

2. Parameterized Constructor:
● A constructor with parameters is called a parameterized constructor.
● It allows you to pass values at the time of object creation to initialize data
members.
● Example:

class Example {

private:

int x, y;

public:

// Parameterized Constructor

Example(int a, int b) {

x = a;

y = b;

cout << "Parameterized Constructor called with values " << a << " and
" << b << endl;

};

3. Copy Constructor:
C++ 2016 1. A

● A copy constructor is used to create a new object as a copy of an existing object.


● It is invoked when an object is initialized with another object of the same class.
● Example:

class Example {

private:

int data;

public:

// Copy Constructor

Example(const Example& obj) {

data = obj.data;

cout << "Copy Constructor called." << endl;

};

4. Copy Assignment Operator:

● While not a constructor, the copy assignment operator (operator=) is related to


copying one object to another.
● It is used when an already initialized object is assigned the value of another
object.
● Example:

class Example {

private:
C++ 2016 1. A

int data;

public:

// Copy Assignment Operator

Example& operator=(const Example& obj) {

if (this != &obj) {

data = obj.data;

cout << "Copy Assignment Operator called." << endl;

return *this;

};

5. Destructor:
● A destructor is used to clean up resources or perform cleanup operations when
an object goes out of scope or is explicitly deleted.
● It has the same name as the class, preceded by a tilde (~).
● Example:

class Example {

public:

// Destructor

~Example() {
C++ 2016 1. A

cout << "Destructor called." << endl;

};

3 (b) Explain various control structure in c++.

Answer : (1) Sequential Structure:

● Executes statements in the order they appear.


● Example:

statement1();

statement2();

(2) Selection Structure:

● Makes decisions based on conditions.


● Example:

if (condition) {

// Statement executed if condition is true

} else {

// Statement executed if condition is false

(3)Repetition Structure:

● Repeats a block of code multiple times.


C++ 2016 1. A

● Example:

for (int i = 0; i < 5; ++i) {

// Statement executed 5 times

(4)Jump Structure:

● Alters the normal sequence of execution.


● Example:

for (int i = 0; i < 10; ++i) {

if (i == 5) {

// Jump out of the loop when i is 5

break;

// Statements executed until i is 5

4. (a) What are static members ? how static Variables are different from
normal variables ? Explain with suitable example.

Answer : Static members in C++ are class members that are shared
among all instances of the class rather than being specific to each
instance. They include static variables and static functions. The keyword
static is used to declare static members.
C++ 2016 1. A

Static Variables vs. Normal Variables:

​ (1)Static Variables:
● Declaration: Declared using the static keyword.
● Memory Allocation: Memory is allocated once for the static variable,
shared by all instances of the class.
● Lifetime: Exists throughout the program's execution.
● Access: Accessed using the class name or object, but usually, it is
associated with the class rather than instances.

class Example {

public:

static int count; // Static variable

int value; // Normal variable

};

int Example::count = 0; // Initialization outside the class

int main() {

Example obj1, obj2;

obj1.count++; // Incrementing the static variable

obj1.value = 10;

obj2.value = 20;

cout << "Count: " << Example::count << endl; // Accessing static variable
C++ 2016 1. A

cout << "Value of obj1: " << obj1.value << endl;

cout << "Value of obj2: " << obj2.value << endl;

return 0;

(2) Normal Variables:


● Declaration: Declared without the static keyword.
● Memory Allocation: Memory is allocated separately for each instance of the
class.
● Lifetime: Exists as long as the instance of the class exists.
● Access: Accessed using the object.

class Example {

public:

int value; // Normal variable

};

int main() {

Example obj1, obj2;

obj1.value = 10;

obj2.value = 20;

cout << "Value of obj1: " << obj1.value << endl;


C++ 2016 1. A

cout << "Value of obj2: " << obj2.value << endl;

return 0;

4(b) Explain similarities between constructor and destructor.

Answer : (1) .Naming:

● Both constructors and destructors have the same name as the class they
belong to.
● Constructor: ClassName()
● Destructor: ~ClassName()
​ (2) .Member Function Syntax:
● Both are member functions of a class and use the same syntax for
defining and declaring within the class.

​ class Example {
​ public:
​ // Constructor
​ Example() {
​ // Initialization code
​ }

​ // Destructor
​ ~Example() {
​ // Cleanup code
​ }
​ };

(3). Automatic Invocation:

● Constructors are automatically invoked when an object of the class is


created.
C++ 2016 1. A

● Destructors are automatically invoked when the object goes out of scope,
is explicitly deleted, or the program terminates.
​ (4). Initialization and Cleanup:
● Constructors are used for initializing the object's state, allocating
resources, and setting up necessary conditions for the object to function
properly.
● Destructors are used for cleaning up resources, releasing memory, and
performing necessary cleanup operations before the object is destroyed.
​ (5). No Return Type:
● Both constructors and destructors do not have a return type. They don't
return values.

class Example {

public:

Example(); // Constructor

~Example(); // Destructor

};

5. What is inheritance ? why it is required ? explain its various types with the help of
suitable example.

Answer : Inheritance is a fundamental concept in object-oriented programming (OOP)


that allows a class (subclass or derived class) to inherit the properties and behaviors of
another class (superclass or base class). It enables code reuse and establishes a
relationship between classes, where the derived class inherits the members (attributes
and methods) of the base class.

Why Inheritance is Required:

​ Code Reusability:
● Inheritance promotes the reuse of code, as the properties and behaviors
defined in a base class can be inherited by multiple derived classes.
C++ 2016 1. A

​ Logical Organization:
● Inheritance facilitates the organization of classes in a logical hierarchy,
making it easier to understand and manage complex software systems.
​ Maintenance and Extensibility:
● Changes made to the base class are automatically reflected in all derived
classes, reducing redundancy and making the code easier to maintain.
New functionality can be added to derived classes without modifying the
base class.
​ Polymorphism:
● Inheritance is closely related to polymorphism, allowing objects of derived
classes to be treated as objects of the base class. This enhances
flexibility and adaptability.

Types of Inheritance in C++:

​ (1) .Single Inheritance:


● A class inherits from only one base class.
● Example:
​ class Shape {
​ public:
​ void draw() {
​ // Draw shape
​ }
​ };

​ class Circle : public Shape {
​ public:
​ void drawCircle() {
​ // Draw circle
​ }
​ };

​ (2). Multiple Inheritance:
● A class inherits from more than one base class.
● Example:

class Animal {
C++ 2016 1. A

public:

void eat() {

// Animal eats

};

class Bird {

public:

void fly() {

// Bird flies

};

class Eagle : public Animal, public Bird {

public:

// Eagle can access both eat() and fly()

};

(3). Multilevel Inheritance:

● A class is derived from another derived class.


● Example:
C++ 2016 1. A

class Vehicle {

public:

void start() {

// Start the vehicle

};

class Car : public Vehicle {

public:

// Car inherits start() from Vehicle

};

class Sedan : public Car {

public:

// Sedan inherits start() from Car

};

(4). class Animal {

public:

void eat() {

// Animal eats
C++ 2016 1. A

};

class Dog : public Animal {

public:

// Dog inherits eat() from Animal

};

class Cat : public Animal {

public:

// Cat inherits eat() from Animal

};

(5). Hybrid Inheritance:

● A combination of multiple types of inheritance within a program.


● Example:

class A {

// ...

};

class B : public A {
C++ 2016 1. A

// ...

};

class C : public A {

// ...

};

class D : public B, public C {

// ...

};

6. Write short notes on following -

(a) Virtual function


(b) Friend function
(c) Inline function
(d) Default arguments

Answer : Virtual Function:

● Definition: A virtual function is a function declared in a base class with the


virtual keyword and is meant to be overridden by derived classes.
● Usage: Enables polymorphism by allowing objects of derived classes to be
treated as objects of the base class. The actual function called is determined at
runtime.

Friend Function:
C++ 2016 1. A

● Definition: A friend function is a function that is not a member of a class but is


granted access to its private and protected members.
● Usage: Useful for providing external functions or classes with access to the
private members of a class without making them public.

Inline Function:

● Definition: An inline function is a function defined with the inline keyword,


suggesting the compiler to replace the function call with the actual code at the
point of invocation.
● Usage: Reduces function call overhead, suitable for small, frequently used
functions.

Default Arguments:

● Definition: Default arguments are values provided in the function declaration,


allowing parameters to have a default value if no argument is provided during the
function call.
● Usage: Enhances function flexibility by providing default values for certain
parameters, making the function more user-friendly.

7. (a) Write a program in c++ to overload a binary operator.

Answer : #include <iostream>

class ComplexNumber {

public:

double real;
C++ 2016 1. A

double imaginary;

// Constructor to initialize complex numbers

ComplexNumber(double r, double i) : real(r), imaginary(i) {}

// Function to add two complex numbers

ComplexNumber add(const ComplexNumber& other) const {

ComplexNumber sum(real + other.real, imaginary + other.imaginary);

return sum;

// Function to display the complex number

void display() const {

std::cout << "Real: " << real << " + Imaginary: " << imaginary << "i" << std::endl;

};

int main() {

// Create two complex numbers

ComplexNumber complex1(3.0, 4.0);

ComplexNumber complex2(1.5, 2.5);


C++ 2016 1. A

// Add complex numbers using the add function

ComplexNumber result = complex1.add(complex2);

// Display the result

std::cout << "Result of Addition:" << std::endl;

result.display();

return 0;

7. (b) Differentiate between stack & queue

Answer :

Stack:

​ Definition:
● A stack is a data structure that follows the Last In, First Out (LIFO)
principle.
● The last element added is the first one to be removed.
​ Operations:
● Elements are added or removed only from the top (end) of the stack.
● Common operations include push (to add an element) and pop (to remove
the top element).
​ Access:
● Direct access to elements is not provided; only the top element is
accessible.
​ Real-life Analogy:
C++ 2016 1. A

● Think of a stack of plates where you add a plate on top and remove the
topmost plate.
​ Implementation:
● Can be implemented using arrays or linked lists.

Queue:

​ Definition:
● A queue is a data structure that follows the First In, First Out (FIFO)
principle.
● The first element added is the first one to be removed.
​ Operations:
● Elements are added at the rear (enqueue) and removed from the front
(dequeue) of the queue.
​ Access:
● Direct access to elements is not provided; elements are accessed in the
order of their arrival.
​ Real-life Analogy:
● Think of a queue of people waiting in line, where the person who arrives
first is served first.
​ Implementation:
● Can be implemented using arrays or linked lists.

Key Differences:

​ Order of Elements:
● Stack: LIFO (Last In, First Out)
● Queue: FIFO (First In, First Out)
​ Insertion and Deletion:
● Stack: Elements are inserted and deleted from the top.
● Queue: Elements are inserted at the rear and deleted from the front.
​ Access:
● Stack: Direct access to elements is not provided.
● Queue: Direct access to elements is not provided; elements are accessed
in the order of arrival.
​ Common Operations:
● Stack: Push (add to the top), Pop (remove from the top).
● Queue: Enqueue (add to the rear), Dequeue (remove from the front).
C++ 2016 1. A

​ Real-life Analogy:
● Stack: Think of a stack of plates.
● Queue: Think of people waiting in a line.

Both stacks and queues are fundamental data structures with their specific use cases.

The choice between them depends on the requirements of the problem being solved.

You might also like