0
0
Blockchain / Solidityprogramming~20 mins

If-else statements in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-else Mastery in Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple if-else in Solidity
What is the output of this Solidity function when value = 10?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    function checkValue(uint value) public pure returns (string memory) {
        if (value > 10) {
            return "Greater";
        } else if (value == 10) {
            return "Equal";
        } else {
            return "Less";
        }
    }
}
A"Less"
B"Greater"
C"Equal"
DCompilation error
Attempts:
2 left
💡 Hint
Check the condition that matches exactly 10.
Predict Output
intermediate
2:00remaining
If-else behavior with boolean in smart contract
What will the function return when flag = false?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract FlagCheck {
    function checkFlag(bool flag) public pure returns (string memory) {
        if (flag) {
            return "True";
        } else {
            return "False";
        }
    }
}
A"False"
BCompilation error
CRuntime error
D"True"
Attempts:
2 left
💡 Hint
Think about what happens when the flag is false.
Predict Output
advanced
2:30remaining
Nested if-else output in Solidity
What does this function return when score = 75?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract ScoreCheck {
    function grade(uint score) public pure returns (string memory) {
        if (score >= 90) {
            return "A";
        } else {
            if (score >= 80) {
                return "B";
            } else {
                if (score >= 70) {
                    return "C";
                } else {
                    return "F";
                }
            }
        }
    }
}
A"A"
B"F"
C"B"
D"C"
Attempts:
2 left
💡 Hint
Check each condition from top to bottom.
🧠 Conceptual
advanced
2:30remaining
Error type from missing else in Solidity if-else
What error will this Solidity code produce when compiled?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    function check(uint x) public pure returns (string memory) {
        if (x > 5)
            return "High";
        else
            return "Low";
    }
}
ASyntaxError: 'else' without 'if'
BNo error, returns "Low" for x <= 5
CTypeError: incompatible types
DRuntime error: unreachable code
Attempts:
2 left
💡 Hint
Look at the placement of else after a return statement.
Predict Output
expert
3:00remaining
Output of complex if-else with multiple conditions
What is the output of this Solidity function when value = 15?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Complex {
    function test(uint value) public pure returns (string memory) {
        if (value < 10) {
            return "Less than 10";
        } else if (value < 20) {
            if (value % 2 == 0) {
                return "Even and less than 20";
            } else {
                return "Odd and less than 20";
            }
        } else {
            return "20 or more";
        }
    }
}
A"Odd and less than 20"
B"Even and less than 20"
C"20 or more"
D"Less than 10"
Attempts:
2 left
💡 Hint
Check if 15 is less than 20 and if it is even or odd.