1.
Which statement cannot be used to declare a variable in JavaScript?
Correct Answer
C. Int
Explanation
The statement "int" cannot be used to declare a variable in JavaScript. In JavaScript, variables can be declared using "let", "var", or "const" keywords, but "int" is not a valid keyword for variable declaration in JavaScript.
2.
What are block scoped variables in JavaScript?
Correct Answer
A. The variable is only accessible inside the block(if-else/for block) inside which it is declared
Explanation
Block scoped variables in JavaScript are variables that are declared inside a block (such as an if-else or for block) and can only be accessed within that block. They are not accessible outside of the function in which they are declared. Therefore, the correct answer is "The variable is only accessible inside the block(if-else/for block) inside which it is declared."
3.
Which of the following is block scoped?
Correct Answer
D. Both b and c
Explanation
Both "let" and "const" are block scoped variables in JavaScript. This means that they are only accessible within the block of code where they are defined. They are not accessible outside of this block, such as in other functions or loops. This is different from the "var" keyword, which has function scope and can be accessed anywhere within the function it is defined in.
4.
What are Functions in JavaScript?
Correct Answer
B. Functions are basically a set of statements that perform some task
Explanation
Functions in JavaScript are a set of statements that perform a specific task. They allow us to encapsulate reusable code and execute it whenever needed. By defining a function, we can group related code together and call it multiple times without having to rewrite it. This makes our code more organized, modular, and easier to maintain. Therefore, the correct answer is that functions are basically a set of statements that perform some task.
5.
Which of the following is an array method?
Correct Answer
D. All of these
Explanation
All of the options listed (map, filter, reduce) are array methods. Array methods are functions that can be applied to arrays in programming languages to perform specific operations on the elements of the array. The map method is used to create a new array by applying a function to each element of the original array. The filter method is used to create a new array with elements that pass a certain condition. The reduce method is used to reduce the array to a single value by applying a function to each element. Therefore, all of these options are correct.
6.
Which of the following is considered a first class citizen in JavaScript?
Correct Answer
A. Functions
Explanation
Functions are considered first class citizens in JavaScript because they can be assigned to variables, passed as arguments to other functions, and returned as values from other functions. This means that functions have the same rights and abilities as any other data type in JavaScript, allowing for a high level of flexibility and functionality in the language.
7.
What is destructuring in JavaScript?
Correct Answer
A. Through destructuring, we can unpack values from arrays or object properties into separate variables.
Explanation
Destructuring in JavaScript refers to the process of unpacking values from arrays or object properties into separate variables. This allows for more concise and efficient code by enabling the restructuring of object properties or array elements. In this case, the correct answer states the accurate explanation of destructuring in JavaScript.
8.
What will be the output of the below code snippet?
let{name,age}={name:"abc",age:1,id:1}
console.log(name,age)
Correct Answer
A. Abc 1
Explanation
The code snippet uses object destructuring to create variables "name" and "age" and assigns them the corresponding values from the object {name: "abc", age: 1, id: 1}. The console.log statement then prints the values of "name" and "age", which are "abc" and 1 respectively.
9.
What do you mean by Function Composition?
Correct Answer
A. It is the process of combining two or more functions to create a new function.
Explanation
Function composition refers to the process of combining two or more functions in order to create a new function. This allows the output of one function to be used as the input for another function, creating a chain of functions that can be executed in sequence. In JavaScript, this process is achieved by declaring the function composition, which represents the combined functionality of the individual functions. Therefore, the correct answer is that function composition is the process of combining two or more functions to create a new function.
10.
Which of the following is the right way to add a single line comment in JavaScript?
Correct Answer
D. //This is a comment
Explanation
The correct answer is "//This is a comment" because in JavaScript, a single line comment is denoted by "//". This allows the programmer to add a comment within the code without affecting its functionality. The other options provided are not valid ways to add single line comments in JavaScript.
11.
Which is the correct way to bind an object to the current method?
Correct Answer
C. Both a and b
Explanation
Both options a and b are correct ways to bind an object to the current method. Option a, "func = func.bind(this)", is the correct way to bind an object to a regular function. Option b, "let func = () => {}", is the correct way to bind an object to an arrow function. Both methods ensure that the "this" keyword within the function refers to the object it is bound to, allowing access to its properties and methods.
12.
Which of the following is a valid function declaration syntax?
Correct Answer
C. Both a and b
Explanation
Both options "a" and "b" are valid function declaration syntax in JavaScript. Option "a" declares a function named "abc" using the traditional function declaration syntax. Option "b" declares a function using the arrow function syntax, where the function is assigned to a variable named "abc". Both options are valid ways to define a function in JavaScript.
13.
What will ‘0 == false ’ evaluate to?
Correct Answer
D. True
Explanation
The expression '0 == false' will evaluate to true because in JavaScript, the double equals operator (==) performs type coercion. In this case, the boolean value false is coerced to the number 0, and since 0 is equal to 0, the expression evaluates to true.
14.
Which array method is used to iterate on all the array elements and perform some task/transformation on them and return the new array?
Correct Answer
A. Map
Explanation
The map method is used to iterate on all the array elements and perform some task/transformation on them. It returns a new array with the transformed elements. This method is commonly used when we want to modify each element of an array and create a new array with the modified values.
15.
What will be the output of the below code snippet?
let numbers=[1,2,3,4,5,6,7,8];
console.log(numbers.filter(x=>x%2==0);
Correct Answer
D. 2,4,6,8
Explanation
The code snippet uses the filter method on the numbers array to filter out the elements that are not divisible by 2. The condition x%2==0 checks if the element is even. Therefore, the output will be the elements in the array that are divisible by 2, which are 2, 4, 6, and 8.
16.
What is the right syntax to accept an indefinite number of parameters?
Correct Answer
A. Function sum(...theArgs) {}
Explanation
The correct answer is "function sum(...theArgs) {}". This syntax uses the spread operator (...) to accept an indefinite number of parameters. The spread operator allows us to pass multiple arguments as an array to the function. This means that we can pass any number of arguments to the function, and they will be collected into an array called theArgs. This allows us to perform operations on all the arguments passed to the function.
17.
Which method is used to listen to events from DOM elements?
Correct Answer
D. Element.addEventListener(event => {})
Explanation
The correct answer is "element.addEventListener(event => {})". This method is used to listen to events from DOM elements. It takes two parameters: the event type and the event listener function. The event listener function is executed when the specified event is triggered on the element.
18.
What is the full form of DOM?
Correct Answer
C. Document Object Model
Explanation
The correct answer is Document Object Model. The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the structure of the document as a tree-like model, where each element in the document is represented as an object. This allows developers to manipulate the content, structure, and style of a web page dynamically using scripting languages like JavaScript. The DOM provides a set of methods and properties that allow for easy traversal and manipulation of the document's elements, making it an essential tool for web development.
19.
Which method is used to convert the JSON string to a JSON object?
Correct Answer
A. JSON.parse(string)
Explanation
The method used to convert a JSON string to a JSON object is JSON.parse(string). This method takes a JSON string as input and returns a JavaScript object. It parses the JSON string and converts it into a corresponding object representation, allowing for easy manipulation and access to the data within the JSON string. The other options provided (JSON.convertToObject(string) and JSON.object(string)) are not valid methods for converting a JSON string to a JSON object.
20.
Which of the following are the Object methods?
Correct Answer
D. All of these
Explanation
The given answer is "All of these" because all three statements are valid object methods in JavaScript. Object.keys(obj) returns an array of a given object's own enumerable property names, Object.values(obj) returns an array of a given object's own enumerable property values, and Object.entries(obj) returns an array of a given object's own enumerable property key-value pairs. Therefore, all three options are correct.