Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

React

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

React

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

javaScript and React Basics MCQs

JavaScript MCQs

1. Which of the following is not a valid JavaScript variable name?


○ A) _name
○ B) 2name
○ C) $name
○ D) name_2
2. Which of the following methods is used to access HTML elements using
JavaScript?
○ A) getElementById()
○ B) getElementsByClassName()
○ C) getElementsByTagName()
○ D) All of the above
3. What is the correct syntax for referring to an external script called "app.js"?
○ A) <script href="app.js">
○ B) <script name="app.js">
○ C) <script src="app.js">
○ D) <script file="app.js">
4. How do you create a function in JavaScript?
○ A) function myFunction()
○ B) function:myFunction()
○ C) function = myFunction()
○ D) function => myFunction()
5. How do you call a function named "myFunction"?
○ A) call myFunction()
○ B) call function myFunction()
○ C) myFunction()
○ D) Call.myFunction()

React MCQs

1. What is React?
○ A) A JavaScript library for building user interfaces
○ B) A JavaScript framework for building server-side applications
○ C) A JavaScript library for data manipulation
○ D) A JavaScript framework for building mobile applications
2. Which of the following is used to pass data to a component from outside in
React?
○ A) setState
○ B) render with arguments
○ C) props
○ D) PropTypes
3. What is the purpose of render() in React?
○ A) To initialize state
○ B) To update state
○ C) To return HTML to be rendered to the DOM
○ D) To define default props
4. Which method in React component lifecycle is called after the component is
rendered for the first time?
○ A) componentWillMount
○ B) componentDidMount
○ C) componentWillUpdate
○ D) componentDidUpdate
5. In React, what is the method used to update the state of a component?
○ A) this.setState
○ B) this.updateState
○ C) this.changeState
○ D) this.stateUpdate

JavaScript Exercises

1. Sum of Two Numbers: Write a function sum that takes two numbers as arguments and
returns their sum.

Ans.

javascript code:
function sum(num1, num2){

return num1+num2;
}

console.log(sum(4, 8));
console.log(sum(-2, 6));
console.log(sum(22, 12));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\sumofnum.js"
12
4
34
2. Factorial of a Number: Write a function factorial that takes a number as an
argument and returns its factorial.
Ans.

javascript code:
function factorial(n){

if (n===0){

return 1;

}
return n * factorial(n-1);
}

console.log(factorial(0));
console.log(factorial(5));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\factorialofanumber.js"
1
120

3. Palindrome Check: Write a function isPalindrome that checks if a given string is a


palindrome.
Ans.
Javascript code:

function isPalindrome(str) {
str = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
return str === str.split('').reverse().join('');
}

console.log(isPalindrome("racecar"));
console.log(isPalindrome("manikanta"));

output:

[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\palindromecheck.js"


true
false
4. Array Filtering: Write a function filterEvenNumbers that takes an array of numbers
and returns a new array containing only the even numbers.
Ans.
Javascript code:
function filterEvenNumber(numbers){
const evenNumbers = numbers.filter(n => n % 2 === 0);

console.log(filterEvenNumber([2, 3, 4, 6, 7, 8, 10]));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\filterevennumber.js"
[ 2, 4, 6, 8, 10 ]

5. Object Property Count: Write a function countProperties that takes an object and
returns the number of properties it has.
Ans.
Javascript code:
function countProperties(obj) {
return Object.keys(obj).length;
}

const person = {
name: 'manikanta',
age: 27,
city: 'guntur'
};

console.log(countProperties(person));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\objectpropertycount.js"
3

6. Reverse a String: Write a function reverseString that takes a string as an argument


and returns the string reversed.
Ans.
Javascript code:
function reverseString(str){
return str.split('').reverse().join('');
}

console.log(reverseString('manikanta'));
console.log(reverseString('i am web developer'));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\reversestring.js"
atnakinam
repoleved bew ma i

7. Fibonacci Sequence: Write a function fibonacci that returns the first n numbers of
the Fibonacci sequence.
Ans.
Javascript code:
function fibonacci(n) {
const sequence = [0, 1];

if (n <= 0) {
return [];
} else if (n === 1) {
return [0];
} else if (n === 2) {
return sequence;
} y

for (let i = 2; i < n; i++) {


const nextFib = sequence[i - 1] + sequence[i - 2];
sequence.push(nextFib);
}

return sequence;

console.log(fibonacci(10));

output:

[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\fibonaccisequence.js"


[
0, 1, 1, 2, 3,
5, 8, 13, 21, 34
]
8. Find Maximum Number: Write a function findMax that takes an array of numbers and
returns the maximum number.
Ans.
Javascript code:
function findmax(numbers){

return Math.max(...numbers);
}

console.log(findmax([2, 3, 5, 7, 12, 18]));


output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\findthemaxnumber.js"
18

9. Prime Number Check: Write a function isPrime that takes a number as an argument
and returns true if the number is prime, and false otherwise.
Ans.
Javascript code:
function isPrime(n) {
if (n <= 1) {
return false;
}
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
return false;
}
}
return true;
}
console.log(isPrime(3));
console.log(isPrime(8));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\js assignment 21-6\
primenumberchecker.js"
true
false

10. Array Sum: Write a function arraySum that takes an array of numbers and returns the
sum of all the numbers.
Ans.
Javascript code:
function arraySum(numbers) {
let sum = 0;
for (let num of numbers) {
sum += num;
}
return sum;
}
console.log(arraySum([2, 3, 5, 6, 8, 9]));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\arraysum.js"
33

11. Count Vowels: Write a function countVowels that takes a string as an argument and
returns the number of vowels in the string.
Ans.
Javascript code:
function countVowels(str) {
let count = 0;
for (let i = 0; i < str.length; i++) {
const char = str[i].toLowerCase();
if (char === 'a' || char === 'e' || char === 'i' || char === 'o' || char
=== 'u') {
count++;
}
}
return count;
}
console.log(countVowels("leaning javascript"))
output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\countvowels.js"
6

12. Merge Arrays: Write a function mergeArrays that takes two arrays and returns a new
array that combines both arrays.
Ans.
Javascript code:
function mergeArrays(arr1, arr2) {
return [...arr1, ...arr2];
}
console.log(mergeArrays([1, 2, 3], [4, 5, 6]));

output:

[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\mergarray.js"


[ 1, 2, 3, 4, 5, 6 ]

13. Square of Each Number: Write a function squareNumbers that takes an array of
numbers and returns a new array with the square of each number.
Ans.
Javascript :
function squareNumbers(numbers) {

return numbers.map(num => num * num);


}
console.log(squareNumbers([1, 2, 3, 4, 5]));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\squareofeachother.js"
[ 1, 4, 9, 16, 25 ]

14. Find Longest Word: Write a function findLongestWord that takes a string and
returns the longest word in the string.
Ans.
Javascript code:
function findLongestWord(str) {
const words = str.split(' ');
let longestWord = '';
for (let i = 0; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}
return longestWord;
}
console.log(findLongestWord("my name is manikanta. i am full stack web
developer."));

output:
[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\findthelongestword.js"
manikanta.

15. Sort Numbers: Write a function sortNumbers that takes an array of numbers and
returns a sorted array in ascending order.

Ans.

Javascript code:

function sortNumbers(numbers) {

return numbers.slice().sort((a, b) => a - b);


}
console.log(sortNumbers([3, 1, 4, 1, 5, 9, 2, 6, 5, 3]));

output:

[Running] node "c:\Users\LENOVO V330\Desktop\mocktest\sortnumber.js"


[
1, 1, 2, 3, 3,
4, 5, 5, 6, 9
]

You might also like