118w Midterm 2
What would this code print? var theArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; var total1 = 0; for ( var i = 0; i < theArray.length; i++ ) total1 *= theArray[ i ]; document.write(total1);
0
The length of an array in a particular Javascript program is 6. The subscripts of the first and second last elements in this array are:
0 and 4
What will the browser display if the following script is executed? <script type = "text/javascript"> <!-- var theArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; modifyArray( theArray[ 3 ] ); document.write( theArray.join( " " ) ); function modifyArray( i ) { i = 11; } //--> </script>
1 2 3 4 5 6 7 8 9
What is the result of the statement 22 % 5?
2
Please read the code below: var n1 = new Array( 5 ); for ( var i = 0; i < n1.length; ++i ) n1[ i ] = i+2; The code above assigns ______________ values to elements of Array n1
2,3,4,5,6
What would the browser display if the following code were executed in a script? var product = 10; while (product <= 18); product = 2 * product; document.writeln( product );
20
Math.pow(3,3) and Math.floor(-9.8) will return ________ and __________ respectively
27 and -10
What is the value of i after the following statements? i = 0; --i; i += 29;
28
What is the value of i after the following statements? i = 3; i++; i--;
3
What is the value of num after the following statement is performed? num = 3 * Math.ceil ( 2.3 );
3
What would the browser display if the following script were executed? <script type = "text/javascript"> var i = 0; do { document.write( "O" ); i++; } while ( i > 5 ); </script>
O
What would the browser display if the following script were executed? <script type = "text/javascript"> for( var i = 0; i < 3; i++ ) document.write( "O" ); </script>
OOO
What would the browser display if the following code were executed in a script? var grade = 60; if ( grade >= 60 ) document.writeln( "Passed." ); else document.write( "Failed. " ); document.writeln( "You must take this course again." );
Passed. You must take this course again.
What would the browser display if the following script were executed? <script type = "text/javascript"> for( var i = 0; i <= 4; i++ ) document.write( "X" ); </script>
XXXXX
The document.write( "<h1 style = ___"color: magenta___">" ); In the above line of code, enter the escape character in the blanks
\
Which of the following is the correct abbreviation for the statement a = a + 2; ?
a += 2;
In the following line of code: var x = 4; var y = 5 document.write(x + y) the + sign acts as a ___________ operator
addition
Which of the following is the proper method to access the length of the array arr?
arr.length
What is the problem with the following code: var face, frequency = [0, 0, 0, 0, 0, 0 ]; // summarize results for ( var roll = 1; roll <= 6000; ++roll ) { face = Math.floor( 1 + Math.random() * 6 ); ++frequency[ face ];
array frequency is too small
Which of the following statements is correctly written?
if ( studentGrade >= 60 ) document.write( "Passed" );
To refer to a particular location or element in the array, we specify the name of the array and the ________ of the particular element in the array.
index
In the following line, the word round is a(n) _________ that performs a task or action in the script. amount = principal * Math.pow( 1.0 + rate, year ); document.writeln( "<tbody><tr><td>" + year + "</td><td>" + Math.round( amount * 100 ) / 100 + "</td></tr>");
method
What would the browser display if the following code were executed in a script? var x = 0, y = 1; if ( x > 0 ) if ( y == 1 ) document.writeln( "y is equal to 1" ); else document.writeln( "x is equal to 0" );
nothing
_________ converts its string argument to a float
parseFloat
In JavaScript, all objects and arrays are passed to functions by ________.
reference
What type of loop is shown in the script below? <script type = "text/javascript"> var gradeValue = 0, total = 0, grade = 0; while ( gradeValue != - 1 ) { total = total + gradeValue; grade = window.prompt( "Enter Integer Grade, -1 to Quit:" , "0" ); gradeValue = parseInt( grade ); } </script>
sentinel controlled
The _____________ method returns a string in which all uppercase letters are converted to lowercase letters.
toLowerCase();
Which of the following is the proper method to dynamically allocate memory to an array of 100 elements?
var c = new Array( 100 );
Which of the following is an illegal array initialization statement?
var n[ 10, 20, 30, 40, 50 ];
How would you call the following function if you wanted to find 7-squared, and have the result stored in a variable called result? function square( y ) { return y * y; }
var result = square(7);
To ask the user for input, one could use the follow method:
window.prompt
To divide the value of the first element of array a by 2 and assign the result to the variable x, we would write ________.
x = a[ 0] / 2
What is the value of i after the following statements? i = 1; i--; i--;
-1
What would the browser display if the following script were executed? <script type = "text/javascript"> var count = 5, total = 0; while ( count >= 0 ) { total = total - 10; count = count - 1; } document.write( total ); </script>
-60
What would the browser display if the following script were executed? <script type = "text/javascript"> var count = 0, total = 0; while ( count < 5 ) { total = total + 10; count = count + 1; } document.write( total ); </script>
50
What many times will the following while loop iterate? var counter = 2; // initialization while ( counter < 8 ) { // repetition counter++; }
6
What would the function cube return, assuming it is called with the following statement var result = cube( 25 ) function cube( y ) { return y * y; }
625
Read the code below: <html> <head> <script type = "text/javascript"> var c; c = 33 + 10 * 3; document.writeln(c++); </script> </head> <body> </body> </html> The output of the code will be:
63
What does the following expression evaluate to? ( ( 1 + ( 5 + 1 ) * 7 ) + 2 ) / 5
9
What would the browser display if the following script was executed, and the user entered 9 at both prompts? <script type = "text/javascript"> var firstNumber = window.prompt( "Enter an integer" , "0" ); var secondNumber= window.prompt( "Enter an integer" , "0" ); var thirdNumber; thirdNumber = firstNumber + secondNumber; document.write( thirdNumber ); </script>
99
Which of the following is not a valid equality or relational operator?
=
document.write ( "<img src = \"" + pictures[ 1 + Math.floor( Math.random() * 7 ) ] + ".gif\" width = \"105\" height = \"100\" />" );
All of the above
________________________ creates string containing all array elements
Array.join
What will be the output of this program: [5 points] <html> <head> <title>Using the do...while Repetition Statement</title> <script type = "text/javascript"> var counter = 1; do { document.writeln( "<h" + counter + ">This is " + "an h" + counter + " level head" + "</h" + counter + ">" ); ++counter; } while ( counter <= 2 ); </script> < /head><body></body> </html>
Both (a) and (b)
if (x > 5) if (y > 5) document.writeln("x and y are greater than 5"); else document.writeln("x is <=5" ); The above piece of code has a:
Error in the logic
Every switch statement must have ________ labels that are separated by the keyword break.
case
A variable's value can _______________ during the scriptâ execution, and variable names are case _____________.
change, sensitive
Variable names must begin with a _________or the _____________ .
either a) or b)
Which of the following will evaluate to false?
false && true
Which of the following will not evaluate to true?
false || false
