JavaScript Questions and Answers Part-10

1. What will be the output of the following JavaScript code?
var arr = [7, 5, 9, 1];
var min = Math.min.apply(null, arr);
document.writeln(min);
a) 7
b) 5
c) 1
d) 9

Answer: c
Explanation: The function apply() is used to call a function that contains “this” value and the argument contains elements of an array. Unlike call() method, it contains a single array of arguments.

2. What will be the output of the following JavaScript code?
var add=new Function("num1","num2","return num1+num2");
document.writeln(add(2,5));
a) 2
b) 5
c) error
d) 7

Answer: d
Explanation: add function is defined in the first line of the code using the new property. Add function returns the sum of the two arguments passed to it.

3. What will be the output of the following JavaScript code?
var a=3.7;
var b=2;
a=ciel(a)
document.writeIn(a*b);
a) 6
b) 7.4
c) 7.5
d) 8

Answer: d
Explanation: The ciel function in javascript round offs the value passed in its argument to the next higher closest value. Therefore the value of a will be converted to 4 and hence the output will be 8.

4. What will be the output of the following JavaScript code?
var a=2.99;
var ans=floor(a)*floor(a)
console.log(ans);
a) 9
b) 8.31
c) Error
d) 4

Answer: d
Explanation: The floor method round offs the value of the decimal number to the lower limit. Therefore floor of a will be 2 which will result in the output of 4.

5. What will be the output of the following JavaScript code?
var a=225;
document.writeln(Math.sqrt(a));
a) 225
b) 15
c) error
d) undefined

Answer: b
Explanation: The JavaScript math sqrt() method returns the square root of a number. If the provided number is negative, it returns NaN.

6. The behaviour of the instances present of a class inside a method is defined by __________
a) Method
b) Classes
c) Interfaces
d) Classes and Interfaces

Answer: b
Explanation: Objects of the class are also known as instances of the class. The behaviour of the instance of a class is defined by the class and is shared by all instances.

7. The keyword or the property that you use to refer to an object through which they were invoked is _________
a) from
b) to
c) this
d) object

Answer: c
Explanation: ‘this’ keyword is used to refer to the object through which the properties or methods were invoked. This use of ‘this’ is a fundamental characteristic of the methods of any class.

8. What will be the output of the following JavaScript code?
var o = new F();
o.constructor === F
a) false
b) true
c) 0
d) 1

Answer: b
Explanation: Constructor is a function property of the class which is used to create objects of that class. In the above code, both statements create an instance of the class.

9. The basic difference between JavaScript and Java is _________
a) There is no difference
b) Functions are considered as fields
c) Variables are specific
d) Functions are values, and there is no hard distinction between methods and fields

Answer: d
Explanation: Java is an OOP programming language while JavaScript is an OOP scripting language. The basic difference between JavaScript and Java is that the functions are values, and there is no hard distinction between methods and fields.

10. The meaning for Augmenting classes is that ___________
a) objects inherit prototype properties even in a dynamic state
b) objects inherit prototype properties only in a dynamic state
c) objects inherit prototype properties in the static state
d) object doesn’t inherit prototype properties in the static state

Answer: a
Explanation: JavaScript’s prototype-based inheritance mechanism is dynamic an object inherits properties from its prototype, even if the prototype changes after the object is created. This means that we can augment JavaScript classes simply by adding new methods to their prototype objects.