0
0
C++programming~20 mins

Return inside loops in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Return Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this C++ code with return inside a loop?

Look at the code below. What will it print when run?

C++
#include <iostream>

int findFirstEven(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] % 2 == 0) {
            return arr[i];
        }
    }
    return -1;
}

int main() {
    int numbers[] = {3, 7, 5, 8, 10};
    std::cout << findFirstEven(numbers, 5);
    return 0;
}
A8
B3
C-1
D10
Attempts:
2 left
๐Ÿ’ก Hint

Return inside the loop stops the function immediately when condition is met.

โ“ Predict Output
intermediate
2:00remaining
What does this function return when no return is inside the loop?

What will this C++ function return when called with the array below?

C++
#include <iostream>

int findNegative(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] < 0) {
            break;
        }
    }
    return 0;
}

int main() {
    int data[] = {1, 2, 3, 4, 5};
    std::cout << findNegative(data, 5);
    return 0;
}
AUndefined behavior
B1
CCompilation error
D0
Attempts:
2 left
๐Ÿ’ก Hint

Notice the function returns 0 after the loop finishes.

๐Ÿ”ง Debug
advanced
2:00remaining
Why does this function always return 0?

Examine the code below. The function is supposed to return the first positive number, but it always returns 0. Why?

C++
#include <iostream>

int firstPositive(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] > 0) {
            return 0;
        }
    }
    return -1;
}

int main() {
    int nums[] = {-3, -1, 4, 5};
    std::cout << firstPositive(nums, 4);
    return 0;
}
AThe function returns 0 inside the loop instead of the positive number.
BThe loop never runs because size is zero.
CThe function has a syntax error and won't compile.
DThe function returns -1 before the loop starts.
Attempts:
2 left
๐Ÿ’ก Hint

Look carefully at what is returned inside the loop.

๐Ÿ“ Syntax
advanced
2:00remaining
Which option causes a compilation error due to return inside loop?

Which of the following code snippets will cause a compilation error?

Aint foo() { for(int i=0; i<3; i++) return i; }
Bint foo() { for(int i=0; i<3; i++) { if(i==2) return; } return 0; }
Cint foo() { for(int i=0; i<3; i++) { if(i==2) return i; } return 0; }
Dint foo() { for(int i=0; i<3; i++) { if(i==2) return i; } }
Attempts:
2 left
๐Ÿ’ก Hint

Check if the return statements match the function's return type.

๐Ÿš€ Application
expert
2:00remaining
What is the output of this nested loop with return inside inner loop?

Consider the following C++ code. What will it print?

C++
#include <iostream>

int nestedLoop() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (i * j > 3) {
                return i * j;
            }
        }
    }
    return 0;
}

int main() {
    std::cout << nestedLoop();
    return 0;
}
A0
B6
C4
D9
Attempts:
2 left
๐Ÿ’ก Hint

The function returns as soon as the product i*j is greater than 3.