0% found this document useful (0 votes)
53 views16 pages

Javascript MCQ

The document consists of a series of JavaScript questions and answers covering various topics such as functions, arrays, objects, and error handling. It includes multiple-choice questions that test knowledge on JavaScript syntax, methods, and concepts. An answer key is provided at the end for reference.

Uploaded by

Sushmitha R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views16 pages

Javascript MCQ

The document consists of a series of JavaScript questions and answers covering various topics such as functions, arrays, objects, and error handling. It includes multiple-choice questions that test knowledge on JavaScript syntax, methods, and concepts. An answer key is provided at the end for reference.

Uploaded by

Sushmitha R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Javascript

1. What will alert("Hello World") do?


A. Return a string
B. Show a message in the console
C. Display a popup alert with "Hello World"
D. Log an error

2. Which function is used to collect input from a user?


A. alert()
B. prompt()
C. console.log()
D. confirm()

3. What is "use strict"; used for in JavaScript?


A. To enable jQuery
B. To catch silent errors
C. To disable functions
D. To allow dynamic typing

4. Which variable declaration allows reassignment but is block scoped?


A. var
B. let
C. const
D. static

5. What is the value of a variable that has been declared but not assigned?
A. null
B. undefined
C. false
D. NaN

6. Which operator is used to check both value and type?


A. ==
B. =
C. ===
D. !=
7. What does document.getElementById("myDiv") return?
A. An array of elements
B. A node list
C. A specific DOM element
D. Nothing

8. Which dialog box in JavaScript asks the user to confirm an action?


A. alert()
B. confirm()
C. prompt()
D. input()

9. Which value represents intentional absence of any value?


A. undefined
B. null
C. 0
D. NaN

10. Which operator is used for logical AND?


A. &&
B. ||
C. ==
D. !=

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


A. 7
B. 52
C. NaN
D. Error

12. How do you define a function in JavaScript?


A. function myFunc[] {}
B. def myFunc() {}
C. function myFunc() {}
D. create function myFunc()

13. How can you convert a string to a number in JavaScript?


A. Number("5")
B. toInt("5")
C. parseNum("5")
D. int("5")

14. Which method joins two arrays?


A. append()
B. merge()
C. concat()
D. join()

15. What does array.splice() do?


A. Extract a portion
B. Remove/replace elements
C. Sort the array
D. Add to end

16. What keyword is used to declare a constant in JavaScript?


A. let
B. const
C. var
D. define

17. What is the default return value of a function if no return is specified?


A. 0
B. null
C. undefined
D. false

18. How do you define an array in JavaScript?


A. let arr = {}
B. let arr = ()
C. let arr = []
D. let arr = <>

19. What does the map() function return?


A. A new array
B. A string
C. An object
D. Undefined
20. Which method is used to filter elements of an array based on a condition?
A. map()
B. reduce()
C. sort()
D. filter()

21. What does the reduce() method do?


A. Filters the array
B. Applies a function to accumulate a single result
C. Sorts the array
D. Maps the values

22. What is the output of typeof null?


A. null
B. undefined
C. object
D. string

23. Which keyword refers to the current object context?


A. self
B. that
C. this
D. context

24. What will Math.floor(4.9) return?


A. 4.9
B. 5
C. 4
D. 0

25. What is the purpose of Object.freeze(obj)?


A. Delete the object
B. Make object values uppercase
C. Prevent object modification
D. Hide properties

26. What is used to handle exceptions in JavaScript?


A. if...else
B. try...catch
C. error block
D. do...while

27. What is the output of Array.isArray([])?


A. false
B. true
C. undefined
D. error

28. Which operator checks if an object is an instance of a specific class?


A. typeof
B. ==
C. instanceOf
D. instanceof

29. What will "JavaScript".slice(4,10) return?


A. Script
B. Java
C. JavaSc
D. avasc

30. Which keyword is used to define a class in JavaScript?


A. object
B. define
C. class
D. function

31. What is the output of [1, 2, 3].length?


A. 2
B. 3
C. 1
D. undefined

32. How would you remove the last element from an array?
A. pop()
B. shift()
C. splice()
D. slice()
33. What does destructuring in JavaScript allow?
A. Assigning values from arrays/objects to variables
B. Destroying variables
C. Freezing variables
D. Sorting arrays

34. What is the result of Boolean(0)?


A. true
B. false
C. 0
D. undefined

35. Which method can be used to copy part of an array without modifying the original?
A. splice()
B. slice()
C. pop()
D. filter()

36. Which method adds elements to the end of an array?


A. unshift()
B. push()
C. shift()
D. concat()

37. What is returned by typeof NaN?


A. number
B. NaN
C. undefined
D. object

38. Which object is used to perform mathematical operations?


A. Calc
B. Math
C. Arithmetic
D. Number

39. How can form validation be achieved using JavaScript?


A. Using formValidator()
B. Adding required to input
C. JavaScript condition checks and regular expressions
D. CSS rules

40. What does the prompt() function return if the user clicks Cancel?
A. undefined
B. null
C. false
D. ""

📘 Answer Key:

1. C
2. B
3. B
4. B
5. B
6. C
7. C
8. B
9. B
10. A
11. B
12. C
13. A
14. C
15. B
16. B
17. C
18. C
19. A
20. D
21. B
22. C
23. C
24. C
25. C
26. B
27. B
28. D
29. A
30. C
31. B
32. A
33. A
34. B
35. B
36. B
37. A
38. B
39. C
40. B

1. Which of the following is the correct way to write a "Hello, World!" message in JavaScript?
A. console.log("Hello, World!");
B. alert("Hello, World!");
C. print("Hello, World!");
D. write("Hello, World!");

2. In JavaScript, what does alert("Hello!"); do?


A. Displays an alert box with the message "Hello!"
B. Logs the message "Hello!" in the console
C. Sends an email with the message "Hello!"
D. Outputs the message "Hello!" in the browser window

3. Which of the following is the correct way to link an external JavaScript file to an HTML
document?
A. <script src="file.js"></script>
B. <link rel="script" href="file.js">
C. <javascript src="file.js"></javascript>
D. <script link="file.js"></script>

4. What is the purpose of 'use strict'; in JavaScript?


A. It allows usage of undefined variables
B. It enforces stricter parsing and error handling in JavaScript
C. It automatically fixes bugs in the code
D. It optimizes the JavaScript code for faster execution

5. What will let x = 10; const y = 20; x = 15; y = 25; result in?
A. x = 15, y = 25
B. x = 10, y = 25
C. x = 15, y = 20
D. TypeError for y

6. Which of the following values is of type undefined in JavaScript?


A. null
B. ""
C. undefined
D. false

7. What is the difference between null and undefined in JavaScript?


A. null is a value, while undefined is a data type
B. Both represent the same concept
C. null is used for variables that have been declared but not initialized
D. undefined is the default value for variables that have been assigned null

8. What is the output of console.log(5 + "5"); in JavaScript?


A. 10
B. 55
C. Error
D. NaN

9. How do you declare a constant in JavaScript?


A. var const x = 10;
B. let x = 10;
C. const x = 10;
D. constant x = 10;

10. Which of the following is a valid way to declare a function in JavaScript?


A. function = myFunction(){}
B. let myFunction() {}
C. function myFunction() {}
D. function: myFunction() {}
Operators & Control Structures

11. Which operator is used for comparison in JavaScript?


A. ==
B. =
C. ===
D. Both A and C

12. What is the output of console.log(3 < 2); in JavaScript?


A. true
B. false
C. undefined
D. NaN

13. What will be the result of Math.pow(2, 3)?


A. 6
B. 8
C. 3
D. 9

14. Which of the following is an example of a logical operator?


A. +
B. !
C. *
D. []

15. What does the following JavaScript code do?

javascript
CopyEdit
let x = 10;
if (x > 5) {
console.log("Greater than 5");
} else {
console.log("Less than or equal to 5");
}
A. Outputs "Greater than 5"
B. Outputs "Less than or equal to 5"
C. Throws an error
D. No output

16. Which of the following is used for looping in JavaScript?


A. for
B. foreach
C. while
D. All of the above

17. What is the output of this code?

javascript
CopyEdit
let i = 5;
while (i > 0) {
console.log(i);
i--;
}

A. 1 2 3 4 5
B. 5 4 3 2 1
C. 5 4 3 2 1 0
D. 5 4 3 2

Functions & Arrays

18. What will the following code output?

javascript
CopyEdit
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));
A. "Hello, Alice!"
B. Hello Alice
C. greet(Alice)
D. undefined

19. What is the result of calling let a = [1, 2, 3]; a[2] = 4; console.log(a);?
A. [1, 2, 4]
B. [1, 2, 3, 4]
C. [1, 2, undefined, 4]
D. [1, 2, 4, undefined]

20. What does the map() method do on an array in JavaScript?


A. Filters elements in an array based on a condition
B. Reduces the array to a single value
C. Transforms each element in an array
D. Sorts the array

21. Which of the following methods is used to add elements to the end of an array?
A. push()
B. pop()
C. shift()
D. unshift()

22. Which method is used to remove the first element from an array?
A. pop()
B. shift()
C. unshift()
D. push()

Objects

23. What is the correct syntax for accessing the property name in an object person?
A. person.name
B. person["name"]
C. Both A and B
D. None of the above
24. How do you create an empty object in JavaScript?
A. let obj = {};
B. let obj = []
C. let obj = new Object()
D. Both A and C

25. How can you remove a property age from an object person?
A. delete person.age;
B. remove person.age;
C. delete person["age"];
D. Both A and C

26. What does the this keyword refer to in JavaScript?


A. The current function
B. The global object
C. The current object
D. The previous object

27. What is the result of typeof "hello" in JavaScript?


A. string
B. object
C. undefined
D. boolean

Web Fundamentals: Dynamic Web Pages

28. What is the purpose of the document.getElementById() method?


A. To get the value of an element by its id
B. To create a new HTML element
C. To modify the structure of the DOM
D. To log an error message

29. What does the innerHTML property do in JavaScript?


A. Sets the content of an HTML element
B. Creates new elements in the DOM
C. Adds CSS styles to an element
D. Deletes an element from the DOM
30. Which of the following is used to manipulate the DOM in JavaScript?
A. document.querySelector()
B. document.getElementById()
C. document.createElement()
D. All of the above

31. What is the use of localStorage in JavaScript?


A. Stores data that persists across page reloads
B. Stores data that expires after a specified time
C. Stores data on the server
D. Stores data in the session only

Error Handling and Validation

32. Which statement is used to handle errors in JavaScript?


A. try/catch
B. throw
C. finally
D. All of the above

33. What is the function of console.error() in JavaScript?


A. It logs an error message to the console
B. It throws an error
C. It returns an error object
D. It displays an alert box with an error message

Math & Date Operations

34. Which method is used to get the current date and time in JavaScript?
A. Date.now()
B. new Date()
C. Date.getTime()
D. currentDate()

35. What will Math.random() return?


A. A random integer
B. A random number between 0 and 1
C. A random number between 1 and 100
D. A random string

Classes & Objects

36. What is the correct syntax for creating a class in JavaScript?


A. class MyClass {}
B. class MyClass: {}
C. class MyClass() {}
D. function MyClass() {}

37. How do you create a new instance of a class Person?


A. let p = Person();
B. let p = new Person();
C. let p = class Person();
D. let p = Person.new();

Answers:

1. A
2. A
3. A
4. B
5. D
6. C
7. A
8. B
9. C
10. C
11. D
12. B
13. B
14. B
15. A
16. D
17. B
18. A
19. B
20. C
21. A
22. B
23. C
24. D
25. D
26. C
27. A
28. A
29. A
30. D
31. A
32. D
33. A
34. B
35. B
36. A
37. B

You might also like