JavaScript MCQ

Q 1. Which type of JavaScript language is ___

A. Object-Based

B. Object-Oriented

C. Assembly-language

D. High-level

Show Answer Answer:-A. Object-Based
Explanation JavaScript is not a pure OOP’s (object oriented programming) based languages such as PHP, java or many other languages, 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.

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

varx=5,y=1  

var obj ={ x:10}  

with(obj)  

{  

      alert(y)  

}  

A. 1

B. Error

C. 10

D. 5

Show Answer Answer:-A. 1
Explanation The output of the above snippet code will be one because, first of all, the interpreter will search “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.

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

A. immediate if

B. Switch statement

C. If-then-else statement

D. Alternative to if-else

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

Q 4. Among the following given JavaScript snipped codes, which is more efficient:PlayNextMute

Current Time 0:05

/

Duration 18:10

Loaded: 4.04%

 FullscreenBackward Skip 10sPlay VideoForward Skip 10s

Code A

for(var number=10;number>=1;number–)  

{  

document.writeln(number);  

}  

Code B

var number=10;  

while(number>=1)  

{  

document.writeln(number);  

       number++;  

}  

A. Code 1

B. Code 2

C. Both Code 1 and Code 2

D. Cannot Compare

Show Answer Answer:-A. Code 1
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.

Q 5. In JavaScript, what is a block of statement?

A. Conditional block

B. block that contains a single statement

C. both conditional block and a single statement

D. block that combines a number of statements into a single compound statement

Show Answer Answer:-D. block that combines a number of statements into a single compound statement
Explanation A block of statement can be understand 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 .

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

A. Shows a warning

B. Prompts to complete the statement

C. Ignores the statements

D. Throws an error

Show Answer Answer:-C. Ignores the statements
Explanation In JavaScript, when the interpreter encounters a empty statements it normally ignores or not response to that empty statement. The empty statements also sometimes very useful like we use the empty statements for creating loop for nothing.

Q 7. The “function” and ” var” are known as:

A. Keywords

B. Data types

C. Declaration statements

D. Prototypes

Show Answer Answer:-C. Declaration statements
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.

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

switch(expression)  

{  

    statements  

}  

A. ==

B. equals

C. ===

D. equals

Show Answer Answer:-C. ===
Explanation The strict comparison operator returns true only if the operand are 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).

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

var count =0;  

while (count <10)  

{  

     console.log(count);  

     count++;  

}  

A. An error is displayed

B. An exception is thrown

C. The values of count variable are logged or stored in a particular location or storage

D. The value of count from 0 to 9 is displayed in the console

Show Answer Answer:-C. The values of count variable are logged or stored in a particular location or storage
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.

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

Int x=8;  

if(x>9)  

{  

document.write(9);  

}  

else  

{  

document.write(x);  

}  

A. 9

B. 0

C. 8

D. Undefined

Show Answer Answer:-C. 8
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.

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

var grade=’C’;  

var result;  

switch(grade)  

{  

case’A’:  

{  

        result+=”10″;  

break;  

}  

case’B’:  

{  

        result+=” 9″;  

break;  

}  

case’C’:  

{  

        result+=” 8″;  

break;  

}  

default:  

    result+=” 0″;  

}  

document.write(result);  

A. 10

B. 9

C. 8

D. 0

Show Answer Answer:-C. 8
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.

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

var grade=’D’;  

var result;  

switch(grade)  

{  

case’A’:  

        result+=”10″;  

case’B’:  

        result+=” 9″;  

case’C’:  

        result+=” 8″;  

case ‘D’  

result+=” 6″;  

default:  

        result+=” 0″;  

}  

document.write(result);  

A. 10

B. 6

C. 33

D. 0

Show Answer Answer:-B. 6
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 executes if the following program is executed.

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

var x=3;  

var y=2;  

var z=0;  

If(x==y)  

document.write(x);  

elseif(x==y)  

document.write(x);  

else  

document.write(z);  

A. 3

B. 0

C. Error

D. 2

Show Answer Answer:-B. 0
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.

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

var grade=’Z’;  

var result;  

switch(grade)  

{  

case’A’:  

        result+=”10″;  

case’B’:  

        result+=” 9″;  

case’C’:  

        result+=” 8″;  

default:  

        result+=” 0″;  

}  

document.write(result);  

A. 10

B. 17

C. 18

D. 0

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page