1. What is Object and Object Oriented Programming?
List and Explain various
applications of Object Oriented Programming.
Object Oriented Programming (OOP) is a programming paradigm that revolves around the
concept of objects and classes. Objects are instances of classes, which define the properties
and behaviors of the objects. OOP is used to create reusable and modular code, making it
easier to maintain and extend programs.
Applications of OOP include:
- Encapsulation: Hiding internal details of an object from the outside world.
- Abstraction: Showing only necessary information to the outside world.
- Inheritance: Creating a new class based on an existing class.
- Polymorphism: Providing multiple forms of a class or function.
- Code Reusability: Using existing code to create new programs.
2. Explain Various Characteristics of Object Oriented Programming in detail.
The main characteristics of OOP are:
- Encapsulation: Hiding internal details of an object from the outside world.
- Abstraction: Showing only necessary information to the outside world.
- Inheritance: Creating a new class based on an existing class.
- Polymorphism: Providing multiple forms of a class or function.
- Code Reusability: Using existing code to create new programs.
- Composition: Creating objects from other objects.
3. Differentiate Object Oriented Programming v/s Procedure Oriented Programming.
Object Oriented Programming (OOP):
- Focuses on objects and their interactions.
- Uses classes and objects to define and organize code.
- Emphasizes encapsulation, inheritance, and polymorphism.
- Encourages code reusability and modularity.
Procedure Oriented Programming (POP):
- Focuses on procedures and functions.
- Uses procedures and functions to define and organize code.
- Emphasizes step-by-step execution of procedures.
- Encourages procedural code organization.
4. Differentiate C v/s C++.
C:
- A procedural programming language.
- Focuses on procedures and functions.
- Does not support object-oriented programming.
- Does not have exception handling.
C++:
- An extension of C.
- Supports object-oriented programming.
- Includes exception handling.
- Has additional features like templates and operator overloading.
5. Write a simple program to print "Hello World" using C++ and explain each step.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
- Step 1: Include the necessary header file (`<iostream>`).
- Step 2: Use the `using namespace std;` directive to avoid prefixing standard library functions
with `std::`.
- Step 3: Define the `main` function, which is the entry point of the program.
- Step 4: Use `cout` to print the message "Hello World".
- Step 5: Use `endl` to insert a newline character.
- Step 6: Return 0 to indicate successful execution.
6. Explain C++ basic Structure.
The basic structure of a C++ program includes:
- Header Files: Include necessary libraries and functions.
- Using Directives: Specify the namespace to use.
- Function Definitions: Define the functions that will be used.
- Main Function: The entry point of the program.
- Variables and Data Types: Declare and initialize variables.
- Control Structures: Use if-else, switch, and loops to control the flow of the program.
7. Explain SETW and ENDL Manipulators with example.
- SETW: Sets the width of the output field.
- ENDL: Inserts a newline character.
Example:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello" << setw(10) << "World" << endl;
return 0;
}
8. Explain C++ Token.
In C++, a token is a single unit of the source code that has a specific meaning. Tokens can be:
- Keywords: Reserved words like `if`, `else`, `while`, etc.
- Identifiers: Names given to variables, functions, and classes.
- Literal Values: Numbers, characters, and strings.
- Symbols: Operators like `+`, `-`, `*`, `/`, etc.
- Punctuators: Symbols like `(`, `)`, `[`, `]`, etc.
9. What is identifier? Explain various rules of identifier.
An identifier is a name given to a variable, function, or class. The rules for identifiers in C++ are:
- Must start with a letter or underscore.
- Can contain letters, digits, and underscores.
- Cannot contain special characters.
- Must be unique within a scope.
10. What is variable? How to declare and access it in program.
A variable is a storage location that holds a value. In C++, variables are declared using the
`type` `name` syntax. For example, `int x;` declares an integer variable named `x`.
To access a variable, you can use its name in your program.
11. Explain C++ Operator. OR Explain Arithmetic Operator with example. Explain
Relational Operator with example. Explain Logical Operator with example. Explain
Increment and Decrement Operator with example.
- Arithmetic Operators:
- `+` for addition
- `-` for subtraction
- `*` for multiplication
- `/` for division
- `%` for modulus
Example:
int a = 5;
int b = 2;
int sum = a + b;
- Relational Operators:
- `==` for equality
- `!=` for inequality
- `>` for greater than
- `<` for less than
- `>=` for greater than or equal to
- `<=` for less than or equal to
Example:
int a = 5;
int b = 2;
if (a > b)
{
cout << "a is greater than b";
}
- Logical Operators:
- `&&` for logical AND
- `||` for logical OR
- `` for logical NOT
Example:
int a = 5;
int b = 2;
if (a > b && a % 2 == 0)
{
cout << "a is greater than b and divisible by 2";
}
- Increment and Decrement Operators:
- `++` for increment
- `--` for decrement
Example:
int a = 5;
a++;
cout << a; // prints 6
12. Explain Conditional Operator with example.
The conditional operator (`?:`) is used to evaluate a condition and return one of two values
based on the condition.
Example:
int a = 5;
int b = 2;
int result = (a > b) ? a : b;
cout << result; // prints 5
13. Explain Scope resolution Operator With example.
The scope resolution operator (`::`) is used to access members of a class or namespace.
Example:
class MyClass
{
public:
int x = 5;
};
int main()
{
MyClass obj;
cout << obj.x; // prints 5
return 0;
}
14. Explain Reference variable with example.
A reference variable is an alias for another variable. It does not create a copy of the variable but
rather refers to the original variable.
Example:
int a = 5;
int &ref = a;
cout << ref; // prints 5
15. Explain various Conditional Making Statement with Syntax and Example. OR Explain
If statement with Syntax and Example.
- If Statement:
- Syntax: `if (condition) { code }`
- Example:
int a = 5;
if (a > 0)
{
cout << "a is positive";
}
- If-Else Statement:
- Syntax: `if (condition) { code } else { code }`
- Example:
int a = 5;
if (a > 0) {
cout << "a is positive";
}
else
{
cout << "a is negative";
}
- Switch Statement:
- Syntax: `switch (expression) { case value: code; break; ... }`
- Example:
int a = 2;
switch (a)
{
case 1:
cout << "a is 1";
break;
case 2:
cout << "a is 2";
break;
default:
cout << "a is neither 1 nor 2";
}
16. Explain Looping Statement with Syntax and Example.
- For Loop:
- Syntax: `for (initialization; condition; increment) { code }`
- Example:
for (int i = 0; i < 5; i++)
{
cout << i << endl;
}
- While Loop:
- Syntax: `while (condition) { code }`
- Example:
int i = 0;
while (i < 5)
{
cout << i << endl;
i++;
}
- Do-While Loop:
- Syntax: `do { code } while (condition);`
- Example:
int i = 0;
do
{
cout << i << endl;
i++;
} while (i < 5);
17. What is Function? Explain Various types of function with Example. List Advantages of
Function.
A function is a block of code that can be called multiple times from different parts of a program.
There are several types of functions:
- Function Declaration: Declares the function signature.
- Function Definition: Defines the function body.
- Recursive Function: Calls itself repeatedly.
- Inline Function: Expands the function body at the point of call.
Advantages of functions include:
- Code Reusability: Functions can be called multiple times.
- Modularity: Functions can be used to organize code into smaller, manageable units.
- Easier Maintenance: Functions can be modified or replaced without affecting the rest of the
program.
18. Explain Various types of function with Example.
- Function Declaration:
- Example:
int add(int, int);
- Function Definition:
- Example:
int add(int a, int b)
{
return a + b;
}
- Recursive Function:
- Example:
int factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
- Inline Function:
- Example:
inline int add(int a, int b)
{
return a + b;
}
19. Explain Inline Function? List out conditions where it can't work.
An inline function is a function that is expanded at the point of call, reducing the overhead of
function calls.
Conditions where inline functions can't work include:
- Large Functions: Inline functions can't handle large functions because they would increase the
size of the program.
- Recursive Functions: Inline functions can't handle recursive functions because they would
cause a stack overflow.
- Functions with Side Effects: Inline functions can't handle functions with side effects because
they would make the code harder to understand.
20. Explain Default Argument with example.
A default argument is a value that is used if no argument is provided.
Example:
int add(int a = 0, int b = 0)
{
return a + b;
}
21. Explain function overloading with example.
Function overloading is the ability to define multiple functions with the same name but different
parameters.
Example:
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
22. What is Class and Object in C++. How to create and access it?
A class is a blueprint for creating objects. An object is an instance of a class.
To create and access a class in C++, you can:
- Define the class: Use the `class` keyword to define the class.
- Create an object: Use the `object_name` syntax to create an object.
- Access the object: Use the object name to access its members.
Example:
class MyClass
{
public:
int x = 5;
};
int main()
{
MyClass obj;
cout << obj.x; // prints 5
return 0;
}
23. Explain Access Specifiers in C++.
Access specifiers in C++ are used to control access to members of a class.
- Public: Members can be accessed from anywhere.
- Private: Members can only be accessed within the class.
- Protected: Members can only be accessed within the class and its derived classes.
Example:
class MyClass
{
public:
int x = 5;
private:
int y = 10;
};
24. Explain Friend Function in C++.
A friend function is a function that has access to the private and protected members of a class.
Example:
class MyClass
{
public:
int x = 5;
friend void print(MyClass& obj);
};
void print(MyClass& obj)
{
cout << obj.x;
}
25. Explain Member Functions outside a class with example.
Member functions can be defined outside the class definition.
Example:
class MyClass
{
public:
int x = 5;
void print()
{
cout << x;
}
};
void MyClass::print()
{
cout << x;
}
26. Explain array within a class.
Arrays can be used within a class to store multiple values.
Example:
class MyClass
{
public:
int arr[5];
};
int main()
{
MyClass obj;
obj.arr[0] = 1;
obj.arr[1] = 2;
cout << obj.arr[0] << endl; // prints 1
return 0;
}
27. Explain array of object with example.
Arrays of objects can be used to store multiple objects of the same class.
Example:
class MyClass
{
public:
int x = 5;
};
int main()
{
MyClass arr[5];
arr[0].x = 1;
arr[1].x = 2;
cout << arr[0].x << endl; // prints 1
return 0;
}
28. Create a class student which contain set data() and display data() as functions,
Access it using object.
class Student
{
public:
int rollNo;
string name;
void setData(int roll, string nm)
{
rollNo = roll;
name = nm;
}
void displayData()
{
cout << "Roll No: " << rollNo << endl;
cout << "Name: " << name << endl;
}
};
int main()
{
Student obj;
obj.setData(1, "John");
obj.displayData();
return 0;
}
29. What is Inheritance? Explain it with its advantages.
Inheritance is a mechanism in OOP where a new class (child) inherits the properties and
behavior of an existing class (parent).
Advantages of inheritance include:
- Code Reusability: Inheritance allows code to be reused.
- Modularity: Inheritance helps to organize code into smaller, manageable units.
- Easier Maintenance: Inheritance makes it easier to modify or extend existing code.
30. Explain Single level Inheritance with example.
Single-level inheritance is when a child class inherits from a single parent class.
Example:
class Parent
{
public:
int x = 5;
};
class Child : public Parent
{
public:
void display()
{
cout << x;
}
};
int main()
{
Child obj;
obj.display(); // prints 5
return 0;
}
31. Explain Multilevel Inheritance with example.
Multilevel inheritance is when a child class inherits from a parent class, which itself inherits from
another parent class.
Example:
class GrandParent
{
public:
int x = 5;
};
class Parent : public GrandParent
{
public:
int y = 10;
};
class Child : public Parent
{
public:
void display()
{
cout << x << endl;
cout << y << endl;
}
};
int main()
{
Child obj;
obj.display(); // prints 5 and 10
return 0;
}
32. Explain Multiple Inheritance with example.
Multiple inheritance is when a child class inherits from multiple parent classes.
Example:
class Parent1
{
public:
int x = 5;
};
class Parent2
{
public:
int y = 10;
};
class Child : public Parent1, public Parent2
{
public:
void display()
{
cout << x << endl;
cout << y << endl;
}
};
int main()
{
Child obj;
obj.display(); // prints 5 and 10
return 0;
}
33. Explain Hierarchical Inheritance with example.
Hierarchical inheritance is when multiple child classes inherit from a single parent class.
Example:
class Parent
{
public:
int x = 5;
};
class Child1 : public Parent
{
public:
void display()
{
cout << x;
}
};
class Child2 : public Parent
{
public:
void display()
{
cout << x;
}
};
int main()
{
Child1 obj1;
Child2 obj2;
obj1.display(); // prints 5
obj2.display(); // prints 5
return 0;
}
34. Explain Hybrid Inheritance with example.
Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. It occurs
when a class inherits from multiple base classes, and at least one of those base classes is
inherited from another base class.
Example:
class A
{
public:
int x;
};
class B : public A
{
public:
int y;
};
class C : public A
{
public:
int z;
};
class D : public B, public C
{
public:
void display()
{
cout << x << " " << y << " " << z << endl;
}
};
int main()
{
D obj;
obj.x = 1; // Inherited from A
obj.y = 2; // Inherited from B
obj.z = 3; // Inherited from C
obj.display();
return 0;
}
35. Explain Virtual Base class with example.
A virtual base class is used to resolve the problem of multiple inheritance when a class is
inherited from two or more classes that have the same base class. It ensures that only one copy
of the base class is inherited.
Example:
class A
{
public:
int x;
};
class B : virtual public A
{
};
class C : virtual public A
{
};
class D : public B, public C
{
public:
void display()
{
cout << x << endl;
}
};
int main()
{
D obj;
obj.x = 10;
obj.display();
return 0;
}
36. Explain polymorphism with its types.
Polymorphism is the ability of an object to take on many forms. It allows objects of different
classes to be treated as objects of a common superclass.
Types of polymorphism:
1. Compile-time polymorphism (Static Polymorphism):
- Function overloading
- Operator overloading
2. Runtime polymorphism (Dynamic Polymorphism):
- Virtual functions
- Abstract classes
37. Explain virtual functions with its rules & limitations.
Virtual functions are member functions that are declared with the virtual keyword in the base
class and are redefined in the derived class. They allow dynamic binding and runtime
polymorphism.
Rules for virtual functions:
- They must be member functions.
- They must be defined in the base class.
- They must be accessed through pointers or references.
Limitations:
- They cannot be static or friend functions.
- They cannot be overloaded.
- They cannot have default arguments.
38. Explain constructor with its properties.
A constructor is a special member function that is automatically called when an object is
created. It has the same name as the class and does not have a return type.
Properties of constructors:
- They are automatically called when an object is created.
- They can be overloaded.
- They can have default arguments.
- They cannot be virtual or static.
39. Explain default constructor with example.
A default constructor is a constructor that takes no arguments. If no constructor is defined in a
class, the compiler provides a default constructor.
Example:
class MyClass
{
public:
MyClass()
{
cout << "Default constructor called" << endl;
}
};
int main()
{
MyClass obj;
return 0;
}
40. Explain parameterised constructor.
A parameterized constructor is a constructor that takes arguments. It allows you to initialize the
object with specific values.
Example:
class MyClass
{
public:
int x, y;
MyClass(int a, int b)
{
x = a;
y = b;
}
};
int main()
{
MyClass obj(10, 20);
cout << obj.x << " " << obj.y << endl;
return 0;
}
41. Explain copy constructor.
A copy constructor is a constructor that creates an object by initializing it with another object of
the same class. It is called when an object is passed by value as an argument to a function or
when an object is returned by value from a function.
Example:
class MyClass
{
public:
int x;
MyClass(int a)
{
x = a;
}
MyClass(const MyClass& obj)
{
x = obj.x;
}
};
int main()
{
MyClass obj1(10);
MyClass obj2(obj1);
cout << obj2.x << endl;
return 0;
}
42. Explain destructor with example.
A destructor is a special member function that is automatically called when an object is
destroyed. It has the same name as the class but is preceded by a tilde (`~`).
Example:
class MyClass
{
public:
MyClass()
{
cout << "Constructor called" << endl;
}
~MyClass()
{
cout << "Destructor called" << endl;
}
};
int main()
{
MyClass obj;
return 0;
}
43. Explain various file operations.
File operations in C++ include:
1. Opening a file: Using `fopen()` or `ifstream` or `ofstream`.
2. Reading from a file: Using `fscanf()`, `fread()`, or `>>` operator with `ifstream`.
3. Writing to a file: Using `fprintf()`, `fwrite()`, or `<<` operator with `ofstream`.
4. Closing a file: Using `fclose()` or `ifstream` or `ofstream` destructors.
44. What is Exception handling? How it is used in C++.
Exception handling is a mechanism for handling runtime errors or exceptional situations in a
program. It allows you to separate the error-handling code from the normal code.
In C++, exception handling is done using the following keywords:
- `try`: Encloses the code that might throw an exception.
- `catch`: Handles the exception if it is thrown.
- `throw`: Throws an exception.
Example:
#include <iostream>
using namespace std;
int main()
{
try
{
int x = 10, y = 0, z;
if (y == 0)
{
throw "Division by zero";
}
z = x / y;
cout << "Result: " << z << endl;
}
catch (const char* msg)
{
cout << "Exception caught: " << msg << endl;
}
return 0;
}