0
0
Blockchain / Solidityprogramming~20 mins

Short-circuiting in conditions in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Short-circuiting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of short-circuit AND in Solidity
What is the output of this Solidity code snippet when executed?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    function check() public pure returns (string memory) {
        bool a = false;
        bool b = true;
        if (a && b) {
            return "Both true";
        } else {
            return "Short-circuited";
        }
    }
}
A"Short-circuited"
B"Both true"
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Remember that in AND conditions, if the first value is false, the second is not checked.
Predict Output
intermediate
2:00remaining
Output of short-circuit OR in Solidity
What will this Solidity function return when called?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    function check() public pure returns (string memory) {
        bool a = true;
        bool b = false;
        if (a || b) {
            return "Short-circuited OR";
        } else {
            return "Both false";
        }
    }
}
A"Short-circuited OR"
BCompilation error
C"Both false"
DRuntime error
Attempts:
2 left
💡 Hint
In OR conditions, if the first value is true, the second is not checked.
🔧 Debug
advanced
2:00remaining
Identify the error caused by short-circuiting in Solidity
What error will this Solidity code produce when the function is called?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint x = 0;
    function check() public returns (uint) {
        if (x != 0 && (10 / x) > 1) {
            return 1;
        } else {
            return 0;
        }
    }
}
AReturns 1 always
BNo error, returns 0
CCompilation error due to division
DDivision by zero runtime error
Attempts:
2 left
💡 Hint
Think about how short-circuiting prevents division by zero.
🧠 Conceptual
advanced
1:30remaining
Understanding short-circuit evaluation in smart contracts
Why is short-circuit evaluation important in smart contract conditions?
AIt disables all conditional checks
BIt always causes runtime errors to be ignored
CIt makes contracts run slower but safer
DIt reduces gas costs by avoiding unnecessary computations
Attempts:
2 left
💡 Hint
Think about how blockchain charges for computation.
Predict Output
expert
2:30remaining
Output of complex short-circuit condition in Solidity
What is the output of this Solidity function when called?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint public count = 0;
    function check() public returns (string memory) {
        if ((increment() > 0) || (increment() > 1)) {
            return "True";
        } else {
            return "False";
        }
    }
    function increment() internal returns (uint) {
        count += 1;
        return count;
    }
}
A"False" with count = 2
B"True" with count = 2
C"True" with count = 1
D"False" with count = 1
Attempts:
2 left
💡 Hint
Remember OR short-circuits after the first true condition.