CGS 3066: Practice Test 2

advertisement
CGS 3066: Practice Test 2
The questions below are indicative of the questions that you may be asked on the test; the length of this
practice test is not indicative of the length of the unit test. Answers are included at the end of this document;
you are encouraged to attempt all questions before reviewing the answers.
Part 1: Multiple Choice
1. Consider a page that has 2 forms on it. The first is named ”getContact”. The second is named “getInfo”. If
the second form has a text box named “marbles”, which of the following correctly references value of the text
box?
a. document.forms[0].getInfo.marbles
b. document.forms[1].marbles.value
c. document.forms[getInfo].marbles.value
d. document.getContact.marbles.value
2. Consider the JavaScript statement var s = 5 + “5”. What value is assigned to s?
a. 55
b. 10
c. statement is illegal and causes an error
d. none of the above
3. Consider the JavaScript statement var s = 5 + parseInt(“5”). What value is assigned to s?
a. 55
b. 10
c. statement is illegal and causes an error
d. none of the above
4. JavaScript functions are required to include a return statement
a. False
b. True
5. The JavaScript statement s *= (y++ % 3) is equivalent to which of the following?
a. s = s * ((y + 1) % 3)
b. s = s * (y +1 % 3)
c. y = y + 1; s = s * (y % 3)
d. s = s * (y % 3); y = y + 1
6. Consider the following JavaScript statements: x = 5 and y = 6.
What will the expression (x == y) || (x < 13) evaluate to?
a. False
b. True
7. Within JavaScript, the body of a do…while loop is guaranteed to execute at least once.
a. False
b. True
CGS 3066 Practice Test 2
1
8. The Document Object Model provides access to every element in a document.
a. False
b. True
9. Inside which XHTML element is JavaScript placed?
a. <script>
b. <javascript>
c. <js>
d. <scripting>
10. Where is the correct place to insert a JavaScript?
a. The <head> section
b. The <body> section
c. Any of the above
11. How can you add a comment in JavaScript?
a. //This is a comment
b. ‘This is a comment
c. <!--This is a comment-->
12. An external JavaScript file must contain the <script> tag
a. False
b. True
13. JavaScript is case-sensitive
a. False
b. True
14. Within JavaScript, variables that are declared (using the keyword var) within a function:
a. can only be accessed within that function.
b. when you exit the function, the variable is destroyed.
c. these variables are called local variables.
d. all of the above
15. Which of the following property, of the window object, sets/returns the text of the status bar?
a. window.statusBar
b. window.status
c. window.status()
d. window.statusBar()
16. window.alert() and window.open() create dialog boxes that require user input for the page to continue
processing
a. False
b. True
CGS 3066 Practice Test 2
2
17. How many times does the loop, below, execute?
<script type="text/javascript">
<!-v = 2;
while (v > -10)
{
p = v + 1;
v = v-2;
}
-->
</script>
a.
b.
c.
d.
0
4
5
6
18. What are the values of the variables "n" and "c" when the following code finishes execution?
<script type="text/javascript">
<!-var c = 0;
var n = 0;
do
{
switch (c % 3)
{
case 0:
case 1:
n += 3;
break;
case 2:
n *= 2;
c++;
break;
}
c++;
} while ( c <= 6 )
-->
</script>
a.
b.
c.
d.
n = 30; c =
n = 30; c =
n = 18; c =
n = 18; c =
8
7
8
7
CGS 3066 Practice Test 2
3
19. What is the value of the variable “gibberish” when the following code finishes execution?
<script type="text/javascript">
<!-var startValue = 1;
var gibberish = " zero ";
var numbers = new Array;
numbers[0] = " one ";
numbers[1] = " two ";
numbers[2] = " three ";
if (startValue < 0)
{
startValue = startValue * -1;
}
for (var i = startValue; i < numbers.length; i++)
{
gibberish += numbers[i];
}
-->
</script>
a.
b.
c.
d.
one two three
zero one two three
zero two three
zero three two
20. How many new windows does the code below create?
<script type="text/javascript">
<!-for (i = 0; i < 4; i++)
{
window.open("somePage.html", "w2");
}
-->
</script>
a.
b.
c.
d.
0
1
4
5
21. The JavaScript statements self.close() and window.close() are equivalent to one another.
a. False
b. True
CGS 3066 Practice Test 2
4
For questions 22-25, consider the code snippets below.
<script type="text/javascript">
<!-function func(f)
{
//statements here
return false;
}
//-->
</script>
<form name="form1" id="form1" method="post" action="" onsubmit="return func(this);">
<p>
<input type="text" name="name" />
</p>
<p>
<input name="a" type="radio" value="yes" /> Yes
<input name="a" type="radio" value="no" /> No
<input name="a" type="radio" value="maybeso" /> Maybe so
</p>
<p>
<select name="size">
<option value=""></option>
<option value="small">S</option>
<option value="medium">M</option>
<option value="large">L</option>
<option value="x-large">XL</option>
</select>
</p>
<input name="submit" type="submit" value="submit form" />
</form>
22. Within the function "func", which of the following would return the value the form element named, "name"?
a. f.name.text
b. f.name.value
c. f.name
d. f.name.input
23. Within the function "func", which of the following would return the number of radio buttons whose name is
"a"?
a. f.length
b. f.a.length.value
c. f.a.length
d. f.a.radio.num
24. Within the function "func", which of the following would return the value of the first radio button, in the
series of radio buttons whose name is "a"?
a. f.a.[1].value
b. f.a.[0].value
c. f.a[0].value
d. f.a[1].value
CGS 3066 Practice Test 2
5
25. Within the function "func", which of the following would return the value of the selected option of the select
drop-down menu?
a. f.size[f.size.selectedIndex].value
b. f.size.selectedIndex.value
c. f.size.options[f.size.selectedIndex].value
d. f.size.value
26. What does the following code output?
var s = "Hello. ";
s = "Hello and Goodbye. ";
s = "Goodbye. " + s;
document.write(s);
a.
b.
c.
d.
Goodbye. Hello and Goodbye. Hello.
Hello and Goodbye. Goodbye.
Hello. Hello and Goodbye. Goodbye.
Goodbye. Hello and Goodbye.
27. Consider the JavaScript statement below. How would you return the last, and only the last, character of
string?
var s = "red blue green";
a.
b.
c.
d.
s.charAt(s.length);
s.charAt(s.length-1);
s.charAt(s.length+1);
s.charAt("n");
28. Which of the following creates a dialog box that allows the user to type in input?
a. window.alert()
b. window.confirm()
c. window.prompt()
d. window.opener()
29. How do you write "Hello World" in an alert box?
a. window.alert.value ="Hello World"
b. window.alert("Hello World")
c. window.alert.msg = "Hello World"
d. window.alert() = "Hello World"
30. Consider the following JavaScript statements. What does the script output?
var str = "some string"
if (str.indexOf("ring") != -1)
{ document.write("True"); }
else
{ document.write("False"); }
a. False
b. True
CGS 3066 Practice Test 2
6
31. Consider the following JavaScript statements: x = 5 and y = 6.
What will the expression (x == y) || (!x < 13) && (y > y) evaluate to?
a. False
b. True
32. Consider the following JavaScript statements: x = 5 and y = 6.
What will the expression (x = y) || (x > 13) evaluate to? (Be careful).
a. False
b. True
33. Assume that a browser window was opened by another window. Which of the following returns the name
of that window?
a. window.opener
b. window.open
c. window.opener.name
d. window.open.name
34. Consider the following JavaScript statements: var w = window.open("somepage.html", "win4"). How would
you close the newly opened window?
a. w.close()
b. w.closed = true
c. win4.closed = true
d. win4.close()
35. Consider the following JavaScript statements: var x = 5; x += 6 + ++x; What is the value of x after these
statements are executed?
a. 5
b. 6
c. 16
d. 17
36. A JavaScript function, which is placed within the body section, is guaranteed to be executed at least once
a. False
b. True
37. A JavaScript function may be called by which of the following:
a. an event handler
b. other JavaScript code
c. a link
d. any of the above
CGS 3066 Practice Test 2
7
Part 2: Code Writing
Write code to produce the indicated output.
Given the for-loop below, write an equivalent while loop.
var p = 1;
for (i = 0; i < 15; i++)
{
p = p * i;
}
Given the if-else if-else construct below, write an equivalent switch statement.
if (c == "a" || c == "b")
{
document.write("matched a lowercase letter");
}
else if (c == "A" || c == "B")
{
document.write("matched an uppercase letter");
}
else
{
document.write("no match");
}
CGS 3066 Practice Test 2
8
Write JavaScript code that will be placed in head section. The code should set the document title to be the
filename. I.e., if the file is named somepage.html, then the document title should become somepage.html
You are given 2 functions, named f1() and f2(). Write JavaScript code that will call f1() if some variable, call it y,
is less than or equal to 3 and some variable, call it x, is greater than or equal to 13.
CGS 3066 Practice Test 2
9
Write a JavaScript function that takes one argument, call it n . The script determines if n is a power of two. If n
is a power of two, it should return the power. If not, it should return -1. The script need only consider numbers
greater than or equal to 1. For example,
isPowTwo(1) returns 0
isPowTwo(2) returns 1
isPowTwo(248) returns -1
isPowTwo(256) returns 8
Write a JavaScript function that sets the value of the status bar to the name of the current window. If the
current window has no name, then the function sets the value of the status bar to the current URL.
CGS 3066 Practice Test 2
10
Write a HTML-form and a JavaScript validation function. The form must contain 2 checkboxes and a submit
button. If the form is submitted, then the validation function should allow the form to be passed to the action
page only if one of the checkboxes has been checked; otherwise, it should output an appropriate message to
the user and prevent the form from being passed to the action page.
Same question as above except that the validation function should convey, via an alert box, how many
checkbox(es) were checked and should not pass the form to the action page. To do this, you may not need to
modify your form. If this is the case, then you need only write the new validation function.
CGS 3066 Practice Test 2
11
Write a JavaScript function that determines how many times a given word is included within a larger piece of
text. The function should take 2 arguments: the first is the text to be searched and the second is the word that
you are looking for. Example function calls:
s = "ring around the rosey"
n = "ring"
countWord(s, n) returns 1
s = "ring around the ringy rosey"
n = "ring"
countWord(s, n) returns 2
s = "ring ring ring"
n = "ring"
countWord(s, n) returns 3
s = "wrong wrong wrong"
n = "ring"
countWord(s, n) returns 0
CGS 3066 Practice Test 2
12
Object Definitions
These object definitions will be provided with the test.
String Object
Form Object
Properties
• length
Objects
• checkbox
• radio
• reset
• select
• submit
• text
• textarea
Methods
• charAt(index)
• indexOf(substring, index)
• lastIndexOf(substring, index)
• replace(strFind, strReplace)
• slice(start, end)
• split(delimiter)
• substr(start, length)
• substring(start, end)
• toLowerCase()
Window Object
Properties
• closed
• name
• opener
• self
• status
Properties
• elements
• length
• method
• name
Methods
• reset()
• submit()
Methods
• alert("msg")
• blur()
• close()
• confirm("msg")
• focus()
• open("URL", "name", "specs", replace)
• prompt("msg", "default")
Document Object
Properties
• lastModified
• referrer
• title
• URL
Methods
• write("str")
CGS 3066 Practice Test 2
13
Answers
1. B
18. B
35. D
2. A
19. C
36. A
3. B
20. B
37. D
4. A
21. B
5. D
22. B
6. B
23. C
7. B
24. C
8. B
25. C
9. A
26. D
10. C
27. B
11. A
28. C
12. A
29. B
13. B
30. B
14. D
31. A
15. B
32. A
16. A
33. C
17. D
34. A
CGS 3066 Practice Test 2
14
Part 2: Code Writing
Write code to produce the indicated output.
Given the for-loop below, write an equivalent while loop.
var p = 1;
for (i = 0; i < 15; i++)
{
p = p * i;
}
var p = 1;
i = 0;
while (i < 15)
{
p = p * i++;
}
Given the if-else if-else construct below, write an equivalent switch statement.
if (c == "a" || c == "b")
{
document.write("matched a lowercase letter");
}
else if (c == "A" || c == "B")
{
document.write("matched an uppercase letter");
}
else
{
document.write("no match");
}
switch (c)
{
case "a":
case "b":
document.write("matched a lowercase letter");
break;
case "A":
case "B":
document.write("matched an uppercase letter");
break;
default:
document.write("no match");
}
CGS 3066 Practice Test 2
15
Write JavaScript code that will be placed in head section. The code should set the document title to be the
filename. I.e., if the file is named somepage.html, then the document title should become somepage.html
//solution 1
document.title = document.URL.substring(document.URL.lastIndexOf("\\")+1);
//solution 2
var pgURL = document.URL;
var n = pgURL.lastIndexOf("\\");
document.title = pgURL.substring(n+1);
//solution 3
var pgURL = document.URL;
var pgTitle = "";
var n = pgURL.indexOf("\\");
do
{
pgTitle = pgURL.substring(n+1);
n = pgURL.indexOf("\\", n+1);
} while (n != -1)
document.title = pgTitle;
You are given a functions, call it f1(). Write JavaScript code that will call f1() if some variable, call it y, is less
than or equal to 3 and some variable, call it x, is greater than or equal to 13.
if (y <=3 && x >=13)
{
f1();
}
CGS 3066 Practice Test 2
16
Write a JavaScript function that takes one argument, call it n . The script determines if n is a power of two. If n
is a power of two, it should return the power. If not, it should return -1. The script need only consider numbers
greater than or equal to 1. For example, if the function was called isPowTwo():
isPowTwo(1) returns 0
isPowTwo(2) returns 1
isPowTwo(248) returns -1
isPowTwo(256) returns 8
function isPowTwo(n)
{
var rVal = -1; //rVal is the value to be returned; -1 by default
var p = 0;
//p is the power. update this via while loop
/*while loop
condition: uses the mod operator to see if current value of n is even and greater
than 1. if it is, then it could be a power of two.
loop: divides n by 2 and increments p by one. Dividing by 2 reduces n by 1 power
of 2; p then keeps track of that.
*/
while ((n % 2) == 0 && n > 1)
{
n = n / 2;
p++;
}
/*if, after the while loop, n is 1 then it was either 1 to before the loop or it
was reduced to 1 by the while loop. in either case, it is a power of 2. update
rVal accordingly
*/
if (n == 1)
{ rVal = p; }
return rVal;
}
CGS 3066 Practice Test 2
17
Write a JavaScript function that sets the value of the status bar to the name of the current window. If the
current window has no name, then the function sets the value of the status bar to the last modified time of the
document
function setStatus()
{
if (window.name == "")
{
window.status = document.lastModified;
}
else
{
window.status = window.name;
}
}
CGS 3066 Practice Test 2
18
Write a HTML-form and a JavaScript validation function. The form must contain 2 checkboxes and a submit
button. If the form is submitted, then the validation function should allow the form to be passed to the action
page only if one of the checkboxes has been checked; otherwise, it should output an appropriate message to
the user and prevent the form from being passed to the action page.
function val(f)
{
if (f.c[0].checked || f.c[1].checked)
{
return true;
}
else
{
window.alert("Check at least 1 checkbox");
return false;
}
}
<form name="form1" id="form1" method="post" action="" onsubmit="return
val(this);">
<input type="checkbox" name="c" value="someValue" />
<input type="checkbox" name="c" value="someOtherValue" />
<input type="submit" name="submit" value="submit" />
</form>
Same question as above except that the validation function should convey, via an alert box, how many
checkbox were checked and should not pass the form to the action page. To do this, you may not need to
modify your form. If this is the case, then you need only write the new validation function.
function val(f)
{
var numChecked = 0;
for (i = 0; i < f.c.length; i++)
{
if (f.c[i].checked)
{
numChecked++;
}
}
window.alert("You checked " + numChecked + " checkbox(es)");
return false;
}
CGS 3066 Practice Test 2
19
Write a JavaScript function that determines how many times a given word is included within a larger piece of
text. The function should take 2 arguments: the first is the text to be searched and the second is the word that
you are looking for. Example function calls:
s = "ring around the rosey"
n = "ring"
countWord(s, n) returns 1
s = "ring around the ringy rosey"
n = "ring"
countWord(s, n) returns 2
s = "ring ring ring"
n = "ring"
countWord(s, n) returns 3
s = "wrong wrong wrong"
n = "ring"
countWord(s, n) returns 0
//solution 1
function countWord(s1, s2)
{
var n = 0;
var x = 0;
while (s1.indexOf(s2, x) != -1)
{
x = s1.indexOf(s2, x) + s2.length;
n++;
}
return n;
}
//solution 2
function countWord(s1, s2)
{
var n = 0;
var x = 0;
while (s1.indexOf(s2) != -1)
{
x = s1.indexOf(s2) + s2.length;
s1 = s1.substr(x);
n++;
}
}
return n;
CGS 3066 Practice Test 2
20
Download