0% found this document useful (0 votes)
15 views5 pages

OOPS UNIT 1 important questions

N
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)
15 views5 pages

OOPS UNIT 1 important questions

N
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
You are on page 1/ 5

Q1 How recursion different from iteration?

Recursion

Recursion is a method where a function calls itself directly or indirectly to solve smaller instances of the same
problem. It typically involves:

1. Base Case: A condition that stops the recursion. Without a base case, the recursion would continue
indefinitely.
2. Recursive Case: The part of the function that reduces the problem to a simpler or smaller instance and
calls the function itself.

Example of Recursion:
#include <iostream>

int factorial(int n) {
if (n == 0) {
return 1; // Base case
} else {
return n * factorial(n - 1); // Recursive case
}
}

int main() {
int num = 5;
std::cout << "Factorial of " << num << " is " << factorial(num) << std::endl;
return 0;
}

Iteration

Iteration involves using loops (such as for or while loops) to repeat a block of code until a condition is met. It
typically involves:

Initialization: Setting up the initial state or variables.

Condition: The loop continues to execute as long as the condition is true.

Update: Modifying the state or variables within the loop to eventually meet the condition to exit the
loop.

Q2 :What is the need of dynamic memory allocation?


● When memory is allocated or de-allocated during run-time, it is referred to as Dynamic Memory
Allocation in C++.
● A variable space is allocated that can be changed during the execution of the program.
● We use dynamic/heap memory to allocate and de-allocate a block of memory during the execution of
the program using new and delete operators.
● We can re-use our heap memory during the run-time of our program. As a result, it is highly effective.
Q3: Insert a newline and flush the output stream.
Ans. std::endl and \n both seem to do the same thing but there is a subtle
difference between them.
std::cout << std::endl inserts a new line and flushes the stream(output buffer), whereas std::cout << “\n” just
inserts a new line.

Using std::endl

The std::endl manipulator inserts a newline character into the output stream and then flushes the stream. It’s a
convenient way to ensure that the output buffer is flushed immediately.

If you only want to insert a newline without flushing the stream, you can use the newline character '\n'. To
flush the stream manually, you use std::flush:
Q4. What is the difference between a declaration and a definition of a variable?

Declaration vs. Definition

Declaration

A declaration of a variable introduces its name and type to the compiler but does not allocate memory for it.
Declarations are used to tell the compiler about the variable’s existence and its type, enabling other parts of the
code to recognize and use it.

Definition

A definition of a variable not only introduces its name and type but also allocates memory for it. It initializes
or provides space for the variable’s value, making it a concrete instance in memory.

Q5: Differentiate between break and continue.

In C++, break and continue are control flow statements used within loops to alter the normal flow of execution.
They serve different purposes and have distinct effects on loop execution.

break

Purpose:

● break is used to exit from the innermost loop or switch statement immediately, regardless of the loop’s
condition or the remaining iterations.

continue

Purpose:

● continue is used to skip the remaining statements in the current iteration of the loop and immediately
proceed to the next iteration of the loop.

#include <iostream>

int main() {
for (int i = 0; i < 10; ++i) {
if (i % 2 == 0) {
continue; // Skips the current iteration when i is even
}
std::cout << "i: " << i << std::endl;
}
return 0;
}
Q6: Justify inline function are better than macros.

Inline Function

An inline function is a regular function that is defined by the inline keyword. An inline function is a short
function that is expanded by the compiler. And its arguments are evaluated only once. Inline functions are
short-length functions that are created automatically without the use of the inline keyword inside the class. In
other words, the code for an inline function is inserted directly into the code of the calling function, which can
result in faster execution and less overhead compared to regular function calls.

Syntax of Inline Function

inline return_type function_name ( parameters )


{
// inline function code
}

2. Macro :
It is also known as the preprocessor directive. The #define keyword is used to define the macros. The
preprocessor examines the program prior to program compilation, and if it detects macros, it replaces
the macro with the macro definition.A macro function is a piece of code that performs a specific task
and can be used repeatedly throughout a program.
Syntax of Macro:
#define MACRO_NAME Macro_definition
Q7 : What is a reference variable? What is its major use?

A reference is defined as an alias for another variable. In short, it is like giving a different name to a
pre-existing variable. Once a reference is initialized to the variable, we can use either the reference name or the
variable to refer to that variable.

Creating references in C++

The basic syntax to create a reference is -

Data type& new variable = previous variable

Major Uses:
● Function Parameters: To pass arguments by reference to avoid copying and to modify the original
arguments.
● Function Return Values: To return objects by reference, allowing modifications or avoiding
unnecessary copying.
● Avoiding Copying: To prevent the overhead of copying large objects.
● Operator Overloading: To implement operators that modify the current object efficiently.
● In-Place Modifications: To directly modify the original variable without returning new values or using
pointers.

Q9: Can we assign a void pointer to an int type pointer ?if not why ? Which concept can be used to
perform this assignment?

In C++, you cannot directly assign a void* pointer to an int* pointer without an explicit cast. This restriction
exists because void* is a special type of pointer that represents a generic pointer to any type, but it does not
have a specific type associated with it. To perform such an assignment, you need to use a type cast to tell the
compiler how to interpret the void* pointer as an int* pointer.

Why Direct Assignment is Not Allowed

Direct assignment from void* to a typed pointer (like int*) is not allowed because the compiler does not know
the size or type of data that void* is pointing to. In C++, type safety is a priority, and implicit conversions that
could lead to undefined behavior or type errors are restricted.

Performing the Assignment with a Cast

To assign a void* to an int*, you need to perform an explicit cast using a C++ cast operator. Here’s how you
can do it:

Example with C++ Cast:


cpp
Copy code
#include <iostream>

int main() {
// Allocate memory for an int
void* voidPtr = new int(42);

// Explicitly cast voidPtr to int*


int* intPtr = static_cast<int*>(voidPtr);

// Use intPtr
std::cout << "Value: " << *intPtr << std::endl; // Outputs: 42

// Clean up
delete intPtr;

return 0;
}

In this example:
● voidPtr is a void* that points to an integer.
● static_cast<int*>(voidPtr) is used to convert voidPtr to an int*.
● The static_cast operator is used here because it is safer and more explicit than a C-style cast.

Concept Used for the Conversion

The concept used for this type of conversion is type casting. In C++, there are several casting operators
available to handle different types of casts:

● static_cast<>(): Used for safe type conversions between related types, such as from void* to int*. It
performs compile-time type checking and is the preferred cast for this purpose.
● dynamic_cast<>(): Used for safely casting pointers and references within an inheritance hierarchy,
primarily for polymorphic types. It is not used for void* conversions.
● const_cast<>(): Used to add or remove const qualifiers from a pointer or reference.
● reinterpret_cast<>(): Used for low-level casting between unrelated types. This cast is less safe and is
generally used for casting between different pointer types when you are sure of the type and memory
alignment.

static_cast is typically the most appropriate choice for casting void* to a specific pointer type like int*, as it
provides type checking and is designed for conversions between compatible types.

Summary

● You cannot assign a void* directly to an int* due to type safety concerns in C++.
● You must use an explicit cast, such as static_cast<int*>(voidPtr), to convert a void* to an int*.
● The concept used for this conversion is type casting, with static_cast being the appropriate choice for
converting void* to a specific pointer type.

Q10 a) Compare procedure and object oriented programming


( b) What are the different data types present in C++?
(c )Define polymorphism ,encapsulation and abstraction .
(d) What is an array in C++ and how is it different from a pointer?
(e)Write a recursive function to implement power function.?
(f)How generic programming is achieved in C++.
(g)What are the different data types present in C++?
(h). Define enumerated data type.
(i)What are operators in C++ ?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy