JavaScript 1.3 Test answer for oDesk


You created a cookie using JavaScript and want to delete it. How do you do it?
a. You can't. They are valid until they expire
b. Overwrite it with an expiry date in the past
c. escape() the value of the path attribute
d. unescape() the value of the path attribute
e. You will have to remove the cookie file from the client machine
f. You will have to wait till the expiry date is reached
Which of the following Regular Expression pattern flags is not valid?
a. gi
b. p
c. i
d. g
You declared a variable. What value would JavaScript assign to the uninitialized variable?
a. NaN
b. null
c. undefined
d. false
e. 0
You have defined an image tag as follows:

<img id="ERImage" width="100" height="100" onmouseover="ImageChange()"  src="Image1.jpg">

The purpose of the ImageChange() function is to change the image source to "Image2.jpg". What will the ImageChange() function look like?
a. document.getElementById('ERImage').src="Image1.jpg"
b. document.getElementById('ERImage').src="Image2.jpg"
c. document.getElementById('ERImage').style.src="Image1.jpg"
d. document.getElementById('ERImage').style.src="Image2.jpg"
Which of the following is true about setTimeOut():
a. The statement(s) it executes run(s) only once
b. It pauses the script in which it is called
c. clearTimeOut() won't stop its execution
d. The delay is measured in hundredths of a second
e. It is required in every JavaScript function
Which is the topmost object in the object hierarchy?
a. document
b. window
c. history
d. browser
e. form
Which of the following are valid JavaScript methods?
a. scrollBy(dx, dy)
b. moveBy(dx, dy)
c. moveTo(x, y)
d. scrollTo(x, y)
e. All of the above are valid
You want to access the methods and properties of a Java Applet. What feature would allow you to do that?
a. The Java console
b. LiveConnect
c. LiveWire
d. LiveWire Pro
What will the following code snippet do?

<form onreset="alert('Please try again')">
a. It validates the contents of the form
b. It prompts the user not to leave the fields of the form empty
c. It prompts the user to fill the form once the form is cleared
d. None of the above
You want to ascertain the protocol of the current URL. Which object would you use?
a. document
b. window
c. history
d. browser
e. form
f. location
What is the final value of the variable apt?

var apt=2;
apt=apt<<2;
a. 2
b. 4
c. 6
d. 8
e. 16
Which of the following is not a valid JavaScript operator?
a. *=
b. /=
c. %=
d. ^+
What would be the value of 'ind' after execution of the following code?

var msg="Welcome to ExpertRating"
var ind= msg.substr(3, 3)
a. lco
b. com
c. ome
d. Welcome
What is the result of the following:

var vName="Kodak Camera"
vName.indexOf("Camera")
a. 4
b. 5
c. 6
d. 7
You want to ascertain the page from which the user has navigated to the current page. How would you accomplish this?
a. It is not possible in JavaScript. This can be done only through server side scripting
b. Use document.referrer property
c. Use the window object
d. None of the above
You declared an array. What value would JavaScript assign to an uninitialized array element?
a. NaN
b. null
c. undefined
d. false
e. 0
Which of the following built-in functions is used when you want to access the form elements using their ids?
a. getItem(id)
b. getFormElement(id)
c. getElementById(id)
d. All of the above
The following statements show two ways of calling a JavaScript function:

i)onclick = "a();"
ii)onclick="JavaScript:a();"

Which of the following options is correct with regard to the above two statements?
a. There is no difference between the two statements
b. The first statement is incorrect and the second one is correct
c. The first statement is correct and the second statement is incorrect.
You are displaying image thumbnails on a page. Upon clicking a thumbnail, the image is being displayed in its actual size. You do not want the user to be able to click the thumbnails unless they are completely downloaded. What event would you use to prevent the user from clicking on the thumbnails until they are completely downloaded?
a. OnLoad
b. OnKeyPress
c. OnKeyUp
d. onClick
The statement navigator.platform indicates 'Win16', but you are running Windows NT. Which of the following is true?
a. navigator.platform is supposed to show 'Win16' on Windows NT
b. The userAgent property reflects the correct operating system
c. The property can be modified
d. navigator.platform shows the operating system for which the browser was compiled
e. The browser version is outdated
Which of the following can be used for disabling the right click event in Internet Explorer?
a. event.button == 2
b. event.button == 4
c. event.click == 2
d. event.click == 4
You want to create a popup window that closes after n seconds. How would you do it?
a. You cannot program a window to close automatically after n seconds. The user will have to manually close it
b. You would use the setTimeout function
c. You would use the exit function of the window object
d. You would use the terminate function
e. None of the above
While coding a loop, you come across a condition where you have to ignore the rest of the statements and continue with the beginning of the loop. Which statement would you use?
a. exit
b. return
c. continue
d. while
e. break
You want to display an alert to the user which contains a ' character. Which character would you use to escape the ' character?
a. *
b. \
c. -
d. @
e. #
f. %
g. |
h. ~
Which best describes void?
a. A method
b. A function
c. A statement
d. An operator
e. An in-built object
Which of the following is not a valid JavaScript operator?
a. |
b. ===
c. <<<
d. ^
Which of the following is not a valid HTML event?
a. ondblclick
b. onmousemove
c. onclick
d. onblink
Which of the following is not a valid HTML event?
a. onunload
b. onchange
c. onupdate
d. onkeydown
The following code snippet is used in form validation, the validate function returns a true if all validations are correct and returns false if the validation fails. The data is always submitted even if the validation fails, what could be the cause for this?

<form name=frmadd method=post action=add.asp onsubmit="validate()">
a. There is no error, the code is fine
b. There should be a return statement before the call to the validate function
c. onsubmit="validate()" should be replaced with "JavaScript:validate()"
Consider the following code:

var seter=new Object()
seter.color="blue"

How would you delete the color property of the variable seter?
a. seter.color delete;
b. delete seter.color
c. delete(seter.color)
d. seter.color.delete
An HTML form contains 10 checkboxes all named "chkItems". Which JavaScript function can be used for checking all the checkboxes together?
a. function CheckAll()
   {
           for (z = 0; z < document.forms.chkItems.length; z++)
           {
           document.forms.chkItems[z].checked=true
           }
   }
b. function CheckAll()
   {
           for (z = 0; z < document.forms[0].chkItems.length; z++)
           {
           document.forms[0].chkItems[z].checked=true
           }
   }
c. function CheckAll()
   {
           for (z = 0; z < document.forms[0].chkItems.length; z++)
           {
           document.forms[0].chkItems.list[z].checked=true
           }
   }
d. function CheckAll()
   {
           for (z = 0; z < document.forms[0].chkItems.length; z++)
           {
           document.forms[0].chkItems.list[z].checked=false
           }
   }
Which JavaScript feature uses JAR files?
a. Object signing
b. Style sheets
c. Netcaster channels
d. Image rollovers
e. Form validation
Using JavaScript, how can you determine whether the browser is Netscape or some other?
a. if(appName=="Netscape"){}
b. if(document.appName=="Netscape"){}
c. if(navigator=="Netscape"){}
d. if(browser=="Netscape"){}
e. None of the above
Which of the following could you do using JavaScript?
a. Form validation
b. Display a popup window
c. Verify usernames and passwords against the database
d. Database access
e. Change the color of the page
f. Display an alert
g. Prompt the user to enter a value
You've embedded the document.write() method to write some text within a pair of <TD></TD> table tags.

Upon loading the file, however, you get some garbled junk on the page where that text should be. What could be the reason for this?
a. The browser does not support JavaScript
b. You are using an older version of the browser
c. The browser does not support cookies
After clicking on a link, your main browser window spawns a small new window and writes some text to it. However, when viewing both window page sources, the small window page source is identical to that of the large. How can this be?
a. The function writing to the new window didn't close the document stream
b. It cannot happen
c. Windows receive the source code of the window that wrote to them
d. Since the new window has no filename, it receives the source code of its parent
Choose another way to write x ? a = b : a = c
a. if ('x') { a = b; } else { a = c; }
b. if (x) { a = c; } else { a = b; }
c. x : a = c ? a = b
d. None of above
While coding a loop, you come across a condition where you have to terminate the loop and continue with the statements following the loop. Which statement would you use?
a. exit
b. return
c. continue
d. while
e. break
If an image is placed styled with z-index=-1 and a text paragraph is overlapped with it. Which one will be displayed on top?
a. The paragraph
b. The image
c. Could be any of the above
Given the statement var charConvert = toCharCode('x'), what is the error?
a. toCharCode() is a bogus method
b. Nothing. It will work fine
c. toCharCode accepts only numbers
d. toCharCode takes no arguments
Consider three variables:

someText = 'JavaScript1.2';
pattern = /(\w+)(\d)\.(\d)/i;
outCome = pattern.exec(someText);

What does pattern.global contain?
a. true
b. false
c. undefined
d. null
e. 0
f. 1
The form tag is defined as follows:

<form onsubmit="return Validate()" action="http://www.mysite.com/">

The validate() function is intended to prevent the form from being submitted if the name field in the form is empty. What will the validate() function look like?
a. <script type="text/JavaScript">
    function Validate()
    {
    if(document.forms[0].name.value == "")
            return true;
    else
        return false;
    }
    </script>
b. <script type="text/JavaScript">
    function Validate()
    {
    if(document.forms[0].name.value == "")
        return false;
    else
            return true;
    }
    </script>
c. <script type="text/JavaScript">
    function Validate()
    {
    if(document.forms[0].name== "")
            return false;
    else
            return true;
    }
    </script>
d. <script type="text/JavaScript">
    function Validate()
    {
    if(document.forms[0].name == "")
            return true;
    else
            return false;
    }
    </script>
You want to delete an element from an options array. How would you do it?
a. Set it to false
b. Set it to null
c. Set it to undefined
d. Set it to -1
e. Set it to 0
f. Set it to 1
Consider the following variable declarations:

var a="adam"
var b="eve"

Which of the following would return the sentence "adam and eve"?
a. a.concatinate("and", b)
b. a.concat("and", b)
c. a.concatinate(" and ", b)
d. a.concat(" and ", b)
Which of the following code snippets is more efficient and why?

<script language="JavaScript">
        for(i=0;i<document.images.length;i++)
                document.images[i].src="blank.gif";

</script>


<script language="JavaScript">
        var theimages = document.images;
        for(i=0;i<theimages.length;i++)
                theimages[i].src="blank.gif"
</script>
a. Both are equally efficient
b. The first code is more efficient as it contains less code
c. The first code is more efficient as it employs object caching
d. The second code is more efficient as it employs object caching
You have written the following function to change the source of an image at runtime:

function changeimg(givenImg)
{
document.getElementById('imageID').src=givenImg
}

You would like the source of the image to change to "image2.gif" only for the time that the user clicks on the image. Which of the following will you use?
a. <img id="imageID" src="image1.gif" width="50" height="60" onmousedown="changeimg(image1.gif)" onmouseup="changeimg(image2.gif)">
b. <img id="imageID" src="image1.gif" width="50" height="60" onmouseclick="changeimg(image2.gif)" onmouseup="changeimg(image1.gif)">
c. <img id="imageID" src="image2.gif" width="50" height="60" onmousedown="changeimg(image1.gif)" onmouseup="changeimg(image2.gif)">
d. <img id="imageID" src="image2.gif" width="50" height="60" onmousedown="changeimg(image2.gif)" onmouseup="changeimg(image1.gif)">
e. <img id="imageID" src="image1.gif" width="50" height="60" onmousedown="changeimg('image2.gif')" onmouseup="changeimg('image1.gif')">
Are the following two statements the same?

object.property
object[''property'']
a. Yes
b. No
Which of the following is not a JavaScript string method?
a. rep
b. split
c. substr
d. slice
Consider the following JavaScript alert:

<script type="text/JavaScript">
function message()
{
alert("Welcome to ExpertRating!!!")
}
</script>

You want the user to see the above message upon opening the page. How will you implement this?
a. <body onload="message()">
b. <body onunload="message()">
c. <body onsubmit="message()">
d. <body onreset="message()">
You are setting cookies with JavaScript. What happens to cookies.txt data if the file exceeds the maximum size?
a. Your script automatically generates a run-time error
b. Your script automatically generates a load-time error
c. All processes using document.cookie are ignored
d. The file is truncated to the maximum length
What does the following code snippet do?

<input type="radio" name="r1" value="radio1" onclick="this.checked=false;alert('sorry')">
a. The code is invalid
b. The code makes it necessary for the user to select the radio button
c. The code disables the radio button
d. The code displays an alert when the user selects the button
How would you declare a global variable in your JavaScript code?
a. All variables are local in JavaScript
b. You will have to declare the variable between the 'script' tags and outside a function to make the variable global
c. You will have to precede the variable name with the constant global
d. You will have to declare the variable in an external file
How would you randomly choose an element from an array named myStuff if the number of elements changes dynamically?
a. randomElement = myStuff[Math.floor(Math.random() * myStuff.length)];
b. randomElement = myStuff[Math.ceil(Math.random() * myStuff.length)];
c. randomElement = myStuff[Math.random(myStuff.length)];
d. randomElement = Math.random(myStuff.length);
Which of the following declarations is not valid?
a. var a
    var b
    var c
b. var a, b, c
c. var a=10, b=20, c=30
d. All are valid
How will you change the color of all the paragraph text to blue when the mouse is placed over paragraph, and reset it back when the mouse leaves?
a. <p onmouseover="style.color='black'" onmouseout="style.color='blue'"> The text of the paragraph..</p>
b. <p onmouseover="style.color='blue'" onmouseout="style.color='black'"> The text of the paragraph..</p>
c. <p onmouseout="style.color='blue'"> The text of the paragraph..</p>
d. <p onmouseover="style.color='blue'"> The text of the paragraph..</p>
e. <p onmousein="style.color='blue'" onmouseout="style.color='black'"> The text of the paragraph..</p>
Which property would you use to redirect a visitor to another page?
a. document.URL
b. window.location.href
c. document.location.href
d. link.href
e. window.location
What will the following code snippet do?

onMove="window.status='come on in!'"
a. It displays the message 'come on in!' when the user moves the mouse over the document
b. It displays the message 'come on in!' when the user clicks the mouse on the document
c. It displays the message 'come on in!' when the user moves the window
d. None of the above
Consider the three variables:

someText = 'JavaScript1.2';
pattern = /(\w+)(\d)\.(\d)/i;
outCome = pattern.exec(someText);

What does outCome[0] contain?
a. true
b. false
c. JavaScript1.2
d. null
e. 0
You want to keep track of when the user highlights text within a text or textarea object. Which event would you use?
a. onSelect
b. onBlur
c. onChange
d. onMove
e. None of the above
How would you check whether the variable vRast exists or not?
a. if (typeof vRast="undefined") {}
b. if (typeof vRast =="undefined") {}
c. if (vRast.defined =true) {}
d. if (vRast.defined ==true) {}
Which of the following shortcuts can be used for writing multiple document.write statements?
a. for(document){}
b. with(document) {}
c. This(document){}
d. None of the above
Given the following window.open function:

        window.open(url,name,"attributes")

How will you ensure that different urls are opened in the same window?
a. The second attribute, name should be the same
b. The name attribute should be null
c. The name attribute should be omitted
d. The name attribute should be different
e. None of the above
Which of the following is not a valid JavaScript function?
a. apply()
b. call()
c. toString()
d. All of the above are valid
Consider the three variables:

someText = 'JavaScript1.2';
pattern = /(\w+)(\d)\.(\d)/i;
outCome = pattern.exec(someText);

What does pattern.ignoreCase contain?
a. true
b. false
c. undefined
d. null
e. 0
You are allowing the user to upload image files. You want to be able to capture the attributes of the image such as width and height using JavaScript. How would this be possible?
a. It is not possible using JavaScript
b. You would use the Image object such as var img = new Image
c. You would use the Img object
d. This is possible only on the server side using a component
e. None of the above
You need to be able to determine which page of the web site has been the most recently modified. Which object would enable you to do this?
a. document
b. window
c. history
d. browser
e. form
f. location
Consider the following JavaScript function to change the color of the text box named ''txtName'':

function color(col)
{ document.forms[0].txtName.style.background=col }

You want that the color of the text box should change to green only while the user is pressing a key?
a. <input type=''text'' onkeydown=''color('white')'' onkeyup="color('green')'' name=''txtName''>
b. <input type=''text'' onkeydown=''color('green')'' onkeyup=''color('white')'' name=''txtName''>
c. <input type=''text'' onkeydown=''color('green')'' name=''txtName''>
d. <input type=''text'' onkeydown=''color('white')'' name=''txtName''>
e. <input type=''text'' onkeypress=''color('green')'' onkeyup=''color('white')'' name=''txtName''>
Which of the following cannot be achieved using JavaScript?
a. Read or write from external files (except .js files)
b. Access or modify browser settings
c. Launch client processes (eg windows applications)
d. Launching the default email application of the client
You want to create a variable called newDate that contains the date February 1 of the current year. Which option will you use?
a. var newDate=new Date(today.getFullYear(), 2, 2)
b. var newDate=new Date(today.getFullYear(), 1, 1)
c. var newDate=new Date(today.getCurrYear(), 2, 1)
d. var newDate=new Date(today.getCurrYear(), 1, 1)
How would you invoke the browser's Add To Favorite Dialog?
a. Use the window.AddFavorite method
b. Use the document.AddFavorite method
c. Use the window.external.AddFavorite method
d. It is not possible using JavaScript
How would you use a timer with a function called rearrange()?
a. tmr=setTimeout("rearrange ()",1)
b. tmr=Timer(1,"rearrange ()")
c. tmr=Timer("rearrange ()",1)
d. tmr=setTimeout(1,"rearrange ()")
Consider the following image definition:

<img id="logo" src="companylogo1.gif" height="12" width="12" >

How can you change the image to "companylogo2.gif" when the page loads?
a. logo.source="companylogo2.gif"
b. logo.source="companylogo1.gif"
c. document.getElementById('logo').src="companylogo1.gif"
d. document.getElementById('logo').src="companylogo2.gif"
You are enabling cookies with JavaScript. What would be the default setting for the expires attribute of the document.cookie property?
a. The duration of the browser session
b. The duration the current document stays loaded
c. Twenty-four hours from the time the cookie is set
d. There is no default setting
e. The duration for which the machine is on
Which of the following is not a valid string method?
a. link()
b. italics()
c. str()
d. sup()
Which of the following is true?
a. If onKeyDown returns false, the key-press event is cancelled
b. If onKeyPress returns false, the key-down event is cancelled
c. If onKeyDown returns false, the key-up event is cancelled
d. If onKeyPress returns false, the key-up event is canceled
Which of the following properties can you use to dynamically change the value of a <tr> tag?
a. HTML
b. innerHTML
c. outerHTML
d. text
e. innerText
f. None of the above
What will the following code snippet do?

document.alinkColor="green"
a. It makes the background color of the document green
b. It makes the color of the active links green
c. It makes the color of the visited links green
d. It makes the color of all links green
e. None of the above
Consider the following code snippet:

<form name="frmOne">
<select name="selList" size="1" onChange="change()">
<option value="http://www.hotmail.com">tHomail</option>
<option value="http://www.yahoo.com">Yahoo</option>
</select>
</form>

You want that when an option button is selected, the appropriate website should be opened immediately. What will the change() function look like?
a. url=document.frmOne.selList.options[document.frmOne.selList..item].value
b. url=document.frmOne.selList.options[document.frmOne.selList.selectedIndex].value
c. location=document.frmOne.selList.options[document.frmOne.selList.item].value.value
d. location=document.frmOne.selList.options[document.frmOne.selList.selectedIndex].value
How would you detect the operating system of the client machine?
a. It is not possible using JavaScript
b. You would use the navigator object
c. You would use the window object
d. You would use the document object
e. None of the above
What would be the use of the following code?

function validate(field)
{
var valid=''ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'';
var ok=''yes'';
var temp;

for(var i=0;i<field.value.length;i++)
{
temp='''' + field.value.substring(i,i+1)
if(valid.indexOf(temp)==''-1'')
{
ok=''no'';
}
}

if(ok==''no'')
{
alert(''error'');
field.focus();
}

}
a. It will force a user to enter only numeric values
b. It will force a user to enter only alphanumeric values
c. It will force a user to enter only character values
d. None of the above
Consider the following code:
var vNew=new Date()

Which of the following options will return true?
a. vNew instanceof boolean
b. vNew instanceof object
c. vNew instanceof Date
d. All of the above
You want to find out the resolution of the screen. How would you do it?
a. It is not possible
b. You would use the screen object
c. You would use the document object
d. You would use the window object
e. None of the above
Which of the following escape sequences represents form feed?
a. //f
b. 'f'
c. /f
d. \f
You have created an object. How would you add a property to the object class?
a. With the prototype() method
b. With the prototype property
c. It cannot be done
d. With the this object
What is the difference between calling a JavaScript function directly like onclick = "a()" and onclick="JavaScript:a()" where a() is a function written in JavaScript?
a. There is no difference
b. The first technique is correct, only the second is incorrect
c. The second statement is more efficient than the first
d. The first statement looks for a function a() written in any language, whether it is JavaScript or vbscript, and the second technique looks for a function a() specifically written in JavaScript
e. None of the above
How would you double the size of an image on a mouseover event, if the original width and height are both 100px and the id of the image is 'logo'?
a. document.getElementById('logo').style.width="200"
document.getElementById('logo').style.height="200"
b. document.getElementById('logo').style.width="100"
document.getElementById('logo').style.height="100"
c. document.getElementById('logo').width="200"
document.getElementById('logo').height="200"
d. document.getElementById('logo').width="100"
document.getElementById('logo').height="100"
How would you test whether a browser window is still open?
a. Use the isOpen method of the window object reference variable
b. It is not possible to test whether an instance of the browser window is open
c. Use the closed property of the window object to test whether the window is open
d. Use the open method of the document object
Which of the following are not valid HTML 4.01 events?
a. onmouseover
b. onmouseout
c. onmouseabove
d. onmousedown
e. onmousein
You do not want the end user to see the JavaScript code embedded in the HTML file when the user views the source of the HTML page. How would you achieve this?
a. This is not possible
b. Use externally linked files which hold the JavaScript code
c. Modify the headers of the page
d. Change the MIME type of the page
When does the load event of the browser occur?
a. When the browser receives header information
b. When the browser starts displaying the content
c. When the browser receives all the page information, including framesets
d. When the browser receives all the page information, including framesets and displays it
e. None of the above
You are writing a JavaScript function that will accept user input. Which of the following will you use?
a. The prompt method
b. The alert method
c. A form field
d. All of the above
You want to validate the value in a field as soon as the user moves out of the field by pressing the tab key. Which event would you use?
a. onblur
b. onfocus
c. lostfocus
d. gotfocus
e. None of the above
A form contains two fields named id1 and id2.  How can you copy the value of the id2 field to id1?
a. document.forms[0].id1.value=document.forms[0].id2.value
b. document.forms[0].id2.value=document.forms[0].id1.value
c. document.id1.value=document.id2.value
d. document.id2.value=document.id1.value
Consider an HTML form with a checkbox and a text field. When data is entered and the return key is pressed, the data seems to be lost before the user can click on the button that calls the processing function. How do you correct this?
a. Add a TYPE=HIDDEN INPUT to the form
b. Trap the return keypress and return (null)
c. Add 'return false' to onsubmit="..." in the FORM tag
d. Instruct the user not to press the Return key
e. Modify the ENCTYPE property of the form
Which of the following can you use to determine whether cookies are enabled in a browser or not?
a. (navigator.Cookie)? true : false
b. (application.cookieEnabled)? true : false
c. (navigator.cookieEnabled)? true : false
d. (application.cookie)? true : false
Which of the following could you do using JavaScript?
a. Form validation
b. Display a popup window
c. Change the color of the page
d. Display an alert
e. Prompt the user to enter a value
f. All of the Above
Consider the following JavaScript validation function:

<script type="text/JavaScript">
function ValidateField()
{
        if(document.forms[0].txtId.value =="")
                {return false;}

        return true;
}
</script>

Which of the options will you use for calling this function as soon as the user leaves the field?
a. <input name=txtId type="text" onreset="return ValidateField()">
b. <input name=txtId type="text" onfocus="return ValidateField()">
c. <input name=txtId type="text" onsubmit="return ValidateField()">
d. <input name=txtId type="text" onblur="return ValidateField()">