Software Development Exam  >  Software Development Tests  >  Basics of C++  >   Test: Functions - 2 - Software Development MCQ

Test: Functions - 2 - Software Development MCQ


Test Description

30 Questions MCQ Test Basics of C++ - Test: Functions - 2

Test: Functions - 2 for Software Development 2024 is part of Basics of C++ preparation. The Test: Functions - 2 questions and answers have been prepared according to the Software Development exam syllabus.The Test: Functions - 2 MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Functions - 2 below.
Solutions of Test: Functions - 2 questions in English are available as part of our Basics of C++ for Software Development & Test: Functions - 2 solutions in Hindi for Basics of C++ course. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free. Attempt Test: Functions - 2 | 30 questions in 60 minutes | Mock test for Software Development preparation | Free important questions MCQ to study Basics of C++ for Software Development Exam | Download free PDF with solutions
1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Functions - 2 - Question 1

What is a function in C++?

Detailed Solution for Test: Functions - 2 - Question 1

A function in C++ is a reusable block of code that performs a specific task.

Test: Functions - 2 - Question 2

Which of the following is true about function overloading?

Detailed Solution for Test: Functions - 2 - Question 2

Function overloading in C++ allows multiple functions with the same name but different parameter types.

Test: Functions - 2 - Question 3

What is the return type of a function that doesn't return any value?

Detailed Solution for Test: Functions - 2 - Question 3

A function that doesn't return any value has a return type of void.

Test: Functions - 2 - Question 4

What is the purpose of a function prototype?

Detailed Solution for Test: Functions - 2 - Question 4

 A function prototype is used to declare the existence of a function before it is used in the program.

Test: Functions - 2 - Question 5

Which of the following is not a valid function call in C++?

Detailed Solution for Test: Functions - 2 - Question 5

In C++, a function call should have parentheses after the function name. So, myFunction; is not a valid function call.

Test: Functions - 2 - Question 6

What is recursion in C++?

Detailed Solution for Test: Functions - 2 - Question 6

Recursion in C++ is a technique where a function calls itself directly or indirectly.

Test: Functions - 2 - Question 7

What is the output of the following code?

#include <iostream>
using namespace std;

void printNumbers(int n) {
    if (n > 0) {
        printNumbers(n - 1);
        cout << n << " ";
    }
}

int main() {
    printNumbers(5);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 7

The printNumbers function uses recursion to print numbers from n to 1 in reverse order.

Test: Functions - 2 - Question 8

What is the purpose of a template in C++?

Detailed Solution for Test: Functions - 2 - Question 8

Templates in C++ are used to create generic functions or classes that can work with different data types.

Test: Functions - 2 - Question 9

What is a lambda expression in C++?

Detailed Solution for Test: Functions - 2 - Question 9

A lambda expression in C++ is an anonymous function that can be defined inline.

Test: Functions - 2 - Question 10

What is the scope of a variable defined inside a function?

Detailed Solution for Test: Functions - 2 - Question 10

Variables defined inside a function have a local scope, which means they are accessible only within that function.

Test: Functions - 2 - Question 11

What will be the output of the following code?

#include <iostream>
using namespace std;

void myFunction(int& x) {
    x = 10;
}

int main() {
    int a = 5;
    myFunction(a);
    cout << a;
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 11

The myFunction function takes a reference to x and modifies its value to 10.

Test: Functions - 2 - Question 12

What will be the output of the following code?

#include <iostream>
using namespace std;

int myFunction(int x, int y) {
    return x + y;
}

int main() {
    int a = 5, b = 3;
    cout << myFunction(a, b);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 12

The myFunction function takes two integer parameters and returns their sum, which is 8.

Test: Functions - 2 - Question 13

What will be the output of the following code?

#include <iostream>
using namespace std;

int square(int x) {
    return x * x;
}

int main() {
    cout << square(square(2));
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 13

The square function squares the input value. square(2) returns 4, and then square(4) returns 16.

Test: Functions - 2 - Question 14

What will be the output of the following code?

#include <iostream>
using namespace std;

int sum(int a, int b) {
    return a + b;
}

int sum(int a, int b, int c) {
    return a + b + c;
}

int main() {
    cout << sum(1, 2) << " ";
    cout << sum(1, 2, 3);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 14

The sum function is overloaded to accept two or three integer parameters. sum(1, 2) returns 3, and sum(1, 2, 3) returns 6.

Test: Functions - 2 - Question 15

What will be the output of the following code?

#include <iostream>
using namespace std;

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

int main() {
    cout << factorial(5);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 15

The factorial function calculates the factorial of a number. factorial(5) returns 5 * 4 * 3 * 2 * 1, which is 120.

Test: Functions - 2 - Question 16

What will be the output of the following code?

#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << add<int>(5, 2) << " ";
    cout << add<double>(2.5, 3.7);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 16

The add function is a template that can work with different data types. add<int>(5, 2) returns 7, and add<double>(2.5, 3.7) returns 6.2.

Test: Functions - 2 - Question 17

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;
}

Detailed Solution for Test: Functions - 2 - Question 17

The lambda expression captures the variable a by reference. So, when a is modified to 10, the lambda still refers to the updated value.

Test: Functions - 2 - Question 18

What will be the output of the following code?

#include <iostream>
using namespace std;

template <typename T>
void myFunction(T value) {
    cout << "Generic" << endl;
}

void myFunction(int value) {
    cout << "Integer" << endl;
}

int main() {
    myFunction(5);
    myFunction(3.14);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 18

The myFunction template is a generic function that can accept arguments of different types. The function call myFunction(5) matches the template specialization with T=int.

Test: Functions - 2 - Question 19

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;
}

Detailed Solution for Test: Functions - 2 - Question 19

The reference variable y is referencing the same memory location as x. So, when y is assigned 10, it also modifies the value of x.

Test: Functions - 2 - Question 20

What will be the output of the following code?

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    auto lambda = [x]() mutable { x += 5; return x; };
    cout << lambda() << " ";
    cout << x;
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 20

The lambda expression captures the variable x by value. The mutable keyword allows modifying the captured value without changing the original variable x.

Test: Functions - 2 - Question 21

What is the purpose of the scope resolution operator (::) in C++?

Detailed Solution for Test: Functions - 2 - Question 21

The scope resolution operator (::) is used to access members (variables, functions, etc.) of a class or namespace.

Test: Functions - 2 - Question 22

What is a function signature in C++?

Detailed Solution for Test: Functions - 2 - Question 22

The function signature includes the number and types of parameters along with the function name.

Test: Functions - 2 - Question 23

Which of the following is true about default arguments in C++ functions?

Detailed Solution for Test: Functions - 2 - Question 23

Default arguments should be specified in the function declaration, allowing them to be omitted during function call.

Test: Functions - 2 - Question 24

What is the purpose of the "const" keyword in function declarations?

Detailed Solution for Test: Functions - 2 - Question 24

The "const" keyword in function declarations is used to specify that the function doesn't modify the object it is called on.

Test: Functions - 2 - Question 25

What is the difference between pass-by-value and pass-by-reference in function parameters?

Detailed Solution for Test: Functions - 2 - Question 25

Pass-by-value makes a copy of the argument, while pass-by-reference directly operates on the original argument.

Test: Functions - 2 - Question 26

What will be the output of the following code?

#include <iostream>
using namespace std;

void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 5, y = 10;
    swap(x, y);
    cout << x << " " << y;
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 26

The swap function takes references to two variables and swaps their values. So, after calling swap(x, y), the values of x and y are swapped.

Test: Functions - 2 - Question 27

What will be the output of the following code?

#include <iostream>
using namespace std;

int myFunction(int x) {
    if (x == 0)
        return 0;
    return x + myFunction(x - 1);
}

int main() {
    cout << myFunction(3);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 27

The myFunction function uses recursion to calculate the sum of numbers from n to 0. myFunction(3) returns 3 + 2 + 1 + 0, which is 6.

Test: Functions - 2 - Question 28

What will be the output of the following code?

#include <iostream>
using namespace std;

void printNumbers(int n) {
    if (n <= 0)
        return;
    cout << n << " ";
    printNumbers(n - 1);
}

int main() {
    printNumbers(5);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 28

The printNumbers function uses recursion to print numbers from n to 1 in reverse order.

Test: Functions - 2 - Question 29

What will be the output of the following code?

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {
    cout << add(5, 2) << " ";
    cout << add(2.5, 3.7);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 29

The add function is overloaded to accept integer and double arguments. add(5, 2) returns 7, and add(2.5, 3.7) returns 6.2.

Test: Functions - 2 - Question 30

What will be the output of the following code?

#include <iostream>
using namespace std;

int factorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    return n * factorial(n - 2);
}

int main() {
    cout << factorial(5);
    return 0;
}

Detailed Solution for Test: Functions - 2 - Question 30

The factorial function calculates the factorial of a number. factorial(5) returns 5 * 3 * 1, which is 120.

70 videos|45 docs|15 tests
Information about Test: Functions - 2 Page
In this test you can find the Exam questions for Test: Functions - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Functions - 2, EduRev gives you an ample number of Online tests for practice
70 videos|45 docs|15 tests
Download as PDF
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!