JavaScript Questions & Answers – Statements

«
»

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

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
View Answer

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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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

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 :

advertisement
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

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.
advertisement

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

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
View Answer

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
View Answer

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) ===
View Answer

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
View Answer

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
View Answer

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.

11. What will be the output of the following JavaScript code?

Int a=1;
if(a>10)
{  
    document.write(10);  
} 
else
{
    document.write(a);  
}

a) 10
b) 0
c) 1
d) Undefined
View Answer

Answer: c
Explanation: The if else statement is a part of the javascript conditioning statements. The line of code inside the “if” statement is executed if the value passed to “if” is 1.

12. What will be the output of the following JavaScript code?

var grade='B';  
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
View Answer

Answer: b
Explanation: The above code contains a switch loop. It is used as an alternative to if else statement. One of the given condition is satisfied according to the input of the switch case and the output is decided accordingly.

13. What will be the output of the following JavaScript code?

var grade='A';  
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) 27
c) 8
d) 0
View Answer

Answer: b
Explanation: The above code does not have a break statement after the cases in the switch loop. Therefore all of the cases following “A” will get executed.

14. What will be the output of the following JavaScript code?

int a=4;
int b=1;
int 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

Answer: d
Explanation: For checking more than one condition the if else if loop is used. It is the extension of if else loop which is also sometimes known as if else ladder.

15. What will be the output of the following JavaScript code?

var grade='E';  
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) 0
c) 18
d) 17
View Answer

Answer: b
Explanation: The switch case contains a default case along with the other cases. The default case gets executed when no other case results into true.

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.