0
0
Blockchain / Solidityprogramming~20 mins

Visibility modifiers (public, private, internal, external) in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Visibility 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 Solidity contract call?

Consider the following Solidity contract snippet. What will be the output when callPublic() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract VisibilityTest {
    string private secret = "hidden";
    string public info = "visible";

    function getSecret() private view returns (string memory) {
        return secret;
    }

    function callPublic() public view returns (string memory) {
        return info;
    }
}
A"" (empty string)
B"visible"
C"hidden"
DCompilation error due to private function call
Attempts:
2 left
💡 Hint

Remember that public variables create getter functions accessible externally.

Predict Output
intermediate
2:00remaining
What error occurs when calling a private function externally?

What happens if an external caller tries to call the private function getSecret() from the contract below?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract VisibilityTest {
    string private secret = "hidden";

    function getSecret() private view returns (string memory) {
        return secret;
    }
}
AReturns "hidden" successfully
BRuntime error: function call fails
CReturns empty string
DCompilation error: function is private and cannot be called externally
Attempts:
2 left
💡 Hint

Private functions are only callable inside the contract.

Predict Output
advanced
2:00remaining
What is the output of calling an internal function from a derived contract?

Given the following contracts, what will Derived.getInternal() return?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Base {
    string internal data = "base data";

    function getData() internal view returns (string memory) {
        return data;
    }
}

contract Derived is Base {
    function getInternal() public view returns (string memory) {
        return getData();
    }
}
A"base data"
BCompilation error: internal function cannot be called
CRuntime error: function call fails
D"" (empty string)
Attempts:
2 left
💡 Hint

Internal functions are accessible in derived contracts.

Predict Output
advanced
2:00remaining
What happens when an external function is called internally?

Consider this contract. What will happen if callExternal() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract VisibilityTest {
    function externalFunc() external pure returns (string memory) {
        return "external called";
    }

    function callExternal() public view returns (string memory) {
        return externalFunc();
    }
}
ARuntime error: external function call fails
B"external called"
CCompilation error: external function cannot be called internally without this.
D"" (empty string)
Attempts:
2 left
💡 Hint

External functions require a special call syntax when called internally.

🧠 Conceptual
expert
2:00remaining
Which visibility modifier allows function calls only from within the contract and derived contracts, but not externally?

Choose the visibility modifier that restricts function access to the contract itself and any contracts that inherit from it, but disallows external calls.

Ainternal
Bprivate
Cpublic
Dexternal
Attempts:
2 left
💡 Hint

Think about which modifier allows inheritance access but blocks external calls.