JavaScript MCQs

JavaScript MCQs

Answer these 1000+ JavaScript MCQs and assess your grip on the subject of JavaScript. Scroll below and get started!

1: JavaScript is ...

A.   subjective

B.   evil

C.   object based

D.   objective

2: Math.PI returns the mathematical constant π. What standard JavaScript method would return "3.14"?

A.   Math.PI.toString("D2")

B.   Math.PI.toFixed(2)

C.   Math.Round(Math.PI)

D.   Math.PI.toPrecision(2)

3: What is the result? 0 == ""

A.   Error: type mismatch

B.   true

C.   false

4: How do you assign object properties?

A.   obj["age"] = 25 OR obj.age = 25

B.   obj.age = 25 OR obj(@"age") = 25

C.   obj(age) = 25 OR obj.age = 25

5: What is the result of the following statement: 0 == "";

A.   true

B.   null

C.   false

D.   Throws Error, invalid comparison

6: Math.random() returns..

A.   a random number that can be any value

B.   a random number between 0 and 100

C.   a random number between 0 and 1

D.   a random number between 0 and 1000

7: var x = "foo"; x = !!x; What is the value of x?

A.   true

B.   undefined

C.   NaN

D.   "!!foo"

8: How do you create an object in JavaScript?

A.   var obj = new Object();

B.   var obj = {};

C.   function Foo() {} var obj = new Foo();

D.   All of these work.

9: When writing an object literal, what is used to separate the properties from each other?

A.   an underscore "_"

B.   a colon ":"

C.   a full-stop "."

D.   a semicolon ";"

E.   a comma ","

10: What is the value of x? var a = false; var x = a ? “A” : “B”;

A.   false

B.   true

C.   "B"

D.   "A"

E.   undefined

11: Which of these is not a logical operator?

A.   &

B.   &&

C.   ||

D.   !

12: How do you find the number with the highest value of x and y?

A.   Math.ceil(x, y)

B.   max(x, y)

C.   top(x, y)

D.   Math.max(x, y)

E.   ceil(x, y)

13: Which of the following asserts that the variables `A`, `B`, and `C` have unequal values?

A.   A !== B

B.   A !== B && B !== C && A !== C

C.   A !== B & B !== C

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

14: What is the difference between a while loop and a do...while loop?

A.   The code inside a while loop will always be executed at least once, even if the condition is false.

B.   The code inside a do...while loop will always be executed at least once, even if the condition is false.

C.   There is no difference between them.

15: How to return the first value of this array? var myArr = [1, 2, 3, 4, 5]; var myVal = ...

A.   myArr.unshift();

B.   myArr[0];

C.   myArr.shift();

D.   myArr[1];

E.   myArr.pop();

16: Which is NOT a way to create a loop in javascript?

A.   while (...) { }

B.   do { } while(...)

C.   for (...) { }

D.   repeat (...) { }

17: What is the correct JavaScript syntax to insert a comment that can span multiple lines?

A.   / This comment has more than one line /

B.   // This comment has more than one line //

C.   /* This comment has more than one line */

D.   // This comment has mor than one line *//

18: Where do you use the "break" statement?

A.   To add a value to an array.

B.   To delete a (global) variable.

C.   To terminate an Object statement.

D.   To divide (or "break") a mathematical value in half.

E.   To terminate a switch statement, loop, or labeled block.

19: In an array object, what is the key of the first value?

A.   100

B.   $

C.   0

D.   -1

E.   1

20: what is the difference between x++ and ++x?

A.   ++x is depreciated and replaced with x++;

B.   x++ will return the value of x and then increment, where as ++x will increment the variable first then return its value.

C.   They both do the same thing, just different syntax

21: The var statement is used to:

A.   Declare a member of a class

B.   Change a constant

C.   Create a new local variable

D.   Retrieve a variable descriptor

22: Which of the following primitive values exist in JavaScript?

A.   number

B.   All of these

C.   string

D.   boolean

23: Which statement loops through an array?

A.   for (var i=0; i < myArray.length; i++)

B.   for (i = 0; i <= myArray.length;)

C.   for (i < myArray.length; i++)

24: You use the Math.pow() method to:

A.   Return any number

B.   Return a number raised to the power of a second number

C.   Return a random value between 0 and 1

D.   Return a variable value

25: What is the value of ("dog".length)?

A.   2

B.   3

C.   4

26: How do you round the number 7.25, to the nearest whole number?

A.   Math.round(7.25)

B.   round(7.25)

C.   Math.rnd(7.25)

D.   rnd(7.25)

27: Are variable identifiers case-sensitive?

A.   Yes

B.   No

28: String literals are written using:

A.   Just double quotes: "example"

B.   Just single quotes: 'example'

C.   Either double quotes or single quotes: "example" and 'example'

29: Which of the following is a valid function definition?

A.   function myFunc(arg1, arg2):

B.   func myFunc = (arg1 as string, arg2 as int) { }

C.   function myFunc(arg1,arg2) { }

30: JavaScript supports dynamic typing, you can assign different types of values to the same variable.

A.   true

B.   false

31: How do you check what the type of a value in variable x is?

A.   x.__type;

B.   typeof(x);

C.   Object.type(x);

D.   gettype(x);

32: What does the "break" statement do?

A.   Cancels the current event.

B.   Aborts the current loop or switch statement.

C.   Aborts the current function.

D.   Simulates a JavaScript crash.

33: Given the following code, what does myFunc() return? var foo = 'foo'; var bar = 'bar'; function myFunc() { return foo + bar; }

A.   An error is thrown because of illegal out of scope access.

B.   "foo + bar"

C.   NaN

D.   "foobar"

E.   "undefinedundefined"

34: Which is the correct way to write a JavaScript array?

A.   var names = array("Tim", "Kim", "Jim");

B.   var names = {1: "Tim", 2:"Kim", 3:"Jim"};

C.   var names = {0: "Tim", 1: "Kim", 2: "Jim"};

D.   var names = ["Tim","Kim","Jim"];

35: Which of the following is a JavaScript comment?

A.   // comment

B.   \\ comment

C.  

D.   -- comment

E.   # comment

36: How do you define a function called "fName"?

A.   new fName = { }

B.   func fName = function () {}

C.   None of these

D.   function fName() { }

E.   function fName: { }

37: Which symbol is not used in logical operations?

A.   ||

B.   &&

C.   %

D.   !

38: Which of the following declares a variable with a value of string type?

A.   string myVar = "This is a string";

B.   var myVar = "This is a string";

C.   var string myVar = "This is a string";

39: Which of the following is not a reserved word?

A.   throw

B.   program

C.   void

D.   return

40: Which of these could be a correct way to create an instance of Person?

A.   new john = Person('John', 'Doe', 50, 'blue');

B.   var Person john = new Person('John', 'Doe', 50, 'blue');

C.   var john = new Person('John', 'Doe', 50, 'blue');

D.   Person john = new Person('John', 'Doe', 50, 'blue');

41: Which of these will throw a SyntaxError?

A.   if (x = 1) { }

B.   if (x ==== 1) { }

C.   if (x === 1) { }

D.   if (x == 1) { }

42: What is the value of the following expression: 8 % 3

A.   5

B.   2

C.   24

D.   Other/Error

43: What operator is used for string concatenation?

A.   +

B.   &

C.   All of these

D.   .

44: Which of these descriptors applies to JavaScript?

A.   Strongly typed, variables are declared with a type, and you can not assign another type to the variable.

B.   Loosely typed, values of any type can be assigned to any variable.

45: Properties of objects may be accessed using...

A.   none of these

B.   the dot notation in JavaScript.

C.   the redirect notation in JavaScript.

46: Properties of a RegExp object include:

A.   lastIndex

B.   source

C.   All of these

D.   ignoreCase

47: What does isNaN() do?

A.   Only returns true if the argument is not a number

B.   Converts a non-numeric value to a number.

C.   Throws an error if a conditional statement is false.

48: The `else` statement is ___

A.   used inside of an `if` statement. To specify the code that should execute if the `if` condition is no longer true.

B.   used together with the `if` statement to specify the code that should execute when the `if` condition is false.

C.   Does not exist, in JavaScript `or` and `then` are used to specify code to execute for the "false" case of the `if` statement.

49: USERNAME and userName

A.   Represent the name of different variables

B.   Represent the name of the same constant

C.   Represent the name of the same variable

D.   Represent the name of different constants

50: What is JavaScript used for?

A.   Manipulating Web Page Behaviors

B.   Styling web pages

C.   A website cannot be created without JavaScript

D.   None of the above.

E.   Content definition within web pages

51: What is the value of a : var a = 3; var b = 2; var c = a; var a=b=c=1;

A.   true

B.   false

C.   3

D.   2

E.   1

52: What is the difference between == and === ?

A.   The == operator converts both operands to the same type, whereas === returns false for different types.

B.   The == is used in comparison, and === is used in value assignment.

C.   The === is deprecated, and now they are exactly the same.

53: Which of these operators compares two variables by value AND type?

A.   ===

B.   =

C.   None of these

D.   ==

54: What is the value of the array myArr after execution of the following code: var myArr = [1,2,3,4,5]; myArr.shift();

A.   []

B.   [2,3,4,5]

C.   [1,2,3,4]

D.   [1,2,3,4,5]

55: A for loop is written as such: "for (first property; second property; third property) {...}" What does the third property represent?

A.   An action to take at the beginning of the loop cycle

B.   An action to take at the end of the current loop cycle

C.   A condition to check at the beginning of a loop cycle

56: In JavaScript, to call a function directly, you use:

A.   arguments_if_any ( function_expression )

B.   function_expression ( arguments_if_any )

C.   ( arguments_if_any ) -> function_expression

D.   function_expression { arguments_if_any }

E.   function_expression -> ( arguments_if_any )

57: Which event fires whenever a control loses focus?

A.   onclick

B.   onblur

C.   onchange

D.   onmove

58: (function( ) { var x = foo( ); function foo( ){ return "foobar" }; return x; })( ); What does this function return?

A.   "foobar"

B.   foo( )

C.   TypeError: undefined is not a function

D.   undefined

E.   ReferenceError: foo is not defined

59: var a = {1:'one',2:'two',3:'three'}; var b = Object.keys(a); What's the value of b?

A.   An array with all of the distinct keys from the obj a

B.   An obj with autowired getters and setters for it's key/values

C.   A serialized copy of the obj a

60: What is the value of x? var x = '1'+2+3;

A.   15

B.   "123"

C.   The statement generates an error.

D.   6

61: Which of the following orders can be performed with the Array prototype "sort()" callback?

A.   ASCII ordering

B.   Ascending alphabetical

C.   Descending alphabetical

D.   All of these

62: null === undefined

A.   false

B.   true

63: When an array index goes out of bounds, what is returned?

A.   the first or last value in the array

B.   Moderate

C.   An error to the browser

D.   undefined

E.   A default value, like 0

64: Which of the following invokes a user-defined object constructor function?

A.   myConstructor x = create myConstructor();

B.   myConstructor x = new myConstructor();

C.   var x = create myConstructor();

D.   var x = new myConstructor();

65: Which of the following is an Error object constructor?

A.   RangeError

B.   Error

C.   EvalError

D.   All of these

66: The "exploit" property:

A.   Represents a variable

B.   Does not exist in JavaScript

C.   Is obsolete

D.   Is a very important property

67: Which of the following is the equivalent of the following. if (a) { x = b; } else { x = c; }

A.   x = a ? b : c;

B.   x = a : b ? c;

C.   x = a ? b , c;

68: Which of the following operators can assign a value to a variable?

A.   All of these

B.   +=

C.   %=

D.   =

69: How do you declare a function?

A.   function doSomething() {}

B.   all of these

C.   function:doSomething() {}

D.   function=doSomething() {}

70: Which is the correct syntax to write array literals in JavaScript?

A.   var x = array("blank", "blank", "blank”);

B.   var x = {"blank","blank","blank"};

C.   var x = new Array(1:"blank",2:"blank",3:"blank")

D.   var x = ["blank","blank","blank"];

71: JavaScript is an implementation of the ______ language standard.

A.   HTML

B.   ActionScript

C.   ECMAScript

D.   VBScript

72: The function call Math.ceil(3.5) returns:

A.   0

B.   4

C.   3

D.   Throws a MathError exception.

73: How do you assign an anonymous function to a variable?

A.   var anon = new Function () { };

B.   var anon = function() { };

C.   var anon = func() { };

D.   var anon = func({});

74: What is the RegExp object constructor used for?

A.   Provides access to Windows registry express values

B.   Regulates the expression of variables

C.   Match text against regular expressions

D.   Switches numerical notation to exponential

E.   Registers experienced functions with the DOM

75: Which of these is not a JavaScript statement?

A.   throw

B.   None, these are all valid statements.

C.   continue

D.   break

76: split() is a method of which constructors' prototype?

A.   None of these

B.   Number.prototype

C.   String.prototype

D.   Array.prototype

77: Which of the following is a way to add a new value to the end of an array?

A.   arr[arr.length()] = value;

B.   arr[value] = length;

C.   arr[arr.length] = value;

D.   arr.length = value;

78: What is the name of the String prototype that appends the given string to the base string and returns the new string?

A.   None of these does that and/or such method doesn't exist in javascript!

B.   "x".add("foo")

C.   "x".match("foo")

D.   "x".combine("foo")

E.   "x".concat("foo")

79: The _______ operator returns a string that identifies the type of its operand.

A.   typeof

B.   Type

C.   typename

D.   getType

E.   TypeOf

80: What is the value of x? var x = 2 + "2";

A.   "4"

B.   "22"

C.   22

D.   4

81: Which of the following variable types does not exist in JavaScript?

A.   double

B.   string

C.   number

D.   object

E.   boolean

82: What does Math.sqrt(-1) return?

A.   1

B.   -1

C.   i

D.   1.0

E.   NaN

83: How can you concatenate multiple strings?

A.   'One' + 'Two' + 'Three'

B.   'One'.concat('Two', 'Three')

C.   Both of these

84: Which of these is a correct method to create a new array?

A.   var myArray = ();

B.   var myArray = [];

C.   var myArray = array();

D.   var myArray = new Array[];

E.   var myArray = {};

85: To what type are values converted internally when evaluating a conditional statement?

A.   integer

B.   tinyint

C.   negative

D.   positive

E.   boolean

86: What does "2" + 3 + 4 evaluate to?

A.   '234'

B.   9

C.   '27'

87: Which has the correct syntax of a ternary operation?

A.   var x = y === true : "true" ? "false";

B.   var x = ( y === true ) : "true" ? "false";

C.   var x = y === true ? "true" : "false";

D.   var x = ( y === true ) { "true" : "false" };

88: Consider this code: var x = ['Hello']; What value will 'x[1]' return?

A.   null

B.   undefined

C.   ['Hello']

D.   "Hello"

E.   NULL

89: What is result of the following expression: var a = "test"; console.log(!!a);

A.   SyntaxError

B.   false

C.   undefined

D.   true

90: The loop isn't working. What's the problem? var foos = ['a', 'b', 'c' , 'd', 'e']; var bars = ['x', 'y', 'z']; for (var i = 0; i < foos.length; i++) { var foo = foos[i]; for (var i = 0; i < bars.length; i++) { var bar = bars[i]; /* some code using `bar` */ } }

A.   The inner loop resets the outer for-loop, leaving it a fixed position each time, causing an infinite loop (hint: no block scope).

B.   The outer-loop finishes after the first iteration due to a "bug" that unfortunately is part of the ECMAScript specification.

C.   Uncaught SyntaxError.

D.   There is no bug. The loop will run correctly.

91: If a function doesn't explicitly use the "return" operator, what will the return value be when the function is invoked?

A.   undefined

B.   null

C.   NaN

D.   closure

E.   false

92: How do you write a conditional statement that will only execute the contained code if variable x has a value 5 of type number?

A.   if (x === 5) { ... }

B.   if (x == 5) { ... }

C.   if x = 5 then ...

D.   if x = 5 ...

93: What is the value of x? function foo(y) { var z = 10; z = 7; }; var x = foo("bar");

A.   null

B.   10

C.   "bar"

D.   7

E.   undefined

94: In the loop, "for (first clause; second clause; third clause) { statements; }" What does the second clause represent?

A.   Code to execute once, after the loop has ended

B.   A condition to check at the end of each loop cycle

C.   Code to execute once, before the loop starts

D.   A condition to check at the beginning of each loop cycle

95: What are curly braces ("{" and "}") used for?

A.   Setting attributes

B.   Block declarations and object literals

C.   Invoking a function

D.   Parsing JSON

E.   Defining a class

96: Which of the following is the syntax for an object literal (with no properties)?

A.   {};

B.   [];

C.   nil;

D.   object;

E.   ();

97: Functions in javascript are always ..

A.   operators

B.   loops

C.   objects

D.   in the global scope

98: Which of the following is not a method in the "JSON" object according to the ECMAScript specification?

A.   JSON.parse

B.   JSON.fromString

C.   JSON.stringify

99: Given the next statement, what is the value of 'x' ? var a = true, b = false, x = !(a===b) ? !a : b;

A.   true

B.   false

100: Given a variable named stringVar with a string value, what does the following do? stringVar.toUpperCase();

A.   Alters stringVar, changes all letters to uppercase

B.   Return the number of characters in the stringVar variable

C.   Evaluate any string expression in stringVar

D.   Return a copy of stringVar with all letters in uppercase