You are on page 1of 3

Practice Problems for JavaScript

Multiple Choice Questions 1. _______________ are used to group several statements into a compound statement. A. () B. { } C. <> D. /* */ 2. In JavaScript the for statement is used for A. Assignment B. Increment C. Iteration D. Selection 3. The command to display an alert box in JavaScrpit is A. write.alert B. display.alert C. alert( ) D. alert = text 4. In JavaScript, a line beginning with // is a(n) A. assignment B. comparison C. comment D. function 5. Which of the following are correct JavaScript variable declarations? A. var d = a ( b*c ); B. var time = ((( days * 24) + hours) * 60 + minutes) * 60 sec; C. var name = Sam; D. var bool = true; 6. Which of the following can be used as part of a variable name A. Hyphen B. Blank space C. Underscore _ D. Parentheses ( ) Writing Code Segment. Do not worry about other tags like <html>, <body> and so on. Start and end with the <Script> tag. 1. Assume that you are creating a new array called seasons A. Write the statement to declare the array with space allotted for four elements. B. Assuming that the array has been created, write a statement to set the element at the initial position of the array to the season name spring. C. Write a loop that prints out all of the elements in seasons.

2. Assign half the value of the variable B to the 14th element of the array J. (Make all necessary variable declarations.) 3. Create an array of the correct size to hold the names of the days in a weekend and get the user to input names of all the days one by one.

Find the errors in the code. There are three errors in each question 1. for (var i=1; i<6; i++) { if (i= 4) { document.write(i+<br>); }

2. while (x<=12) { doc.write("<h3>" + x + " times table</h3>") for(i = 1; i <= 12; i++) document.write("<li>" + i + " times " + x + " = " + i * x) x+; } What will be the output for the following lines of code? Be careful to be precise with your output- line breaks, alphabet case, etc. Remember that your output will have the text within a document.write as output too. Draw a box and write the output within it. 1. <script type="text/javascript"> var start = 1; var end = 4; var current = start; var sum = 0; while (current <= end) { sum = sum + current; current ++; } document.write( "The sum of natural numbers up to 4 is </script> 2. <script type="text/javascript"> for (i = 1; i <= 6; i++) { document.write("<h" + i + ">This is header " + i); document.write("</h" + i + ">"); }

"+ sum);

</script> 3. <script type="text/javascript"> var a = 2, b=4, c= 10; b = --a + 2 * b++; c = ++c + a-- - b--; document.write("These are the results: " ); document.write(a+"<br>" + b + "<br>" + c); </script> 4. <script type="text/javascript"> var a = 5; var b = 2; var c = 10; var d = b + (a + a * b) *c ; document.write ("The value of d is " + d + "."); </script> Write JavaScript code for the following: 1. Declare and initialize the students and scores arrays again. Make a new array for grades. If the score is greater than 81 assign A, between 61 and 80 assign B, between 41 and 60 assign C and lower than 41 assign D. Using document.write display on the screen the names, scores and grades for all the students in a table. 2. Write a JavaScript program to take as input three numbers from the user. Find the minimum and maximum of the three numbers. Print the following output in BOLD in the following format: MINIMUM = MAXIMUM = 3. Write a program to find and print how many times the number 8 occurs in an array A. The array A should take as input as many values as the user wishes to enter. 4. Write a program to find and print how many times the digit 8 occurs in an array A. The array A should take as input as many values as the user wishes to enter. [This is lot more complicated.way above the level of difficulty of the exam. But it is an interesting exercise] Do the following conditional statements evaluates to true or false. a = 10 , b = 4 1. ( a > 8 2. (!(a<=10) 3. !( a>9 && || && b > 5) b=3) b<=7)

Are the following statements equivalent for any Boolean values of the variables !(a && b) (!a || !b)

You might also like