All Exams  >   EmSAT Achieve  >   C++ for EmSAT Achieve  >   All Questions

All questions of Functions for EmSAT Achieve Exam

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 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?

Introduction:
In the C programming language, a function prototype is a declaration that specifies the name of a function, the number and types of its parameters, and its return type. It provides information to the compiler about the existence and signature of the function before its actual implementation.

Purpose of a Function Prototype:
The main purpose of a function prototype in C is to declare the existence of a function. Let's discuss this in detail:

1. Declare the Existence of a Function:
A function prototype declares the existence of a function to the compiler before its actual implementation is encountered. It provides the necessary information to the compiler about the function's name, parameters, and return type. This allows the compiler to validate the usage of the function throughout the program and ensures that the function is used correctly.

2. Enable Type Checking:
The function prototype allows the compiler to perform type checking on the function call. When a function is called without a prototype, the compiler assumes a default return type of `int`. However, if the actual return type is different, it may lead to unexpected behavior or errors. By providing a function prototype, the compiler can verify that the function call matches the expected return type and parameter types.

3. Resolve Forward Declarations:
In cases where functions are defined after their usage in the program, a function prototype is essential. It allows the compiler to resolve forward declarations. Without a prototype, the compiler may assume a default return type and parameter list, resulting in potential errors or warnings. With a function prototype, the compiler can correctly interpret the function's signature and generate appropriate code.

4. Facilitate Modular Programming:
Function prototypes play a crucial role in modular programming. By declaring the existence and signature of functions in a separate header file, different source files can include the header and use the functions without needing the actual implementation details. This promotes code reusability, readability, and maintainability.

Conclusion:
In summary, the purpose of a function prototype in C is to declare the existence of a function, provide information about its signature, enable type checking, resolve forward declarations, and facilitate modular programming. It ensures that the function is used correctly and allows the compiler to generate efficient and error-free code.

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?

Explanation:

Function Overloading:
- In the given code, two functions with the same name `myFunction` are defined but with different parameters (one takes an integer and the other takes an integer reference).
- This is an example of function overloading in C++, where multiple functions can have the same name but different parameters.

myFunction(int x):
- When `myFunction(int x)` is called with the value `5`, it returns the square of `5`, which is `25`.

Output:
- Therefore, the output of the code will be 25.

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?

Sonal Yadav answered
A variable declared inside a function in C++ has local scope, which means it is only accessible within that function. It cannot be accessed from outside the function.

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?

Invalid Function Declaration in C

The correct answer is option 'B', which is not a valid function declaration in C. Let's understand why this option is invalid and explore the valid function declarations.

Invalid Function Declaration

b) int myFunction(int a, int ;b)

In this option, there is a semicolon (;) placed incorrectly after the second parameter declaration. In C language, semicolons are used to terminate statements, not within parameter declarations. Therefore, this option is not a valid function declaration.

Valid Function Declarations

a) int myFunction(int a, int b) {}

This option is a valid function declaration. It declares a function named myFunction that takes two integer parameters (a and b) and returns an integer value. The function body is enclosed within curly braces {}.

c) int myFunction();

This option is also a valid function declaration. It declares a function named myFunction that takes no parameters and returns an integer value. The empty parentheses () indicate that the function does not have any parameters.

d) void myFunction(int a, int b);

This option is a valid function declaration. It declares a function named myFunction that takes two integer parameters (a and b) and does not return any value. The keyword "void" is used to indicate that the function does not return a value.

In C, function declarations follow the syntax: return_type function_name(parameter_list);

- The return_type specifies the data type of the value returned by the function.
- The function_name is the name given to the function.
- The parameter_list is a comma-separated list of parameters (data type followed by parameter name) enclosed in parentheses.

Conclusion

The invalid function declaration is option 'B' because it contains a semicolon (;) after the second parameter declaration. The correct function declarations include options 'A', 'C', and 'D'. These options demonstrate the correct syntax for declaring functions in C, including specifying the return type, function name, and parameter list.

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

Explanation:
- Reference Variable:
- In the given code, a reference variable y is created which is assigned to reference x. This means that y is an alias for x.
- Value Assignment:
- The value of y is changed to 10.
- Output:
- Since y is a reference to x, changing the value of y also changes the value of x. Therefore, the output of the code will be 10.

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.

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>
void myFunction(int* x) {
    *x = 20;
}
int main() {
    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 'C'. Can you explain this answer?

Sonal Yadav answered
The code defines a function myFunction that takes an integer pointer x and modifies the value pointed to by x to 20. In main, an integer variable value is declared and assigned a value of 10. The address of value is passed to myFunction, attempting to modify its value to 20. However, value is not declared as a pointer, and passing its address to myFunction results in a compilation error.

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>
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>
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 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 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.

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

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

C++ for EmSAT Achieve

71 videos|45 docs|15 tests

Top Courses EmSAT Achieve