0
0
Blockchain / Solidityprogramming~20 mins

Factory pattern in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factory Pattern 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 factory contract?

Consider this Solidity factory contract that creates simple storage contracts. What will be the value of storedData in the newly created contract after deployment?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract SimpleStorage {
    uint public storedData;
    constructor(uint initVal) {
        storedData = initVal;
    }
}

contract StorageFactory {
    SimpleStorage[] public storages;

    function createStorage(uint _initVal) public {
        SimpleStorage s = new SimpleStorage(_initVal);
        storages.push(s);
    }

    function getLastStoredData() public view returns (uint) {
        return storages[storages.length - 1].storedData();
    }
}
AThe value passed to createStorage function
BThe default value of uint (which is 1)
C0
DIt will revert with an error
Attempts:
2 left
💡 Hint

Look at how the SimpleStorage contract is created and initialized.

🧠 Conceptual
intermediate
1:30remaining
Why use a factory pattern in blockchain smart contracts?

Which of the following is the main reason to use a factory pattern in blockchain smart contracts?

ATo reduce gas costs by storing all logic in one contract
BTo automatically upgrade all contracts without redeployment
CTo create multiple instances of a contract with different states easily
DTo prevent any contract from being created on the blockchain
Attempts:
2 left
💡 Hint

Think about how factories help manage many contracts.

🔧 Debug
advanced
2:00remaining
Identify the error in this factory contract code

What error will this Solidity factory contract produce when calling createStorage?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract SimpleStorage {
    uint public storedData;
    constructor(uint initVal) {
        storedData = initVal;
    }
}

contract StorageFactory {
    SimpleStorage[] public storages;

    function createStorage(uint _initVal) public {
        SimpleStorage s = SimpleStorage(_initVal);
        storages.push(s);
    }
}
ASyntaxError: Missing semicolon
BTypeError: Explicit constructor call is not allowed
CNo error, works fine
DRuntime error: Out of gas
Attempts:
2 left
💡 Hint

Check how the new contract instance is created.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a new contract instance in a factory?

Given the contract Child with a constructor that takes a uint, which option correctly creates a new Child instance inside a factory contract?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Child {
    uint public value;
    constructor(uint _value) {
        value = _value;
    }
}

contract Factory {
    Child[] public children;

    function createChild(uint _val) public {
        // Which line is correct here?
    }
}
AChild c = new Child(_val); children.push(c);
BChild c = Child(_val); children.push(c);
CChild c = Child.new(_val); children.push(c);
DChild c = new Child; children.push(c);
Attempts:
2 left
💡 Hint

Remember the syntax for creating new contract instances in Solidity.

🚀 Application
expert
2:30remaining
How many child contracts are created after these calls?

Given the following factory contract, how many Child contracts exist after these calls?

Calls made in order:

  1. createChild(10)
  2. createChild(20)
  3. createChild(30)
  4. deleteChild(1)
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Child {
    uint public value;
    constructor(uint _value) {
        value = _value;
    }
}

contract Factory {
    Child[] public children;

    function createChild(uint _val) public {
        Child c = new Child(_val);
        children.push(c);
    }

    function deleteChild(uint index) public {
        require(index < children.length, "Index out of range");
        delete children[index];
    }

    function getChildrenCount() public view returns (uint) {
        return children.length;
    }
}
A0
B2
C1
D3
Attempts:
2 left
💡 Hint

Deleting an element from an array in Solidity does not reduce its length.