Challenge - 5 Problems
Short-circuiting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"; } } }
Attempts:
2 left
💡 Hint
Remember that in AND conditions, if the first value is false, the second is not checked.
✗ Incorrect
Since 'a' is false, the AND condition short-circuits and the else branch runs, returning "Short-circuited".
❓ Predict Output
intermediate2: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"; } } }
Attempts:
2 left
💡 Hint
In OR conditions, if the first value is true, the second is not checked.
✗ Incorrect
Since 'a' is true, the OR condition short-circuits and the if branch runs, returning "Short-circuited OR".
🔧 Debug
advanced2: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; } } }
Attempts:
2 left
💡 Hint
Think about how short-circuiting prevents division by zero.
✗ Incorrect
Because 'x != 0' is false, the right side '(10 / x) > 1' is not evaluated, so no division by zero occurs and the function returns 0.
🧠 Conceptual
advanced1:30remaining
Understanding short-circuit evaluation in smart contracts
Why is short-circuit evaluation important in smart contract conditions?
Attempts:
2 left
💡 Hint
Think about how blockchain charges for computation.
✗ Incorrect
Short-circuit evaluation stops checking conditions early, saving computation steps and thus reducing gas costs.
❓ Predict Output
expert2: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; } }
Attempts:
2 left
💡 Hint
Remember OR short-circuits after the first true condition.
✗ Incorrect
The first call to increment() returns 1, which is > 0, so the OR condition short-circuits and the second increment() is not called. count is 1 and function returns "True".