React JS Multiple Choice Questions – Part 2

This is the part-2 of React JS Multiple Choice Questions (MCQs). Please find below URL for Part-1 MCQs on React JS:-

React JS MCQs – Part 1 

  Q1) Which of the following lifecycle methods does not get triggered on the component’s initial render?
a) componentWillMount()
b) componentWillReceiveProps()
c) render()
d) componentDidMount()
  Q2) Given the below class component: –
 class Hello extends React.Component {
render() {
return <div>{this.props.myProp}</div>;
}
}
Which code snippet below declares “newValue” as the default prop value for this.props.myProp?
a) this.setState({ myProp: “newValue” });
b) Hello.props.default = { myProp: ‘newValue’ };
c) Hello.myProp = “newValue”;
d) this.myProp = “newValue”;
e) Hello.defaultProps = { myProp: ‘newValue’ };
  Q3) How do you stop event propagation in React?
a) e.false()
b) return true
c) return false
d) e.stopPropagation()
e) e.preventPropagation()
  Q4) How do you define a React pure component?
a) extends React.PureComponent
b) All function components are Pure Components
c) class PureComponent {}
d) extends React.Component(Pure)
  Q5) Mixins are not available when defining components with JavaScript classes. What’s the recommended alternative?
a) Modules
b) JavaScript multiple inheritance
c) Higher Order Components
d) Decorators
  Q6) Which lifecycle methods are not triggered if shouldComponentUpdate() returns false?
a) componentWillMount(), render(), and componentDidMount()
b) componentWillReceiveProps() and render()
c) render()
d) render(), and componentDidUpdate()
  Q7) What is shallow rendering?
a) Rendering the component without needing to pass any props or setting any state.
b) Rendering just the component and not its children.
c) Rendering the component with a shallow context object.
d) Rendering the component without enforcing the prop types.
  Q8) Will any lifecycle methods be triggered if the “props” of a component change?
a) Yes, but only if props were changed from within the component
b) No, props cannot be changed
c) No, only state changes trigger update lifecycle methods
d) Yes, both props and state change triggers update lifecycle methods
  Q9) What is the way to avoid passing props through intermediate elements in the component tree?
a) React.fragment();
b) React.Component();
c) React.createContext();
d) React.createRef();
  Q10) Which of the following is NOT a good reason for having presentational and container logic separate in a component?
a) Shorter code overall
b) Separating responsibilities
c) Readability
d) Testability
  Q11) Why use async/await over simple promise chains?
a) They are faster.
b) Readability
c) They are required in modern JavaScript.
d) You cannot alter a components state without them.
  Q12) What is wrong with the key attribute in the below code: –

function ListItem({ value }) {
return <li key={value}>{value}</li>;
}
function NumberedList(props) {
return (
<ol>
{props.numbers.map(number => <ListItem value={number} />)}
</ol>
);
}

a) This code will cause a syntax error because value is a reserved property.
b) The key attribute cannot be used on HTML elements like <li>.
c) The key attribute should be used on <ListItem /> instead of <li>.
d) A number cannot be used as a key.

ANSWERS

1 B
2 E
3 D
4 A
5 C
6 D
7 B
8 D
9 C
10 A
11 B
12 C

Set Lookup field in another site collection using REST API

In this article, I will explain how you can set a lookup field in another list in different site collection using REST API.

You need to get other site collection form digest value and pass it to the X-RequestDigest in the headers. You can get this value by making a POST request to http://<server url>/_api/contextinfo and then use d.GetContextWebInformation.FormDigestValue as the form digest value while making the call.

Note that you need to specify method:”POST” instead of type:”POST” for the REST API call to create the item.

Set Lookup field using REST API in SharePoint

In this article, I will explain how you can set value in a Lookup field in a SharePoint List using REST API. I have tested the below code in SharePoint 2013/2016 and in SharePoint Online.

Scenario:-

List Name – MasterCountry
Lookup Column – Region
Parent List Name – MasterRegion

You need to pass the ID value (idToGet in below code) from the parent list. For that, pass the column name concatenated with Id – “RegionId”

No need to pass any select or expand parameter in REST API URL in POST operation.

React JS Multiple Choice Questions (MCQs)

In this article, we will see beginner level multiple choice questions (MCQs) on React JS:-

  Q1) What is a controlled input element?
a) An input element with the controlled flag
b) An input element whose value is being controlled by a component’s state
c) An input element that can only accept a list of characters
d) An input element that is controlled by the value of another input element
  Q2) What is the declarative way to render a dynamic list of components based on values in an array
a) Using the reduce array method
b) With a for/while loop
c) Using the <Each /> component
d) Using the Array.map() method
  Q3) When it is recommended to pass this.setState as a function instead of an object?
a) When the new state should completely replace the old state
b) When the new state depends on the old state
c) When the new state does not depend on the old state
  Q4) What will happen if you render an input element with disabled = {false}
a) It will be rendered as enabled
b) It will be rendered as disabled
c) It will not be rendered at all
  Q5) What is a good use case for using a function while rendering a dynamic list of items
a) If we need to compute a value based on properties of items in the loop.
b) None. Functions should not be used while rendering a dynamic list.
c) To make the code shorter
  Q6) What function can be used to change the state of a React component?y
a) this.setState
b) this.changeState
c) this.State = {}
  Q7) What is the second argument for setState useful for?
a) To invoke code after the setState operation is done
b) To replace the state completely instead of the default merge action
c) To access the previous state before the setState operation
  Q8) Which of the following below act as the input of a class-based component?
a) class and factory
b) render and mount
c) props

ANSWERS

1 B
2 D
3 B
4 A
5 A
6 A
7 A
8 C