JavaScript Exam Question and Answer

 





1. Which of the following is not a primitive data type in JavaScript?

a) String

b) Array

c) Boolean

d) Number

Answer: b) Array

 

2. What keyword is used to declare a variable in JavaScript?

a) var

b) int

c) string

d) let

Answer: a) var

 

3. How do you create a function in JavaScript?

a) function myFunction()

b) create function myFunction()

c) new function myFunction()

d) func myFunction()

Answer: a) function myFunction()

 

4. Which method is used to add an element to the end of an array in JavaScript?

a) push()

b) append()

c) addToEnd()

d) addElement()

Answer: a) push()

 

5. How do you access the third element in an array named myArray?

a) myArray[2]

b) myArray[3]

c) myArray(2)

d) myArray.third()

Answer: a) myArray[2]

 

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

a) //

b) /*

c) #

d) '

Answer: a) //

 

7. What does the isNaN() function do in JavaScript?

a) Checks if a value is an array

b) Checks if a value is null

c) Checks if a value is not a number

d) Checks if a value is a number

Answer: c) Checks if a value is not a number

 

8. Which operator is used for strict equality comparison in JavaScript?

a) ==

b) =

c) ===

d) !==

Answer: c) ===

 

9. What does the "typeof" operator do in JavaScript?

a) Returns the type of a variable or expression

b) Checks if a variable is defined

c) Converts a variable to a specific type

d) Returns the length of a string

Answer: a) Returns the type of a variable or expression

 

10. How do you declare a JavaScript object?

a) var myObject = {}

b) new Object myObject = {}

c) create object myObject = {}

d) object myObject = {}

Answer: a) var myObject = {}

 

11. Which method is used to remove the last element from an array in JavaScript?

a) pop()

b) removeLast()

c) deleteLast()

d) removeFromEnd()

Answer: a) pop()

 

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

a) "7"

b) 7

c) "52"

d) Error

Answer: c) "52"

 

13. Which function is used to round a number down to the nearest integer in JavaScript?

a) Math.floor()

b) Math.roundDown()

c) round()

d) floor()

Answer: a) Math.floor()

 

14. What is the correct syntax to include an external JavaScript file named "script.js"?

a) <script src="script.js">

b) <script href="script.js">

c) <script name="script.js">

d) <script file="script.js">

Answer: a) <script src="script.js">

 

15. How do you check if a variable is equal to either x or y in JavaScript?

a) if (variable == x || variable == y)

b) if (variable = x || y)

c) if (variable === x || y)

d) if (variable == (x || y))

Answer: a) if (variable == x || variable == y)

 

16. What does the "this" keyword refer to in JavaScript?

a) The current function

b) The parent object

c) The global object

d) The child object

Answer: b) The parent object

 

17. Which method is used to concatenate two or more strings in JavaScript?

a) concat()

b) merge()

c) combine()

d) append()

Answer: a) concat()

 

18. What is the output of the following code: console.log(3 + 2 + "7")?

a) 57

b) 12

c) "57"

d) Error

Answer: c) "57"

 

19. What is the correct syntax for an if statement in JavaScript?

a) if (condition) { // code }

b) if {condition} // code

c) (condition) if { // code }

d) {condition} if ( // code )

Answer: a) if (condition) { // code }

 

20. Which function is used to convert a string to uppercase in JavaScript?

a) toUpperCase()

b) upperCase()

c) toUpper()

d) caseUpper()

Answer: a) toUpperCase()

 

21. How do you declare a JavaScript array?

a) var myArray = ()

b) var myArray = []

c) array myArray = []

d) new Array myArray = []

Answer: b) var myArray = []

 

22. What does the JavaScript "&&" operator do?

a) Logical OR

b) Bitwise AND

c) Logical AND

d) Bitwise OR

Answer: c) Logical AND

 

23. Which method is used to find the index of a specified item in an array in JavaScript?

a) indexOf()

b) findIndex()

c) search()

d) find()

Answer: a) indexOf()

 

24. What is the output of the following code: console.log(typeof NaN)?

a) "number"

b) NaN

c) "NaN"

d) Error

Answer: a) "number"

 

25. How do you declare a constant variable in JavaScript?

a) const myVar = value;

b) let myVar = value;

c) var myVar = value;

d) constant myVar = value;

Answer: a) const myVar = value;

 

26. What does the JavaScript "!" operator do?

a) Logical NOT

b) Bitwise NOT

c) Logical OR

d) Bitwise OR

Answer: a) Logical NOT

 

27. Which method is used to convert a string to a number in JavaScript?

a) parseInt()

b) toNumber()

c) parseNumber()

d) number()

Answer: a) parseInt()

 

28. What is the output of the following code: console.log(2 + "2" + 2)?

a) 6

b) "222"

c) 4

d) Error

Answer: b) "222"

 

29. What does the "break" statement do in JavaScript?

a) Exits the loop or switch statement

b) Continues to the next iteration of the loop

c) Ends the function execution

d) Skips the condition check

Answer: a) Exits the loop or switch statement

 

30. How do you write a comment that spans multiple lines in JavaScript?

a) /* This is a comment */

b) // This is a comment //

c) <!-- This is a comment -->

d) ''' This is a comment '''

Answer: a) / This is a comment /

 

31. What is the output of the following code: console.log(2 * 3 + "7")?

a) 77

b) "77"

c) 13

d) Error

Answer: b) "77"

 

32. What is the correct way to declare a JavaScript function called "myFunction"?

a) function myFunction() {}

b) func myFunction() {}

c) create function myFunction() {}

d) new function myFunction() {}

Answer: a) function myFunction() {}

 

33. Which method is used to convert a number to a string in JavaScript?

a) toString()

b) toStr()

c) numberToString()

d) stringify()

Answer: a) toString()

 

34. What is the output of the following code: console.log(3 == "3")?

a) true

b) false

c) Error

d) undefined

Answer: a) true

 

35. What does the "continue" statement do in JavaScript?

a) Skips the condition check

b) Exits the loop or switch statement

c) Continues to the next iteration of the loop

d) Ends the function execution

Answer: c) Continues to the next iteration of the loop

 

36. Which method is used to join the elements of an array into a string in JavaScript?

a) concat()

b) join()

c) merge()

d) combine()

Answer: b) join()

 

37. What is the output of the following code: console.log(2 == "2")?

a) true

b) false

c) Error

d) undefined

Answer: a) true

 

38. How do you declare a block of code in JavaScript?

a) { // code }

b) block { // code }

c) block ( // code )

d) { code }

Answer: a) { // code }

 

39. What is the output of the following code: console.log(typeof [1, 2, 3])?

a) "array"

b) "object"

c) "list"

d) Error

Answer: b) "object"

 

40. Which method is used to remove the first element from an array in JavaScript?

a) shift()

b) removeFirst()

c) removeFromStart()

d) deleteFirst()

Answer: a) shift()

 

41. What is the correct way to write an if-else statement in JavaScript?

a) if (condition) { // code } else { // code }

b) {condition} if { // code } else { // code }

c) if { // code } else (condition) { // code }

d) if (condition) { // code } { else // code }

Answer: a) if (condition) { // code } else { // code }

 

42. Which method is used to convert a string to lowercase in JavaScript?

a) toLowerCase()

b) lowerCase()

c) toLower()

d) caseLower()

Answer: a) toLowerCase()

 

43. What is the output of the following code: console.log(4 % 2)?

a) 2

b) 0

c) 1

d) Error

Answer: b) 0

 

44. What is the correct way to write a for loop in JavaScript?

a) for (var i = 0; i < 10; i++) { // code }

b) loop (var i = 0; i < 10; i++) { // code }

c) iterate (var i = 0; i < 10; i++) { // code }

d) for (i = 0; i < 10; i++) { // code }

Answer: a) for (var i = 0; i < 10; i++) { // code }

 

45. What is the output of the following code: console.log(2 + 2 + "2")?

a) 6

b) "22"

c) 4

d) Error

Answer: b) "22"

 

46. What is the correct way to write a while loop in JavaScript?

a) while (condition) { // code }

b) iterate (condition) { // code }

c) loop (condition) { // code }

d) for (condition) { // code }

Answer: a) while (condition) { // code }

 

47. Which method is used to sort the elements of an array in JavaScript?

a) sort()

b) arrange()

c) order()

d) organize()

Answer: a) sort()

 

48. What is the output of the following code: console.log("5" - 2)?

a) 7

b) "7"

c) 3

d) Error

Answer: c) 3

 

49. What does the JavaScript "||" operator do?

a) Logical OR

b) Bitwise OR

c) Logical AND

d) Bitwise AND

Answer: a) Logical OR

 

50. Which method is used to return the length of a string in JavaScript?

a) length()

b) len()

c) count()

d) size()

Answer: a) length()

No comments:

Post a Comment