Software Development Exam  >  Software Development Questions  >  What will be the output of the following code... Start Learning for Free
What will be the output of the following code?
#include <iostream>
using namespace std;
int binarySearch(int arr[], int target, int low, int high) {
    if (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == target) {
            return mid;
        } else if (arr[mid] < target) {
            return binarySearch(arr, target, mid + 1, high);
        } else {
            return binarySearch(arr, target, low, mid - 1);
        }
    }
    return -1;
}
int main() {
    int arr[] = {2, 5, 7, 10, 15};
    int target = 7;
    int n = sizeof(arr) / sizeof(arr[0]);
    int result = binarySearch(arr, target, 0, n - 1);
    cout << "Result: " << result << endl;
    return 0;
}
  • a)
    Result: 0
  • b)
    Result: 1
  • c)
    Result: 2
  • d)
    Result: 3
Correct answer is option 'C'. Can you explain this answer?
Most Upvoted Answer
What will be the output of the following code?#include <iostream>...
The code performs a recursive binary search to find the index of the target element in the given array. In this case, the target is 7, which is present at index 2 of the array.
Free Test
Community Answer
What will be the output of the following code?#include <iostream>...
Explanation:

Binary Search Algorithm
- The given code demonstrates the binary search algorithm to find the index of a target element in a sorted array.
- The function binarySearch recursively divides the array into two halves and checks if the target element is present in the middle of the array.

Input Array and Target
- The input array is {2, 5, 7, 10, 15} and the target element is 7.
- The size of the array is calculated using sizeof(arr) / sizeof(arr[0]) which gives n as 5.

Binary Search Function
- The binary search function is called with the array, target, low index as 0, and high index as n-1 (4).
- In this case, the middle element is arr[2] which is 7, hence the function returns the index 2.

Output
- The output statement in the main function prints the result which is the index returned by the binary search function.
- Therefore, the output will be "Result: 2" as the target element 7 is found at index 2 in the array.
Attention Software Development Students!
To make sure you are not studying endlessly, EduRev has designed Software Development study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Software Development.
Explore Courses for Software Development exam

Top Courses for Software Development

What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer?
Question Description
What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer? for Software Development 2024 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer? covers all topics & solutions for Software Development 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer?.
Solutions for What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer?, a detailed solution for What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer? has been provided alongside types of What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following code?#include <iostream>using namespace std;int binarySearch(int arr[], int target, int low, int high) { if (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { return binarySearch(arr, target, mid + 1, high); } else { return binarySearch(arr, target, low, mid - 1); } } return -1;}int main() { int arr[] = {2, 5, 7, 10, 15}; int target = 7; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, target, 0, n - 1); cout << "Result: " << result << endl; return 0;}a)Result: 0b)Result: 1c)Result: 2d)Result: 3Correct answer is option 'C'. Can you explain this answer? tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev