JavaScript

Choose all answers that apply !

1. Choose another way to write
a = x ? a = b : a = c
a. if (‘x’) { a = b; } else { a = c; }
b. if (x) { a = c; } else { a = b; }
c. a == x ? a = b : a = c
d. x : a = c ? a = b
e. none of them is correct

2. Seek the truth 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. none of them is correct

3. How do substring() and substr() differ?
a. One is not a method of the String object.
b. substr() takes three arguments, substring() only two.
c. Only one accepts a desired string length as an argument.
d. Only one is supported by Internet Explorer 6.
e. Besides the spelling, nothing.

4. Which would JavaScript assign to an uninitialized variable?
a. NaN
b. null
c. undefined
d. void
e. 0
f. false

5. Which would JavaScript assign to an uninitialized array element?
a. NaN
b. null
c. undefined
d. void
e. 0
f. false

6. Which is not considered a JavaScript operator?
g. new
h. this
i. delete
j. typeof

7. Which is not an attribute of the cookie property?
a. path
b. host
c. secure
d. domain

8. How do you delete a cookie? (Choose all answers that apply)
a. You can’t. They’re valid until they expire.
b. Set it’s value to null.
c. Set it’s value to undefined.
d. Overwrite it with an expiry date in the past.
e. escape() the value of the path attribute.
f. unescape() the value of the path attribute.

9. How would you add a method to an object class?
a. With the prototype() method.
b. With the prototype property.
c. Sorry. Can’t be done.
d. With the this object.

10. How would you randomly choose an element from an array named arr if the number of
elements changes dynamically?
a. elem = arr[Math.floor(Math.random() * arr.length)];
b. elem = arr[Math.ceil(Math.random() * arr.length)];
c. elem = arr[Math.random(arr.length)];
d. elem = Math.random(arr.length);

11. Consider three variables:
someText = “JavaScript1.2”;
pattern = /(\w+)(\d)\.(\d)/i;
outCome = pattern.exec(someText);
What does outCome[0] contain?
a. undefined
b. null
c. true
d. false
e. 0
f. “JavaScript”
g. 1
h. “1”
i. 2
j. “2”
k. “JavaScript1.2″

12. 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. <input type=”hidden” … />
b. Trap the return keypress and return (null)
c. Add return false; to onsubmit=”…” in the <form>
d. Instruct the user not to press the Return key.

13-16. Study the following HTML source code:
<button id=”button” x=”A”>button</button>
<script type=”text/javascript”>
var x = ‘B’;
var Class = function(x) {
var button = document.getElementById(‘button’);
button.onclick = this.clicked;
this.x = ‘C’;
};
Class.prototype.x = ‘D’;
Class.prototype.clicked = function(x) {
// this.prototype.x = ‘H’;
var x = ‘E’;
alert(this.x);
};
var instance = new Class(‘F’);
instance.clicked(‘G’);
delete instance.x;
instance.clicked();
var clickedOfInstance = instance.clicked;
clickedOfInstance();
</script>
We open this file in a browser, close all of the alerts then we click on the button.
Write down the content of appearing alerts in the correct order, if we use

13. Internet Explorer 6: ………….. ………….. ………….. …………..

14. Mozilla Firefox 3.5: ………….. ………….. ………….. …………..

After uncommenting the line: // this.prototype.x = ‘H’;

15. Internet Explorer 6: ………….. ………….. ………….. …………..

16. Mozilla Firefox 3.5: ………….. ………….. ………….. …………..

17-18. Consider the following HTML page:
<!DOCTYPE html>
<html>
<head>
<script type=”text/javascript”>
alert(‘A’);
window.onload = function() {
alert(‘B’);
};
if (window.addEventListener) {
window.addEventListener(‘DOMContentLoaded’, function() {
alert(‘C’);
}, false);
} else {
window.attachEvent(‘DOMContentLoaded’, function() {
alert(‘D’);
});
}
</script>
</head>
<body>
<div>
the content of the page
</div>
<script type=”text/javascript”>
alert(‘E’);
</script>
</body>
</html>
We open this file in a browse, write down the content of appearing alerts in the correct
order, if we use

17. Internet Explorer 6: ………………………………………………..

18. Mozilla Firefox 3.5: ………………………………………………..
Please classify the following fragments:
[{“a”:”A”,”b”:null,”c”:undefined}]

19. Is it a valid JSON? a. Yes b. No c. Depends on browser

20. Is it a valid JavaScript statement? a. Yes b. No c. Depends on browser
[{“b”:false,”c”:.2}]

21. Is it a valid JSON? a. Yes b. No c. Depends on browser

22. Is it a valid JavaScript statement? a. Yes b. No c. Depends on browser
[{b:false,”c”:0.2}]

23. Is it a valid JSON? a. Yes b. No c. Depends on browser

24. Is it a valid JavaScript statement? a. Yes b. No c. Depends on browser
{“c”:[true]}

25. Is it a valid JSON? a. Yes b. No c. Depends on browser

26. Is it a valid JavaScript statement? a. Yes b. No c. Depends on browser
[“a”,0,Infinity,]

27. Is it a valid JSON? a. Yes b. No c. Depends on browser

28. Is it a valid JavaScript statement? a. Yes b. No c. Depends on browser