What is an interface in blockchain smart contracts?
An interface in blockchain smart contracts is a way to define a set of functions that a contract must implement, without providing the function bodies. It acts like a contract blueprint.
Click to reveal answer
beginner
Why use interfaces in smart contracts?
Interfaces help ensure that different contracts follow the same rules and can work together. They make code easier to understand and maintain by defining clear function signatures.
Click to reveal answer
beginner
Can an interface contain function implementations in blockchain smart contracts?
No, interfaces only declare function signatures without any implementation. The actual code must be written in the contract that implements the interface.
Click to reveal answer
intermediate
How does a contract implement an interface?
A contract implements an interface by including the interface name and providing code for all the functions declared in the interface.
Click to reveal answer
intermediate
Give a simple example of an interface in Solidity.
Example:
interface IExample {
function getValue() external view returns (uint);
function setValue(uint _value) external;
}
This defines two functions that any contract implementing IExample must have.
Click to reveal answer
What does an interface in blockchain smart contracts define?
AFunction signatures without implementation
BComplete function code
CStorage variables only
DEvents only
✗ Incorrect
Interfaces only declare function signatures without any code.
Can a contract implement multiple interfaces?
AOnly if interfaces have no functions
BNo, only one interface is allowed
CYes, a contract can implement many interfaces
DOnly if interfaces are from the same file
✗ Incorrect
Contracts can implement multiple interfaces to combine different function sets.
What happens if a contract does not implement all functions of an interface it claims to implement?
AInterface is ignored
BContract deploys but fails at runtime
CFunctions are auto-generated
DCompilation error
✗ Incorrect
The compiler requires all interface functions to be implemented, or it throws an error.
Which keyword is used to declare an interface in Solidity?
Ainterface
Bcontract
Clibrary
Dabstract
✗ Incorrect
The 'interface' keyword declares an interface in Solidity.
Interfaces in smart contracts help mainly with:
AStoring data
BStandardizing function signatures
CExecuting transactions
DMining blocks
✗ Incorrect
Interfaces standardize function signatures so contracts can interact predictably.
Explain what an interface is in blockchain smart contracts and why it is useful.
Think of an interface as a promise about what functions a contract will have.
You got /4 concepts.
Describe how a contract implements an interface and what happens if it misses a function.
Implementation means writing the actual function code for the interface's declared functions.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of an interface in blockchain smart contracts?
easy
A. To define a set of function signatures without implementation
B. To store data variables for the contract
C. To execute transactions automatically
D. To create user interfaces for blockchain apps
Solution
Step 1: Understand what an interface is
An interface only declares function names and their inputs/outputs but does not provide code inside them.
Step 2: Identify the purpose in blockchain contracts
Interfaces ensure different contracts agree on function names and types to interact properly.
Final Answer:
To define a set of function signatures without implementation -> Option A
Quick Check:
Interface = function signatures only [OK]
Hint: Interfaces declare functions without code to ensure consistency [OK]
Common Mistakes:
Thinking interfaces store data variables
Confusing interfaces with executable code
Believing interfaces create UI elements
2.
Which of the following is the correct syntax to declare an interface named IPayment in Solidity?
?
easy
A. interface IPayment { function pay() external; }
B. contract IPayment { function pay() public {} }
C. interface IPayment { function pay() public {} }
D. contract IPayment { function pay() external; }
Solution
Step 1: Recall interface syntax in Solidity
Interfaces use the keyword interface and declare functions without bodies, marked external.
Step 2: Check each option
interface IPayment { function pay() external; } correctly declares an interface with an external function without implementation. Versions using contract are wrong. interface IPayment { function pay() public {} } has a function body which is not allowed in interfaces.
Final Answer:
interface IPayment { function pay() external; } -> Option A
Quick Check:
Interface + external function + no body = correct syntax [OK]
Hint: Interfaces use 'interface' keyword and external functions without bodies [OK]
Common Mistakes:
Using 'contract' instead of 'interface'
Adding function bodies inside interfaces
Using 'public' instead of 'external' in interface functions
3.
Given the interface and contract below, what will be the output when calling getValue()?
interface IValue {
function getValue() external view returns (uint);
}
contract Value is IValue {
function getValue() external pure returns (uint) {
return 42;
}
}
medium
A. 42
B. 0
C. Runtime error
D. Compilation error
Solution
Step 1: Compare function signatures in interface and contract
The interface declares getValue() as view, but the contract implements it as pure. These must match exactly.
Step 2: Understand Solidity rules for interface implementation
Function modifiers like view and pure must match. Mismatch causes a compilation error.
Final Answer:
Compilation error -> Option D
Quick Check:
Modifier mismatch = compilation error [OK]
Hint: Function modifiers must match exactly in interface and implementation [OK]
Common Mistakes:
Ignoring function modifier differences
Assuming pure and view are interchangeable
Expecting runtime error instead of compile error
4.
Identify the error in the following Solidity code implementing an interface:
interface ICalculator {
function add(uint a, uint b) external returns (uint);
}
contract Calculator is ICalculator {
function add(uint a, uint b) public returns (uint) {
return a + b;
}
}
medium
A. Contract should not use 'is' keyword
B. Function visibility mismatch between interface and contract
C. Interface cannot have functions with parameters
D. Missing function body in contract
Solution
Step 1: Check function visibility in interface and contract
The interface function is declared as external, but the contract function is public. These must match.
Function visibility must be the same; otherwise, it causes a compilation error.
Final Answer:
Function visibility mismatch between interface and contract -> Option B
Quick Check:
Visibility mismatch = compilation error [OK]
Hint: Match function visibility exactly between interface and contract [OK]
Common Mistakes:
Using public instead of external in implementation
Thinking interfaces cannot have parameters
Ignoring 'is' keyword for inheritance
5.
You want to create a contract Token that implements two interfaces IERC20 and IERC721. Which of the following is the correct way to declare this contract?
hard
A. interface Token is IERC20, IERC721 { /* implementation */ }
Step 1: Recall Solidity syntax for multiple interface inheritance
Solidity uses the keyword is to inherit from multiple interfaces or contracts.
Step 2: Evaluate each option
contract Token is IERC20, IERC721 { /* implementation */ } correctly uses is with both interfaces. contract Token implements IERC20, IERC721 { /* implementation */ } uses implements which is invalid in Solidity. interface Token is IERC20, IERC721 { /* implementation */ } declares Token as an interface, not a contract. contract Token inherits IERC20, IERC721 { /* implementation */ } uses inherits which is not a Solidity keyword.
Final Answer:
contract Token is IERC20, IERC721 { /* implementation */ } -> Option C
Quick Check:
Use 'is' for multiple interface inheritance [OK]
Hint: Use 'is' keyword to inherit multiple interfaces in Solidity [OK]
Common Mistakes:
Using 'implements' instead of 'is'
Declaring contract as interface by mistake
Using non-existent keywords like 'inherits'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.