Javatpoint Logo
Javatpoint Logo

JavaScript MCQ

This set of questions focuses on operators and expression in JavaScript

1) Which type of javascript language is ___

  1. Object-Oriented
  2. Object-Based
  3. Assembly-language
  4. High-level

Answer: B

Explanation: JavaScript is not a pure OOP (object = oriented programming) based languages such as PHP or many other languages such as Java, although it is an object-based language. it is not OOP's based language, because it doesn't have three basic properties of object-oriented programming languages, such as polymorphism, encapsulation, and inheritance.

2) Which of the following is the correct output for the following JavaScript code:

  1. 1
  2. Error
  3. 10
  4. 5

Answer: A

Explanation: The output of the above snippet code will be 1 because the first interpreter searches "obj" for the property (y). But it fails to find "obj" for property "y", so it chooses a value from outside the object, which is available within the given code.

3) Which one of the following also known as Conditional Expression:

  1. Alternative to if-else
  2. Switch statement
  3. If-then-else statement
  4. immediate if

Answer: D

Explanation: A conditional expression can only evaluate two things, which either true or false, that are purely based on the evaluation of the condition

4) Among the following given JavaScript snipped codes, which is more efficient:

Code A

Code B

  1. Code 1
  2. Code 2
  3. Both Code 1 and Code 2
  4. Cannot Compare

Answer: A

Explanation: Code 1 will be more efficient. In fact, the second code may encounter a runtime error because the value of "number" is never going to be equal to or less than one.

5) In JavaScript, what is a block of statement?

  1. Conditional block
  2. block that combines a number of statements into a single compound statement
  3. both conditional block and a single statement
  4. block that contains a single statement

Answer: B

Explanation: A block of statement can be understood as the set of the zero or more statements. In general, a block of statement has common definition "which combines one or a number of statements into a single statement for ease.

6) When interpreter encounters an empty statements, what it will do:

  1. Shows a warning
  2. Prompts to complete the statement
  3. Throws an error
  4. Ignores the statements

Answer: D

Explanation: In JavaScript, when the interpreter encounters an empty statement it normally ignores or does not provide response to that empty statement. The empty statements also sometimes very useful like we use the empty statements for creating loop for nothing.

7) The "function" and " var" are known as:

  1. Keywords
  2. Data types
  3. Declaration statements
  4. Prototypes

Answer: C

Explanation: The "function" and "var" both are the Declaration statements. These both are used for defining, and declaring variable, function in anywhere in the program.

8) In the following given syntax of the switch statement, the Expression is compared with the labels using which one of the following operators?

  1. ===
  2. equals
  3. ==
  4. equals

Answer: A

Explanation: The strict comparison operator returns true only if the operand is of the same type and content matches. When the switch statement is executed, the value of the expression is calculated and compared to the case labels, and looks for a case whose expressions produce the same value after evaluations (where the comparison is determined by the === operator).

9) What will happen, if the following JavaScript code is executed?

  1. An error is displayed
  2. An exception is thrown
  3. The values of count variable are logged or stored in a particular location or storage
  4. The value of count from 0 to 9 is displayed in the console

Answer: C

Explanation: The function "console.log ()" used in the above function is one of the pre-defined functions of JavaScript. It takes values as arguments passed to it, and displays that value in arguments inside the console when the code is executed.

10) Which of the following is the correct output for the following JavaScript code:

  1. 9
  2. 0
  3. 8
  4. Undefined

Answer: C

Explanation: The "if-else" is one of the conditional statements available in JavaScript like several other languages. Here the comparison performed in the "if" statement evaluates to false, so the instructions written in the else part gets executed. If the comparison performed in the "if" statement evaluates to true, then the instruction written in the if statement will be executed.

11) Which of the following is the correct output for the following JavaScript code:

  1. 10
  2. 9
  3. 8
  4. 0

Answer: C

Explanation: The code of the given program uses a switch statement, in which the value of the expression is compared with the available case labels. If the value matches with any case label, the code written corresponding to that case is executed otherwise the instruction written to the default is executed. Another important point is that switch statements are also used as an alternative to "if-else" statements to reduce the complexity and size of the code.

12) Which of the following is the correct output for the following JavaScript code:

  1. 10
  2. 6
  3. 33
  4. 0

Answer: B

Explanation: If we look at the given code carefully, we can see that here the "break" statement is not used after any of the case labels. Which means all the cases following "A" get executed if the following program is executed.

13) Which of the following is the correct output for the following JavaScript code:

  1. 3
  2. 0
  3. Error
  4. 2

Answer: B

Explanation: The "if-and if" statement is used to examine more than one condition. This is an extension of the "if-else" statement and is also known as the "if-else ladder". We can extend the "if-else" statement to check several conditions.

14) Which of the following is the correct output for the following JavaScript code:

  1. 10
  2. 17
  3. 18
  4. 0

Answer: D

Explanation: The switch case statement contains several cases in which the Default case is also one of them. The default case only is get executed, when no other case matches with the expression's value.

Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA