javascript_mcq
javascript_mcq
a. Object-Oriented
b. Object-Based
c. Assembly-language
d. High-level
Answer: B
Answer: b
Explanation: If the script tag is placed after the body tag, then, it will
not be evaluated at all. Also, it is always recommended and effective to
use the script snippet in the <head> tag.
Answer: c
Explanation: JavaScript can be executed on different operating systems
therefore the program developed on UNIX will work perfectly fine on
windows also.
JavaScript is ideal to ________
a) make computations in HTML simpler
b) minimize storage requirements on the web server
c) increase the download time for the client
d) increase the loading time of the website
Answer: b
Explanation: JavaScript helps in performing various tasks with minimum
storage requirements. Therefore to minimize storage requirements,
JavaScript is always a better say.
Answer: c
Explanation: The defer attribute is a Boolean attribute. When present, it
specifies that the script is executed when the page has finished parsing.
Answer: d
Explanation: JavaScript code can be called by making a function call to
the element on which JavaScript has to be run. There are many other
methods like onclick, onload, and onsubmit etc.
Answer: a
Explanation: JavaScript files can be saved by .JS extension and can be
included in the HTML files. Script tag along with src attribute is used to
include the js files.
Answer: a
Explanation: Script “tag” is used to include the JavaScript code. To
include external JavaScript files “src” attribute is used inside the script
tag.
Answer: a
Explanation: JavaScript is a high-level programming language that is
interpreted by another program at runtime rather than compiled by the
computer’s processor. Scripting languages, which can be embedded
within HTML, commonly are used to add functionality to a Web page, such
as different menu styles or graphics displays or to serve dynamic
advertisements.
Answer: a
Explanation: JavaScript provides with for, while loops and if, else,
switch cases for checking the information entered by the user.
Additionally, all development environments provide syntax to create and
use memory variables, constants, and functions.
Answer: b
Explanation: A Live Wire database driver also supports a number of
non-relational databases.
<p id="demo"></p>
var txt1 = "good";
var txt2 = "day";
document.getElementById("demo").innerHTML = txt1 + txt2;
a) good day
b) goodday
c) error
d) undefined
Answer: b
Explanation: The + operator acts as a concatenation operator when
used with string. The new string does not have any space between the
two added string.
a) 0
b) 1
c) 2
d) 5
Answer: b
Explanation: The % operator returns the remainder between the two
numbers. It is used many times with if condition to check whether the
number is divisible or not.
<p id="demo"></p>
<script>
var x = 10;
x *= 5;
document.getElementById("demo").innerHTML = x;
</script>
a) 5
b) 10
c) 50
d) Error
Answer: c
Explanation: The *= operator is a shorthand expression for
multiplication of a particular number. It is a combination of two operators
* and = .
<p id="demo"></p>
<script>
txt1 = “ one”;
txt1 += “two”;
document.getElementById("demo").innerHTML = txt1;
</script>
a) onetwo
b) one two
c) error
d) undefined
Answer: a
Explanation: The += operator acts in the same way as the
concatenation operator in the string. There is no space added when two
string are added together with += operator.
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = typeof "John"
</script>
a) integer
b) number
c) string
d) error
Answer: c
Explanation: The typeof operator returns the type of the argument
passed to it. The typeof operator returns number for an integer and string
for a character array.
Answer: d
Explanation: Generally, X or x denotes hexadecimal values. So, any
integer literal that begins with 0X or 0x denotes a hexadecimal number.
Answer: a
Explanation: Floating-point literals may also be represented using
exponential notation: a real number followed by the letter e (or E),
followed by an optional plus or minus sign, followed by an integer
exponent. This notation represents the real number multiplied by 10 to
the power of the exponent.
Answer: c
Explanation: When the result of a numeric operation is larger than the
largest representable number (overflow), JavaScript prints the value as
Infinity. Similarly, when a negative value becomes larger than the largest
representable negative number, the result is negative infinity. The infinite
values behave as you would expect: adding, subtracting, multiplying, or
dividing them by anything results in an infinite value (possibly with the
sign reversed).
Answer: c
Explanation: Division by zero is not an error in JavaScript: it simply
returns infinity or negative infinity. There is one exception, however: zero
divided by zero does not have a well defined value, and the result of this
operation is the special not-a-number value, printed as NaN.
Answer: d
Explanation: \f is the JavaScript escape sequence that stands for Form
feed (\u000C). It skips to the start of the next page. (Applies mostly to
terminals where the output device is a printer rather than a VDU).
Answer: c
Explanation: ”===” operator is known as the strict comparison
operator. A strict comparison (===) is only true if the operands are of the
same type and the contents match.
Answer: d
Explanation: The three approaches for converting to string are:
value.toString(),”” + value and String(value). A non-string can be
converted in two ways without using a new operator false.toString () and
String(false).
function compare()
{
int num=2;
char b=2;
if(a==b)
return true;
else
return false;
}
a) true
b) false
c) runtime error
d) compilation error
Answer: a
Explanation: The == convert different type of operands to the same
type before making the comparison. A strict comparison results into true
value if the operands are of the same type and the contents match.
function equalto()
{
let num=10;
if(num===”10”)
return true;
else
return false;
}
a) true
b) false
c) runtime error
d) compilation error
Answer: b
Explanation: A === operator is only true if the operands are of the
same type and the contents match. Two strings are strictly equal when
they have the same sequence of characters, same length, and same
characters in corresponding positions.
a) true
b) false
c) runtime error
d) logical error
Answer: a
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.
int a==2;
int b=4;
int ans=a+b;
print(ans);
a) 2
b) 6
c) 0
d) error
Answer: d
Explanation: The following code will generate into an error output as a
comparator operator is used outside of if statement. A single equalto (’=’)
operator is used for initialization.
a) 1
b) 0
c) runtime error
d) compiler error
Answer: a
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.
a) 123246
b) 246
c) 123123
d) Exception
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.
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.
Answer: d
Explanation: The given pattern is applied on the text given in the
parenthesis.
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.
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.
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.
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
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.
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)
x = ~(-y); w = (x = (y = z));
q = (c?d:(e?f:g));
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.
function output(option)
{
return (option ? “yes” : “no”);
}
bool ans=true;
console.log(output(ans));
a) Yes
b) No
c) Runtime error
d) Compilation error
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.
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
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.
function height()
{
var height = 123.56;
var type = (height>=190) ? "tall" : "short";
return type;
}
a) 123.56
b) 190
c) tall
d) short
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.
string a = ”hi”;
string b =”there”;
alert(a+b);
a) hi
b) there
c) hithere
d) undefined
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.
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
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.
<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
Answer: a
Explanation: The abs() method returns the absolute value of a number.
The method is find in the math library of Javascript.
<p id="demo"></p>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML = Math.cbrt(125);
}
</script>
a) 125
b) 25
c) 5
d) Error
Answer: c
Explanation: cbrt return the cubic root of a number. The method is find
in the math library of Javascript.
<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
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.