Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

SPDX license and pragma version in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SPDX and Pragma Mastery
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 SPDX license and pragma version check?
Consider this Solidity code snippet:
pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
contract Test {
    function version() public pure returns (string memory) {
        return "^0.8.0";
    }
}

What will the function version() return when called?
Blockchain / Solidity
pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
contract Test {
    function version() public pure returns (string memory) {
        return "^0.8.0";
    }
}
AMIT
B0.8.0
Cpragma solidity
D"^0.8.0"
Attempts:
2 left
💡 Hint
Look at what the function returns as a string literal.
❓ Predict Output
intermediate
2:00remaining
What error occurs with mismatched pragma version?
Given this Solidity code:
pragma solidity ^0.7.0;
// SPDX-License-Identifier: GPL-3.0
contract Sample {
    uint public x = 10;
}

If you try to compile this code with Solidity compiler version 0.8.10, what will happen?
Blockchain / Solidity
pragma solidity ^0.7.0;
// SPDX-License-Identifier: GPL-3.0
contract Sample {
    uint public x = 10;
}
ARuntime error: invalid SPDX license
BCompiles successfully
CCompilation error: pragma version mismatch
DWarning: SPDX license missing
Attempts:
2 left
💡 Hint
Check if the compiler version matches the pragma version range.
🔧 Debug
advanced
2:00remaining
Identify the error caused by missing SPDX license identifier
Examine this Solidity code:
pragma solidity ^0.8.0;
contract MissingSPDX {
    uint public value = 5;
}

What issue will the Solidity compiler report when compiling this code?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract MissingSPDX {
    uint public value = 5;
}
AWarning: SPDX license identifier not provided
BError: Missing pragma version
CError: Contract name missing
DNo issues, compiles cleanly
Attempts:
2 left
💡 Hint
SPDX license identifier is a comment at the top of the file.
❓ Predict Output
advanced
2:00remaining
What is the effect of multiple SPDX license identifiers in one file?
Consider this Solidity code:
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract MultiSPDX {
    uint public data = 42;
}

What will the Solidity compiler do when compiling this file?
Blockchain / Solidity
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract MultiSPDX {
    uint public data = 42;
}
AError: Multiple SPDX license identifiers found
BWarning: Multiple SPDX license identifiers, using the first one
CCompiles successfully using the last SPDX license
DIgnores SPDX license comments and compiles
Attempts:
2 left
💡 Hint
SPDX license identifier should appear only once per file.
❓ Predict Output
expert
2:00remaining
What is the output of this pragma version range check?
Given this Solidity code snippet:
pragma solidity >=0.6.0 <0.9.0;
// SPDX-License-Identifier: Apache-2.0
contract VersionCheck {
    function checkVersion() public pure returns (string memory) {
        return "Version supported";
    }
}

If compiled with Solidity version 0.9.1, what will happen?
Blockchain / Solidity
pragma solidity >=0.6.0 <0.9.0;
// SPDX-License-Identifier: Apache-2.0
contract VersionCheck {
    function checkVersion() public pure returns (string memory) {
        return "Version supported";
    }
}
ACompiles and returns "Version supported"
BCompilation error: pragma version not satisfied
CRuntime error: version mismatch
DWarning: SPDX license deprecated
Attempts:
2 left
💡 Hint
Check if 0.9.1 fits in the version range >=0.6.0 and <0.9.0.

Practice

(1/5)
1.

What is the purpose of the SPDX-License-Identifier line in a Solidity smart contract?

easy
A. To specify the license type of the contract's source code
B. To set the Solidity compiler version
C. To declare the contract's owner
D. To define the contract's storage variables

Solution

  1. Step 1: Understand SPDX-License-Identifier usage

    The SPDX-License-Identifier line is a comment that clearly states the license under which the source code is released.
  2. Step 2: Differentiate from pragma version

    Unlike pragma which sets compiler version, SPDX is about legal licensing information.
  3. Final Answer:

    To specify the license type of the contract's source code -> Option A
  4. Quick Check:

    SPDX-License-Identifier = License type [OK]
Hint: SPDX line always shows license info, not compiler version [OK]
Common Mistakes:
  • Confusing SPDX line with pragma version
  • Thinking SPDX sets compiler behavior
  • Ignoring SPDX line as just a comment
2.

Which of the following is the correct way to specify the Solidity compiler version to accept any version from 0.8.0 up to but not including 0.9.0?

pragma solidity ?;
easy
A. pragma solidity ^0.8.19;
B. pragma solidity 0.8.0;
C. pragma solidity >=0.8.0 <0.9.0;
D. pragma solidity >0.8.0 <=0.9.0;

Solution

  1. Step 1: Understand version range syntax

    The syntax >=0.8.0 <0.9.0 means any version from 0.8.0 up to but not including 0.9.0.
  2. Step 2: Compare with caret (^) operator

    The ^0.8.19 syntax allows >=0.8.19 <0.9.0, excluding earlier 0.8.x versions like 0.8.0. The explicit range >=0.8.0 <0.9.0 matches exactly.
  3. Final Answer:

    pragma solidity >=0.8.0 <0.9.0; -> Option C
  4. Quick Check:

    Version range syntax = >=0.8.0 <0.9.0 [OK]
Hint: Use >= and < to specify exact version range [OK]
Common Mistakes:
  • Using exact version without range
  • Misplacing comparison operators
  • Confusing caret (^) with range syntax
3.

What will happen if you compile this Solidity code snippet?

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract Test {
  uint public x = 5;
}

Assuming you use Solidity compiler version 0.8.10.

medium
A. Compilation fails due to pragma version mismatch
B. Compilation fails due to SPDX license missing
C. Compilation succeeds but with a warning
D. Compilation succeeds without errors

Solution

  1. Step 1: Check pragma version compatibility

    The pragma ^0.7.0 means compiler versions from 0.7.0 up to but not including 0.8.0 are allowed.
  2. Step 2: Compare with compiler version used

    Compiler version 0.8.10 is outside the allowed range, so compilation will fail due to version mismatch.
  3. Final Answer:

    Compilation fails due to pragma version mismatch -> Option A
  4. Quick Check:

    Pragma ^0.7.0 excludes 0.8.10 = mismatch [OK]
Hint: Compiler version must fit pragma range exactly [OK]
Common Mistakes:
  • Ignoring pragma version mismatch
  • Thinking SPDX license affects compilation
  • Assuming caret (^) allows all newer versions
4.

Identify the error in this Solidity contract header:

// SPDX-License-Identifier MIT
pragma solidity ^0.8.0;
contract Sample {}
medium
A. SPDX license identifier must be in quotes
B. Incorrect pragma version syntax
C. Contract name must be lowercase
D. Missing colon ':' after SPDX-License-Identifier

Solution

  1. Step 1: Check SPDX license line format

    The SPDX license line must be in the format // SPDX-License-Identifier: LicenseName with a colon after the identifier.
  2. Step 2: Verify other parts

    The pragma syntax and contract name are correct; SPDX license identifier does not require quotes.
  3. Final Answer:

    Missing colon ':' after SPDX-License-Identifier -> Option D
  4. Quick Check:

    SPDX line requires colon ':' [OK]
Hint: SPDX line always has colon after identifier [OK]
Common Mistakes:
  • Omitting colon after SPDX-License-Identifier
  • Adding quotes around license name
  • Thinking contract names must be lowercase
5.

You want to write a Solidity contract that is compatible with any compiler version from 0.6.0 up to but not including 0.9.0, and you want to specify the MIT license. Which of the following headers is correct?

hard
A. // SPDX-License-Identifier: MIT pragma solidity 0.6.0;
B. // SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0;
C. // SPDX-License-Identifier MIT pragma solidity ^0.6.0;
D. // SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <=0.9.0;

Solution

  1. Step 1: Check SPDX license line correctness

    The SPDX line must have a colon after the identifier and specify the license, so // SPDX-License-Identifier: MIT is correct.
  2. Step 2: Verify pragma version range

    The pragma >=0.6.0 <0.9.0 correctly specifies compatibility from 0.6.0 up to but not including 0.9.0. Using <=0.9.0 would include 0.9.0, which is not desired.
  3. Final Answer:

    // SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; -> Option B
  4. Quick Check:

    SPDX with colon + correct pragma range = // SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; [OK]
Hint: Use colon in SPDX and < for upper pragma bound [OK]
Common Mistakes:
  • Omitting colon in SPDX line
  • Using exact version instead of range
  • Including upper bound version with <= instead of <
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