JavaScript Multiple choice Questions
JavaScript is …
A. subjective
B. objective
C. evil
D. object based
Ans: D
var obj1 = {}; var obj2 = {}; What is the value of (obj1 === obj2)
A. true
B. false
Ans: B
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. There is no bug. The loop will run correctly.
D. Uncaught SyntaxError.
Ans: A
What does the following expression return? 1 + 5 + ” bottles of milk”;
A. “15 bottles of milk”
B. “6 bottles of milk”
C. undefined. An exception is thrown
D. “5 bottles of milk”
Ans: B
How do you create an object in JavaScript?
A. var obj = {};
B. function Foo() {} var obj = new Foo();
C. All of these work.
D. var obj = new Object();
Ans: C
What is the result of the following statement: typeof “x”;
A. “character”
B. “[object String]”
C. Throws error “ReferenceError: x is not defined”
D. “string”
E. “undefined”
Ans: D
Primitive types are passed by :
A. Value
B. Pointer
C. Reference
Ans: A
Which is not a primitive data type in JavaScript?
A. boolean
B. number
C. string
D. character
Ans: D
Which of these is a correct method to create a new array?
A. var myArray = ();
B. var myArray = [];
C. var myArray = new Array[];
D. var myArray = {};
E. var myArray = array();
Ans: B
To what type are values converted internally when evaluating a conditional statement?
A. positive
B. negative
C. integer
D. tinyint
E. boolean
Ans: E
Which of these is not a logical operator?
A. !
B. &
C. &&
D. ||
Ans: B
What is the value of x? var a = false; var x = a ? “A” : “B”;
A. undefined
B. true
C. “A”
D. “B”
E. false
Ans: D
Which of the following variable types does not exist in JavaScript?
A. boolean
B. number
C. object
D. double
E. string
Ans: D
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) { … }
D. if x = 5 then …
Ans: C
Which event fires whenever a control loses focus?
A. onclick
B. onmove
C. onblur
D. onchange
Ans: C
Which of the following invokes a user-defined object constructor function?
A. var x = new myConstructor();
B. var x = create myConstructor();
C. myConstructor x = create myConstructor();
D. myConstructor x = new myConstructor();
Ans: A
The function call Math.ceil(3.5) returns:
A. Throws a MathError exception.
B. 4
C. 0
D. 3
Ans: B
How is an object property referenced?
A. myObj(foo)
B. myObj->foo
C. myObj
D. myObj.foo
E. myObj[foo]
Ans: D
Which of these operators compares two variables by value AND type?
A. ===
B. None of these
C. ==
D. =
Ans: A
How would one declare a string variable?
A. Any of these
B. var fName = “Mary”;
C. var names = “7”;
D. var fName = new String;
Ans: A
function foo(){ var tmp = ‘one_two_three_four_five’; return tmp.replace(/_/g, ‘+’); } What does foo() return?
A. one_two_three_four_five
B. _______________________
C. one+
D. one+two_three_four_five
E. one+two+three+four+five
Ans: E
USERNAME and userName
A. Represent the name of the same variable
B. Represent the name of different variables
C. Represent the name of different constants
D. Represent the name of the same constant
Ans: B
Which of these could be a correct way to create an instance of Person?
A. var Person john = new Person(‘John’, ‘Doe’, 50, ‘blue’);
B. var john = new Person(‘John’, ‘Doe’, 50, ‘blue’);
C. new john = Person(‘John’, ‘Doe’, 50, ‘blue’);
D. Person john = new Person(‘John’, ‘Doe’, 50, ‘blue’);
Ans: B
Which is the correct way to write a JavaScript array?
A. var names = {0: “Tim”, 1: “Kim”, 2: “Jim”};
B. var names = {1: “Tim”, 2:”Kim”, 3:”Jim”};
C. var names = [“Tim”,”Kim”,”Jim”];
D. var names = array(“Tim”, “Kim”, “Jim”);
Ans: C
Which of the following asserts that the variables `A`, `B`, and `C` have unequal values?
A. A !== B || B !== C
B. A !== B & B !== C
C. A !== B && B !== C && A !== C
D. A !== B
Ans: C
The `else` statement is
A. Does not exist, in JavaScript `or` and `then` are used to specify code to execute for the “false” case of the `if` statement.
B. used inside of an `if` statement. To specify the code that should execute if the `if` condition is no longer true.
C. used together with the `if` statement to specify the code that should execute when the `if` condition is false.
Ans: C
The “if” statement is used to:
A. Deal with logic that should execute only when a condition is false
B. Convert an integer value to a boolean
C. Create a loop that runs as long as a condition is true
D. Deal with logic that should execute only when a condition is true
Ans: D
String literals are written using:
A. Just single quotes: ‘example’
B. Either double quotes or single quotes: “example” and ‘example’
C. Just double quotes: “example”
Ans: B
How to return the first value of this array? var myArr = [1, 2, 3, 4, 5]; var myVal = …
A. myArr[0];
B. myArr.pop();
C. myArr[1];
D. myArr.shift();
E. myArr.unshift();
Ans: A
How does a “while” loop start?
A. while i=(1 <> 10)
B. while i=1 to 10
C. while (i<=10)
D. while (i<=10;i++)
Ans: C
Properties of a RegExp object include:
A. source
B. ignoreCase
C. lastIndex
D. All of these
Ans: D
What is the value of the following expression: 8 % 3
A. 5
B. 2
C. 24
D. Other/Error
Ans: B
Given the following code, what does myFunc() return? var foo = ‘foo’; var bar = ‘bar’; function myFunc() { return foo + bar; }
A. “foobar”
B. NaN
C. “undefinedundefined”
D. An error is thrown because of illegal out of scope access.
E. “foo + bar”
Ans: A
Which symbol is not used in logical operations?
A. ||
B. %
C. &&
D. !
Ans: B
How do you round the number 7.25, to the nearest whole number?
A. Math.round(7.25)
B. rnd(7.25)
C. round(7.25)
D. Math.rnd(7.25)
Ans: A
Which of these will throw a SyntaxError?
A. if (x == 1) { }
B. if (x = 1) { }
C. if (x ==== 1) { }
D. if (x === 1) { }
Ans: C
What is the correct JavaScript syntax to insert a comment that can span multiple lines?
A. // This comment has mor than one line *//
B. / This comment has more than one line /
C. // This comment has more than one line //
D. /* This comment has more than one line */
Ans: D
JavaScript supports dynamic typing, you can assign different types of values to the same variable.
A. true
B. false
Ans: A
How do you define a function called “fName”?
A. function fName: { }
B. func fName = function () {}
C. function fName() { }
D. new fName = { }
E. None of these
Ans: C
How do you check what the type of a value in variable x is?
A. gettype(x);
B. x.__type;
C. Object.type(x);
D. typeof(x);
Ans: D
Which of the following is not a reserved word?
A. throw
B. void
C. program
D. return
Ans: C
Which keyboard character represents the assignment operator?
A. !
B. ?
C. #
D. :
E. =
Ans: E
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):
Ans: A
String concatenation…
A. is the splitting of a String into two or more Strings
B. Is a complex String
C. Is the combination of two or more text Strings
D. Is an elemental String
Ans: C
What is the value of (“dog”.length)?
A. 4
B. 3
C. 2
Ans: B
What is the value of x? var a = “A”; var x = a.concat(“B”);
A. “B”
B. “A”
C. “AB”
D. [“A”, ” B”];
Ans: C
Which is NOT a way to create a loop in javascript?
A. for (…) { }
B. do { } while(…)
C. while (…) { }
D. repeat (…) { }
Ans: D
In an array object, what is the key of the first value?
A. 0
B. $
C. 1
D. -1
E. 100
Ans: A
Which statement loops through an array?
A. for (i < myArray.length; i++)
B. for (i = 0; i <= myArray.length;)
C. for (var i=0; i < myArray.length; i++)
Ans: C
Where do you use the “break” statement?
A. To divide (or “break”) a mathematical value in half.
B. To add a value to an array.
C. To delete a (global) variable.
D. To terminate an Object statement.
E. To terminate a switch statement, loop, or labeled block.
Ans: E
The var statement is used to:
A. Create a new local variable
B. Retrieve a variable descriptor
C. Declare a member of a class
D. Change a constant
Ans: A
What does the “break” statement do?
A. Cancels the current event.
B. Aborts the current function.
C. Aborts the current loop or switch statement.
D. Simulates a JavaScript crash.
Ans: C
What character ends a javascript statement?
A. An exclamation mark “!”.
B. A semicolon “;”.
C. A period “.”.
D. A colon “:”.
Ans: B
Which of the following primitive values exist in JavaScript?
A. boolean
B. string
C. number
D. All of these
Ans: D
Are variable identifiers case-sensitive?
A. No
B. Yes
Ans: B
Which of the following declares a variable with a value of string type?
A. var string myVar = “This is a string”;
B. var myVar = “This is a string”;
C. string myVar = “This is a string”;
Ans: B
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
Ans: B
What keyword is used to begin a conditional statement?
A. when
B. how
C. if
D. condition
Ans: C
What character combination is used to create a single line comment?
A. !!
B. —
C. $$
D. //
Ans: D