react programming banner

React Multiple Choice Questions (MCQs) and Answers

Master React with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of React. Begin your placement preparation journey now!

Q61

Q61 What is a controlled component in React?

A

A component that controls all the forms in the app

B

A component whose state is controlled by React

C

A component that cannot be modified

D

None of the above

Q62

Q62 In React, how do you handle form submission?

A

Using a submit button outside the form

B

By manually updating the DOM

C

Using an onSubmit event handler in the form element

D

None of the above

Q63

Q63 How do you collect form input values in React?

A

Using refs to directly access the DOM element

B

By storing the values in the state on every change

C

Using document.getElementById()

D

None of the above

Q64

Q64 What is the main difference between controlled and uncontrolled components in React?

A

Controlled components do not use state, while uncontrolled components do

B

Controlled components use refs, while uncontrolled components do not

C

Controlled components manage their own state, while uncontrolled components store their state in the DOM

D

None of the above

Q65

Q65 How would you create a basic text input field in a React component?

A

<input type="text" value={this.state.value} />

B

<input type="text" />

C

<textfield value={this.state.value} />

D

<textinput value={this.state.value} />

Q66

Q66 What is the correct way to handle a form input change in React?

A

onChange={(event) => this.setState({value: event.target.value})}

B

onInput={(event) => this.setState({value: event.target.value})}

C

onChange={this.updateValue}

D

onInputChange={(event) => this.setState({value: event})}

Q67

Q67 Identify the mistake in this form input:

<input type="text" value={this.state.value} onchange={this.handleChange} />

A

The value attribute is incorrect

B

The type attribute is incorrect

C

The onchange event handler should be onChange

D

No mistake

Q68

Q68 What's wrong with this form submission handling?

<form onSubmit={this.handleSubmit}> <input type="submit" value="Submit" /> </form>

A

onSubmit should be onsubmit

B

The form should have an action attribute

C

The form is missing an onChange handler for the input

D

No issue

Q69

Q69 What does "lifting state up" mean in React?

A

Moving state to a child component for shared access

B

Moving state to a parent component for shared access

C

Deleting state

D

None of the above

Q70

Q70 Why is lifting state up beneficial in React applications?

A

It enhances the performance of the application

B

It allows child components to modify each other's state directly

C

It enables multiple components to share and synchronize state changes

D

It simplifies the debugging process

Q71

Q71 In the context of lifting state up, what is a common pattern for two-way data binding between parent and child components?

A

Using refs to update parent state from the child

B

Using a callback function passed from parent to child

C

Relying on global variables

D

None of the above

Q72

Q72 What is a potential downside of lifting state up in a large React application?

A

Increased rendering time for all components

B

Difficulty in managing state across distant components

C

Increased risk of security vulnerabilities

D

None of the above

Q73

Q73 How do you lift state up from a child component to a parent component?

A

By using global state management libraries like Redux

B

By passing a state setter function from parent to child

C

By using React context

D

None of the above

Q74

Q74 What is the correct way to update the parent's state from a child component?

A

Directly modify the parent's state

B

Use a callback function provided by the parent

C

Send a request to the parent via an event bus

D

None of the above

Q75

Q75 Identify the problem in this code:
class Parent extends React.Component { updateState() { /.../ } render() { return ; } }

A

updateState should be bound to this in the constructor

B

updateState is missing parameters

C

Child component should not receive onChange prop

D

No issue

Q76

Q76 What's the issue with this lifted state approach?
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = { value: '' };
} render() {
return <Child value={this.state.value} onChange={value => this.setState({value})} />; } }

A

onChange handler in Child is not implemented correctly

B

The value prop in Child is redundant

C

setState should not be used in the render method

D

No issue

Q77

Q77 In React, what is favored for reusing component logic?

A

Inheritance

B

Composition

C

Both equally

D

Neither

Q78

Q78 What is a key advantage of composition over inheritance in React?

A

Composition allows you to use multiple inheritances

B

Composition provides a clearer component hierarchy

C

Composition is easier to debug

D

Inheritance is more performant

Q79

Q79 How does React achieve code reuse with composition?

A

By inheriting methods from parent components

B

By rendering props children

C

By using higher-order components

D

By using React hooks

Q80

Q80 What is an example of composition in React?

A

Extending one component from another

B

Using state and lifecycle methods in class components

C

Passing components as props to other components

D

Using context API

Q81

Q81 Why might you choose composition over inheritance when designing React components?

A

Inheritance is not supported in React

B

Composition allows for easier code refactoring and maintenance

C

Composition makes components more complex

D

None of the above

Q82

Q82 Which React code snippet demonstrates composition?

A

<Parent><Child /></Parent>

B

class Parent extends React.Component { /*...*/ }

C

<Parent>{this.props.children}</Parent>

D

React.cloneElement(this.props.children)

Q83

Q83 How can you use composition to pass content into a component in React?

A

By using a prop to pass a function

B

By passing content as a child between component tags

C

By extending the component class

D

By using React context

Q84

Q84 What is a higher-order component (HOC) in React, and how does it relate to composition?

A

A component that inherits from another component

B

A function that takes a component and returns a new component

C

A class that renders multiple components

D

None of the above

Q85

Q85 Identify the error in this composition approach:
where does not render this.props.children.

A

The should not be nested inside

B

The component needs to render this.props.children

C

There is no error in this approach

D

The components should be connected using inheritance

Q86

Q86 What's wrong with using inheritance to extend a React component like this:
class EnhancedComponent extends BaseComponent { /.../ }?

A

React does not support class inheritance

B

Inheritance can lead to a more complex and less predictable component structure

C

BaseComponent methods can't be overridden

D

There is no issue

Q87

Q87 What is React Router used for in a React application?

A

Managing state within components

B

Routing between different components based on URL

C

Styling components

D

None of the above

Q88

Q88 Which component in React Router is used to configure a route?

A

<Router>

B

<Route>

C

<Switch>

D

<Link>

Q89

Q89 What does the component do in React Router?

A

It switches the application's theme

B

It provides conditional logic for rendering components

C

It toggles between two routes

D

None of the above

Q90

Q90 How do you navigate to a different URL using React Router?

A

Using the navigate() function

B

Using the component

C

Using the component

D

All of the above

ad verticalad vertical
ad