






















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























































































Question 1 What is the primary role of JavaScript in web development? A) Styling web pages B) Structuring web content C) Adding interactivity to web pages D) Managing server-side databases Correct: C) Adding interactivity to web pages Explanation: JavaScript is primarily used for client-side scripting to make web pages interactive. It allows developers to manipulate the DOM, handle events, and create dynamic content. Question 2 Which of the following is NOT a primitive data type in JavaScript? A) Number B) String C) Object D) Boolean Correct: C) Object Explanation: In JavaScript, primitive data types include Number, String, Boolean, Undefined, and Null. Object is a reference data type, not a primitive. Question 3 How do you declare a constant in JavaScript? A) var B) let C) const D) define Correct: C) const
Explanation: The const keyword is used to declare constants in JavaScript. Variables declared with const cannot be reassigned. Question 4 What is the output of the following code? let x = 5; let y = '5'; console.log(x == y); A) true B) false C) undefined D) error Correct: A) true Explanation: The == operator performs type coercion, converting y to a number before comparison. Thus, 5 == '5' evaluates to true. Question 5 Which loop is used to iterate over the properties of an object? A) for loop B) while loop C) for...in loop D) for...of loop Correct: C) for...in loop Explanation: The for...in loop is specifically used to iterate over the enumerable properties of an object. Question 6 What does the this keyword refer to in the context of an object method? A) The global object
B) Function expression C) Function constructor D) Function prototype Correct: D) Function prototype Explanation: Functions can be declared using function declarations, function expressions, or the Function constructor. Function prototype is not a way to declare a function. Question 10 What is the output of the following code? console.log(0.1 + 0.2 === 0.3); A) true B) false C) undefined D) error Correct: B) false Explanation: Due to floating-point precision issues in JavaScript, 0.1 + 0.2 does not exactly equal 0.. Question 11 Which method is used to add an event listener to an element? A) addEventListener B) attachEvent C) onEvent D) bindEvent Correct: A) addEventListener Explanation: The addEventListener method is used to attach an event handler to an element. Question 12 What is the purpose of the JSON.parse() method?
A) To convert a JavaScript object to a JSON string B) To convert a JSON string to a JavaScript object C) To validate JSON data D) To format JSON data Correct: B) To convert a JSON string to a JavaScript object Explanation: The JSON.parse() method parses a JSON string and converts it into a JavaScript object. Question 13 Which of the following is a correct way to create a new array in JavaScript? A) let arr = array(1, 2, 3); B) let arr = new Array(1, 2, 3); C) let arr = [1, 2, 3]; D) Both B and C Correct: D) Both B and C Explanation: Arrays can be created using the Array constructor ( new Array(1, 2, 3) ) or array literal syntax ( [1, 2, 3] ). Question 14 What is the output of the following code? let x = 10; function test() { console.log(x); let x = 20; } test(); A) 10 B) 20 C) undefined
D) Both B and C Correct: D) Both B and C Explanation: Variables declared with let and const have block scope, meaning they are only accessible within the block (e.g., {} ) they are declared in. Question 18 What is the output of the following code? let arr = [1, 2, 3]; arr.push(4); console.log(arr.length); A) 3 B) 4 C) 5 D) undefined Correct: B) 4 Explanation: The push method adds an element to the end of the array, increasing its length by 1. Thus, the length of arr becomes 4. Question 19 Which of the following is NOT a valid JavaScript operator? A) + B) - C) ****** D) ^ Correct: C) **** Explanation:** The ****** operator is used for exponentiation in JavaScript. All the options are valid JavaScript operators. Question 20
What is the purpose of the fetch API in JavaScript? A) To manipulate the DOM B) To make AJAX requests C) To handle form submissions D) To create web workers Correct: B) To make AJAX requests Explanation: The fetch API is used to make network requests (e.g., AJAX requests) to retrieve resources from a server. Question 21 Which of the following is a correct way to check if a variable is an array in JavaScript? A) Array.isArray(var) B) var instanceof Array C) typeof var === 'array' D) Both A and B Correct: D) Both A and B Explanation: Both Array.isArray(var) and var instanceof Array can be used to check if a variable is an array. Question 22 What is the output of the following code? let x = 5; function test() { x = 10; } test(); console.log(x); A) 5
B) String C) Float D) Boolean Correct: C) Float Explanation: JavaScript does not have a separate Float data type. Numbers in JavaScript are of type Number , which can represent both integers and floating-point values. Question 26 What is the output of the following code? let x = '5'; let y = 10; console.log(x + y); A) 15 B) '510' C) 510 D) Error Correct: B) '510' Explanation: The + operator concatenates the string '5' with the number 10 , resulting in the string '510'. Question 27 Which of the following is used to remove the last element from an array? A) pop() B) push() C) shift() D) unshift() Correct: A) pop() Explanation: The pop() method removes the last element from an array and returns it.
Question 28 What is the purpose of the async keyword in JavaScript? A) To define a synchronous function B) To define an asynchronous function C) To handle errors D) To create a new promise Correct: B) To define an asynchronous function Explanation: The async keyword is used to define an asynchronous function, which returns a promise. Question 29 Which of the following is NOT a valid way to declare a variable in JavaScript? A) var x = 10; B) let y = 20; C) const z = 30; D) variable a = 40; Correct: D) variable a = 40; Explanation: The correct keywords for declaring variables in JavaScript are var , let , and const. The option D is invalid syntax. Question 30 What is the output of the following code? let x = 5; let y = x++; console.log(y); A) 5 B) 6 C) undefined D) Error
Correct: D) repeat loop Explanation: JavaScript does not have a repeat loop. The valid loops are for , while , and do-while. Question 34 What is the output of the following code? let x = 5; let y = '5'; console.log(x === y); A) true B) false C) undefined D) Error Correct: B) false Explanation: The === operator checks for both value and type equality. Since x is a number and y is a string, the result is false. Question 35 Which of the following is used to remove the first element from an array? A) pop() B) push() C) shift() D) unshift() Correct: C) shift() Explanation: The shift() method removes the first element from an array and returns it. Question 36 What is the purpose of the Promise object in JavaScript? A) To handle synchronous operations
B) To handle asynchronous operations C) To manipulate the DOM D) To create web workers Correct: B) To handle asynchronous operations Explanation: The Promise object is used to handle asynchronous operations and their resulting values. Question 37 Which of the following is NOT a valid way to declare an object in JavaScript? A) let obj = {}; B) let obj = new Object(); C) let obj = Object.create(null); D) let obj = {}; Correct: C) let obj = Object.create(null); Explanation: The Object.create(null) method creates an object with null as its prototype, which is valid but not a common way to declare an object. Question 38 What is the output of the following code? let x = 5; function test() { let x = 10; console.log(x); } test(); A) 5 B) 10 C) undefined D) Error
Correct: D) function = myFunction() {}; Explanation: The correct syntax for function declaration is function myFunction() {}. The option D is invalid syntax. Question 42 What is the output of the following code? let arr = [1, 2, 3]; arr.unshift(0); console.log(arr[0]); A) 1 B) 0 C) 2 D) 3 Correct: B) 0 Explanation: The unshift() method adds the element 0 to the beginning of the array, making arr[0] equal to 0. Question 43 Which of the following is used to convert a string to a number in JavaScript? A) parseInt() B) parseFloat() C) Number() D) All of the above Correct: D) All of the above Explanation: All of the options ( parseInt() , parseFloat() , and Number() ) can be used to convert a string to a number in JavaScript. Question 44 What is the purpose of the bind() method in JavaScript?
A) To create a new function with a specific this value B) To execute a function immediately C) To remove an event listener D) To create a new array Correct: A) To create a new function with a specific this value Explanation: The bind() method creates a new function with a specific this value and initial arguments. Question 45 Which of the following is NOT a valid way to create an array in JavaScript? A) let arr = [1, 2, 3]; B) let arr = new Array(1, 2, 3); C) let arr = Array.of(1, 2, 3); D) let arr = Array(1, 2, 3); Correct: D) let arr = Array(1, 2, 3); Explanation: The correct syntax for creating an array is let arr = [1, 2, 3]; , let arr = new Array(1, 2, 3); , or let arr = Array.of(1, 2, 3);. The option D is not a valid way to create an array. Question 46 What is the output of the following code? let x = 5; let y = x * '2'; console.log(y); A) 10 B) '52' C) NaN D) Error Correct: A) 10
Explanation: The correct keywords for declaring variables in JavaScript are var , let , and const. The option D is invalid syntax. Question 50 What is the output of the following code? let x = 5; let y = x++; console.log(x); A) 5 B) 6 C) undefined D) Error Correct: B) 6 Explanation: The post-increment operator ( x++ ) increments the value of x after the current expression is evaluated. Thus, x is incremented to 6. Question 51 Which of the following is used to find the index of an element in an array? A) indexOf() B) findIndex() C) Both A and B D) Neither A nor B Correct: C) Both A and B Explanation: Both the indexOf() and findIndex() methods can be used to find the index of an element in an array. Question 52 What is the purpose of the filter() method in JavaScript? A) To create a new array with elements that pass a test
B) To map elements in an array C) To reduce an array to a single value D) To sort an array Correct: A) To create a new array with elements that pass a test Explanation: The filter() method creates a new array with all elements that pass the test implemented by the provided function. Question 53 Which of the following is NOT a valid way to declare a function in JavaScript? A) function myFunction() {} B) let myFunction = function() {}; C) let myFunction = () => {}; D) function = myFunction() {}; Correct: D) function = myFunction() {}; Explanation: The correct syntax for function declaration is function myFunction() {}. The option D is invalid syntax. Question 54 What is the output of the following code? let arr = [1, 2, 3]; arr.push(4); console.log(arr.length); A) 3 B) 4 C) 5 D) undefined Correct: B) 4 Explanation: The push method adds an element to the end of the array, increasing its length by 1. Thus, the length of arr becomes 4.