50+ JavaScript MCQs

                     

1. JavaScript is the programming language of the _____.

  1. Desktop
  2. Mobile
  3. Web
  4. Server

Answer: C) Web

2. Which type of JavaScript language is _____?

  1. Object-oriented
  2. Object-based
  3. Functional programming
  4. All of the above

Answer: B) Object-based

3. Which of the following statement(s) is true about the JavaScript?

  1. It is a scripting language used to make the website interactive
  2. It is an advanced version of Java for Desktop and Mobile application development
  3. It is a markup language of Java to develop the webpages
  4. All of the above

Answer: A) It is a scripting language used to make the website interactive

4. In which HTML element, we put the JavaScript code?

  1. <javascript>…</javascript>
  2. <js>…</js>
  3. <script>…</script>
  4. <css>…</css>

Answer: C) <script>…</script>

5. JavaScript code can be written in ____.

  1. JavaScript file (.js file)
  2. HTML document directly
  3. JavaScript file and in HTML document directly
  4. In style sheets (.css file)

Answer: C) JavaScript file and in HTML document directly

6. Which symbol is used separate JavaScript statements?

  1. Comma (,)
  2. Colon (:)
  3. Hyphen (_)
  4. Semicolon (;)

Answer: D) Semicolon (;)

7. JavaScript ignores?

  1. newlines
  2. tabs
  3. spaces
  4. All of the above

Answer: D) All of the above

8. Which is the correct syntax to call an external JavaScript file in the current HTML document?

  1. <script src=”jsfile.js”></script>
  2. <script href=” jsfile.js”></script>
  3. <import src=” jsfile.js”></import>
  4. <script link=” jsfile.js”></script>

Answer: A) <script src=”jsfile.js”></script>

9. Which JavaScript method is used to access an HTML element by id?

  1. getElementById()
  2. getElement(id)
  3. getElementById(id)
  4. elementById(id)

Answer: C) getElementById(id)

10. Which property is used to define the HTML content to an HTML element with a specific id?

  1. innerText
  2. innerContent
  3. elementText
  4. innerHTML

Answer: D) innerHTML

Click Here For Programming MCQs

11. Which JavaScript method is used to write HTML output?

  1. document.write()
  2. document.output()
  3. console.log()
  4. document.writeHTML()

Answer: A) document.write()

12. Which JavaScript method is used to write on browser’s console?

  1. console.write()
  2. console.output()
  3. console.log()
  4. console.writeHTML()

Answer: C) console.log()

13. Which JavaScript method is used to write into an alert box?

  1. window.alertHTML()
  2. window.alert()
  3. window.alertBox()
  4. window.alertContent()

Answer: B) window.alert()

14. Which is the correct JavaScript statement to display “Hello Boss!” into an alert box?

  1. alert(“Hello Boss!”);
  2. alert(‘Hello Boss!’);
  3. alert(Text:’Hello Boss!’);
  4. Both A. and B.

Answer: D) Both A. and B.

15. Which is the correct JavaScript statement to print the addition of two numbers 10 and 2o in a paragraph whose id is “result”?

  1. getElementById(“result”).innerHTML = 10+20;
  2. getElementById(“result”).innerHTML = “10+20”;
  3. getElementById(“#result”).innerHTML = 10+20;
  4. All of the above

Answer: A) getElementById(“result”).innerHTML = 10+20;

16. What is the use of this JavaScript statement?

<button onclick="window.print()">Submit</button>
  1. It will write “Submit” on the current Window
  2. It will print the content of the current page
  3. It will write the content of the current page in the browser’s console
  4. None of the above

Answer: B) It will print the content of the current page

17. In JavaScript, single line comment begins with ___.

  1. #
  2. /*
  3. $
  4. //

Answer: D) //

18. In JavaScript, multi-line comments start with __ and end with ___.

  1. /* and */
  2. <!—and –>
  3. ## and ##
  4. // and //

Answer: A) /* and */

19. Which JavaScript keyword is used to declare a variable?

  1. Var
  2. var
  3. Let
  4. All of the above

Answer: B) var

20. How many keywords are there in JavaScript to declare variables or constants?

  1. 1
  2. 2
  3. 3
  4. 4

Answer: C) 3

Click here for More…

21. What is the main difference between var and let keywords in JavaScript?

  1. var defines a variable while let defines a constant
  2. var defined function scoped variable while let define block scoped variable
  3. The value of a variable declared with var can be changed while the value of a variable declared with let cannot be changed
  4. All of the above

Answer: B) var defined function scoped variable while let define block scoped variable

22. The const keyword is used to define a ______.

  1. Function scopes variable
  2. Block scoped variable
  3. Constant
  4. Constant with no initial value

Answer: C) Constant

23. Which is the correct syntax to declare a constant in JavaScript?

  1. const constant_name;
  2. constant_name const;
  3. constant_name const = value;
  4. const constant_name = value;

Answer: D) const constant_name = value;

24. What will be the value of VALUE?

<script>
	const VALUE = 10;
	VALUE = 20;
</script>
  1. 10
  2. 20
  3. ValueError
  4. TypeError

Answer: D) TypeError

25. What is the default value of an uninitialized variable?

  1. 0
  2. undefined
  3. null
  4. NaN

Answer: B) undefined

26. What is the output of the following JavaScript code?

<script>
	var a;
	document.getElementById("demo").innerHTML = a+1;
</script>
  1. 0
  2. undefined
  3. 1
  4. NaN

Answer: D) NaN

27. Can be redeclare a variable that is declared with var keyword?

  1. Yes
  2. No

Answer: A) Yes

28. What is the output of the following JavaScript code?

<script>
	var name = "Alex" + " " + "Alvin";
	document.getElementById("demo").innerHTML = name;
</script>
  1. Alex Alvin
  2. AlexAlvin
  3. TypeError
  4. ValueError

Answer: A) Alex Alvin

29. What is the output of the following JavaScript code?

<script>
	var a = 10 + 20 + "5";
	document.getElementById("demo").innerHTML = a;
</script>
  1. 35
  2. 305
  3. TypeError
  4. ValueError

Answer: B) 305

30. Can be redeclare a variable that is declared with let keyword?

  1. Yes
  2. No

Click Here For Programming MCQs

Answer: B) No

31. What is the output of the following JavaScript code?

<script>
	let a = 10;
	let a = 0;
</script>
  1. 10
  2. 0
  3. SyntaxError
  4. TypeError

Answer: C) SyntaxError

32. Which is the exponentiation operator in JavaScript?

  1. exp()
  2. ^
  3. **
  4. pow

Answer: C) **

33. Does JavaScript support increment (++) and decrements (–) Operators?

  1. Yes
  2. No

Answer: A) Yes

34. What will be the output of the following JavaScript code?

<script>
	var x = 5;
	document.getElementById("demo").innerHTML = x--;
</script>
  1. 5
  2. 4
  3. TypeError
  4. ValueError

Answer: B) 4

35. What will be the output of the following JavaScript code?

<script>
	var x = 10 + 20 * 5;
	document.getElementById("tes").innerHTML = x;
</script>
  1. 110
  2. 150
  3. TypeError
  4. ValueError

Answer: A) 110

36. What will be the output of the following JavaScript code?

<script>
	var x = (10 + 20) * 5;
	document.getElementById("tes").innerHTML = x;
</script>
  1. 110
  2. 150
  3. TypeError
  4. ValueError

Answer: B) 150

37. JavaScript types are _____.

  1. Static
  2. Dynamic

Answer: B) Dynamic

38. JavaScript arrays are written with _____.

  1. round brackets ()
  2. curly brackets {}
  3. double quotes “”
  4. square brackets []

Answer: D) square brackets []

39. JavaScript objects are written with _____.

  1. round brackets ()
  2. curly brackets {}
  3. double quotes “”
  4. square brackets []

Answer: B) curly brackets {}

40. Which JavaScript operator is used to determine the type of a variable?

  1. typeof
  2. TypeOf
  3. typeOf
  4. sizeof

Answer: A) typeof

Click here for More…

41. Which is the correct syntax of JavaScript typeof operator?

  1. typeof variable/value
  2. typeof(variable/value)
  3. Both A. and B.
  4. None of the above

Answer: C) Both A. and B.

42. What will be the output of the following JavaScript code?

<script>
	var x = 12.34;
	document.getElementById("test").innerHTML = typeof(x);
</script>
  1. int
  2. float
  3. long
  4. number

Answer: D) number

43. Which keyword is used to define a JavaScript function?

  1. module
  2. fun
  3. func
  4. function

Answer: D) function

44. Which is the correct syntax for the function definition?

  1. return_type function function_name(parameter1, parameter2, …) { /*Function’s body*/ }
  2. function function_name(parameter1, parameter2, …) { /*Function’s body*/ }
  3. return_type function_name(parameter1, parameter2, …) { /*Function’s body*/ }
  4. function function_name(parameter1, parameter2, …) as return_type { /*Function’s body*/ }

Answer: B) function function_name(parameter1, parameter2, …) { /*Function’s body*/ }

45. What will be the output of the following JavaScript code?

<script>
	function addition(a, b) {
		return a+b;
	}
	document.getElementById("test").innerHTML = addition;
</script>
  1. SyntaxError
  2. ValueError
  3. 0
  4. function addition(a, b) { return a+b; }

Answer: D) function addition(a, b) { return a+b; }

46. Can we use a function as a variable value?

  1. Yes
  2. No

Answer: A) Yes

47. In JavaScript a variable contains one value while an object may contain ___.

  1. One value
  2. Two values
  3. Three values
  4. Many values

Answer: D) Many values

48. Which is the correct syntax to access an object property in JavaScript?

  1. objectName:propertyName
  2. propertyName
  3. objectName[“propertyName”]
  4. Both B. and C.

Answer: D) Both B. and C.

49. Which property is used to get the length of a string in JavaScript?

  1. strlen
  2. len
  3. length
  4. Length

Answer: C) length

50. What will be the output of the following JavaScript code?

<script>
	let str = "Progies";
	document.getElementById("test").innerHTML = str.length;
</script>
  1. 7
  2. 12
  3. ValueError
  4. SyntaxError

Answer: A) 7

Click Here For Programming MCQs

51. Which character is used to break up a code line within a text string in JavaScript?

  1. Single quote (‘)
  2. Single backslash (\)
  3. Double quote (“)
  4. Tipple single quote (”’)

Answer: B) Single backslash (\)

52. Will the following JavaScript code work?

<script>
	document.getElementById("test").innerHTML = \
	"Hello, Progies!";
</script>
  1. Yes
  2. No

Answer: B) No

53. Which is the correct JavaScript statement to define string as object?

  1. var s = new String(“Progies!”);
  2. var s = String(“Progies!”);
  3. var s = “Progies!”
  4. All of the above

Answer: A) var s = new String(“Progies!”);

54. What will be the output of the following JavaScript code?

<script>
	let str1 = new String("Progies!");
	let str2 = new String("Progies!");
	document.getElementById("test").innerHTML = (str1==str2);
</script>
  1. true
  2. false
  3. True
  4. False

Answer: B) false

55. Which is/are the valid JavaScript method(s) to extract string parts?

  1. slice(start, end)
  2. substring(start, end)
  3. substr(start, length)
  4. All of the above

Answer: D) All of the above

56. What will be the output of the following JavaScript code?

<script>
    let x = "Hello, Progies!";
    document.getElementById("test").innerHTML = x.slice(-8,-1);
</script>
  1. Progies!
  2. Progies
  3. ValueError
  4. Hello,

Answer: B) Progies

57. In JavaScript, the string template literals use ____ rather than the quotes (“”) to define a string?

  1. Single quotes (”)
  2. Backslash with single quote (\’’\’)
  3. Backslashes (\\)
  4. Back-ticks (“)

Answer: D) Back-ticks (“)

58. Does the following JavaScript variable definition is correct?

let x = `I'm "David!"`;
  1. Yes
  2. No

Answer: A) Yes

59. Which JavaScript method is used to get a number as a string?

  1. toString()
  2. intToString()
  3. parseInteger()
  4. All of the above

Answer: A) toString()

Click here for More…

60. What will be the output of the following JavaScript code?

<script>
    const myArray = ['h', 'e', 'l', 'l', 'o'];
    document.write(myArray[0]);
    document.write(myArray[1]);
</script>
  1. he
  2. undefinedh
  3. ValueError
  4. TypeError

Answer: A) he

61. What will be the output of the following JavaScript code?

<script>
    let cars = ['Honda', 'Hyundai'];
    cars.push('Mahindra');
    document.write(typeof cars + " " + cars);
</script>
  1. array Honda,Hyundai,Mahindra
  2. string Honda,Hyundai,Mahindra
  3. object Honda,Hyundai,Mahindra
  4. object “Honda”, “Hyundai”, “Mahindra”

Answer: C) object “Honda”, “Hyundai”, “Mahindra”

62. What will be the output of the following JavaScript code?

<script>
    let cars1 = ['Honda', 'Hyundai'];
    let cars2 = cars1;
    
    cars1.push('Mahinda');
    
    document.write(cars1 + "---" + cars2);
</script>
  1. Honda,Hyundai,Mahinda—Honda,Hyundai
  2. Honda,Hyundai,Mahinda—Honda,Hyundai,Mahinda
  3. Honda,Hyundai —Honda,Hyundai
  4. [Honda,Hyundai,Mahinda]—[Honda,Hyundai,Mahinda]

Answer: B) Honda,Hyundai,Mahinda—Honda,Hyundai,Mahinda

63. What will be the output of the following JavaScript code?

<script>  
	var msgs=new Array("Hello","Hey","Morning!");  

	for (i=0;i<msgs.length;i++){  
		document.write(msgs[i] + " | ");  
	}
</script>
  1. Hello | Hey | Morning! |
  2. Hello | Hey |
  3. ValueError
  4. TypeError

Answer: A) Hello | Hey | Morning! |

64. What will be the output of the following JavaScript code?

<script>
	var values = [10, 20, 30, 40];
	
	var result = values.reduceRight(function(x,y){
		return (x + y);
	});
	
	document.write("Result: " + result);
</script>
  1. Result: 40
  2. Result: 70
  3. Result: 90
  4. Result: 100

Answer: D) Result: 100

65. What will be the output of the following JavaScript code?

<script>
	var cars = ["Honda","Hyundai","Mahindra"];
	
	var result = cars.shift();
	
	document.writeln("Result: ", cars);
</script>
  1. Result: Honda,Hyundai,Mahindra
  2. Result: Honda
  3. Result: Hyundai,Mahindra
  4. Result: Honda,Mahindra

Answer: C) Result: Hyundai,Mahindra

66. What will be the output of the following JavaScript code?

<script>
	var cars = ["Honda","Hyundai","Mahindra"];

	var result = cars.unshift("Toyota", "Tata");

	document.writeln("[", result, "] ", cars);
</script>
  1. [5] Toyota,Tata,Honda,Hyundai,Mahindra
  2. [5]Honda,Hyundai,Mahindra,Toyota,Tata
  3. [2] Toyota,Tata
  4. [5] Honda,Hyundai,Toyota,Tata,Mahindra

Answer: A) [5] Toyota,Tata,Honda,Hyundai,Mahindra

67. Which JavaScript method is used to call a function (a callback function) once for each array element?

  1. for()
  2. traverse()
  3. forEach()
  4. foreach()

Answer: C) forEach()

68. What will be the output of the following JavaScript code?

<script>
	const arr = [10, 20, 30];
	let result = 0;
	
	arr.forEach(myFunction);
	
	document.write("Result: " , result)
	function myFunction(value, index, array) {
	  result += value; 
	}
</script>
  1. Result: 60
  2. Result: 102030
  3. Result: 10,20,30
  4. ValueError

Answer: A) Result: 60

69. What will be the output of the following JavaScript code?

<script>
	const values = [10, 20, 30];
	const result = values.map(myFunction);

	document.write("Result: ", result);

	function myFunction(value, index, array) {
	  return value * value;
	}
</script>
  1. Result: 10,20,30
  2. Result: 10*10,20*20,30*30
  3. Result: 100,400,900
  4. ValueError

Answer: C) Result: 100,400,900

70. Which JavaScript method is used to create a new array with array elements that passes a test?

  1. forEach()
  2. map()
  3. forMap()
  4. filter()

Answer: D) filter()

Click Here For Programming MCQs

71. Which JavaScript object works with the dates?

  1. Date
  2. DateTime
  3. date
  4. dateTime

Answer: A) Date

72. Which JavaScript statement(s) is correct to create Date object(s) with new Date() constructor?

  1. new Date()
  2. new Date(year, month, day, hours, minutes, seconds, milliseconds)
  3. new Date(milliseconds)
  4. new Date(date string)
  5. All of the above

Answer: E) All of the above

73. What will be the output of the following JavaScript code?

<script>
	const curr = new Date();
	document.write(curr);
</script>
  1. Tue Dec 21 2021 13:04:36 GMT+0530
  2. Tue Dec 21 2021 13:04:36 (India Standard Time)
  3. Tue Dec 21 2021 13:04:36::00::01 GMT+0530 (India Standard Time)
  4. Tue Dec 21 2021 13:04:36 GMT+0530 (India Standard Time)

Answer: D) Tue Dec 21 2021 13:04:36 GMT+0530 (India Standard Time)

74. Which JavaScript method is used to convert a date to a UTC string (a date display standard)?

  1. toUTCString()
  2. toUtcString()
  3. utcString()
  4. toutcstring()

Answer: A) toUTCString()

75. The internal clock in JavaScript counts from midnight _____.

  1. January 1, 1972
  2. January 1, 1947
  3. January 1, 1980
  4. January 1, 1970

Answer: D) January 1, 1970

76. What does the Date object’s method getTime() return?

  1. Date in DD-MM-YYYY format
  2. Date in DD MON YYYY format
  3. Date in MON, DD YYYY format
  4. Number of milliseconds since January 1, 1970

Answer: D) Number of milliseconds since January 1, 1970

77. Which method is used to get the year of a date as a four-digit number?

  1. getYear()
  2. fullYear()
  3. getFullYear()
  4. getfullyear()

Answer: C) getFullYear()

78. What will be the output of the following JavaScript code?

<script>
	document.write(Math.round(107.5))
</script>
  1. 107.5
  2. 107
  3. 108
  4. 107.00

Answer: C) 108

79. What will be the output of the following JavaScript code?

<script>
try{
	const cars = {  
		company: 'Honda'
	};  

	delete cars.company;
	document.write(cars.company);  
}
catch (err){
	document.write(err.message);
}
</script>
  1. undefined
  2. Honda
  3. ValueError
  4. TypeError

Answer: A) undefined

80. What will be the output of the following JavaScript code?

<script>
try{
    const cars = {  
        company: 'Honda'
    };  
    
    Object.seal(cars);
    delete cars.company;
    document.write(cars.company);  
}
catch (err){
    document.write(err.message);
}
</script>
  1. undefined
  2. Honda
  3. ValueError
  4. TypeError

Answer: B) Honda

Click here for More…

81. Output of the following JavaScript code?

<script>
    let x = "10";
    let y = + x;
    
    document.write(typeof y);
</script>
  1. string
  2. object
  3. undefined
  4. number

Answer: D) number

82. What will be the output of the JavaScript code?

<script>
    let x = 10;
    
    document.write(typeof x, " , ", typeof String(x));
</script>
  1. number , string
  2. number , number
  3. object , string
  4. object , object

Answer: A) number , string

83. What will be the output ?

<script>
    let x = 10;
    
    document.write(x, " , ", toString(x));
</script>
  1. 10 , 10
  2. 10 , undefined
  3. 10 , [object Undefined]
  4. None of the above

Answer: C) 10 , [object Undefined]