Javascript Multiple Choice Questions on “Statements”.
1. JavaScript is a _______________ language.
a) Object-Oriented
b) High-level
c) Assembly-language
d) Object-Based
Answer: d
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
