JavaScript Questions & Answers – Expressions and Operators

«
»

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Expressions and Operators”.

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

advertisement
var string1 =123;
var intvalue = 123;
alert( string1 + intvalue );

a) 123246
b) 246
c) 123123
d) Exception
View Answer

Answer: c
Explanation: In JavaScript the alert function does the type casting and converts the integer value to string. After that it concatenates both the strings and shows the result as a concatenated string. Thus 123123 would be correct.

2. A function definition expression can be called as __________
a) Function prototype
b) Function literal
c) Function calling
d) Function declaration
View Answer

Answer: b
Explanation: A function definition expression is a “function literal” in the same way that an object initializer is an “object literal.” A Function definition expression typically consists of the keyword function followed by a comma-separated list of zero or more identifiers (the parameter names) in parentheses and a block of JavaScript code (the function body) in curly braces.

3. The property of a primary expression is ____________
a) stand-alone expressions
b) basic expressions containing all necessary functions
c) contains variable references alone
d) contains only keywords
View Answer

Answer: a
Explanation: The simplest expressions, known as primary expressions, are those that stand alone — they do not include any simpler expressions. Primary expressions in JavaScript are constant or literal values, certain language keywords, and variable references.

4. Consider the following JavaScript statements.

var text = "testing: 1, 2, 3"; // Sample text
var pattern = /\d+/g // Matches all instances of one or more digits

In order to check if the pattern matches with the string “text”, the statement is ____________
a) text==pattern
b) text.equals(pattern)
c) text.test(pattern)
d) pattern.test(text)
View Answer

Answer: d
Explanation: The given pattern is applied on the text given in the parenthesis.
advertisement

5. The expression of calling (or executing) a function or method in JavaScript is called ________
a) Primary expression
b) Functional expression
c) Invocation expression
d) Property Access Expression
View Answer

Answer: c
Explanation: An invocation expression is JavaScript’s syntax for calling (or executing) a function or method). It starts with a function expression that identifies the function to be called.

6. What kind of expression is “new Point(2,3)”?
a) Primary Expression
b) Object Creation Expression
c) Invocation Expression
d) Constructor Calling Expression
View Answer

Answer: b
Explanation: An object creation expression creates a new object and invokes a function (called a constructor) to initialize the properties of that object. Object creation expressions are like invocation expressions except that they are prefixed with the keyword new.

7. Which of the operator is used to test if a particular property exists or not?
a) in
b) exist
c) within
d) exists
View Answer

Answer: a
Explanation: The operator “in” tests whether a particular property exists or not. In operator is usually added in looping statements to traverse the array or the object.

8. Among the following, which one is a ternary operator?
a) +
b) :
c) –
d) ?:
View Answer

Answer: d
Explanation: JavaScript supports one ternary operator, the conditional operator ?:, which combines three expressions into a single expression. If else case can be replaced by the conditional operator

9. “An expression that can legally appear on the left side of an assignment expression.” is a well known explanation for variables, properties of objects, and elements of arrays. They are called ___________
a) Properties
b) Prototypes
c) Lvalue
d) Definition
View Answer

Answer: c
Explanation: lvalue is a historical term that means “an expression that can legally appear on the left side of an assignment expression.” In JavaScript, variables, properties of objects, and elements of arrays are lvalues.

10. What will be the equivalent output of the following JavaScript code snippet?

advertisement
x = ~-y;
w = x = y = z;
q = a?b:c?d:e?f:g;

a)

x = ~(-y); w = (x = (y = z));
q = a?b:(c?d:(e?f:g));

b)

x = a?b:(c?d:(e?f:g));
q = ~(-y); w = (x = (y = z));

c)

x = (x = (y = z));w = ~(-y);
q = a?b:(c?d:(e?f:g));

d)

advertisement
x = ~(-y); w = (x = (y = z)); 
q = (c?d:(e?f:g));
View Answer
Answer: a
Explanation: Brackets have higher precedence than any other operator. The placement of brackets results in the same result as without putting any bracket.
 
 

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

function output(option)
{
	return (option ?  “yes” :  “no”);
}
	bool ans=true;
console.log(output(ans));

a) Yes
b) No
c) Runtime error
d) Compilation error
View Answer

Answer: a
Explanation: “?” is called the ternary operator which is used for choosing one choice from the given two choices. It is used instead of if else statement and makes the code shorter.

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

var  obj=
{
	length:20,
	height:35,
}
if (‘breadth' in obj === false) 
{
  	obj.breadth = 12;
}
 
console.log(obj.breadth);

a) 20
b) 12
c) undefined
d) error
View Answer

Answer: b
Explanation: The “in” operator checks for the presence of a particular property in object. It returns true if the property is present and returns false if the property is not present.

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

function height()
{	
    var  height = 123.56;
    var type = (height>=190) ? "tall" : "short";
    return type;
}

a) 123.56
b) 190
c) tall
d) short
View Answer

Answer: d
Explanation: The ternery operator is used as a comparison operator which works on three operands. The statement in the above code initializes type variable with the value short which is returned through the function.

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

string  a = ”hi”;
string  b =”there”;
alert(a+b);

a) hi
b) there
c) hithere
d) undefined
View Answer

Answer: c
Explanation: alert function is used to print the value passed as argument in a dialog box in a browser. The alert function adds both the string and prints the result as a combined string.

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

function output(object)
{
	var place=object ? object.place : “Italy”;
	return “clean:+ place;
}
console.log(output({place:India}));

a) clean:India
b) clean:Italy
c) error
d) undefined
View Answer

Answer: a
Explanation: ”?” operator is used to compare the values and place is initialized according to the true condition that whether it is true or false. The function is called in the console.log and the object value is passed.

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

<p id="demo"></p>
<script>
function myFunction() 
{
    document.getElementById("demo").innerHTML = Math.abs(-7.25);
}
</script>

a) 7.25
b) -7.25
c) 7
d) -7
View Answer

Answer: a
Explanation: The abs() method returns the absolute value of a number. The method is find in the math library of Javascript.

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

<p id="demo"></p>
<script>
function myFunction() 
{
    document.getElementById("demo").innerHTML = Math.cbrt(125);
}
</script>

a) 125
b) 25
c) 5
d) Error
View Answer

Answer: c
Explanation: cbrt return the cubic root of a number. The method is find in the math library of Javascript.

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

<p id="demo"></p>
<script>
function myFunction() 
{
    document.getElementById("demo").innerHTML = Math.acos(0.5);
}
</script>

a) 1.01
b) 1.047
c) 1.00
d) 1.4
View Answer

Answer: b
Explanation: The acos() method returns the arccosine of a number as a value value between 0 and PI radians. If the parameter x is outside the range -1 to 1, the method will return NaN.

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn