javascript-mcqs - Set (1)

Correct Answer: Orange && Worng Answer: Red

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

	Int a = 1;
	if (a > 10) {
		document.write(10);
	} else {
		document.write(a);
	}
  • (A). 0
  • (B). 1
  • (C). 10
  • (D). Undeinified
  • View Answer

    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.

    2. What kind of expression is “new Point(2,3)”?

  • (A). Constructor Calling Expression
  • (B). Primary Expression
  • (C). Invocation Expression
  • (D). Object Creation Expression
  • View Answer

    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.

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

    	x = ~-y;
    	w = x = y = z;
    	q = a?b:c?d:e?f:g; 
  • (A). x = (x = (y = z));w = ~(-y); q = a?b:(c?d:(e?f:g));
  • (B). x = ~(-y); w = (x = (y = z)); q = (c?d:(e?f:g));
  • (C). x = a?b:(c?d:(e?f:g)); q = ~(-y); w = (x = (y = z));
  • (D). x = ~(-y); w = (x = (y = z)); q = a?b:(c?d:(e?f:g));
  • View Answer

    Explanation: Brackets have higher precedence than any other operator. The placement of brackets results in the same result as without putting any bracket.

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

    	const obj = {prop: 12};
    	Object.preventExtensions(obj);
    	console.log( Object.isExtensible(obj));
  • (A). error
  • (B). true
  • (C). 12
  • (D). false
  • View Answer

    Explanation: The Object.preventExtensions() only prevents the addition of new properties from ever being added to an object. This change is a permanent that means once an object has been made non-extensible, it cannot be made extensible again.

    5. What happens in the following javaScript code snippet?

    	var count = 0;
    	while (count < 10)
    	{
    		console.log(count);
    		count++;
    	}
  • (A). he value of count from 0 to 9 is displayed in the console
  • (B). An error is displayed
  • (C). The values of count are logged or stored in a particular location or storage
  • (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.

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

    	function compare() {
    		int a = 1;
    		char b = 1;
    		if(a.tostring() === b) return true;
    		else return false;
    	}
  • (A). runtime error
  • (B). true
  • (C). logical error
  • (D). false
  • View Answer

    Explanation: A non string (integer) can be converted to string using .tostring() function. A strict comparison is only true if the operands are of the same type and the contents match. Hence the following code snippet would result into true output.

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

    	int a = 1;
    	if(a != null) return 1;
    	else return 0;
  • (A). 1
  • (B). runtime error
  • (C). 0
  • (D). compiler error
  • View Answer

    Explanation: != is the not equal to operator. It gives a value of 1 if the two values which are compared are not equal and give 0 if the two values are equal.

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

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

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

    	int a=0;
    	for(a;a<5;a++);
    	console.log(a);
  • (A). 5
  • (B). error
  • (C). 0
  • (D). 4
  • View Answer

    Explanation: The value of a will increase until it will become equal to 5 after that the cursor will come out the loop. There are no statements for the for loop therefore only the value of a will increase. Hence the output will be five.

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

    	var a1 = [,,,]; 
    	var a2 = new Array(3); 
    	0 in a1 
    	0 in a2
  • (A). false true
  • (B). true true
  • (C). false true
  • (D). true false
  • View Answer

    Explanation: Array a1 is defined with null values. Therefore we can access the indexes 0, 1 and 2. But array a2 is only defined not declared. Therefore we cannot access index 0.