All Exams  >   Software Development  >   Basics of C++  >   All Questions

All questions of Functions for Software Development Exam

What is the scope of a variable declared inside a function in C++?
  • a)
    Local to the function
  • b)
    Global to the program
  • c)
    Local to the block where it is declared
  • d)
    Global to the function
Correct answer is option 'A'. Can you explain this answer?

Scope of a Variable Declared Inside a Function in C++

Local to the function:
- When a variable is declared inside a function in C++, it is local to that function.
- This means that the variable is only accessible within the function in which it is declared.
- Any attempts to access the variable from outside the function will result in a compilation error.

Example:
cpp
#include
using namespace std;
void myFunction() {
int x = 5; // x is local to myFunction
cout < x="" />< endl;="" output:="" />
}
int main() {
myFunction();
// cout < x="" />< endl;="" this="" line="" will="" result="" in="" a="" compilation="" />
return 0;
}

Advantages of Local Variables:
- Helps in reducing naming conflicts with variables in other parts of the program.
- Improves code readability by keeping variable declarations close to their usage.
- Allows for better memory management as local variables are automatically destroyed when the function exits.

Conclusion:
In C++, variables declared inside a function have a scope limited to that function, making them accessible only within the function body. This local scope helps in organizing code, preventing naming conflicts, and improving memory management.
1 Crore+ students have signed up on EduRev. Have you? Download the App

Which of the following is not a valid function declaration in C++?
  • a)
    int myFunction(int a, int ;b)
  • b)
    int myFunction(int a, int b) {}
  • c)
    int myFunction();
  • d)
    void myFunction(int a, int b);
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
This is not a valid function declaration in C++. Function definitions should have a body enclosed in curly braces. The correct declaration should be: int myFunction(int a, int b);

What will be the output of the following code?
#include <iostream>
int myFunction(int x) {
    return x * x;
}
int myFunction(int& x) {
    return ++x;
}
int main() {
    int value = 5;
    int result = myFunction(value);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    25
  • b)
    6
  • c)
    Compilation error
  • d)
    Undefined behavior
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code defines two overloaded functions named myFunction. The first myFunction takes an integer x and returns its square. The second myFunction takes an integer reference x and increments its value. In main, an integer variable value is declared and assigned a value of 5. The first myFunction is called with value as an argument, resulting in a return value of 25. The value 25 is then printed.

What is the purpose of a function prototype in C++?
  • a)
    To define the implementation of a function
  • b)
    To declare the existence of a function
  • c)
    To specify the return type of a function
  • d)
    To provide an alternative name for a function
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
A function prototype in C++ is used to declare the existence of a function, specifying its name, return type, and parameter types. It provides information about the function before its actual definition.

What will be the output of the following code?
#include <iostream>
using namespace std;
int main() {
    int a = 5;
    auto lambda = [&a]() { return a; };
    a = 10;
    cout << lambda();
    return 0;
}
  • a)
    5
  • b)
    10
  • c)
    Compiler Error
  • d)
    Undefined Behavior
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Lambda Function Capture:
- In the given code, a lambda function is created which captures the variable 'a' by reference using the lambda capture syntax '[&a]'.
- This means that the lambda function will always refer to the current value of 'a' when it is called.

Changing Value of 'a':
- Initially, the value of 'a' is set to 5.
- The lambda function is created before changing the value of 'a' to 10.
- Even though the value of 'a' is changed to 10 after creating the lambda function, the lambda function still captures the reference to the original value of 'a' (which was 5).

Output:
- When the lambda function is called using 'lambda()', it returns the value of 'a' that was captured when the lambda function was created.
- So, the output of the code will be 5, as the lambda function returns the original value of 'a' (which is 5) and not the updated value (which is 10).
Therefore, the correct output of the code is:
a) 5

What is the purpose of the scope resolution operator (::) in C++?
  • a)
    To define the scope of a variable
  • b)
    To access members of a class or namespace
  • c)
    To declare a global function
  • d)
    To create a nested namespace
Correct answer is option 'B'. Can you explain this answer?

The purpose of the scope resolution operator (::) in C is to access members of a class or namespace.

The scope resolution operator (::) is an essential symbol in C programming that allows programmers to access members (variables, functions, and nested classes) of a class or namespace. It is used to specify the scope or context in which a particular member is defined or accessed. This operator is particularly useful when dealing with class hierarchies or nested namespaces.

Accessing Class Members:
When working with classes, the scope resolution operator is used to access members defined within a class. It is used in the format: `ClassName::MemberName`. For example, if we have a class called `Person` with a member function called `display`, we can access it using `Person::display()`. This is especially useful when there are multiple classes with the same member names, and it helps to differentiate between them.

Accessing Nested Class Members:
In the case of nested classes, the scope resolution operator is used to access members within the nested class. For example, if we have a class called `Outer` with a nested class called `Inner` and both classes have a member function called `display`, we can access the `display` function of the `Inner` class using `Outer::Inner::display()`.

Accessing Namespace Members:
The scope resolution operator is also used to access members within a namespace. It is used in the format: `NamespaceName::MemberName`. For example, if we have a namespace called `Math` with a function called `add`, we can access it using `Math::add()`. This is particularly useful when there are multiple namespaces with the same member names.

In conclusion, the scope resolution operator (::) in C is used to access members of a class or namespace. It helps in specifying the scope or context in which a particular member is defined or accessed, ensuring clarity and avoiding naming conflicts.

What will be the output of the following code?
#include <iostream>
int myFunction(int x) {
    if (x <= 1)
        return x;
    
    return myFunction(x - 1) * x;
}
int main() {
    int result = myFunction(4);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    4
  • b)
    6
  • c)
    10
  • d)
    24
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes an integer x. Inside the function, it checks if x is less than or equal to 1 and returns x if true. Otherwise, it calls itself recursively with x decremented by 1 and multiplies the result by x. In main, myFunction is called with an argument of 4. The recursive calls result in the calculation 4 * 3 * 2 * 1, which equals 24. The value 24 is then printed.

What will be the output of the following code?
#include <iostream>
int myFunction(int x) {
    if (x <= 1)
        return x;
    
    return myFunction(x - 1) + myFunction(x - 2);
}
int main() {
    int result = myFunction(5);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    5
  • b)
    8
  • c)
    10
  • d)
    15
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The code defines a recursive function myFunction that calculates the Fibonacci sequence. The function takes an integer x and returns the sum of the two previous Fibonacci numbers. In main, myFunction is called with an argument of 5. The recursive calls result in the calculation of the 5th Fibonacci number, which is 8. The value 8 is then printed.

What will be the output of the following code?
#include <iostream>
void myFunction(int x) {
    std::cout << "Value: " << x << std::endl;
}
int main() {
    int value = 10;
    myFunction(value);
    return 0;
}
  • a)
    Value: 10
  • b)
    10
  • c)
    Value: x
  • d)
    Compilation error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes an integer x and prints its value. In main, an integer variable value is declared and assigned a value of 10. The function myFunction is then called with value as an argument, resulting in the output "Value: 10".

What will be the output of the following code?
#include <iostream>
void myFunction(int x) {
    if (x <= 0)
        return;
    
    myFunction(x - 1);
    std::cout << x << " ";
}
int main() {
    myFunction(5);
    return 0;
}
  • a)
    1 2 3 4 5
  • b)
    5 4 3 2 1
  • c)
    5 4 3 2 1 1 2 3 4 5
  • d)
    Compilation error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The code defines a recursive function myFunction that takes an integer x. Inside the function, it checks if x is less than or equal to 0 and returns if true. Otherwise, it calls itself recursively with x decremented by 1 and prints x before the recursive call. In main, myFunction is called with an argument of 5. The recursive calls result in the printing of the values 5, 4, 3, 2, and 1 in that order.

What will be the output of the following code?
#include <iostream>
int myFunction(int& x) {
    x += 5;
    return x;
}
int main() {
    int value = 10;
    int result = myFunction(value);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    15
  • b)
    10
  • c)
    20
  • d)
    Compilation error
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes an integer reference x and increments its value by 5. In main, an integer variable value is declared and assigned a value of 10. The function myFunction is called with value as an argument, modifying its value to 15. The modified value is then assigned to result and printed, resulting in the output "15".

What will be the output of the following code?
#include <iostream>
int myFunction(int x) {
    if (x == 0)
        return 0;
    
    return x + myFunction(x - 1);
}
int main() {
    int result = myFunction(0);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    0
  • b)
    1
  • c)
    Compilation error
  • d)
    Undefined behavior
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code defines a recursive function myFunction that takes an integer x. Inside the function, it checks if x is equal to 0 and returns 0 if true. Otherwise, it adds x to the result of the recursive call with x decremented by 1. In main, myFunction is called with an argument of 0. Since the initial value of x is 0, the function immediately returns 0. The value 0 is then printed.

What will be the output of the following code?
#include <iostream>
void myFunction(int x) {
    std::cout << "Value: " << x << std::endl;
}
void myFunction(int* x) {
    std::cout << "Pointer: " << *x << std::endl;
}
int main() {
    int value = 10;
    myFunction(&value);
    return 0;
}
  • a)
    Value: 10
  • b)
    Pointer: 10
  • c)
    Compilation error
  • d)
    Undefined behavior
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The code defines two overloaded functions named myFunction. The first myFunction takes an integer x and prints its value. The second myFunction takes an integer pointer x and prints the value pointed to by x. In main, an integer variable value is declared and assigned a value of 10. The address of value is passed to the second myFunction, resulting in the output "Pointer: 10".

What will be the output of the following code?
#include <iostream>
void myFunction(int x) {
    if (x <= 0)
        return;
    
    std::cout << x << std::endl;
    myFunction(x - 1);
}
int main() {
    myFunction(5);
    return 0;
}
  • a)
    1 2 3 4 5
  • b)
    5 4 3 2 1
  • c)
    1
  • d)
    Compilation error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes an integer x. Inside the function, it checks if x is less than or equal to 0 and returns if true. Otherwise, it prints the value of x and calls itself recursively with x decremented by 1. In main, myFunction is called with an argument of 5. During the recursive calls, the values 5, 4, 3, 2, and 1 are printed.

What will be the output of the following code?
#include <iostream>
void myFunction(int x) {
    std::cout << "Value: " << x << std::endl;
}
int main() {
    int value = 10;
    myFunction(value++);
    return 0;
}
  • a)
    Value: 10
  • b)
    Value: 11
  • c)
    Value: 12
  • d)
    Compilation error
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes an integer x and prints its value. In main, an integer variable value is declared and assigned a value of 10. The myFunction is then called with value++ as an argument. The value of value++ is temporarily stored as 10, and the myFunction is called with that value. The value 10 is printed inside the function. After the function call, the value of value is incremented to 11. Finally, the incremented value of value is printed outside the function, resulting in the output "Value: 12".

What will be the output of the following code?
#include <iostream>
int myFunction(int x, int y) {
    return x - y;
}
int main() {
    int result = myFunction(5, 3) * 2;
    std::cout << result << std::endl;
    return 0;
}
  • a)
    2
  • b)
    4
  • c)
    6
  • d)
    8
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes two integers x and y and returns their difference. In main, the function myFunction is called with arguments 5 and 3, resulting in a difference of 2. The difference is then multiplied by 2, resulting in the output of 8.

What will be the output of the following code?
#include <iostream>
int myFunction(int x) {
    if (x <= 1)
        return x;
    
    return myFunction(x - 1) + x;
}
int main() {
    int result = myFunction(4);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    4
  • b)
    6
  • c)
    10
  • d)
    24
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes an integer x. Inside the function, it checks if x is less than or equal to 1 and returns x if true. Otherwise, it calls itself recursively with x decremented by 1 and adds x to the result. In main, myFunction is called with an argument of 4. The recursive calls result in the calculation 4 + 3 + 2 + 1, which equals 10. The value 10 is then printed.

What will be the output of the following code?
#include <iostream>
int add(int x, int y) {
    return x + y;
}
int add(int x, int y, int z) {
    return x + y + z;
}
int main() {
    int result = add(2, 3);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    5
  • b)
    6
  • c)
    7
  • d)
    Compilation error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The code defines two functions named add. The first add function takes two integers as parameters and returns their sum. The second add function takes three integers as parameters and returns their sum. In main, the first add function is called with arguments 2 and 3, resulting in a sum of 5. The value of 5 is then printed.

What will be the output of the following code?
#include <iostream>
int myFunction(int x) {
    return x * 2;
}
int myFunction(int x, int y) {
    return x + y;
}
int main() {
    int result1 = myFunction(2);
    int result2 = myFunction(2, 3);
    std::cout << result1 << " " << result2 << std::endl;
    return 0;
}
  • a)
    4 2
  • b)
    4 5
  • c)
    2 5
  • d)
    Compilation error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The code defines two functions named myFunction. The first myFunction takes an integer x and returns its doubled value. The second myFunction takes two integers x and y and returns their sum. In main, the first myFunction is called with an argument of 2, resulting in a value of 4. The second myFunction is called with arguments 2 and 3, resulting in a value of 5. The values 4 and 5 are then printed.

What will be the output of the following code?
#include <iostream>
int myFunction(int x) {
    if (x <= 0)
        return 1;
    
    return myFunction(x - 1) * x;
}
int main() {
    int result = myFunction(0);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    1
  • b)
    0
  • c)
    Compilation error
  • d)
    Undefined behavior
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code defines a recursive function myFunction that takes an integer x. Inside the function, it checks if x is less than or equal to 0 and returns 1 if true. Otherwise, it multiplies x with the result of the recursive call with x decremented by 1. In main, myFunction is called with an argument of 0. Since the initial value of x is 0, the function immediately returns 1. The value 1 is then printed.

What will be the output of the following code?
#include <iostream>
int myFunction(int x, int y) {
    return x + y;
}
int main() {
    int result = myFunction(5, 3);
    std::cout << result << std::endl;
    return 0;
}
  • a)
    8
  • b)
    5
  • c)
    3
  • d)
    Compilation error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes two integers and returns their sum. In main, the function is called with arguments 5 and 3, and the result is assigned to result. The value of result is then printed, which is 8.

What will be the output of the following code?
#include <iostream>
void myFunction(int& x) {
    x *= 2;
}
int main() {
    const int value = 10;
    myFunction(value);
    std::cout << value << std::endl;
    return 0;
}
  • a)
    10
  • b)
    20
  • c)
    Compilation error
  • d)
    Undefined behavior
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes an integer reference x and doubles its value. In main, a constant integer variable value is declared and assigned a value of 10. Since value is declared as const, its value cannot be modified. The attempt to modify value inside myFunction results in a compilation error. The original value of value, 10, is then printed.

Chapter doubts & questions for Functions - Basics of C++ 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Functions - Basics of C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of C++

70 videos|45 docs|15 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev