MCQs Of Day 1:Introduction to JavaScript

Rashmi Mishra
0

  

MCQs Of Day 1

Introduction to JavaScript


1.  What is JavaScript primarily used for in web development?

o    A) Server-side scripting

o    B) Styling webpages

o    C) Client-side scripting and adding interactivity

o    D) Database management

o    Answer: C

2.  Which of the following is the correct syntax to link an external JavaScript file?

o    A) <script href="file.js"></script>

o    B) <script link="file.js"></script>

o    C) <script src="file.js"></script>

o    D) <script file="file.js"></script>

o    Answer: C

3.  Which company developed JavaScript?

o    A) Microsoft

o    B) Google

o    C) Oracle

o    D) Netscape

o    Answer: D

4.  In what year was JavaScript created?

o    A) 1991

o    B) 1995

o    C) 1999

o    D) 2002

o    Answer: B

5.  JavaScript is a ____ typed language.

o    A) Statically

o    B) Dynamically

o    C) Strongly

o    D) Weakly

o    Answer: B


6.  Which keyword is used to declare a variable in JavaScript?

o    A) int

o    B) var

o    C) define

o    D) set

o    Answer: B

7.  Which of the following correctly declares a constant variable?

o    A) const myConstant = 10;

o    B) constant myConstant = 10;

o    C) let myConstant = 10;

o    D) var myConstant = 10;

o    Answer: A

8.  What will typeof "hello" return?

o    A) number

o    B) string

o    C) boolean

o    D) object

o    Answer: B

9.  Which data type is NOT available in JavaScript?

o    A) String

o    B) Number

o    C) Tuple

o    D) Object

o    Answer: C

10.  What will the following code output? console.log(typeof null);

o    A) object

o    B) null

o    C) undefined

o    D) number

o    Answer: A


11.                   What is the output of 5 + "5" in JavaScript?

o    A) 10

o    B) 55

o    C) NaN

o    D) 5

o    Answer: B

12.                   Which operator is used to assign a value to a variable?

o    A) =

o    B) ==

o    C) ===

o    D) !=

o    Answer: A

13.                   What will 10 % 3 output in JavaScript?

o    A) 1

o    B) 3

o    C) 0

o    D) 10

o    Answer: A

14.                   What does the ++ operator do to a variable?

o    A) Multiplies by 2

o    B) Divides by 2

o    C) Increases by 1

o    D) Decreases by 1

o    Answer: C

15.                   Which of the following is the strict equality operator?

o    A) =

o    B) ==

o    C) ===

o    D) !=

o    Answer: C


16.                   Which of the following is NOT a valid loop in JavaScript?

o    A) for

o    B) while

o    C) do-while

o    D) repeat-until

o    Answer: D

17.                   What will for (let i = 0; i < 3; i++) console.log(i); output?

o    A) 0 1 2 3

o    B) 0 1 2

o    C) 1 2 3

o    D) 1 2

o    Answer: B

18.                   How can you break out of a loop in JavaScript?

o    A) exit

o    B) return

o    C) stop

o    D) break

o    Answer: D

19.                 What will the following code print?

let x = 10;

if (x > 5) {

    console.log("Greater");

} else {

    console.log("Smaller");

}

o    A) Greater

o    B) Smaller

o    C) Undefined

o    D) null

o    Answer: A

20.                   Which statement is used to skip an iteration in a loop?

o    A) skip

o    B) continue

o    C) pass

o    D) stop

o    Answer: B


21.    Which of the following correctly defines a function in JavaScript?

o    A) def myFunction() {}

o    B) function myFunction() {}

o    C) myFunction = function {}

o    D) define myFunction() {}

o    Answer: B

22.                   Which keyword is used to return a value from a function?

o    A) return

o    B) output

o    C) yield

o    D) print

o    Answer: A

23.                   What will the following code output?

function add(a, b) {

    return a + b;

}

console.log(add(3, 4));

o    A) 7

o    B) 34

o    C) undefined

o    D) null

o    Answer: A

24.                   What will console.log(add); output if function add() {} is defined?

o    A) undefined

o    B) [object Function]

o    C) function add() {}

o    D) add()

o    Answer: C

25.                   Can functions in JavaScript be anonymous?

o    A) Yes

o    B) No

o    Answer: A


26.                   What does DOM stand for in JavaScript?

o    A) Document Oriented Model

o    B) Data Object Model

o    C) Document Object Model

o    D) Data Oriented Model

o    Answer: C

27.                   Which method is used to select an element by its ID in JavaScript?

o    A) getElementByName()

o    B) getElementById()

o    C) getElementsByTagName()

o    D) querySelector()

o    Answer: B

28.                   How can you change the text content of an HTML element with JavaScript?

o    A) element.innerHTML

o    B) element.textContent

o    C) element.value

o    D) element.content

o    Answer: B

29.                   What will document.querySelector("p"); return?

o    A) All <p> elements

o    B) The first <p> element

o    C) Undefined

o    D) An array of all <p> elements

o    Answer: B

30.                   Which method adds a new child element to a selected element?

o    A) append()

o    B) addChild()

o    C) appendChild()

o    D) addElement()

o    Answer: C

31.                   What does removeChild() do in JavaScript?

o    A) Removes a child element

o    B) Removes all child elements

o    C) Removes an attribute

o    D) Removes the parent element

o    Answer: A

32.                   How would you change the background color of an element with id="box"?

o    A) document.getElementById("box").style.backgroundColor = "blue";

o    B) document.getElementById("box").backgroundColor = "blue";

o    C) document.querySelector("#box").style.color = "blue";

o    D) document.querySelector("#box").style.background = blue;

o    Answer: A

33.                   Which method is used to find all elements of a class name?

o    A) getElementsByClass()

o    B) getElementsByClassName()

o    C) querySelector()

o    D) getElementsById()

o    Answer: B

34.                   What does document.createElement("div") do?

o    A) Creates a new HTML document

o    B) Creates a new <div> element

o    C) Selects a <div> element

o    D) Replaces an existing <div> element

o    Answer: B

35.                   Which of the following correctly appends an element to the document body?

o    A) document.appendChild(element);

o    B) document.body.appendChild(element);

o    C) body.appendChild(element);

o    D) appendChild(body, element);

o    Answer: B


36.                   Which event occurs when a user clicks on an HTML element?

o    A) onmouseover

o    B) onclick

o    C) onload

o    D) onchange

o    Answer: B

37.                   How do you add an event listener to a button with id="btn"?

o    A) document.getElement("btn").click = function(){}

o    B) document.querySelector("#btn").click(function(){})

o    C) document.getElementById("btn").addEventListener("click", function(){});

o    D) addEventListener("click", function(){}, "btn");

o    Answer: C

38.                   Which event is triggered when a user presses a key?

o    A) keydown

o    B) keypress

o    C) keyup

o    D) All of the above

o    Answer: D

39.                   Which event is used when an HTML form is submitted?

o    A) onsubmit

o    B) onchange

o    C) oninput

o    D) onsave

o    Answer: A

40.                   How can you prevent the default action in an event?

o    A) event.prevent()

o    B) event.preventAction()

o    C) event.preventDefault()

o    D) event.stop()

o    Answer: C


Error Handling

41.                   Which of the following keywords is used to handle exceptions in JavaScript?

o    A) catch

o    B) try

o    C) throw

o    D) All of the above

o    Answer: D

42.                   Which statement is used to throw an error manually?

o    A) throw

o    B) try

o    C) catch

o    D) manual

o    Answer: A

43.                   What will happen if an error occurs in the try block?

o    A) The code execution stops

o    B) The catch block will handle the error

o    C) The code skips to the end

o    D) None of the above

o    Answer: B

44.                   Which keyword is used to execute code regardless of whether an error occurs?

o    A) always

o    B) finally

o    C) after

o    D) lastly

o    Answer: B

45.                  What is the output of the following code?

try {

  throw "Error!";

} catch (e) {

  console.log(e);

}

o    A) undefined

o    B) Error!

o    C) null

o    D) Exception

o    Answer: B


46.                   Which keyword was introduced in ES6 to declare block-scoped variables?

o    A) var

o    B) const

o    C) let

o    D) Both let and const

o    Answer: D

47.                   What does the => symbol signify in JavaScript?

o    A) Equality

o    B) Function keyword

o    C) Arrow function

o    D) Scope resolution

o    Answer: C

48.                   What is the default parameter in functions used for?

o    A) To pass mandatory parameters

o    B) To initialize arguments if no value is passed

o    C) To handle exceptions

o    D) To specify variable types

o    Answer: B

49.                   Which of the following is NOT a feature of arrow functions?

o    A) They do not have their own this

o    B) They are shorter syntax

o    C) They are block-scoped

o    D) They are compatible with arguments object

o    Answer: D

50.                   How would you write a template literal in JavaScript?

o    A) "Hello ${name}"

o    B) "Hello" + name + ""

o    C) `Hello ${name}`

o    D) 'Hello {name}'

o    Answer: C


51.                   Which method adds one or more elements to the end of an array and returns the new length?

o    A) push()

o    B) pop()

o    C) shift()

o    D) unshift()

o    Answer: A

52.                   What does the pop() method do in an array?

o    A) Removes the first element

o    B) Adds a new element at the beginning

o    C) Removes the last element

o    D) Sorts the array

o    Answer: C

53.                   How can you find the number of elements in an array arr?

o    A) arr.size

o    B) arr.length

o    C) arr.count()

o    D) arr.length()

o    Answer: B

54.                   Which method creates a new array with all elements that pass the test implemented by the provided function?

o    A) filter()

o    B) map()

o    C) reduce()

o    D) forEach()

o    Answer: A

55.What will the following code output?

let fruits = ["Apple", "Banana", "Cherry"];

console.log(fruits[1]);

o    A) Apple

o    B) Banana

o    C) Cherry

o    D) undefined

o    Answer: B

56.                   Which method combines two or more arrays?

o    A) concat()

o    B) merge()

o    C) join()

o    D) combine()

o    Answer: A

57.                   How do you remove the first element from an array?

o    A) arr.pop()

o    B) arr.shift()

o    C) arr.unshift()

o    D) arr.splice(0, 1)

o    Answer: B

58.                   What does the slice() method do?

o    A) Removes elements from the array

o    B) Returns a shallow copy of a portion of an array

o    C) Sorts the array

o    D) Reverses the array

o    Answer: B

59.                   Which method executes a provided function once for each array element?

o    A) map()

o    B) filter()

o    C) forEach()

o    D) reduce()

o    Answer: C

60.                   How can you check if a variable arr is an array?

o    A) typeof arr === "array"

o    B) arr instanceof Array

o    C) Array.isArray(arr)

o    D) Both B and C

o    Answer: D


61.                   How do you create an object in JavaScript?

o    A) let obj = {};

o    B) let obj = [];

o    C) let obj = () => {};

o    D) let obj = new Object[];

o    Answer: A

62.                   How can you access the value of the property name in an object person?

o    A) person.name

o    B) person[name]

o    C) person->name

o    D) Both A and B

o    Answer: D

63.                   Which method is used to add a new property to an existing object?

o    A) addProperty()

o    B) setProperty()

o    C) Direct assignment (e.g., obj.newProp = value)

o    D) Object.assign()

o    Answer: C

64.                   What will delete obj.property do?

o    A) Remove the property from the object

o    B) Set the property value to null

o    C) Set the property value to undefined

o    D) Throw an error

o    Answer: A

65.                   How can you iterate over all properties in an object obj?

o    A) for (let key in obj) { ... }

o    B) for (let key of obj) { ... }

o    C) obj.forEach((key) => { ... })

o    D) while (obj.hasNext()) { ... }

o    Answer: A

66.                   Which of the following is a correct way to define a method inside an object?

o    A)

let obj = {

    greet: function() {

        console.log("Hello");

    }

};

o    B)

let obj = {

    greet() {

        console.log("Hello");

    }

};

o    C) Both A and B

o    D) Neither A nor B

o    Answer: C

67.                 What will the following code output?

let car = { make: "Toyota", model: "Camry" };

console.log(car["make"]);

o    A) car.make

o    B) "make"

o    C) "Toyota"

o    D) undefined

o    Answer: C

68.                   Which function can be used to convert an object to a JSON string?

o    A) JSON.parse()

o    B) JSON.stringify()

o    C) JSON.convert()

o    D) JSON.toString()

o    Answer: B

69.                   How do you define a constructor function for an object Person?

o    A)

function Person(name) {

    this.name = name;

}

o    B)

let Person = {

    name: ""

};

o    C)

class Person {

    constructor(name) {

        this.name = name;

    }

}

o    D) Both A and C

o    Answer: D

70.What is the output of the following code?

let obj = { a: 1, b: 2 };

for (let key in obj) {

    console.log(key);

}

o    A) 1 2

o    B) a b

o    C) undefined

o    D) ["a", "b"]

o    Answer: B


71.                   Which method returns the length of a string?

o    A) length()

o    B) size()

o    C) count()

o    D) length

o    Answer: D

72.                   How do you convert a string to uppercase in JavaScript?

o    A) str.toUpper()

o    B) str.toUpperCase()

o    C) str.upper()

o    D) str.changeCase("upper")

o    Answer: B

73.                   Which method is used to find the index of a substring within a string?

o    A) search()

o    B) indexOf()

o    C) find()

o    D) locate()

o    Answer: B

74.                   What will "Hello World".substring(0, 5) return?

o    A) "Hello"

o    B) "World"

o    C) "Hello World"

o    D) "ello "

o    Answer: A

75.                   Which method replaces a specified value with another value in a string?

o    A) replace()

o    B) swap()

o    C) change()

o    D) modify()

o    Answer: A

76.                   How do you extract the last character from a string str?

o    A) str.slice(-1)

o    B) str.substring(str.length - 1)

o    C) str.charAt(str.length - 1)

o    D) All of the above

o    Answer: D

77.What is the result of "JavaScript".includes("Script")?

o    A) true

o    B) false

o    C) undefined

o    D) Throws an error

o    Answer: A

78.                   Which method splits a string into an array of substrings?

o    A) split()

o    B) divide()

o    C) segment()

o    D) partition()

o    Answer: A

79.                   How can you trim whitespace from both ends of a string str?

o    A) str.trim()

o    B) str.removeSpaces()

o    C) str.trimSpaces()

o    D) str.strip()

o    Answer: A

80.                   What does the charAt() method do?

o    A) Returns the character at a specified index

o    B) Returns the ASCII value of a character

o    C) Replaces a character at a specified index

o    D) Converts a character to uppercase

o    Answer: A


81.                   How do you create a new Date object representing the current date and time?

o    A) new Date()

o    B) Date.now()

o    C) new DateTime()

o    D) Date.create()

o    Answer: A

82.                   Which method returns the number of milliseconds since January 1, 1970?

o    A) Date.now()

o    B) Date.getTime()

o    C) Date.current()

o    D) Date.milliseconds()

o    Answer: A

83.                   How can you get the current year from a Date object d?

o    A) d.getFullYear()

o    B) d.getYear()

o    C) d.year

o    D) d.currentYear()

o    Answer: A

84.                   What will new Date("2024-11-06").getDay() return?

o    A) 2

o    B) 3

o    C) 4

o    D) 5

o    Answer: B

85.                   Which method sets the month of a Date object?

o    A) setMonth()

o    B) updateMonth()

o    C) changeMonth()

o    D) modifyMonth()

o    Answer: A

86.                   How do you convert a Date object to a string?

o    A) d.toString()

o    B) d.stringify()

o    C) d.convertToString()

o    D) d.asString()

o    Answer: A

87.                   What is the output of new Date(0)?

o    A) The current date and time

o    B) January 1, 1970, 00:00:00 UTC

o    C) Invalid Date

o    D) null

o    Answer: B

88.                   Which method retrieves the hours from a Date object d?

o    A) d.getHours()

o    B) d.getTime()

o    C) d.getUTCHours()

o    D) d.getHour()

o    Answer: A

89.                   How can you add 5 days to the current date?

o    A)

let d = new Date();

d.setDate(d.getDate() + 5);

o    B)

let d = new Date();

d.addDays(5);

o    C)

let d = new Date();

d.date += 5;

o    D)

let d = new Date();

d.setDay(d.getDay() + 5);

o    Answer: A

90.                   What does the toISOString() method do?

o    A) Converts the date to a string using the local time zone

o    B) Converts the date to a string in ISO 8601 format

o    C) Returns the date as a UNIX timestamp

o    D) Formats the date as "MM/DD/YYYY"

o    Answer: B


91.                   What does JSON stand for?

o    A) JavaScript Object Notation

o    B) Java Source Object Notation

o    C) JavaScript Ordered Namespace

o    D) Java Simple Object Notation

o    Answer: A

92.                   Which method parses a JSON string into a JavaScript object?

o    A) JSON.parse()

o    B) JSON.stringify()

o    C) JSON.convert()

o    D) JSON.decode()

o    Answer: A

93.                   Which method converts a JavaScript object into a JSON string?

o    A) JSON.parse()

o    B) JSON.stringify()

o    C) JSON.encode()

o    D) JSON.toString()

o    Answer: B

94.                   What is the correct JSON format for an object with a name and age?

o    A) {name: "John", age: 30}

o    B) {"name": "John", "age": 30}

o    C) {"name" => "John", "age" => 30}

o    D) {name => "John", age => 30}

o    Answer: B

95.                   Can JSON contain functions as values?

o    A) Yes

o    B) No

o    Answer: B

96.                   Which of the following is a valid JSON array?

o    A) ["apple", "banana", "cherry"]

o    B) {1, 2, 3}

o    C) ("apple", "banana", "cherry")

o    D) "apple", "banana", "cherry"

o    Answer: A

97.                   **How do you access the age property from the following JSON string?

'{"name": "Alice", "age": 25}'

**

o    A) obj.age

o    B) obj["age"]

o    C) Both A and B

o    D) Neither A nor B

o    Answer: C

98.                   What will JSON.stringify({a:1, b:2}) return?

o    A) "{a:1, b:2}"

o    B) '{"a":1, "b":2}'

o    C) ["a",1,"b",2]

o    D) "{\"a\":1, \"b\":2}"

o    Answer: D

99.                   Which of the following is true about JSON?

o    A) JSON is a subset of JavaScript

o    B) JSON is language-independent

o    C) JSON requires property names to be double-quoted

o    D) All of the above

o    Answer: D

100.            How would you handle errors when parsing a JSON string? - A) Using try...catch blocks - B) Using if statements - C) Using for loops - D) It cannot be handled - Answer: A


101.            Which keyword is used to declare a block-scoped variable that cannot be reassigned?

o    A) var

o    B) let

o    C) const

o    D) static

o    Answer: C

102.            **What is the output of the following code? javascript const greet = () => "Hello"; console.log(greet()); ** - A) greet - B) "Hello" - C) undefined - D) Hello - Answer: B

103.            Which feature allows for destructuring objects and arrays?

o    A) Spread Operator

o    B) Rest Operator

o    C) Destructuring Assignment

o    D) Template Literals

o    Answer: C

104.            What does the spread operator (...) do in JavaScript?

o    A) Combines arrays or objects

o    B) Creates a copy of an array or object

o    C) Both A and B

o    D) Neither A nor B

o    Answer: C

105.            Which of the following is a valid arrow function syntax?

o    A) function() => {}

o    B) () => {}

o    C) => () {}

o    D) {} => ()

o    Answer: B

106.            What is a default parameter in ES6?

o    A) A parameter that is optional

o    B) A parameter with a default value if none is provided

o    C) A parameter that must be provided

o    D) A deprecated feature

o    Answer: B

107.            How do you import a default export from a module in ES6?

o    A) import { default } from 'module';

o    B) import default from 'module';

o    C) import anyName from 'module';

o    D) require('module').default;

o    Answer: C

108.            Which method can be used to combine multiple promises into a single promise?

o    A) Promise.all()

o    B) Promise.race()

o    C) Promise.combine()

o    D) Promise.merge()

o    Answer: A

109.            What is a template literal in ES6?

o    A) A string defined with single quotes

o    B) A string defined with double quotes

o    C) A string defined with backticks allowing embedded expressions

o    D) A new data type

o    Answer: C

110.            Which feature allows you to write asynchronous code that looks synchronous?

o    A) Callbacks

o    B) Promises

o    C) Async/Await

o    D) Event Loop

o    Answer: C


111.            What is the purpose of the typeof operator?

o    A) To check if a variable is defined

o    B) To determine the type of a variable

o    C) To convert a variable to a string

o    D) To clone a variable

o    Answer: B

112.            Which symbol is used for single-line comments in JavaScript?

o    A) /* */

o    B) //

o    C) <!-- -->

o    D) #

o    Answer: B

113.            What will console.log(0.1 + 0.2 === 0.3) output?

o    A) true

o    B) false

o    C) undefined

o    D) Throws an error

o    Answer: B

114.            Which statement is used to exit a function and return a value?

o    A) break

o    B) continue

o    C) return

o    D) exit

o    Answer: C

115.            What is the result of typeof NaN?

o    A) number

o    B) NaN

o    C) undefined

o    D) object

o    Answer: A

116.            Which method can be used to clone an object?

o    A) Object.clone()

o    B) Object.assign({}, obj)

o    C) JSON.parse(JSON.stringify(obj))

o    D) Both B and C

o    Answer: D

117.            What does the void operator do in JavaScript?

o    A) Returns undefined

o    B) Deletes a property

o    C) Converts a value to boolean

o    D) No such operator exists

o    Answer: A

118.            Which of the following is a self-invoking function?

o    A)

function() { console.log("Hi"); }

- B)

    ```javascript

    (function() { console.log("Hi"); })();

    ```

- C)

    ```javascript

    () => { console.log("Hi"); }

    ```

- D)

    ```javascript

    function greet() { console.log("Hi"); }

    greet();

    ```

- **Answer:** B 

**Explanation:** A self-invoking function is immediately invoked after its definition, as shown in option B.

119. What will console.log(typeof undefined === typeof null) output? - A) true - B) false - C) undefined - D) null - Answer: B
Explanation: typeof undefined is "undefined", while typeof null is "object", so the comparison is false.

120.            Which statement is used to declare a block scope in ES6?

o    A) var

o    B) let

o    C) const

o    D) Both B and C

o    Answer: D


Advanced Topics

121.            What is the purpose of the bind() method in JavaScript?

o    A) To create a new function with a bound this value

o    B) To attach an event listener

o    C) To merge two objects

o    D) To iterate over array elements

o    Answer: A

122.            Which statement is true about JavaScript closures?

o    A) They allow functions to access variables from an outer function scope

o    B) They are a way to bind this keyword

o    C) They are similar to classes in other languages

o    D) They are deprecated in ES6

o    Answer: A

123.            What does the new keyword do in JavaScript?

o    A) Creates a new object

o    B) Calls a function

o    C) Returns a copy of an object

o    D) Deletes an object

o    Answer: A

124.            Which of the following is NOT a primitive data type in JavaScript?

o    A) String

o    B) Number

o    C) Boolean

o    D) Object

o    Answer: D

125.            What is the purpose of the finally block in a try...catch statement?

o    A) To handle errors

o    B) To execute code after try and catch regardless of the outcome

o    C) To retry the try block

o    D) To define custom error messages

o    Answer: B

126.            How do you prevent a form from submitting and refreshing the page?

o    A) event.preventDefault();

o    B) return false;

o    C) Both A and B

o    D) Neither A nor B

o    Answer: C

127.            Which feature allows JavaScript to perform tasks asynchronously?

o    A) Callbacks

o    B) Promises

o    C) Async/Await

o    D) All of the above

o    Answer: D

128.            What is the result of '5' - 3 in JavaScript?

o    A) '53'

o    B) 2

o    C) NaN

o    D) '2'

o    Answer: B

129.            Which statement correctly checks if variable x is exactly equal to 10?

o    A) x == 10

o    B) x === 10

o    C) x = 10

o    D) x != 10

o    Answer: B

130.            What does the async keyword do when placed before a function?

o    A) Makes the function execute faster

o    B) Allows the use of await inside the function

o    C) Converts the function to a promise

o    D) Both B and C

o    Answer: D


Prototypal Inheritance

131.            What is prototypal inheritance in JavaScript?

o    A) A way to create classes

o    B) A mechanism where objects inherit properties from other objects

o    C) A type of loop

o    D) A method for error handling

o    Answer: B

132.            How do you set the prototype of an object child to parent?

o    A) child.prototype = parent;

o    B) Object.setPrototypeOf(child, parent);

o    C) child.__proto__ = parent;

o    D) Both B and C

o    Answer: D

133.            **What is the output of the following code? javascript function Person(name) { this.name = name; } Person.prototype.greet = function() { return "Hello, " + this.name; }; let alice = new Person("Alice"); console.log(alice.greet()); ** - A) "Hello, Person" - B) "Hello, Alice" - C) undefined - D) Throws an error - Answer: B

134.            Which method returns the prototype of an object?

o    A) Object.getPrototype()

o    B) Object.getPrototypeOf()

o    C) Object.prototype()

o    D) getPrototypeOf()

o    Answer: B

135.            Can you modify the prototype of built-in objects like Array in JavaScript?

o    A) Yes, but it's not recommended

o    B) No, it's impossible

o    C) Yes, and it's recommended

o    D) Only in strict mode

o    Answer: A

136.            What is the constructor property used for?

o    A) To create new instances

o    B) To reference the function that created the instance's prototype

o    C) To initialize object properties

o    D) To delete object properties

o    Answer: B

137.            Which keyword is used to inherit from another class in ES6?

o    A) inherit

o    B) extends

o    C) implements

o    D) super

o    Answer: B

138.            What does the super keyword do in a subclass constructor?

o    A) Calls the parent class constructor

o    B) Binds this to the parent class

o    C) Creates a new instance of the parent class

o    D) All of the above

o    Answer: A

139.            What is the default prototype of an object created with {}?

o    A) Object.prototype

o    B) null

o    C) Function.prototype

o    D) Array.prototype

o    Answer: A

140.            How can you check if an object obj has a property prop as its own property (not inherited)?

o    A) obj.hasOwnProperty(prop)

o    B) prop in obj

o    C) Object.hasOwn(obj, prop)

o    D) Both A and C

o    Answer: D


Asynchronous JavaScript

141.            What does the fetch() function do?

o    A) Fetches elements from the DOM

o    B) Makes HTTP requests to retrieve resources

o    C) Fetches data from local storage

o    D) None of the above

o    Answer: B

142.            How do you handle errors in a Promise chain?

o    A) Using .then()

o    B) Using .catch()

o    C) Using try...catch

o    D) Using finally

o    Answer: B

143.            **What will the following code output? javascript async function getData() { return "Data"; } getData().then(data => console.log(data)); ** - A) undefined - B) Promise { "Data" } - C) "Data" - D) Throws an error - Answer: C

144.            Which keyword is used to pause the execution of an async function until a Promise is resolved?

o    A) await

o    B) pause

o    C) sleep

o    D) break

o    Answer: A

145.            What does the Promise.all() method do?

o    A) Resolves as soon as one of the promises resolves

o    B) Rejects as soon as one of the promises rejects

o    C) Waits for all promises to resolve or any to reject

o    D) None of the above

o    Answer: C

146.            Which method starts executing a Promise and returns a promise that resolves when the first promise resolves or rejects when the first promise rejects?

o    A) Promise.all()

o    B) Promise.race()

o    C) Promise.any()

o    D) Promise.resolve()

o    Answer: B

147.            **What will the following code output? javascript console.log(typeof undefined); console.log(typeof null); ** - A) undefined, object - B) undefined, undefined - C) null, object - D) object, object - Answer: A

148.            Which of the following is true about JavaScript's event loop?

o    A) It allows JavaScript to perform non-blocking operations

o    B) It manages the execution of multiple threads

o    C) It is responsible for garbage collection

o    D) It only handles UI rendering

o    Answer: A

149.            What is the purpose of the setTimeout() function?

o    A) To execute a function after a specified delay

o    B) To repeatedly execute a function at specified intervals

o    C) To pause execution indefinitely

o    D) To stop a function from executing

o    Answer: A

150.            Which function can be used to repeatedly execute a function at fixed time intervals?

o    A) setTimeout()

o    B) setInterval()

o    C) requestAnimationFrame()

o    D) setImmediate()

o    Answer: B


Modules and Imports

151.            How do you export a function named myFunction in ES6 modules?

o    A) export myFunction;

o    B) export { myFunction };

o    C) module.exports = myFunction;

o    D) export default myFunction;

o    Answer: B

152.            How do you import all exported members from a module named utils.js?

o    A) import * as utils from './utils.js';

o    B) import utils from './utils.js';

o    C) import { utils } from './utils.js';

o    D) import all from './utils.js';

o    Answer: A

153.            What is the default file extension for ES6 modules?

o    A) .js

o    B) .mjs

o    C) .es6

o    D) Both A and B

o    Answer: D

154.            Which statement allows you to export multiple functions from a module?

o    A) export default function1, function2;

o    B) export { function1, function2 };

o    C) module.exports = { function1, function2 };

o    D) Both B and C

o    Answer: B

155.            How do you import a default export from a module?

o    A) import { default as func } from './module.js';

o    B) import func from './module.js';

o    C) import default func from './module.js';

o    D) import { func } from './module.js';

o    Answer: B

156.            Which of the following is true about ES6 modules?

o    A) They are loaded synchronously by default

o    B) They are loaded asynchronously by default

o    C) They cannot export multiple values

o    D) They are not supported in browsers

o    Answer: B

157.            What is the purpose of the export default statement?

o    A) To export multiple values

o    B) To export a single default value from a module

o    C) To export only functions

o    D) To export only classes

o    Answer: B

158.            Can ES6 modules import other modules?

o    A) Yes

o    B) No

o    Answer: A

159.            Which of the following is a correct way to export a constant PI?

o    A) export const PI = 3.14;

o    B) const PI = 3.14; export PI;

o    C) export { PI };

o    D) All of the above (with appropriate definitions)

o    Answer: D

160.            What will happen if you try to import a non-existent export from a module?

o    A) It will return undefined

o    B) It will throw a compile-time error

o    C) It will throw a runtime error

o    D) It will silently fail

o    Answer: C


Advanced Functions and Concepts

161.            What is the difference between call() and apply() methods?

o    A) call() passes arguments as a list, apply() passes them as an array

o    B) apply() passes arguments as a list, call() passes them as an array

o    C) Both are identical

o    D) Neither are valid methods

o    Answer: A

162.            What is the purpose of Immediately Invoked Function Expressions (IIFE)?

o    A) To create a new scope

o    B) To execute code immediately

o    C) To prevent variable pollution in the global scope

o    D) All of the above

o    Answer: D

163.            Which method returns a new array with all elements that pass the test implemented by the provided function?

o    A) map()

o    B) filter()

o    C) reduce()

o    D) forEach()

o    Answer: B

164.            What is the purpose of the reduce() method?

o    A) To iterate over an array without returning a result

o    B) To transform each element of an array

o    C) To accumulate a single value from an array

o    D) To filter elements of an array

o    Answer: C

165.            How can you clone an array arr in ES6?

o    A) let clone = arr.slice();

o    B) let clone = [...arr];

o    C) let clone = Array.from(arr);

o    D) All of the above

o    Answer: D

166.            What is the purpose of the Symbol type introduced in ES6?

o    A) To create unique identifiers for object properties

o    B) To handle asynchronous operations

o    C) To work with regular expressions

o    D) To create immutable objects

o    Answer: A

167.            Which of the following is NOT a valid way to create a new Set in JavaScript?

o    A) new Set([1, 2, 3])

o    B) Set.of(1, 2, 3)

o    C) Set.create(1, 2, 3)

o    D) new Set()

o    Answer: C

168.            What is a WeakMap in JavaScript?

o    A) A collection of key-value pairs where keys are objects and values can be arbitrary values

o    B) A regular Map with additional security

o    C) A Map that allows garbage collection of keys if there are no other references

o    D) Both A and C

o    Answer: D

169.            Which feature introduced in ES6 allows objects to be extended with new properties dynamically?

o    A) Proxies

o    B) Object.assign()

o    C) Destructuring

o    D) Spread Operator

o    Answer: B

170.            **What is the output of the following code? javascript let a = 10; let b = '10'; console.log(a == b); console.log(a === b); ** - A) true, true - B) true, false - C) false, false - D) false, true - Answer: B


Miscellaneous Advanced Topics

171.            What is the purpose of the Proxy object in JavaScript?

o    A) To intercept and redefine fundamental operations for an object

o    B) To create a clone of an object

o    C) To handle asynchronous operations

o    D) To optimize performance

o    Answer: A

172.            Which of the following is used to handle asynchronous operations in JavaScript without blocking the main thread?

o    A) Web Workers

o    B) Callbacks

o    C) Promises

o    D) All of the above

o    Answer: D

173.            What does the Object.freeze() method do?

o    A) Makes an object immutable

o    B) Prevents adding new properties

o    C) Prevents deleting existing properties

o    D) All of the above

o    Answer: D

174.            Which statement best describes hoisting in JavaScript?

o    A) Variable declarations are moved to the top of their scope before code execution

o    B) Functions are executed before variables are declared

o    C) Variables are always initialized at the top of the code

o    D) All of the above

o    Answer: A

175.            What is the purpose of the bind() method in JavaScript?

o    A) To create a new function with a bound this value

o    B) To attach an event listener

o    C) To merge two objects

o    D) To iterate over array elements

o    Answer: A

176.            Which of the following is a way to create private variables in JavaScript?

o    A) Using closures

o    B) Using Symbols

o    C) Using WeakMaps

o    D) All of the above

o    Answer: D

177.            What is the purpose of the yield keyword in JavaScript?

o    A) To pause and resume generator functions

o    B) To handle asynchronous operations

o    C) To declare variables

o    D) To exit loops

o    Answer: A

178.            Which of the following is true about generator functions?

o    A) They can return multiple values over time

o    B) They are declared using the function* syntax

o    C) They maintain their state between executions

o    D) All of the above

o    Answer: D

179.            **What is the output of the following code? javascript let a = [1, 2, 3]; let b = a; b.push(4); console.log(a); ** - A) [1, 2, 3] - B) [1, 2, 3, 4] - C) [1, 2, 4] - D) Error - Answer: B

180.            What does the finally block do in a try...catch statement?

o    A) It executes code only if an error occurs

o    B) It executes code only if no error occurs

o    C) It executes code regardless of whether an error occurs

o    D) It rethrows the error

o    Answer: C


Miscellaneous JavaScript Concepts

181.            Which of the following is NOT a JavaScript data type?

o    A) Symbol

o    B) BigInt

o    C) Tuple

o    D) Undefined

o    Answer: C

182.            What is the result of typeof []?

o    A) array

o    B) object

o    C) undefined

o    D) null

o    Answer: B

183.            Which of the following is true about the this keyword in JavaScript?

o    A) It refers to the global object in non-strict mode

o    B) It refers to the object that invoked the function

o    C) Its value depends on how a function is called

o    D) All of the above

o    Answer: D

184.            What is the purpose of the debugger statement?

o    A) To stop the execution of code

o    B) To invoke any available debugging functionality

o    C) To log debugging information

o    D) To restart the script

o    Answer: B

185.            Which of the following is used to handle exceptions in asynchronous code with async/await?

o    A) .catch()

o    B) try...catch

o    C) throw

o    D) Both A and B

o    Answer: D

186.            What will console.log(typeof NaN); output?

o    A) number

o    B) NaN

o    C) undefined

o    D) object

o    Answer: A

187.            Which of the following can be used to define a block scope in JavaScript?

o    A) { }

o    B) function() { }

o    C) if statement

o    D) All of the above

o    Answer: D

188.            What is the purpose of the Object.entries() method?

o    A) To get an array of key-value pairs from an object

o    B) To get an array of keys from an object

o    C) To get an array of values from an object

o    D) To clone an object

o    Answer: A

189.            What is the output of typeof function() {}?

o    A) function

o    B) object

o    C) undefined

o    D) string

o    Answer: A

190.            Which of the following is a way to create a class in ES6?

o    A) class MyClass { constructor() {} }

o    B) function MyClass() {} MyClass.prototype.method = function() {};

o    C) const MyClass = class { constructor() {} }

o    D) All of the above

o    Answer: D


Miscellaneous JavaScript Concepts (Continued)

191.            What is the result of 0 || "default" in JavaScript?

o    A) 0

o    B) "default"

o    C) false

o    D) null

o    Answer: B

192.            Which method converts all elements of an array into a single value by executing a reducer function on each element?

o    A) map()

o    B) filter()

o    C) reduce()

o    D) forEach()

o    Answer: C

193.            What does the Object.keys() method return?

o    A) An array of an object's keys

o    B) An array of an object's values

o    C) An array of key-value pairs

o    D) An array of symbols

o    Answer: A

194.            Which of the following is true about JavaScript's single-threaded nature?

o    A) It can perform multiple operations simultaneously

o    B) It executes one operation at a time

o    C) It uses multi-threading to handle tasks

o    D) It cannot handle asynchronous operations

o    Answer: B

195.            How do you define a generator function in JavaScript?

o    A) function* gen() { yield 1; }

o    B) function gen() { return* 1; }

o    C) generator gen() { yield 1; }

o    D) def* gen() { yield 1; }

o    Answer: A

196.            What is the purpose of the Promise.resolve() method?

o    A) To create a promise that is already resolved with a given value

o    B) To convert a value to a promise

o    C) Both A and B

o    D) Neither A nor B

o    Answer: C

197.            Which of the following is used to handle multiple asynchronous operations sequentially?

o    A) Promise.all()

o    B) Promise.race()

o    C) Async/Await

o    D) Callbacks

o    Answer: C

198.            What does the Array.prototype.find() method do?

o    A) Returns the index of the first element that satisfies the provided testing function

o    B) Returns the first element that satisfies the provided testing function

o    C) Returns all elements that satisfy the provided testing function

o    D) Returns a boolean indicating if any element satisfies the provided testing function

o    Answer: B

199.            How can you ensure that a piece of code runs only after the entire page has loaded?

o    A) Using window.onload

o    B) Using document.ready

o    C) Using window.ready

o    D) Using document.onload

o    Answer: A

200.            What is the difference between == and === in JavaScript?

o    A) == checks for value equality, === checks for both value and type equality

o    B) == checks for type equality, === checks for value equality

o    C) Both are identical

o    D) == is deprecated

o    Answer: A


 

Tags

Post a Comment

0Comments

Post a Comment (0)