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

Top 40 Javascript Interview Questions and Answers

This document contains a summary of 40 multiple choice questions and answers related to JavaScript. The questions cover topics such as for loops, functions, classes, scopes, events, DOM manipulation, CSS selectors, images, canvas, SVG, handshakes, minification, and more. The answers provide explanations for each question.

Uploaded by

Ankur Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Top 40 Javascript Interview Questions and Answers

This document contains a summary of 40 multiple choice questions and answers related to JavaScript. The questions cover topics such as for loops, functions, classes, scopes, events, DOM manipulation, CSS selectors, images, canvas, SVG, handshakes, minification, and more. The answers provide explanations for each question.

Uploaded by

Ankur Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Placement handbook of

JAVASCRIPT
TOP 40 MCQ'S WITH ANSWERS

www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

1. What are the three important manipulations done in for loop on a loop variable?
a) Updation, Incrementation, Initialization
b) Initialization,Testing, Updation
c) Testing, Updation, Testing
d) Initialization,Testing, Incrementation

Explanation: In for loop, firstly the loop initializes the variable then test the condition and then
after executing the statement increments its value.

2. A function definition expression can be called as:


a) Function prototype
b) Function literal
c) Function calling
d) Function declaration

Explanation: A function definition expression is a “function 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 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

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.

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

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.

Page 1 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

5. The four kinds of class members are ________


a) Instance methods, Instance fields, Static method, Dynamic method
b) Instance fields, Instance methods, Class fields, Class methods
c) Instance fields, Non-instance fields, Dynamic methods, Global methods
d) Global methods, Local methods, Dynamic methods, Static methods

Explanation: A class mainly contains data members and associated member functions.
Therefore over all different kinds of class members are instance field, instance method, class
fields and class methods.

6. The scope of a function is also called as ________


a) Predefined function
b) Module function
c) Public function
d) Private function

Explanation: The scope of a function can be used as a private namespace for a module.
Therefore, the scope of a function is called a module function.

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

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.

8. The regular expression to match any one character not between the brackets is:
a) […]
b) [^]
c) [^…]
d) [\D]

Explanation: RegExp defines a special set of character that is used to do manipulation on strings

Page 2 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

and other variables. The [^…] character class is used to match or draw any one character not
between the brackets.

9. What are the events generated by the Node objects called?


a) Generators
b) Emitters
c) Dispatchers
d) High events

Explanation: There are two classes of events one is called event listener and the other is called
event emitter. Node objects that generate events (known as event emitters) define an on()
method for registering handlers.

10. Which of the following is an event emitter?


a) Once
b) Process
c) Listeners
d) On

Explanation: The process object is an event emitter. The Node defines other important globals
under the process namespaces that contain properties of that object like version, argv, env, pid,
getuid(), cwd(), chdir() and exit().

11. Which is the function used to retrieve a value?


a) get()
b) retrieve()
c) getItem()
d) retrieveItem()

Explanation: To retrieve a value, pass the name to getItem(). The getItem() method of the
Storage interface, when passed a key name, will return that key’s value, or null if the key does
not exist, in the given Storage object.

12. What is the method used to pause “data” events?


a) s.pause();
b) s.stop();
c) s.halt();
d) s.wait();

Page 3 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

Explanation: Data events are the events which are performed by either the user or the
browser.

13. Which is the handler method used to invoke when uncaught JavaScript exceptions
occur?
a) Onhalt
b) Onerror
c) Both onhalt and onerror
d) Onsuspend

Explanation: The onerror handler method can be registered to be invoked when uncaught
JavaScript exceptions occur. The onerror event is triggered if an error occurs while loading an
external file (e.g. a document or an image).

14. The paragraph “p” is a part of __________


a) h1
b) body
c) html
d) both body and html

Explanation: The paragraph tag belongs to both html and body tag. It is used to write
paragraph on html pages.

15. The URL property belongs to which of the following object?


a) Document
b) Element
c) Location
d) Event

Explanation: The Document object has a URL property, which is a static string that holds the
URL of the document when it was first loaded. If you want to access any element in an HTML
page, you always start with accessing the document object.

16. Which syntax is used to describe elements in CSS?


a) Protectors

Page 4 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

b) Selectors
c) Both Protectors and Selectors
d) Protectors or Selectors

Explanation: CSS style sheets have a very powerful syntax, known as selectors, for describing
elements or sets of elements within a document

17. Which HTML element is used to include images?


a) image
b) img
c) src
d) sourcing

Explanation: Web pages include images using the HTML img element. src tag is used to include
the image link.

18. When JavaScript is called obtrusive?


a) JavaScript code is medium sized
b) JavaScript code is small
c) JavaScript code is so large
d) JavaScript code is Very small

Explanation: When the amount of JavaScript code is so large that it effectively obscures the
HTML, we call JavaScript as obtrusive.

19. What is the purpose of the canvas element?


a) Creates drawing surface
b) Exposes powerful drawing API to client-side JavaScript
c) Creates drawing surface & Exposes powerful drawing API to client-side JavaScript
d) Creates a rectangular box

Explanation: The HTML canvas element is used to draw graphics, on the fly, via JavaScript. The
canvas element is only a container for graphics. The canvas element has no appearance of its
own but creates a drawing surface within the document and exposes a powerful drawing API to
client-side JavaScript.

Page 5 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

20. How does SVG describe complex shapes?


a) Path of lines
b) Path of curves
c) Path of lines and curves
d) Planes

Explanation: SVG stands for ‘Scalable Vector Graphics’ and it is used to define graphics for the
Web. SVG is mostly used for vector type diagrams like Two-dimensional graphs in an X, Y
coordinate system, Pie charts etc. SVG describes complex shapes as a “path” of lines and curves
that can be drawn or filled.

21. What does the handshake pass between the browser and the remote server?
a) Synchronize
b) Accept
c) Reject
d) Decline

Explanation: The handshake consists of a Synchronize, Synchronize-Acknowledge, and


Acknowledge message to be passed between the browser and the remote server.

22. How does the handshake help the server?


a) Acknowledge
b) Accept the attempt
c) Both Acknowledge and Accept the attempt
d) Asynchronize

Explanation: The handshake allows the server to acknowledge and accept the attempt. The
handshake consists of a Synchronize, Synchronize-Acknowledge, and Acknowledge message to
be passed between the browser and the remote server.

23. What is the purpose of minifying the JavaScript?


a) To streamline the visits
b) To save the visits
c) To save data
d) To increase the loading time

Page 6 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

Explanation: Minifying JavaScript results in decreasing the loading time and hence helps in
creating a better user experience.

24. YUI Compressor is analogous to ___________


a) Minify
b) JavaScript
c) Both Minify and JavaScript
d) CSS

Explanation: Just like Minify, YUI Compressor strips out all of the unnecessary characters from
your JavaScript, including spaces, line breaks, and comments.

25. Which of the following is possible to be referenced in external JavaScript?


a) CPP
b) Cs
c) PHP
d) Python

Explanation: PHP is a widely-used open source general-purpose scripting language that is


especially suited for web development and can be embedded into HTML. One of the lesser
known sides of external JavaScript is the ability to reference a PHP file instead of .js file.

26. In which format does JavaScript support external JavaScript?


a) .js
b) .php
c) .js/php
d) .jss

Explanation: JavaScript supports external JavaScript, in the form of .js file. This extension can
be captured by a number of applications including: Windows Script Host, Dreamweaver MX,
Notepad, Netscape Navigator, PavScrip, UltraEdit.

27. Which of the following functions are referenced internally?


a) setTimeout
b) setInterval

Page 7 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

c) both setTimeout and setInterval


d) clearInterval

Explanation: setTimeout(function, milliseconds) executes a function, after waiting a specified


number of milliseconds. Functions used in setTimeout/setInterval are referenced internally
and tracked until complete, then cleaned up.

28. Which of the following is a way to retain an object in memory?


a) Console Log
b) Closures
c) Destroy objects
d) Clear object

Explanation: Any object inside the timer will hold a reference in order to run that piece of code
somewhere in the future without any problems. One particularly obscure way to retain an
object in memory is to log it to the console.

29. What will happen if the browser encounters a script tag without src attribute?
a) Throws an error
b) Throws an exception
c) Sends it to the compiler
d) Sends it to the interpreter

Explanation: If the browser encounters a script tag without src attribute, the rendering engine
simply passes the code to the JavaScript Interpreter for execution. The src attribute specifies
the location (URL) of the external resource.

30. What should be the type of script_url?(30)


a) Object
b) String
c) Array
d) Any of the mentioned

Explanation: The type of script_url can be anything that will be compared with the typeof
keyword’s result. The src attribute specifies the location (URL) of the external resource.

31. You can refresh the webpage in JavaScript by using ___________

Page 8 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

a) window.reload
b) location.reload
c) window.refresh
d) page.refresh

Explanation: The reload() method is used to reload the current document. The reload() method
does the same as the reload button in your browser. One can refresh the webpage in JavaScript
by using location.reload.

32. How many constant values can the property type be represented?
a) 2
b) 3
c) 4
d) 5

Explanation: The type property sets or returns the value of the type attribute of an <object>
element. The type attribute specifies the Internet media type of the object. Totally 4 constant
values can be represented by the property type.

33. What is a heap in JavaScript?


a) Collection of Java objects
b) Collection of JavaScript objects
c) Collection of memory usage values
d) Collection of data stored in memory

Explanation: A heap is a tree-like data structure where each node must be ordered with respect to the
value of its children. The heap is the collection of JavaScript objects that the interpreter keeps in
resident memory.

34. What is the observation made in the following JavaScript code?


var count = [1,,3];
a) The omitted value takes “undefined”
b) This results in an error
c) This results in an exception
d) The omitted value takes an integer value

Page 9 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

Explanation: Array is defined with a null value when no value is mentioned. If you omit a value
from an array literal, the omitted element is given an undefined value.

35. Do functions in JavaScript necessarily return a value?


a) It is mandatory
b) Not necessary
c) Few functions return values by default
d) some functions do not return any value

Explanation: Functions generally have a return statement and hence usually returns a value.
Some functions which do not have a return statement returns value by default during
execution.

36. One of the main advantage of using src attribute is ____________


a) It becomes self-cached
b) It makes the HTML file modular
c) It restricts manipulation in the HTML file
d) It simplifies the HTML files

Explanation: The main advantage of using the src attribute is that it simplifies your HTML files
by allowing you to remove large blocks of JavaScript code from them. Hence separate files for
css and javascript files are made to make the code modular and readable.

37. What will be done if more than one page requires a file of JavaScript code?
a) Downloads that many times
b) Retrieves from the browser cache
c) Must be re executed
d) Must be included in all the pages

Explanation: If a file of JavaScript code is shared by more than one page, it only needs to be
downloaded once, by the first page that uses it—subsequent pages can retrieve it from the
browser cache. This makes the loading process easier and hence faster.

38. In which part does the form validation occur?

Page 10 of 11 www.mobiprep.com
TOP 40 JAVASCRIPT MCQ’s WITH ANSWERS

a) Client
b) Server
c) Both Client and Server
d) User side

Explanation: The data information from the client side is first sent to the server side. Form
validation used to occur at the server after the client had entered all necessary data and then
pressed the Submit button.

39. How is everything treated in HTML DOM?


a) Node
b) Attributes
c) Elements
d) Arrays

Explanation: In the HTML DOM (Document Object Model), everything is a node:


● The document itself is a document node.
● All HTML elements are element nodes.
● All HTML attributes are attribute nodes.
● Texts inside HTML elements are text nodes.
● Comments are comment nodes.

40. What is the purpose of data format validation?


a) Data correctness
b) Mere data existence
c) Both Data correctness and Mere data existence
d) Data modification

Explanation: The data entered through the server side is used for validation. The data that is
entered must be checked for correct form and value. This would need to put more logic to test
the correctness of data.

Page 11 of 11 www.mobiprep.com

You might also like