[go: up one dir, main page]

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

CPP Programming Concepts

Uploaded by

shubhamvanve777
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)
11 views5 pages

CPP Programming Concepts

Uploaded by

shubhamvanve777
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

C++ Programming Concepts

1. Fill in the Blanks

i) Control structure, depending on a certain condition, a certain series of events will be executed.

ii) In the Call by value technique, changes made in the function are local to the function and not

reflected in the caller function.

iii) The conditional operator is equivalent to the following control structure.

iv) In the do...while loop, the condition is tested after the first iteration.

v) Continue is used to continue to the next iteration of the loop.

vi) Goto is an unconditional control structure.

vii) Recursive function is called a circular definition.

viii) Strings in C++ are treated as Character array.

ix) Reference is an alias for the variable to which it is assigned.

x) In the context of pointers, * is the dereference operator.

2. Short Answer Questions

i) What are control structures in C++?

Control structures determine the flow of program execution based on specified conditions. There are

three main types in C++:

1. Sequential: Executes statements one after another.

2. Selection (Branching): Uses if, else, or switch statements to execute specific code based on

conditions.

3. Iteration (Looping): Uses loops like for, while, and do...while to repeat a block of code.

ii) Explain the difference between while and do...while loops.

while loop: The condition is evaluated before executing the loop body. If the condition is false

initially, the loop body may not execute at all.

do...while loop: The loop body is executed first, and then the condition is checked. This ensures that

the loop executes at least once.


iii) Explain the else-if ladder with an example.

An else-if ladder is a series of conditional statements used when multiple conditions need to be

checked sequentially. If one condition is true, the associated block of code executes, and the rest

are skipped.

Example:

int num = 10;

if (num < 0) {

cout << 'Negative number';

} else if (num == 0) {

cout << 'Zero';

} else {

cout << 'Positive number';

iv) Explain the significance of break in the switch-case construct.

The break statement exits the switch-case construct after executing a particular case, preventing

'fall-through' to subsequent cases. Without it, all statements below the matching case will execute.

v) What is an infinite loop? How can it be broken?

An infinite loop is one that continues to execute indefinitely because its termination condition is

never met. It can be broken using:

1. break statement: To exit the loop based on a specific condition.

2. return statement: To exit the loop by returning from the function.

vi) Explain the use of the continue statement in C++.

The continue statement skips the current iteration and proceeds to the next iteration of the loop. It is

useful when certain conditions need to be bypassed within a loop.

vii) Can program control be passed unconditionally? If yes, how?


Yes, using the goto statement. It transfers control to a labeled section of code unconditionally.

viii) Is it possible to have nesting of loops in C++? How?

Yes, loops can be nested by placing one loop inside another. For example:

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

for (int j = 0; j < 2; j++) {

cout << 'i: ' << i << ', j: ' << j << endl;

ix) What is a function? Explain the advantages of using functions in your program.

A function is a block of code designed to perform a specific task, which can be called repeatedly.

Advantages include:

1. Code Reusability: Write once, use multiple times.

2. Modularity: Helps break down a complex program into smaller, manageable parts.

3. Easy Debugging: Makes finding and fixing bugs easier.

x) What is the difference between a function prototype, function call, and function definition?

Function Prototype: Declares the function signature, providing information about the return type and

parameters.

Function Call: Invokes the function for execution.

Function Definition: Provides the actual implementation of the function.

xi) Explain the following terms associated with functions:

a) Recursive Function: A function that calls itself. Useful for problems that can be divided into similar

sub-problems.

b) Default Arguments: Predefined values for parameters that are used if no argument is passed.

c) Function Overloading: The ability to define multiple functions with the same name but different

parameter lists.
d) Pass by Value vs. Pass by Reference: Pass by value creates a copy of the argument, while pass

by reference allows the function to modify the original variable.

e) Inline Functions: Small functions defined with the inline keyword to reduce the overhead of

function calls.

xii) What is an array? Explain the advantages of arrays.

An array is a collection of elements of the same data type stored in contiguous memory. Advantages

include:

1. Efficient Access: Access elements using index numbers.

2. Memory Management: Can store a fixed number of elements.

3. Data Organization: Helps organize related data.

xiii) Explain multidimensional arrays.

Multidimensional arrays are arrays of arrays. For example, a 2D array is an array of 1D arrays. They

are used for representing data in rows and columns, such as matrices.

xiv) What are pointers and references in C++?

Pointers: Variables that store the memory address of another variable.

References: Aliases for other variables, providing an alternative name for the variable they refer to.

xv) How can an array be traversed with pointers?

An array can be traversed by incrementing a pointer to access each element:

int arr[] = {1, 2, 3, 4};

int *ptr = arr;

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

cout << *(ptr + i) << ' ';

xvi) Explain how arrays are passed to functions.


When passing an array to a function, the array name acts as a pointer to the first element. The size

of the array can be specified as an additional parameter.

void printArray(int arr[], int size) {

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

cout << arr[i] << ' ';

You might also like