Pie Logo
Quiz: useState and Hooks
by Tomas Trescak

QuizTest your understanding of the section content.

ContentDetailed description of the section content.

Full Stack Development
Introduction
Generative AI
🍰 Player
💼 Editor
Setup
React Fundamentals
JSX and Components
Props
Assignment 1.2: Front End
CSS Styles
useState and Hooks
Exercise: useState
Quiz: useState and Hooks
Conditional Rendering
Lists
Forms and Events
Storybook
Assignment 1.3: Bonus - Backend
Leaderboards
Next.js
Advanced React
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    Quiz: useState and Hooks
    by Tomas Trescak

    QuizTest your understanding of the section content.

    ContentDetailed description of the section content.

    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Exercise: useState
    Quiz: useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    New Quiz
    by Tomas Trescak· useState and Hooks

    QUESTION 1

    Consider the following code for a React component:

    function Counter() {
      const [count, setCount] = useState(0);
      function increment() {
        setCount(count + 1);
      }
      return (
        <div>
          Count: {count}
          <button onClick={increment}>+</button>
        </div>
      );
    }
    

    What happens when the "+" button is clicked twice in quick succession?


    QUESTION 2

    Here’s a buggy React component:

    function NameList() {
      const [names, setNames] = useState(["Alice"]);
      function addName() {
        names.push("Bob");
        setNames(names);
      }
      return (
        <div>
          {names.join(", ")}
          <button onClick={addName}>Add Bob</button>
        </div>
      );
    }
    

    Why doesn’t "Bob" appear in the UI after clicking the button?


    QUESTION 3

    You’re tasked with fixing the NameList component from Question #2. Which of these solutions will make "Bob" appear after clicking the button?


    QUESTION 4

    Examine this component managing an object in state:

    function Profile() {
      const [user, setUser] = useState({ name: "Eve", age: 25 });
      function birthday() {
        user.age += 1;
        setUser(user);
      }
      return (
        <div>
          {user.name}, Age: {user.age}
          <button onClick={birthday}>+1 Age</button>
        </div>
      );
    }
    

    What’s wrong with this code, and how does it affect the UI?


    QUESTION 5

    How could you fix the Profile component from Question #4 to correctly update the age? (Select all that apply.)


    QUESTION 6

    Consider this custom hook and component:

    function useCounter() {
      const [count, setCount] = useState(0);
      return { count, increment: () => setCount(count + 1) };
    }
    function App() {
      const { count, increment } = useCounter();
      return (
        <div>
          Count: {count}
          <button onClick={increment}>+</button>
        </div>
      );
    }
    

    What’s true about this code?


    QUESTION 7

    Here’s a component with conditional hook usage:

    function Toggle() {
      const [isOn, setIsOn] = useState(false);
      if (isOn) {
        const [value, setValue] = useState(10);
        return <div>{value}</div>;
      }
      return <button onClick={() => setIsOn(true)}>Turn On</button>;
    }
    

    What happens when you click the "Turn On" button?


    QUESTION 8

    Look at this batching example:

    function BatchCounter() {
      const [a, setA] = useState(0);
      const [b, setB] = useState(0);
      function updateBoth() {
        setA(a + 1);
        setB(b + 1);
        console.log(a, b);
      }
      return (
        <div>
          A: {a}, B: {b}
          <button onClick={updateBoth}>Update</button>
        </div>
      );
    }
    

    What will the console log show after clicking the button once, assuming initial values are 0?


    QUESTION 9

    You’re building a todo list app. Here’s a snippet:

    function TodoList() {
      const [todos, setTodos] = useState(["Buy milk"]);
      function addTodo() {
        setTodos([...todos, "Call mom"]);
      }
      function removeLastTodo() {
        todos.pop();
        setTodos(todos);
      }
      return (
        <div>
          {todos.join(", ")}
          <button onClick={addTodo}>Add</button>
          <button onClick={removeLastTodo}>Remove Last</button>
        </div>
      );
    }
    

    What happens when you click "Remove Last" after adding "Call mom"?


    QUESTION 10

    How could you fix the removeLastTodo function from Question #9 to correctly remove the last todo? (Select all that apply.)

    This content is only available to registered users.
    Please register at the the home page.

    New Quiz
    by Tomas Trescak· useState and Hooks

    QUESTION 1

    Consider the following code for a React component:

    function Counter() {
      const [count, setCount] = useState(0);
      function increment() {
        setCount(count + 1);
      }
      return (
        <div>
          Count: {count}
          <button onClick={increment}>+</button>
        </div>
      );
    }
    

    What happens when the "+" button is clicked twice in quick succession?


    QUESTION 2

    Here’s a buggy React component:

    function NameList() {
      const [names, setNames] = useState(["Alice"]);
      function addName() {
        names.push("Bob");
        setNames(names);
      }
      return (
        <div>
          {names.join(", ")}
          <button onClick={addName}>Add Bob</button>
        </div>
      );
    }
    

    Why doesn’t "Bob" appear in the UI after clicking the button?


    QUESTION 3

    You’re tasked with fixing the NameList component from Question #2. Which of these solutions will make "Bob" appear after clicking the button?


    QUESTION 4

    Examine this component managing an object in state:

    function Profile() {
      const [user, setUser] = useState({ name: "Eve", age: 25 });
      function birthday() {
        user.age += 1;
        setUser(user);
      }
      return (
        <div>
          {user.name}, Age: {user.age}
          <button onClick={birthday}>+1 Age</button>
        </div>
      );
    }
    

    What’s wrong with this code, and how does it affect the UI?


    QUESTION 5

    How could you fix the Profile component from Question #4 to correctly update the age? (Select all that apply.)


    QUESTION 6

    Consider this custom hook and component:

    function useCounter() {
      const [count, setCount] = useState(0);
      return { count, increment: () => setCount(count + 1) };
    }
    function App() {
      const { count, increment } = useCounter();
      return (
        <div>
          Count: {count}
          <button onClick={increment}>+</button>
        </div>
      );
    }
    

    What’s true about this code?


    QUESTION 7

    Here’s a component with conditional hook usage:

    function Toggle() {
      const [isOn, setIsOn] = useState(false);
      if (isOn) {
        const [value, setValue] = useState(10);
        return <div>{value}</div>;
      }
      return <button onClick={() => setIsOn(true)}>Turn On</button>;
    }
    

    What happens when you click the "Turn On" button?


    QUESTION 8

    Look at this batching example:

    function BatchCounter() {
      const [a, setA] = useState(0);
      const [b, setB] = useState(0);
      function updateBoth() {
        setA(a + 1);
        setB(b + 1);
        console.log(a, b);
      }
      return (
        <div>
          A: {a}, B: {b}
          <button onClick={updateBoth}>Update</button>
        </div>
      );
    }
    

    What will the console log show after clicking the button once, assuming initial values are 0?


    QUESTION 9

    You’re building a todo list app. Here’s a snippet:

    function TodoList() {
      const [todos, setTodos] = useState(["Buy milk"]);
      function addTodo() {
        setTodos([...todos, "Call mom"]);
      }
      function removeLastTodo() {
        todos.pop();
        setTodos(todos);
      }
      return (
        <div>
          {todos.join(", ")}
          <button onClick={addTodo}>Add</button>
          <button onClick={removeLastTodo}>Remove Last</button>
        </div>
      );
    }
    

    What happens when you click "Remove Last" after adding "Call mom"?


    QUESTION 10

    How could you fix the removeLastTodo function from Question #9 to correctly remove the last todo? (Select all that apply.)

    This content is only available to registered users.
    Please register at the the home page.

    Pie Logo
    Quiz: useState and Hooks
    by Tomas Trescak
    Pie Logo
    Quiz: useState and Hooks
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Exercise: useState
    Quiz: useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Exercise: useState
    Quiz: useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics