Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which visibility modifier allows a function to be called both inside and outside the contract, including by other contracts and users?
easy
A. public
B. private
C. internal
D. external

Solution

  1. Step 1: Understand visibility modifiers

    Public functions can be accessed anywhere, inside or outside the contract.
  2. Step 2: Compare with other modifiers

    Private is only inside the contract, internal is inside and inherited contracts, external only callable from outside.
  3. Final Answer:

    public -> Option A
  4. Quick Check:

    public = open access [OK]
Hint: Public means open to all inside and outside calls [OK]
Common Mistakes:
  • Confusing private with public access
  • Thinking internal allows outside calls
  • Assuming external works inside contract
2. Which of the following is the correct way to declare a function that can only be called from outside the contract in Solidity?
easy
A. function foo() private {}
B. function foo() external {}
C. function foo() internal {}
D. function foo() public {}

Solution

  1. Step 1: Identify external function syntax

    External functions are declared with the keyword external after the function name.
  2. Step 2: Check other options

    Private and internal restrict access inside the contract or inheritance, public allows all calls.
  3. Final Answer:

    function foo() external {} -> Option B
  4. Quick Check:

    external = outside calls only [OK]
Hint: External functions use keyword 'external' after function name [OK]
Common Mistakes:
  • Using private instead of external
  • Confusing public with external
  • Missing parentheses or braces
3. Consider the following Solidity contract snippet:
contract Test {
    uint private data = 10;
    function getData() public view returns (uint) {
        return data;
    }
    function setData(uint _data) external {
        data = _data;
    }
}

What will happen if another contract tries to call data directly?
medium
A. It will succeed and return 10
B. It will fail because data is external
C. It will fail because data is private
D. It will succeed because data is public by default

Solution

  1. Step 1: Check variable visibility

    The variable data is declared private, so it cannot be accessed outside the contract.
  2. Step 2: Understand access from other contracts

    Other contracts cannot access private variables directly, only public or external functions.
  3. Final Answer:

    It will fail because data is private -> Option C
  4. Quick Check:

    private = no outside access [OK]
Hint: Private variables can't be accessed outside their contract [OK]
Common Mistakes:
  • Assuming private variables are public by default
  • Confusing external with private
  • Thinking public functions expose private variables directly
4. Given this Solidity code snippet:
contract Sample {
    uint internal count = 0;
    function increment() private {
        count += 1;
    }
    function callIncrement() public {
        increment();
    }
}

Which of the following fixes the error preventing increment() from being called?
medium
A. No fix needed, code is correct
B. Change increment() visibility to internal
C. Change increment() visibility to external
D. Change increment() visibility to public

Solution

  1. Step 1: Check function call context

    The function increment() is private and called inside the same contract by callIncrement().
  2. Step 2: Understand private function access

    Private functions can be called inside the same contract, so this call is valid.
  3. Final Answer:

    No fix needed, code is correct -> Option A
  4. Quick Check:

    private functions callable inside contract [OK]
Hint: Private functions work inside same contract only [OK]
Common Mistakes:
  • Thinking private functions can't be called inside contract
  • Confusing internal with private
  • Changing visibility unnecessarily
5. You want to create a Solidity contract where a function can only be called by this contract or contracts that inherit from it, but not by external users. Which visibility modifier should you use for this function?
hard
A. public
B. external
C. private
D. internal

Solution

  1. Step 1: Understand access needs

    The function should be callable inside this contract and by derived contracts, but not externally.
  2. Step 2: Match visibility modifier

    Internal visibility allows access inside the contract and by inheriting contracts only.
  3. Final Answer:

    internal -> Option D
  4. Quick Check:

    internal = contract + inheritance only [OK]
Hint: Internal means accessible inside and by child contracts only [OK]
Common Mistakes:
  • Choosing public which allows external calls
  • Choosing private which blocks inheritance access
  • Confusing external with internal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard