VOOZH about

URL: https://www.geeksforgeeks.org/quizzes/context-api/

⇱ Quiz about Context API


Last Updated :
Discuss
Comments

Question 1

Which function is used to create a context in React?

  • React.useContext()

  • React.createContext()

  • React.createProvider()

  • React.useState()

Question 2

What will the following render?

const MyContext = React.createContext("Guest");

const Child = () => {

const user = useContext(MyContext);

return <h3>{user}</h3>;

};

const App = () => {

return <Child />;

};

  • undefined

  • Guest

  • null

  • Throws error

Question 3

Which components re-render when the context value changes?

<MyContext.Provider value={count}>

<Child1 />

<Child2 />

</MyContext.Provider>

  • Only Child1

  • Only Child2

  • Both Child1 and Child2 if they use useContext(MyContext)

  • None of them

Question 4

How do you create a context in React?

  • By using the `useState` hook

  • By defining a class component

  • By using the `createContext` function from React

  • By importing it from a third-party library like Redux

Question 5

Which component consumes the context value?

  • Context.Consumer

  • Context.Provider

  • useState()

  • useContext()

Question 6

Given the following code, what is the output of the useContext(UserContext) hook inside ChildComponent?



  • undefined

  • null

  • John

  • {} (empty object)

Question 7

Given the following code, what will be rendered in the ChildComponent?


  • English

  • Spanish

  • undefined

  • null

Question 8

What is the output when the following code is executed?


  • Hello, Alice!

  • Hello, undefined!

  • Hello, null!

  • Hello!

Question 9

What happens if a component does not consume context?


  • It will display "dark"

  • It will display "light"

  • It will display "Child Component"

  • It will throw an error

Question 10

What will happen if you try to use useContext() inside a class component?


  • It will work and return 'light'

  • It will work and return the current context value

  • Error: Hooks can’t be called inside class components

  • Error: useContext is undefined

There are 10 questions to complete.

Take a part in the ongoing discussion