50 JavaScript & AJAX Interview Questions
50 JavaScript & AJAX Interview Questions
questions
By admin | January 10, 2007
1. Why so JavaScript and Java have similar name?
A. JavaScript is a stripped-down version of Java
B. JavaScript's syntax is loosely based on Java's
C. They both originated on the island of Java
D. None of the above
2. When a user views a page containing a JavaScript program, which machine actually
executes the script?
A. The User's machine running a Web browser
B. The Web server
C. A central machine deep within Netscape's corporate offices
D. None of the above
6. _____ JavaScript statements embedded in an HTML page can respond to user events
such as mouse-clicks, form input, and page navigation.
A. Client-side
B. Server-side
C. Local
D. Native
11. ______ tag is an extension to HTML that can enclose any number of JavaScript
statements.
A. <SCRIPT>
B. <BODY>
C. <HEAD>
D. <TITLE>
13. Which of the following attribute can hold the JavaScript version?
A. LANGUAGE
B. SCRIPT
C. VERSION
D. None of the above
14. What is the correct JavaScript syntax to write "Hello World"?
A. System.out.println("Hello World")
B. println ("Hello World")
C. document.write("Hello World")
D. response.write("Hello World")
15. Which of the following way can be used to indicate the LANGUAGE attribute?
A. <LANGUAGE="JavaScriptVersion">
B. <SCRIPT LANGUAGE="JavaScriptVersion">
C. <SCRIPT LANGUAGE="JavaScriptVersion"> JavaScript
statements…</SCRIPT>
D. <SCRIPT LANGUAGE="JavaScriptVersion"!> JavaScript statements…
</SCRIPT>
17. What is the correct syntax for referring to an external script called " abc.js"?
A. <script href=" abc.js">
B. <script name=" abc.js">
C. <script src=" abc.js">
D. None of the above
19. Which of the following navigator object properties is the same in both Netscape and
IE?
A. navigator.appCodeName
B. navigator.appName
C. navigator.appVersion
D. None of the above
22. If para1 is the DOM object for a paragraph, what is the correct syntax to change the
text within the paragraph?
A. "New Text"?
B. para1.value="New Text";
C. para1.firstChild.nodeValue= "New Text";
D. para1.nodeValue="New Text";
23. JavaScript entities start with _______ and end with _________.
A. Semicolon, colon
B. Semicolon, Ampersand
C. Ampersand, colon
D. Ampersand, semicolon
28. ______method evaluates a string of JavaScript code in the context of the specified
object.
A. Eval
B. ParseInt
C. ParseFloat
D. Efloat
29. Which of the following event fires when the form element loses the focus: <button>,
<input>, <label>, <select>, <textarea>?
A. onfocus
B. onblur
C. onclick
D. ondblclick
32. Using _______ statement is how you test for a specific condition.
A. Select
B. If
C. Switch
D. For
35. The _______ method of an Array object adds and/or removes elements from an
array.
A. Reverse
B. Shift
C. Slice
D. Splice
36. To set up the window to capture all Click events, we use which of the following
statement?
A. window.captureEvents(Event.CLICK);
B. window.handleEvents (Event.CLICK);
C. window.routeEvents(Event.CLICK );
D. window.raiseEvents(Event.CLICK );
39. To enable data tainting, the end user sets the _________ environment variable.
A. ENABLE_TAINT
B. MS_ENABLE_TAINT
C. NS_ENABLE_TAINT
D. ENABLE_TAINT_NS
40. In JavaScript, _________ is an object of the target language data type that encloses
an object of the source language.
A. a wrapper
B. a link
C. a cursor
D. a form
41. When a JavaScript object is sent to Java, the runtime engine creates a Java wrapper
of type ___________
A. ScriptObject
B. JSObject
C. JavaObject
D. Jobject
42. _______ class provides an interface for invoking JavaScript methods and examining
JavaScript properties.
A. ScriptObject
B. JSObject
C. JavaObject
D. Jobject
43. _________ is a wrapped Java array, accessed from within JavaScript code.
A. JavaArray
B. JavaClass
C. JavaObject
D. JavaPackage
44. A ________ object is a reference to one of the classes in a Java package, such as
netscape.javascript .
A. JavaArray
B. JavaClass
C. JavaObject
D. JavaPackage
45. The JavaScript exception is available to the Java code as an instance of __________
A. netscape.javascript.JSObject
B. netscape.javascript.JSException
C. netscape.plugin.JSException
D. None of the above
46. To automatically open the console when a JavaScript error occurs which of the
following is added to prefs.js?
A. user_pref(" javascript.console.open_on_error", false);
B. user_pref("javascript.console.open_error ", true);
C. user_pref("javascript.console.open_error ", false);
D. user_pref("javascript.console.open_on_error", true);
47. To open a dialog box each time an error occurs, which of the following is added to
prefs.js?
A. user_pref("javascript.classic.error_alerts", true);
B. user_pref("javascript.classic.error_alerts ", false);
C. user_pref("javascript.console.open_on_error ", true);
D. user_pref("javascript.console.open_on_error ", false);
49. The syntax of capture events method for document object is ______________
A. captureEvents()
B. captureEvents(args eventType)
C. captureEvents(eventType)
D. captureEvents(eventVal)
What is Ajax?
The term Ajax was coined by Jesse James Garrett and is a short form for
"Asynchronous Javascript and XML". Ajax represents a set of commonly used
techniques, like HTML/XHTML, CSS, Document Object Model(DOM), XML/XSLT,
Javascript and the XMLHttpRequest object, to create RIA's (Rich Internet
Applications).
Ajax gives the user, the ability to dynamically and asynchronously interact with a
web server, without using a plug-in or without compromising on the user’s ability to
interact with the page. This is possible due to an object found in browsers called the
XMLHttpRequest object.
What is ASP.NET AJAX?
‘ASP.NET AJAX’ is a terminology coined by Microsoft for ‘their’ implementation of
AJAX, which is a set of extensions to ASP.NET. These components allow you to build
rich AJAX enabled web applications, which consists of both server side and client side
libraries.
Which is the current version of ASP.NET AJAX Control
Toolkit?
As of this writing, the toolkit version is Version 1.0.20229 (if you are targeting
Framework 2.0, ASP.NET AJAX 1.0 and Visual Studio 2005) and Version 3.0.20229
(if targeting .NET Framework 3.5 and Visual Studio 2008).
What role does the ScriptManager play?
The ScriptManager manages all ASP.NET AJAX resources on a page and renders the
links for the ASP.NET AJAX client libraries, which lets you use AJAX functionality like
PageMethods, UpdatePanels etc. It creates the PageRequestManager and Application
objects, which are prominent in raising events during the client life cycle of an
ASP.NET AJAX Web page. It also helps you create proxies to call web services
asynchronously.
Can we use multiple ScriptManager on a page?
No. You can use only one ScriptManager on a page.
What is the role of a ScriptManagerProxy?
A page can contain only one ScriptManager control. If you have a Master-Content
page scenario in your application and the MasterPage contains a ScriptManager
control, then you can use the ScriptManagerProxy control to add scripts to content
pages.
Also, if you come across a scenario where only a few pages in your application need
to register to a script or a web service, then its best to remove them from the
ScriptManager control and add them to individual pages, by using the
ScriptManagerProxy control. That is because if you added the scripts using the
ScriptManager on the Master Page, then these items will be downloaded on each
page that derives from the MasterPage, even if they are not needed, which would
lead to a waste of resources.
What are the requirements to run ASP.NET AJAX
applications on a server?
You would need to install ‘ASP.NET AJAX Extensions’ on your server. If you are using
the ASP.NET AJAX Control toolkit, then you would also need to add the
AjaxControlToolkit.dll in the /Bin folder.
Note: ASP.NET AJAX 1.0 was available as a separate downloadable add-on for
ASP.NET 2.0. With ASP.NET 3.5, the AJAX components have been integrated into
ASP.NET.
Explain the UpdatePanel?
The UpdatePanel enables you to add AJAX functionality to existing ASP.NET
applications. It can be used to update content in a page by using Partial-page
rendering. By using Partial-page rendering, you can refresh only a selected part of
the page instead of refreshing the whole page with a postback.
Can I use ASP.NET AJAX with any other technology apart
from ASP.NET?
To answer this question, check out this example of using ASP.NET AJAX with PHP, to
demonstrate running ASP.NET AJAX outside of ASP.NET. Client-Side ASP.NET AJAX
framework can be used with PHP and Coldfusion.
How can you cancel an Asynchronous postback?
Yes you can. Read my article over here.
Difference between Server-Side AJAX framework and
Client-side AJAX framework?
ASP.NET AJAX contains both a server-side Ajax framework and a client-side Ajax
framework. The server-side framework provides developers with an easy way to
implement Ajax functionality, without having to possess much knowledge of
JavaScript. The framework includes server controls and components and the drag
and drop functionality. This framework is usually preferred when you need to quickly
ajaxify an asp.net application. The disadvantage is that you still need a round trip to
the server to perform a client-side action.
The Client-Side Framework allows you to build web applications with rich user-
interactivity as that of a desktop application. It contains a set of JavaScript libraries,
which is independent from ASP.NET. The library is getting rich in functionality with
every new build released.
How can you debug ASP.NET AJAX applications?
Explain about two tools useful for debugging: Fiddler for IE and Firebug for Mozilla.
Can we call Server-Side code (C# or VB.NET code) from
javascript?
Yes. You can do so using PageMethods in ASP.NET AJAX or using webservices.
Can you nest UpdatePanel within each other?
Yes, you can do that. You would want to nest update panels to basically have more
control over the Page Refresh.
How can you to add JavaScript to a page when
performing an asynchronous postback?
Use the ScriptManager class. This class contains several methods like the
RegisterStartupScript(), RegisterClientScriptBlock(), RegisterClientScriptInclude(),
RegisterArrayDeclaration(),RegisterClientScriptResource(),
RegisterExpandoAttribute(), RegisterOnSubmitStatement() which helps to add
javascript while performing an asynchronous postback.
Explain differences between the page execution
lifecycle of an ASP.NET page and an ASP.NET AJAX
page?
In an asynchronous model, all the server side events occur, as they do in a
synchronous model. The Microsoft AJAX Library also raises client side events.
However when the page is rendered, asynchronous postback renders only the
contents of the update panel, where as in a synchronous postback, the entire page is
recreated and sent back to the browser.
Explain the AJAX Client life-cycle events
Here’s a good article about the same.
Is the ASP.NET AJAX Control
Toolkit(AjaxControlToolkit.dll) installed in the Global
Assembly Cache?
No. You must copy the AjaxControlToolkit.dll assembly to the /Bin folder in your
application.
What's AJAX ?
AJAX (Asynchronous JavaScript and XML) is a newly coined term for two powerful
browser features that have been around for years, but were overlooked by many
web developers until recently when applications such as Gmail, Google Suggest,
and Google Maps hit the streets.
Ajax isn’t a technology. It’s really several technologies, each flourishing in its own
right, coming together in powerful new ways. Ajax incorporates:
* standards-based presentation using XHTML and CSS;
* dynamic display and interaction using the Document Object Model;
* data interchange and manipulation using XML and XSLT; * asynchronous data
retrieval using XMLHttpRequest;
* and JavaScript binding everything together.
At Adaptive Path, we’ve been doing our own work with Ajax over the last several
months, and we’re realizing we’ve only scratched the surface of the rich
interaction and responsiveness that Ajax applications can provide. Ajax is an
important development for Web applications, and its importance is only going to
grow. And because there are so many developers out there who already know
how to use these technologies, we expect to see many more organizations
following Google’s lead in reaping the competitive advantage Ajax provides.
Moving Forward
The biggest challenges in creating Ajax applications are not technical. The core
Ajax technologies are mature, stable, and well understood. Instead, the
challenges are for the designers of these applications: to forget what we think we
know about the limitations of the Web, and begin to imagine a wider, richer
range of possibilities
Did Adaptive Path invent Ajax? Did Google? Did Adaptive Path help build
Google’s Ajax applications?
Neither Adaptive Path nor Google invented Ajax. Google’s recent products are
simply the highest-profile examples of Ajax applications. Adaptive Path was not
involved in the development of Google’s Ajax applications, but we have been
doing Ajax work for some of our other clients.
Is it possible to set session variables from javascript?
It's not possible to set any session variables directly from javascript as it is purely
a client side technology. You can use AJAX though to asyncronously...
Cannot parse XML generated by JSP I am generating an XML using JSP, when i run
the JSP in IE it shows the XML as per DOM, but when i try to parse it using
Javascript , the command xmldoc.documentElement...
AJAX. If you plan to develop your own JSF components or wire the events of
components together in a tool it is important that you have a basic
understanding of JavaScript. There are client-side JavaScript libraries (discussed
below) that you can call from your in page JavaScript that abstract browser
differences. Object Hierarchy and Inheritance in JavaScript is a great resource for
a Java developer to learn about JavaScript objects.
* The Dojo Toolkit contains APIs and widgets to support the development of rich
web applications. Dojo contains an intelligent packaging system, UI effects, drag
and drop APIs, widget APIs, event abstraction, client storage APIs, and AJAX
interaction APIs. Dojo solves common usability issues such as support for dealing
with the navigation such as the ability to detect the browser back button, the
ability to support changes to the URL in the URL bar for bookmarking, and the
ability to gracefully degrade when AJAX/JavaScript is not fully support on the
client. Dojo is the Swiss Army Knife of JavaScript libraries. It provides the widest
range of options in a single library and it does a very good job supporting new
and older browsers.
* Prototype focuses on AJAX interactions including a JavaScript AJAX object that
contains a few objects to do basic tasks such as make a request, update a
portion of a document, insert content into a document, and update a portion of a
document periodically. Prototype JavaScript library contains a set of JavaScript
objects for representing AJAX requests and contains utility functions for accessing
in page components and DOM manipulations. Script.aculo.us and Rico are built
on top of Prototype and provide UI effects, support for drag and drop, and include
common JavaScript centric widgets. If you are just looking to support AJAX
interactions and a few basic tasks Prototype is great. If you are looking for UI
effects Rico and Script.aculo.us are good options.
* Yahoo UI Library is a utility library and set of widgets using the APIs to support
rich clients. The utility library includes support for cross-browser AJAX
interactions, animation, DOM scriptging support, drag and drop, and cross
browser event support. The Yahoo UI Library is well documnented and contains
many examples.
* DWR (Dynamic Web Remoting) is a client-side and server-side framework that
focuses on allowing a developer to do RPC calls from client-side JavaScript to
plain old Java objects in a Java Enterprise Edition web container. On the server
side DWR uses a Servlet to interact with the Java objects and returns object
representations of the Java objects or XML documents. DWR will be easy to get
up and running and plays well with other Java technologies. If you are looking for
a client-side and server-side framework that integrates well use DWR.
* Google Web Toolkit (GWT) is client/server framework provided by Google that
allows a developer to write an AJAX application in pure Java. The GWT takes care
of the details of generating all the client-side code using a Java-to-JavaScript
compiler. One of the key benefits of the GWT Software Developer Kit (SDK) is
that it allows you to debug your applications in what is known as GWT hosted
mode using an embedded browser (IE on Windows and Mozilla/Gecko on Linux)
that is tied to the toolkit. In GWT hosted mode you setup through the code and
debug it as it is running on both the client and server. The GWT contains a
default set of widgets and widget containers. An application is built by coding a
set of widgets and containers together much like would be done in a Swing
application. The GWT Software Developer Kit (SDK) is limited to Linux and
Windows XP/2000 though the web applications it generates are compatible with
the latest generation of the mainstream browsers.
There are many new and emerging libraries for JavaScript and this list only
reviews some of the more common libraries. When making a choice choose the
library which suites your needs the best. While it might be better to choose one,
there is nothing stopping you from using more than one framework. For a more
extensive list of client-side frameworks see: Survey of AJAX/JavaScript Libraries.
* XML - Web Services and AJAX seem made for one another. You can use client-
side API's for downloading and parsing the XML content from RESTful Web
Services. (However be mindful with some SOAP based Web Services
architectures the payloads can get quite large and complex, and therefore may
be inappropriate with AJAX techniqes.)
* Plain Text - In this case server-generated text may be injected into a document
or evaluated by client-side logic.
* JavaScript - This is an extension to the plain text case with the exception that a
server-side component passes a fragment of JavaScript including JavaScript
object declarations. Using the JavaScript eval() function you can then create the
objects on the client. JavaScript Object Notation (JSON), which is a JavaScript
object based data exchange specification, relies on this technique.
* HTML - Injecting server-generated HTML fragments directly into a document is
generally a very effective AJAX technique. However, it can be complicated
keeping the server-side component in sync with what is displayed on the client.
function init() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
function processRequest () {
if (req.readyState == 4) {
if (req.status == 200) {
if (callback) callback(req.responseXML);
}
}
}
this.doGet = function() {
req.open("GET", url, true);
req.send(null);
}
this.doPost = function(body) {
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "
application/x-www-form-urlencoded");
req.send(body);
}
}
function makeRequest() {
var ai = new AJAXInteraction("processme",
function() { alert("Doing Post Process");});
ai.doGet();
}
response.setContentType("text/xml");
response.getWriter().write("<response>invalid</response>");
You may also want to set whether or not to set the caches header for cases such
as autocomplete where you may want to notify proxy servers/and browsers not
to cache the results.
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<response>invalid</response>");
Note to the developer: Internet Explorer will automatically use a cached result of
any AJAX response from a HTTP GET if this header is not set which can make
things difficult for a developer. During development mode you may want set this
header. Where do I store state with an AJAX client
As with other browser based web applications you have a few options which
include:
* On the client in cookies - The size is limited (generally around 4KB X 20 cookies
per domain so a total of 80KB) and the content may not be secure unless
encrypted which is difficult but not impossible using JavaScript.
* On the client in the page - This can be done securely but can be problematic
and difficult to work with. See my blog entry on Storing State on the Client for
more details on this topic.
* On the client file system - This can be done if the client grants access to the
browser to write to the local file system. Depending on your uses cases this may
be necessary but caution is advised.
* On the Server - This is closer to the traditional model where the client view is of
the state on the server. Keeping the data in sync can be a bit problematic and
thus we have a solution Refreshing Data on this. As more information processing
and control moves to the client where state is stored will need to be re-
evaluated.
Whats with the -alpha in the install instructions?
HTML_AJAX hasn't had a stable release yet and the pear installer doesn't install
non stable packages by default unless you specify a version.
You can also submit data by associating a function with a form button in a similar
way.
Note that the form "onSubmit" attribute is still set. If the user hits the enter key
in the text field the form will be submitted so you still need to handle that case.
When updating the page it is recommend you wait to make sure that the AJAX
update of the form data was successful before updating the data in the page.
Otherwise, the data may not properly update and the user may not know. I like to
provide an informative message when doing a partial update and upon a
successful AJAX interaction I will then update the page.
There are some use cases where an entire AJAX application can be written in a
single page. Keep in mind if you choose this type of architecture that navigation
and bookmarking should be considered.
Both methods are viable depending on what you are trying to accomplish. I tend
to prefer spreading the control across the client and server.