0% found this document useful (0 votes)
3 views

javascript_mcq

The document provides a series of questions and answers related to JavaScript programming concepts, including its characteristics, syntax, and behavior. It covers topics such as object-oriented programming, script placement, data types, and error handling. Each question is followed by an explanation of the correct answer, enhancing understanding of JavaScript fundamentals.

Uploaded by

vips.stud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

javascript_mcq

The document provides a series of questions and answers related to JavaScript programming concepts, including its characteristics, syntax, and behavior. It covers topics such as object-oriented programming, script placement, data types, and error handling. Each question is followed by an explanation of the correct answer, enhancing understanding of JavaScript fundamentals.

Uploaded by

vips.stud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Which type of JavaScript language is ___

a. Object-Oriented

b. Object-Based

c. Assembly-language

d. High-level

Answer: B

Explanation: JavaScript is not a pure OOP's (object oriented


programming) based languages such as PHP, java or many other
languages, although it is an object-based language. It is not OOP's based
language, because it doesn't have three basic properties of object-
oriented programming languages, such as polymorphism, encapsulation,
and inheritance.

The script tag must be placed in __________


a) the head tag
b) the head or body
c) the title or head
d) after the body tag

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.

A JavaScript program developed on a Unix Machine ________


a) will throw errors and exceptions
b) must be restricted to a Unix Machine only
c) will work perfectly well on a Windows Machine
d) will be displayed as a JavaScript text on the browser

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.

Which attribute is used to specify that the script is executed


when the page has finished parsing? (only for external scripts)
a) parse
b) a sync
c) defer
d) type

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.

JavaScript Code can be called by using ____________


a) RMI
b) Triggering Event
c) Preprocessor
d) Function/Method

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.

JavaScript can be written __________


a) directly into JS file and included into HTML
b) directly on the server page
c) directly into HTML pages
d) directly into the css file

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.

Which of the following Attribute is used to include External JS


code inside your HTML Document?
a) src
b) ext
c) script
d) link

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.

A proper scripting language is a __________


a) High level programming language
b) Assembly level programming language
c) Machine level programming language
d) Low level programming language

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.

The web development environment (JavaScript) offers which


standard construct for data validation of the input entered by the
user.
a) Controlled loop constructs
b) Server page access
c) Client side Event
d) Permit server-side

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.

The main purpose of a “Live Wire” in NetScape is to ________


a) Create linkage between client side and server side
b) Permit server side, JavaScript code, to connect to RDBMS
c) Support only non relational database
d) To interpret JavaScript code

Answer: b
Explanation: A Live Wire database driver also supports a number of
non-relational databases.

What will be the output of the following JavaScript code?

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

What will be the output of the following JavaScript code?


<p id="demo"></p>
<script>
var x = 5;
var y = 2;
var z = x % y;
document.getElementById("demo").innerHTML = z;
</script>

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.

What will be the output of the following JavaScript code?

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

What will be the output of the following JavaScript code?

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

What will be the output of the following JavaScript code?

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

The type of a variable that is volatile is _______________


a) Volatile variable
b) Mutable variable
c) Immutable variable
d) Dynamic variable
Answer: b
Explanation: The variables whose values can be changed are called
mutable variable types. In JavaScript, only objects and arrays are
mutable, not primitive values.

A hexadecimal literal begins with __________


a) 00
b) 0x
c) 0X
d) Both 0x and 0X

Answer: d
Explanation: Generally, X or x denotes hexadecimal values. So, any
integer literal that begins with 0X or 0x denotes a hexadecimal number.

The generalised syntax for a real number representation is


__________
a) [digits][.digits][(E|e)[(+|-)]digits]
b) [digits][+digits][(E|e)[(+|-)]digits]
c) [digits][(E|e)[(+|-)]digits]
d) [.digits][digits][(E|e)[(+|-)]digits]

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.

JavaScript _________ when there is an indefinite or an infinite value


during an arithmetic computation.
a) Prints an exception error
b) Prints an overflow error
c) Displays “Infinity”
d) Prints the value as such

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

Which of the following is not considered as an error in JavaScript?


a) Syntax error
b) Missing of semicolons
c) Division by zero
d) Missing of Bracket

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.

The escape sequence ‘\f’ stands for _________


a) Floating numbers
b) Representation of functions that returns a value
c) \f is not present in JavaScript
d) Form feed

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

The snippet that has to be used to check if “a” is not equal to


“null” is _________
a) if(a!=null)
b) if (!a)
c) if(a!null)
d) if(a!==null)
Answer: d
Explanation: A strict comparison (e.g., ===) is only true if the
operands are of the same type and the contents match. The more
commonly-used abstract comparison (e.g. ==) converts the operands to
the same type before making the comparison. The not-equal operator !
== compares 0 to null and evaluates to either true or false.

The statement a===b refers to _________


a) Both a and b are equal in value, type and reference address
b) Both a and b are equal in value
c) Both a and b are equal in value and type
d) There is no such statement

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.

Assume that we have to convert “false” that is a non-string to


string. The command that we use is (without invoking the “new”
operator).
a) false.toString()
b) String(false)
c) String newvariable=”false”
d) Both false.toString() and String(false)

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

What will be the output of the following JavaScript code?

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.

What will be the output of the following JavaScript code?

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.

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

What will be the output of the following JavaScript code?

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.

What will be the output of the following JavaScript code?


int a=1;
if(a!=null)
return 1;
else
return 0;

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.

What will be the output of the following JavaScript code?

var string1 = ”123”;


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

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.

A function definition expression can be called as __________


a) Function prototype
b) Function literal
c) Function calling
d) Function declaration
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.

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

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.

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)

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

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

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.

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


a) Primary Expression
b) Object Creation Expression
c) Invocation Expression
d) Constructor Calling Expression

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.

Which of the operator is used to test if a particular property


exists or not?
a) in
b) exist
c) within
d) exists

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.

Among the following, which one is a ternary operator?


a) +
b) :
c) –
d) ?:

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

“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

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.

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 = ~(-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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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

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

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

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

You might also like