Background
React is a popular tool allows to build UI components using front-end JavaScript library. React is also known as React.js or ReactJS.
Why to read this article?
To check your React fundamental knowledge.
To understand React features along with their use-case and example.
To crack some certifications or interviews of React.
In this article, questions having multiple options. Correct answers are given along with detailed description and with code examples. Hence, one can have actual idea about the concept or feature.
Questions
What is output of 2 + '2' in JavaScript?
How to manage user input in React forms?
Which is correct way to import React component?
Which is NOT valid way to style components in React?
How to pass data from parent component to child component in React?
What is purpose of render() method in React component?
What is purpose of 'ref' attribute in React?
What are props in React?
Which method is used to initialize state in React component?
What is purpose of React.StrictMode component?
What is use of PropTypes in React?
How to update state of functional component in React?
Which is NOT valid way to define inline styles in React?
Which is correct way to render list of items in React?
Which is correct way to render React component?
Which command is used to create new React app?
What is use of React.createElement() function?
How to conditionally render elements in React?
Which is NOT valid way to define React component?
What is purpose of keys in React lists?
Which is NOT valid JSX syntax?
What is use of React.Fragment component?
Which is correct way to handle forms in React?
Which hook is used to perform side effects in functional component?
Which is recommended way to handle events in React?
Answers
1. What is output of 2 + '2' in JavaScript?
A. 4
B. '22'
C. '4'
D. NaN
Correct Answer:
B. '22'
Explanation:
JavaScript uses type coercion with + operator. Hence, it gives '22' output for 2 + '2'.
Here, If either operand is a string then JavaScript converts other operand to a string. Then it performs string concatenation instead of arithmetic addition.
Concatenation performed for string involvement. + performs both operations addition and string concatenation.
Evaluation
2 (number) → "2" (string)
"2" + "2" → "22"
Example
2 + 2 will gives output 2.
2 - '2' will gives output 0, because
-forces numeric conversion.'2' * 2 will gives output 4.
2. How to manage user input in React forms?
A. By using controlled components
B. By directly manipulating the DOM
C. By using useState()
D. By using useEffect()
Correct Answer:
A. By using controlled components
C. By using useState()
Explanation:
Controlled components is used to manage user input in React forms using below steps:
Step-1: Form input’s value is stored in React component state.
Step-2: Changes to input trigger an onChange handler.
Step-3: State is updated using useState().
JSX
import { useState } from "react";
function Form() {
const [name, setName] = useState("");
return (
<input type="text" value={name} onChange={(e) => setName(e.target.value)} />
);
}Here, input is controlled and useState() manages input value.
Why other options are incorrect?
By directly manipulating the DOM - React avoids direct DOM manipulations. It uses virtual DOM to reflect only changes using state updates.
By using useEffect() - It is used for side effects such as API calls, subscriptions, timers, etc. It is not used to manage form input values.
3. Which is correct way to import React component?
A. import MyComponent from './MyComponent';
B. require('./MyComponent');
C. import { MyComponent } from './MyComponent';
D. import MyComponent from './MyComponent.js';
Correct Answer:
A. import MyComponent from './MyComponent';
Explanation:
React and modern JavaScript are using ES Modules. Common and correct way to import component is using default import with assumption of component is exported as default export.
JSX - React component export
// MyComponent.jsx
export default function MyComponent() {
return <div>Hello</div>;
}JSX - Correct import
import MyComponent from './MyComponent';This is a standard and recommended pattern used in modern React applications.
Why other options are incorrect?
require('./MyComponent'); - It is used in CommonJS instead of ES Modules. It is valid in Node.js, but not standard in modern React apps. It is not used with JSX and modern tooling.
import { MyComponent } from './MyComponent'; - It is only valid if component is exported as named export.
import MyComponent from './MyComponent.js'; - It can work, but not standard because .js extension is usually omitted. Webpack and Vite Bundlers resolves it automatically.
4. Which is NOT valid way to style components in React?
A. Inline styles
B. External CSS files
C. Styled-components library
D. HTML style attribute
Correct Answer:
D. HTML style attribute
Explanation:
Inline styles, External CSS files, and Styled-components library are valid ways to style components in React.
HTML style attribute is not valid in React. String-based style attributes are not supported in. Styles must be provided as JavaScript object instead of string.
JSX
<div style="color: red;">Hello</div>Why other options are correct?
Inline styles are supported using a JavaScript object.
JSX
<div style={{ color: "red", fontSize: "16px" }}>Hello</div>External CSS files imports traditional CSS files and then apply class names.
JSX
import "./styles.css";
<div className="container">Hello</div>Styled-components library is popular CSS-in-JS library.
JSX
import styled from "styled-components";
const Button = styled.button`
background: blue;
color: white;
`;5. How to pass data from parent component to child component in React?
A. Using context
B. Using state
C. Using props
D. Using refs
Correct Answer:
C. Using props
Explanation:
Props is short form of properties. It is a standard and primary way to pass data from a parent component to child component.
JSX
function Parent() {
return <Child message="Hello from Parent" />;
}
function Child(props) {
return <p>{props.message}</p>;
}Here, Parent passes data i.e. message to child. Child receives it through props.
Why other options are incorrect?
Using context - It passes data through component tree instead of prop drilling. It is not the basic or primary method to pass data to children. It is mainly used for global or shared data such as themes, auth, locale, etc.
Using state - It is owned by component. State is not direct mechanism to pass data to children.
Using refs - It is used to access DOM elements or component instances. It can’t used to pass data between components.
6. What is purpose of render() method in React component?
A. It is responsible for updating the component's state
B. It is called after the component has been rendered to the DOM
C. It is used to perform side effects in a component
D. It returns the HTML markup to be displayed by the component
Correct Answer:
D. It returns the HTML markup to be displayed by the component
Explanation:
Render() method is used in class components to describe how UI should looks like. It returns JSX which React converts into DOM elements. React uses returned JSX to update DOM.
JSX
class MyComponent extends React.Component {
render() {
return <h1>Hello, React!</h1>;
}
}Here, render() returns a markup which appears on screen.
Why other options are incorrect?
It is responsible for updating the component's state - State update is done through this.setState() instead of render(). Inside render() updates to state can cause infinite loops.
It is called after the component has been rendered to the DOM - render() is called before React updates the DOM.
It is used to perform side effects in a component - Side effects used with API calls, subscriptions, timers, etc. It belongs to lifecycle methods componentDidMount and componentDidUpdate. For functional components it belongs to useEffect() hooks.
7. What is purpose of 'ref' attribute in React?
A. It references another component in the virtual DOM
B. It provides a reference to a DOM element created by a React component
C. It allows you to define default props for a component
D. It specifies the relationship between a parent and child component
Correct Answer:
B. It provides a reference to a DOM element created by a React component
Explanation:
The 'ref' attribute is used to get direct access to the DOM element. It is mainly used to focus an input or to measure DOM element. It can trigger animations and can integrate thirdparty DOM libraries.
JSX
import { useRef } from "react";
function MyComponent() {
const inputRef = useRef(null);
const focusInput = () => {
inputRef.current.focus();
};
return (
<>
<input ref={inputRef} />
<button onClick={focusInput}>Focus</button>
</>
);
}Here, ref provides a reference to actual <input> DOM element.
Why other options are incorrect?
It references another component in the virtual DOM - refs points to actual DOM elements or to instances of component.
It allows you to define default props for a component - Default props defines through defaultProps as well as using default function parameters.
It specifies the relationship between a parent and child component - Props or component nesting allows to define parent-child relationship.
8. What are props in React?
A. Properties that are passed from parent to child components
B. Methods used to manipulate state in React components
C. Functions used to handle events in React components
D. Built-in React components for common UI elements
Correct Answer:
A. Properties that are passed from parent to child components
Explanation:
Props is short form of properties. It is used to pass data from parent component to child component. It is read-only. Child component can’t modify props it receives.
JSX
function Parent() {
return <Child name="React" />;
}
function Child(props) {
return <h1>Hello, {props.name}!</h1>;
}Here, name is a prop. "React" is value and passed from Parent to Child component. Child accesses it using props.name.
Why other options are incorrect?
Methods used to manipulate state in React components - State is managed using useState() or setState() instead of props.
Functions used to handle events in React components - Event handlers such onClick, onChange, etc. can passed as props. Although props themselves are not event-handling functions.
Built-in React components for common UI elements - React provides several built-in elements such as <div>, <span>, etc. Props are data instead of components.
9. Which method is used to initialize state in React component?
A. this.state()
B. useState()
C. initState()
D. constructor()
Correct Answer:
B. useState()
Explanation:
useState() is standard approach to initialize state in React components. It is recommended and mostly used way to initialize state especially in functional components in modern React apps.
JSX
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return <p>{count}</p>;
}Here, useState(0) initializes state with value 0.
Why other options are incorrect?
this.state() - no such method exists.
initState() - no such method exists.
constructor() - It is only used in class components. It is now considered as legacy for most new React development.
JSX
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
}Here, this is valid but class components are now no longer preferred approach in modern React app.
10. What is purpose of React.StrictMode component?
A. It ensures that all state updates are synchronized
B. It disables certain React features for performance optimization
C. It enforces strict mode for all components within its sub-tree
D. It prevents components from rendering unnecessarily
Correct Answer:
C. It enforces strict mode for all components within its sub-tree
Explanation:
React.StrictMode is development-only helper component. It enables additional checks and warnings for all the components wrapped inside it. It is used to help developers to identify the potential problems early in development process. It doesn’t affect any production builds. It runs only in development.
StrictMode mode gives warnings about legacy APIs and identifies unexpected side effects. It executes couple of methods twice such as useEffect to cleanup surface bugs. It prepares code for future React features and also detects unsafe lifecycle methods.
JSX
import React from "react";
function App() {
return (
<React.StrictMode>
<MyComponent />
</React.StrictMode>
);
}Here, component is wrapped in StrictMode therefore React performs additional extra validations on MyComponent component and on it’s children.
Why other options are incorrect?
It ensures that all state updates are synchronized - State synchronization is managed through React’s update mechanism.
It disables certain React features for performance optimization - StrictMode does just opposite. It performs extra renders in development to expose issues.
It prevents components from rendering unnecessarily - It is handled through React.memo, useMemo, or shouldComponentUpdate techniques.
11. What is use of PropTypes in React?
A. To define types of props expected by a component
B. To validate structure of the component's state
C. To handle errors that occur during rendering
D. To provide default values for props
Correct Answer:
A. To define types of props expected by a component
Explanation:
PropTypes are used for type checking props passed to the component. It helps developers to catch bugs early by validating that components receive props of correct type and structure. It runs only in development mode. It don’t impact production performance.
JSX
import PropTypes from "prop-types";
function User({ name, age }) {
return <p>{name} is {age} years old</p>;
}
User.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number
};Here, name prop is required and must be type of string while age is optional and must be number. React shows warning in console if props are incorrect.
Why other options are incorrect?
To validate structure of the component's state - PropTypes validates props instead of component’s state. State validation can be handled either manually or via TypeScript.
To handle errors that occur during rendering - Rendering errors handles using Error Boundaries instead of PropTypes.
To provide default values for props - Default values can be set either using defaultProps or using default parameters in function components.

Join the conversation! Your thoughts help the community grow.