This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Statements”.
1. JavaScript is a _______________ language.
a) Object-Oriented
b) High-level
c) Assembly-language
d) Object-Based
View Answer
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
View Answer
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
View Answer
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
View Answer
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
View Answer
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
View Answer
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
View Answer
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) ===
View Answer
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
View Answer
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
View Answer
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.
11. What will be the output of the following JavaScript code?
let a = 1; if (a > 10) { document.write(10); } else { document.write(a > 10); }
a) 10
b) 0
c) 1
d) false
View Answer
Explanation: The code checks if the value of variable a is greater than 10. Since a is assigned the value 1, the condition a > 10 evaluates to false. In the else block, document.write(a > 10) is executed. The expression a > 10 evaluates to false because 1 is not greater than 10. So, the document.write(a > 10) statement will write false to the document. Therefore, the output will be false.
12. What will be the output of the following JavaScript code?
var B = 65; // ASCII value of A var grade = B; var result = 0; // Initialize result here switch(grade) { case 'A': { result += 10; break; } case 'B': { result += 9; break; } case 'C': { result += 8; break; } default: result += 0; } document.write(result);
a) 74
b) 75
c) 73
d) 0
View Answer
Explanation: Result is 0 because the grade variable is assigned the value 65, which is the ASCII value of the character ‘A’. However, in the switch statement, the cases are comparing against string literals ‘A’, ‘B’, and ‘C’. Since none of the cases match the value of grade, the default case is executed, resulting in result being incremented by 0.
13. What will be the output of the following JavaScript code?
var grade='A'; var result=0; switch(grade) { case 'A': result+=10; case 'B': result+=9; case 'C': result+=8; default: result+=0; } document.write(result);
a) 10
b) 27
c) 8
d) 0
View Answer
Explanation: The above code does not have a break statement after the cases in the switch statement. Therefore, all of the cases following ‘A’ will get executed. The result will be 10 + 9 + 8 + 0 = 27, and the output will be 27.
14. What will be the output of the following JavaScript code?
var a = 4; var b = 1; var c = 0; if (a === b) document.write(a); else if (a === c) document.write(a); else document.write(c);
a) 4
b) 1
c) Error
d) 0
View Answer
Explanation: Given the values a = 4, b = 1, and c = 0, the first condition (a === b) evaluates to false, the second condition (a === c) also evaluates to false, so it will write the value of c, which is 0. Therefore, the output will be 0.
15. What will be the output of the following JavaScript code?
var grade='C'; var result = ''; switch(grade) { case 'A': result+=" 10"; case 'B': result+=" 9"; case 'C': result+=" 8"; default: result+=" 0"; } document.write(result);
a) 8
b) 9
c) 10
d) None of the mentioned
View Answer
Explanation: The answer will be “8 0″. When grade is ‘C’, the ‘C’ case block is executed, but then the execution continues to subsequent case blocks (‘default’), appending ” 0″ to the result. This behavior occurs due to the absence of break statements after each case block, causing fall-through to the subsequent cases.
Sanfoundry Global Education & Learning Series – Javascript Programming.
- Check JavaScript Books
- Practice MCA MCQs
- Check Programming Books
- Practice Programming MCQs
- Practice Information Science MCQs