Q1. Differentiate O.O.P and P.O.
OOP POP
Object oriented. Structure oriented.
Program is divided into objects. Program is divided into functions.
Bottom-up approach. Top-down approach.
Inheritance property is used. Inheritance is not allowed.
It uses access specifier. It doesn’t use access specifier.
Encapsulation is used to hide the data. No data hiding.
Concept of virtual function. No virtual function.
Object functions are linked through message Parts of program are linked through parameter
passing. passing.
Adding new data and functions is easy Expanding new data and functions is not easy.
The existing code can be reused. No code reusability.
use for solving big problems. Not suitable for solving big problems.
C++, Java. C, Pascal.
Q2. List out features of object oriented Language. Explain any one.
Ans. Features of Object-Oriented Programming (OOP):
1. Encapsulation:
2. Abstraction:
3. Inheritance:
4. Polymorphism:
5. Classes and Objects:
6. Message Passing:
Encapsulation (Explained):
Encapsulation is one of the fundamental principles of OOP. It refers to the concept of wrapping the
data (attributes) and code (methods) together into a single unit, i.e., a class. The idea is to restrict
access to certain parts of an object and only expose what's necessary.
For example, in a banking system, the balance of a bank account should be private, meaning it
cannot be directly modified by users. Instead, methods like deposit() and withdraw() are provided to
modify the balance securely, ensuring that the data remains consistent.
Benefits of Encapsulation:
• Data Security: It prevents unauthorized access and modification of sensitive data.
• Easier Maintenance: Since the internal details of how an object works are hidden, changes
can be made without affecting other parts of the code.
• Controlled Access: Through getter and setter methods, you can define how the data should
be accessed and modified.
Q3. Name following operators and explain it.1)<< 2)>>
Ans. The operators << and >> are known as bitwise shift operators in many programming languages
like C, C++, and Java.
1. << (Left Shift Operator):
• Function: Shifts the bits of a number to the left by the specified number of positions.
• Effect: Each left shift operation multiplies the number by 222 for each shift position. Zeros
are added to the right end, and bits on the left end are discarded if shifted out.
int a = 5;
int result = a << 1;
2. >> (Right Shift Operator):
• Function: Shifts the bits of a number to the right by the specified number of positions.
• Effect: Each right shift operation divides the number by 222 for each shift position. The
leftmost bits are filled either with zeros (logical right shift) or the sign bit (arithmetic right
shift), depending on the system and data type.
int b = 20;
int result = b >> 2;
Q4. Explain basic structure of c++ .
Ans. #include <iostream> // 1. Preprocessor Directive
using namespace std; // 2. Using Declaration
// 3. Function Definition
int main() { // 4. Main Function
// 5. Variable Declaration and Initialization
int a = 5;
// 6. Input/Output Operations
cout << "The value of a is: " << a << endl;
// 7. Return Statement
return 0;
Detailed Explanation of Each Component:
1. Preprocessor Directive
This is the first line of a C++ program and begins with the # symbol. It instructs the compiler to
include or process the content of certain files before the actual compilation starts.
• Example: #include <iostream> tells the compiler to include the input/output stream library,
which allows us to use objects like cin and cout.
2. Using Declaration
This line allows the program to use all the elements of a specific namespace. In C++, the std
namespace contains all the standard library functions like cout, cin, etc.
• Example: using namespace std; makes the cout and cin commands available without having
to prefix them with std::.
3. Function Definition
Functions are blocks of code that perform specific tasks. Every C++ program must have a main()
function, which serves as the entry point for program execution.
• Example: int main() defines the main function. It typically returns an integer (int), which
indicates the program's exit status.
4. Main Function
The main function, int main(), is where the program starts executing. Every C++ program must have
exactly one main() function.
• Example: int main() is the main function, and the code inside the curly braces { } is executed
when the program runs.
5. Variable Declaration and Initialization
Variables are used to store data. In this section, you declare variables and assign initial values to
them.
• Example: int a = 5; declares an integer variable a and initializes it with the value 5.
6. Input/Output Operations
C++ uses the cin object for input and the cout object for output. These operations are part of the
iostream library.
• Example: cout << "The value of a is: " << a; prints the message and the value of the variable
a.
7. Return Statement
The return statement indicates that the function is complete. It returns an integer value to the
operating system, where 0 usually means successful execution.
• Example: return 0; indicates that the program has finished executing successfully.
Q5. List out c++ tokens and explain any one.
Ans. In C++, a token is the smallest element of a program that the compiler can understand. The
various types of tokens in C++ are:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuation (Special Symbols)
6. Comments
Operators
• Definition: Operators are symbols that tell the compiler to perform specific mathematical,
logical, or relational operations on operands (variables or values).
• Types of Operators:
1. Arithmetic Operators: Used for mathematical operations.
▪ Example: +, -, *, /, % (modulo)
2. Relational Operators: Used to compare two values.
▪ Example: ==, !=, <, >, <=, >=
3. Logical Operators: Used for logical operations, often in conditional statements.
▪ Example: && (logical AND), || (logical OR), ! (logical NOT)
4. Bitwise Operators: Perform operations on bits.
▪ Example: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), << (left shift), >>
(right shift)
5. Assignment Operators: Used to assign values to variables.
▪ Example: =, +=, -=, *=, /=
Q6. Explain reference variable with example.
Ans. Reference Variable in C++
A reference variable in C++ is an alias or another name for an already existing variable. Once a
reference is initialized to a variable, it can be used to access or modify the value of that variable.
References must be initialized when they are created, and they cannot be changed to refer to
another variable later.
• A reference is essentially an alternative name for an existing variable.
• Declaring a reference is done by using the & operator.
• References must be initialized when they are declared and cannot be reassigned to another
variable.
• They are particularly useful in functions, where they allow arguments to be passed by
reference, avoiding the overhead of copying data.
datatype &reference_name = existing_variable;
#include <iostream>
using namespace std;
int main() {
int x = 10;
int &ref = x;
cout << "Value of x: " << x << endl; // Output: 10
cout << "Value of ref: " << ref << endl; // Output: 10
ref = 20;
cout << "After modifying ref, x: " << x << endl;
cout << "After modifying ref, ref: " << ref << endl;
return 0;
Q7. Explain scope resolution operator without using class.
Ans. Scope Resolution Operator (::) in C++
The scope resolution operator (::) in C++ is used to define and access variables or functions outside
of their local scope. It allows the program to resolve the scope of a variable, function, or any entity
by specifying the namespace or scope to which the entity belongs.
Without using classes, the scope resolution operator is most commonly used in these situations:
1. Accessing global variables when there are local variables with the same name.
2. Accessing a function in a namespace or from a specific scope.
Use Cases of the Scope Resolution Operator
1. Accessing Global Variables
When a local variable and a global variable have the same name, the scope resolution operator can
be used to access the global variable, overriding the local scope.
2. Accessing Functions in a Namespace
C++ allows you to define functions inside different namespaces to avoid name conflicts. The scope
resolution operator is used to access a specific function from a namespace.
3. Defining Functions Outside of a Namespace
You can also use the scope resolution operator to define a function outside of the namespace in
which it is declared.
Key Points:
• The scope resolution operator :: allows you to access global variables when they are hidden
by local variables of the same name.
• It is used to specify a namespace or a scope explicitly, particularly in cases of namespace
conflicts or multiple definitions.
• It can also be used to define functions outside the scope of namespaces or classes,
associating them back with their original context.
Q8. Explain memory management operators.
Ans. Memory Management Operators in C++
C++ provides two key operators, new and delete, for dynamic memory management. These
operators allow you to allocate and deallocate memory during the program's execution, giving you
control over memory usage.
Types of Memory Management Operators
1. new: Allocates memory dynamically (on the heap).
2. delete: Deallocates dynamically allocated memory (frees the memory).
3. new[]: Allocates memory for an array of objects.
4. delete[]: Deallocates memory for an array of objects.
1. new Operator
The new operator dynamically allocates memory for a single variable or an array of variables during
runtime. It returns a pointer to the beginning of the allocated memory block.
2. delete Operator
The delete operator is used to deallocate memory that was previously allocated with the new
operator. If you don't use delete to free up memory, it can lead to memory leaks, where memory is
allocated but never released.
3. new[] Operator (For Arrays)
The new[] operator is used to dynamically allocate memory for an array of objects or variables.
4. delete[] Operator (For Arrays)
The delete[] operator is used to free memory allocated for arrays using new[].
Advantages of new and delete:
1. Dynamic Memory Allocation: Allows memory to be allocated and deallocated at runtime,
giving flexibility to programs with unknown memory requirements at compile time.
2. Better Control: You have explicit control over the memory lifecycle, ensuring efficient use of
resources.
3. Memory Allocation on Heap: Memory is allocated on the heap, allowing larger memory
allocations compared to stack memory.
Q9. Explain manipulators.
Ans. Manipulators in C++
In C++, manipulators are functions or objects used to control the formatting of input and output
streams. They are commonly used with iostream to adjust the appearance of data when it is being
output or read. Manipulators can format output in various ways, such as setting precision, adjusting
width, or changing alignment.
Types of Manipulators
1. Stream Manipulators: These are functions that modify the formatting state of a stream.
2. I/O Manipulators: These include functions that manage various aspects of I/O, such as
setting field width and precision.
Commonly Used Manipulators
1. std::setw
std::setw(int n): Sets the width of the next input or output field. It specifies the minimum number of
characters to be used for the output. If the data is shorter than the specified width, it will be padded
with spaces.
2. std::setprecision
std::setprecision(int n): Sets the decimal precision of floating-point numbers. It specifies the number
of digits to be displayed after the decimal point.
3. std::fixed and std::scientific
std::fixed: Forces the floating-point numbers to be displayed in fixed-point notation.
std::scientific: Forces the floating-point numbers to be displayed in scientific notation.
4. std::left, std::right, and std::internal
std::left: Aligns text to the left within the field width.
std::right: Aligns text to the right within the field width.
std::internal: Aligns the sign of numbers to the internal part of the field width (for numbers only).
5. std::boolalpha and std::noboolalpha
std::boolalpha: Displays boolean values as true or false instead of 1 or 0.
std::noboolalpha: Displays boolean values as 1 or 0 instead of true or false.
Q10. Explain inline function .
Ans. Inline Functions in C++
An inline function in C++ is a function for which the compiler attempts to generate inline code,
substituting the function call with the actual function code itself. This can reduce function call
overhead, particularly for small, frequently called functions, and potentially improve performance.
Definition and Syntax
To define a function as inline, you use the inline keyword before the function's return type in its
declaration.
inline return_type function_name(parameters) {
// function body
When the inline keyword is used, the compiler tries to replace the function call with the function's
code, effectively inserting the function's body directly at the point where the function is called. This
can eliminate the overhead of a function call, which includes pushing arguments onto the stack and
jumping to the function code.
Advantages of Inline Functions
1. Performance Improvement: For small functions, inline expansion can reduce the overhead
of function calls and potentially increase execution speed.
2. Code Clarity: Inline functions can improve code readability by keeping function definitions
close to where they are used.
Limitations and Considerations
1. Code Size: Overusing inline functions can lead to code bloat, where the executable size
increases due to multiple copies of the same function code being inserted into the program.
This can negatively impact performance due to increased cache usage and longer load times.
2. Complexity: Inline functions should be kept small and simple. Complex functions might not
benefit much from inlining and could even degrade performance.
3. Compiler Discretion: The inline keyword is a suggestion to the compiler, not a command.
The compiler may ignore the inline keyword for various reasons, such as when the function is
too complex or if inlining it would not be beneficial.
When to Use Inline Functions
1. Simple Functions: Inline functions are best suited for small, simple functions where the
overhead of a function call is relatively large compared to the function's execution time.
2. Accessor Functions: Functions that simply return a value or perform a straightforward
calculation can be good candidates for inlining.
#include <iostream>
using namespace std;
inline int square(int x) {
return x * x;
}
int main() {
int num = 5;
cout << "Square of " << num << " is " << square(num) << endl; // Output: Square of 5 is 25
return 0;
Q11. What is default argument?
Ans. Default Arguments in C++
A default argument in C++ is a mechanism that allows a function to be called with fewer arguments
than it is defined to accept. If an argument is omitted in a function call, the default value specified in
the function definition is used.
When you define a function with default arguments, you provide default values for some or all of its
parameters. If the caller does not provide a value for these parameters, the default values are used.
Syntax
You specify default values for parameters in the function declaration, not in the definition. However,
you can also specify them in both the declaration and definition.
Rules and Considerations
1. Default Arguments Must Be from Right to Left: Once a default value is specified for a
parameter, all subsequent parameters to the right must also have default values. You cannot
have a default argument followed by a non-default argument.
2. Default Values in Function Declarations and Definitions: You only need to specify default
arguments in the function declaration. However, if the function is defined in a separate
translation unit, you should repeat the default arguments in the definition to avoid
ambiguity.
3. Overloading and Default Arguments: Default arguments can interact with function
overloading. The compiler uses the number and types of arguments passed to determine
which overloaded version to call.
Benefits of Default Arguments
1. Simplify Function Calls: They allow you to call functions with fewer arguments, reducing the
need for multiple overloaded functions.
2. Improves Code Readability: Reduces the number of function overloads and can make the
function interface cleaner.
3. Flexibility: Provides flexibility in function calls, allowing different numbers of arguments
while maintaining default behavior.
Q12. Write a c.p.p program to……using friend function.
Ans. A friend function in C++ is a function that is declared as a friend of a class. This allows the
function to access private and protected members of the class, which it normally would not have
access to.
o The function is not in the scope of the class to which it has been declared as a friend.
o It cannot be called using the object as it is not in the scope of that class.
o It can be invoked like a normal function without using the object.
o It cannot access the member names directly and has to use an object name and dot
membership operator with the member name.
o It can be declared either in the private or the public part.
o #include <iostream>
o using namespace std;
o class Box
o {
o private:
o int length;
o public:
o Box(): length(0) { }
o friend int printLength(Box); //friend function
o };
o int printLength(Box b)
o {
o b.length += 10;
o return b.length;
o }
o int main()
o {
o Box b;
o cout<<"Length of box: "<< printLength(b)<<endl;
o return 0;
o }
Output:
Length of box: 10
Q13. What are the limitations of structures in 'C'?
Ans. Structures in C provide a way to group related variables under a single name, but they come
with certain limitations:
Limitations of Structures in C
1. No Member Functions:
o Structures in C cannot have functions (methods) associated with them. They only
store data. This means you cannot define functions that operate on the data within
the structure directly inside the structure itself, unlike C++ classes.
2. No Access Modifiers:
o C structures do not support access specifiers like private, protected, or public that
are available in C++. All members of a C structure are public, meaning they can be
accessed from anywhere in the program. This lack of access control can lead to
unintended modifications of structure data.
3. No Inheritance:
o Structures in C do not support inheritance, which is a fundamental feature of object-
oriented programming in C++. You cannot create a new structure that inherits
properties from an existing structure.
4. No Polymorphism:
o C structures do not support polymorphism. You cannot have member functions that
behave differently based on the type of structure instance. This is another feature
that is available in C++ with classes and virtual functions.
5. Fixed Size:
o The size of a structure is fixed at compile time. If you need a data structure that can
dynamically resize itself, such as an array or linked list, you must manage this
manually using dynamic memory allocation functions (malloc, free).
6. No Constructor/Destructor:
o C structures do not have constructors or destructors. Initialization and cleanup must
be handled separately, usually through functions. There is no automatic mechanism
to initialize or clean up structure data when the structure is created or destroyed.
7. No Encapsulation:
o Encapsulation is not inherently supported in C structures. All data members are
accessible from any part of the program, which can lead to less controlled and more
error-prone code.
8. No Member Functions:
o Unlike C++ classes, C structures cannot contain member functions. This means
operations that manipulate the structure’s data need to be handled through
separate functions, which can make code less organized.