JavaScript Questions and Answers Part-3

1. JavaScript is a _______________ language.
a) Object-Oriented
b) High-level
c) Assembly-language
d) Object-Based

Answer: d
Explanation: JavaScript is not a full-blown OOP (Object-Oriented Programming) language, such as Java or PHP, but it is an object-based language. The criteria for object orientation are the classic threesome of polymorphism, encapsulation and inheritance and JavaScript doesn’t pass this.

2. What will be the output of the following JavaScript code?
var a=5 , b=1
var obj = { a : 10 }
with(obj)
{
alert(b)
}
a) 10
b) error
c) 1
d) 5

Answer: c
Explanation: Firstly the interpreter checks obj for property b. But it doesn’t find any property b so it takes the value from outside the object within the code snippet.

3. A conditional expression is also called a _______________
a) Alternative to if-else
b) Immediate if
c) If-then-else statement
d) Switch statement

Answer: b
Explanation: A conditional expression can evaluate to either True or False based on the evaluation of the condition. If the condition is true then the value following the operator is taken that’s why it is called immediate if.

4. Which is a more efficient JavaScript code snippet?
Code 1 :
for(var num=10;num>=1;num--)
{
document.writeln(num);
}
Code 2 :
var num=10;
while(num>=1)
{
document.writeln(num);
num++;
}
a) Code 1
b) Code 2
c) Both Code 1 and Code 2
d) Cannot Compare

Answer: a
Explanation: Code 1 would be more efficient. Infact second code will go into runtime error as the value of num will never reach less than or equal to one.

5. What is a block statement in JavaScript?
a) conditional block
b) block that contains a single statement
c) both conditional block and a single statement
d) block that combines multiple statements into a single compound statement

Answer: d
Explanation: A block statement groups zero or more statements. In languages other than JavaScript, it is known as a compound statement. A statement block is a block that combines more than one statements into a single compound statement for ease.

6. When an empty statement is encountered, a JavaScript interpreter __________
a) Ignores the statement
b) Prompts to complete the statement
c) Throws an error
d) Shows a warning

Answer: a
Explanation: The JavaScript interpreter takes no action when it executes an empty statement. The empty statement is occasionally useful when you want to create a loop that has an empty body.

7. The “var” and “function” are __________
a) Keywords
b) Declaration statements
c) Data types
d) Prototypes

Answer: b
Explanation: The var and function are declaration statements—they declare or define variables and functions. These statements define identifiers (variable and function names) that can be used elsewhere in your program and assign values to those identifiers.

8. In the following switch syntax, the expression is compared with the case labels using which of the following operator(s)?
switch(expression)
{
statements
}
a) ==
b) equals
c) equal
d) ===

Answer: d
Explanation: A strict comparison (e.g., ===) is only true if the operands are of the same type and the contents match. When a switch executes, it computes the value of the expression and then looks for a case label whose expression evaluates to the same value (where sameness is determined by the === operator).

9. What happens in the following javaScript code snippet?
var count = 0;
while (count < 10)
{
console.log(count);
count++;
}
a) The values of count are logged or stored in a particular location or storage
b) The value of count from 0 to 9 is displayed in the console
c) An error is displayed
d) An exception is thrown

Answer: b
Explanation: Console.log is a predefined function in JavaScript which takes the value as an argument of its function.console.log prints this value in the argument in the console at the time of execution of the code.

10. The enumeration order becomes implementation dependent and non-interoperable if ___________
a) If the object inherits enumerable properties
b) The object does not have the properties present in the integer array indices
c) The delete keyword is never used
d) Object.defineProperty() is not used

Answer: a
Explanation: In computer programming, an enumerated type (also called enumeration or enum) is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language.