Pages

Friday, September 2, 2011

Javascript Interview Questions


Q) What is JavaScript?
A) JavaScript is a platform-independent,event-driven, interpreted client-side scripting and programming language developed by Netscape Communications Corp. and Sun Microsystems.

Q) How to read and write a file using javascript?
A) I/O operations like reading or writing a file is not possible with client-side javascript.However , this can be done by coding a Java applet that reads files for the script.

Your Advertisement Goes Here

Q) Where are cookies actually stored on the hard disk?
A) This depends on the user's browser and OS.In the case of Netscape with Windows OS,all the cookies are stored in a single file called cookies.txt
c:\Program Files\Netscape\Users\username\cookies.txt
In the case of IE,each cookie is stored in a separate file namely username@website.txt.
c:\Windows\Cookies\username@Website.txt

Q) What can javascript programs do?
A) Generation of HTML pages on-the-fly without accessing the Web server. The user can be given control over the browser like User input validation Simple computations can be performed on the client's machine The user's browser, OS, screen size, etc. can be detected
Date and Time Handling

Q) How to set a HTML document's background color?
A) document.bgcolor property can be set to any appropriate color.

Q) What does the "Access is Denied" IE error mean?
A) The "Access Denied" error in any browser is due to the following reason. A javascript in one window or frame is tries to access another window or frame whose document's domain is different from the document containing the script.

Q) Is a javascript script faster than an ASP script?
A) Yes.Since javascript is a client-side script it does require the web server's help for its computation,so it is always faster than any server-side script like ASP,PHP,etc..

Q) Are Java and JavaScript the Same?
A) No.java and javascript are two different languages. Java is a powerful object - oriented programming language like C++,C whereas Javascript is a client-side scripting language with some limitations.

Q) How to embed javascript in a web page?
A) javascript code can be embedded in a web page between <script language="javascript"></script> tags

Q) How to access an external javascript file that is stored externally and not embedded?
A) This can be achieved by using the following tag between head tags or between body tags. <script src="abc.js"></script> where abc.js is the external javscript file to be accessed.

Q) What is the difference between an alert box and a confirmation box?
A) An alert box displays only one button which is the OK button whereas the Confirm box displays two buttons namely OK and cancel.

Q) What is a prompt box?
A) A prompt box allows the user to enter input by providing a text box.

Q) Can javascript code be broken in different lines?
A) Breaking is possible within a string statement by using a backslash \ at the end but not within any other javascript statement. that is ,
document.write("Hello \
world");
is possible but not
document.write \
("hello world");

Q) How to hide javascript code from old browsers that dont run it?
A) Use the below specified style of comments
< script language=javascript>
<!--
javascript code goes here
// -->
or
Use the < NOSCRIPT> some html code </NOSCRIPT> tags and code the display html statements between these and this will appear on the page if the browser does not support javascript

Q) How to comment javascript code?
A) Use // for line comments and
/* */ for block comments

Q) Name the numeric constants representing max,min values
A) Number.MAX_VALUE
Number.MIN_VALUE

Your Advertisement Goes Here

Q) What does javascript null mean?
A) The null value is a unique value representing no value or no object.It implies no object,or null string,no valid boolean value,no number and no array object.

Q) What does undefined value mean in javascript?
A) Undefined value means the variable used in the code doesnt exist or is not assigned any value or the property doesnt exist.

Q) What is the difference between undefined value and null value?
A) (i)Undefined value cannot be explicitly stated that is there is no keyword called undefined whereas null value has keyword called null
(ii)typeof undefined variable or property returns undefined whereas typeof null value returns object

Q) What is variable typing in javascript?
A) It is perfectly legal to assign a number to a variable and then assign a string to the same variable as follows
example :
i = 10;
i = "string";
This is called variable typing

Q) Does javascript have the concept level scope?
A) No.Javascript does not have block level scope,all the variables declared inside a function possess the same level of scope unlike c,c++,java.

Q) What are undefined and undeclared variables?
A) Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if undeclared variables are assigned then implicit declaration is done .
Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called undefined value.

Q) What is == operator ?
A) == is strict equality operator ,it returns true only when the two operands are having the same value without any type conversion.

Q) What does the delete operator do?
A) The delete operator is used to delete all the variables and objects used in the program ,but it does not delete variables declared with var keyword.

Q) What does break and continue statements do?
A) Continue statement continues the current loop (if label not specified) in a new iteration whereas break statement exits the current loop.

Q) How to create a function using function constructor?
A) The following example illustrates this
It creates a function called square with argument x and returns x multiplied by itself.
var square = new Function ("x","return x*x");

Q) what is the differene between java and java script...?
A) Java Script is object based language it is used for front end validations.java is object oriented language object oriented language supports OOP's Object based not suppoted inheritence.

Q) What are JavaScript types?
A) Number, String, Boolean, Function, Object, Null, Undefined.

Q) How do you convert numbers between different bases in JavaScript?
A) Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F", 16);

Q) What does isNaN function do ?
A) Return true if the argument is not a number.

Q) What boolean operators does JavaScript support?
A) &&, || and !

Q) What does "1"+2+4 evaluate to?
A) Since 1 is a string, everything is a string, so the result is 124.

Your Advertisement Goes Here

Q) What looping structures are there in JavaScript?
A) for, while, do-while loops, but no foreach.

Q) How do you create a new object in JavaScript?
A) var obj = new Object(); or var obj = {};

Q) How do you assign object properties?
A) obj["age"] = 17 or obj.age = 17

Q) What's a way to append a value to an array?
A) arr[arr.length] = value;

Q) What is this keyword?
A) It refers to the current object.

Q) What is the main difference between Client side JavaScript and and Server side Java Script. How actually they run on both side with Example?
A) Clientside Javascript encompasses the core language plus extras such as the predefined objects, only relevant to running Javasript in a browser.The clientside javascript is embedded directly in the HTML pages and is interpreted by the browser completly at the run time. Serverside javascript also encompasses the core language plus extas as the predefined objects and functions only relevant to running Javasript in a server.The serverside javascripts are compiled before they are deployed.

Q) What's relationship between JavaScript and ECMAScript?
A) ECMAScript is yet another name for JavaScript (other names include LiveScript). The current JavaScript that you see supported in browsers is ECMAScript revision 3.

Q) Why so JavaScript and Java have similar name?
A) JavaScript's syntax is loosely based on Java's

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

Q) ______ JavaScript is also called client-side JavaScript.
A) Navigator

Q) __________ JavaScript is also called server-side JavaScript.
A) LiveWire

Q) What are variables used for in JavaScript Programs?
A) Storing numbers, dates, or other values

Q) _____ JavaScript statements embedded in an HTML page can respond to user events such as mouse-clicks, form input, and page navigation.
A) Client-side

Q) What should appear at the very end of your JavaScript? The < script LANGUAGE="JavaScript"> tag
A) The </script>

Q) Which can't be done with client-side JavaScript?
A) Storing the form's contents to a database file on the server

Q) What is the capabilities of functions in JavaScript?
A) Accept parameters

Q) ______ tag is an extension to HTML that can enclose any number of JavaScript statements.
A) < SCRIPT>

Q) How does JavaScript store dates in a date object?
A) The number of milliseconds since January 1st, 1970

Q) What is the correct syntax for referring to an external script called " abc.js"?
A) < script src=" abc.js">

Q) Which types of image maps can be used with JavaScript?
A) Client-side image maps

Your Advertisement Goes Here

Q) Which navigator object properties is the same in both Netscape and IE?
A) navigator.appCodeName

Q) Which is the correct way to write a JavaScript array?
A) var txt = new Array("tim","kim","jim")

Q) What does the < noscript> tag do?
A) Enclose text to be displayed by non-JavaScript browsers.

Q) If para1 is the DOM object for a paragraph, what is the correct syntax to change the text within the paragraph?
A) para1.value="New Text";

Q) JavaScript entities start with _______ and end with _________.
A) Ampersand, semicolon

Q) Which of the following best describes JavaScript?
A) An object-oriented scripting language.

Q) ______method evaluates a string of JavaScript code in the context of the specified object.
A) Eval

Q) Which of the following event fires when the form element loses the focus: < button>, < input>,< label>, < select>, < textarea>?
A) onblur

Q) The syntax of Eval is ________________
A) [objectName.]eval(string)

Q) JavaScript is interpreted by _________
A) Client

Q) Using _______ statement is how you test for a specific condition.
A) If

Q) Which of the following is the structure of an if statement?
A) if (conditional expression is true) {then execute this code>->}

Q) How to create a Date object in JavaScript?
A) dateObjectName = new Date([parameters])

Q) The _______ method of an Array object adds and/or removes elements from an array.
A) Splice

Q) To set up the window to capture all Click events, we use which of the following statement?
A) window.captureEvents(Event.CLICK);

Q) ____________ is the tainted property of a window object.
A) Defaultstatus

Q) To enable data tainting, the end user sets the _________ environment variable.
A) NS_ENABLE_TAINT

Q) In JavaScript, _________ is an object of the target language data type that encloses an object of the source language.
A) a wrapper

Q) When a JavaScript object is sent to Java, the runtime engine creates a Java wrapper of type ___________
A) JSObject

Q) A ________ object is a reference to one of the classes in a Java package, such as netscape.javascript .
A) JavaClass

Q) The JavaScript exception is available to the Java code as an instance of __________
A) netscape.javascript.JSException

Q) 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", true);

Your Advertisement Goes Here

Q) 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);

Q) The syntax of a blur method in a button object is ______________
A) Blur()

Q) The syntax of capture events method for document object is ______________
A) captureEvents(eventType)

Q) The syntax of close method for document object is ______________
A) Close()

Q) How to detect the operating system on the client machine?
A) In order to detect the operating system on the client machine, the navigator.appVersion string (property) should be used.

Q) What is negative infinity?
A) It's a number in JavaScript, derived by dividing negative number by zero.

Q) How can we indicate the LANGUAGE attribute?
A) < SCRIPT LANGUAGE="JavaScriptVersion"> JavaScript statements..</SCRIPT>

Q) _______ class provides an interface for invoking JavaScript methods and examining JavaScript properties.
A) JSObject

Q) _________ is a wrapped Java array, accessed from within JavaScript code.
A) JavaArray

Q) Which of the following attribute can hold the JavaScript version?
A) LANGUAGE




No comments:

Post a Comment