CS131 Introduction to Computer Science I Final Exam (200 Points) Fall 2002, Section 1 Instructor: Jill Forrester Name: _____________________________________________ Score: _____ /200 True/False (2 points each – 20 points total): von Neumann originated the idea that instructions could be stored along ___________ John with data in main memory. ___________ Computer Science is best described as the study of computers. are four built-in data types in JavaScript: number, character, boolean, and ___________ There string. ___________ JavaScript functions produce one or more outputs. Turing created a computer called COLOSSUS during World War II to decrypt ___________ Alan German secret messages. ___________ An assignment statement stores data to a variable in JavaScript. ___________ An integrated circuit is composed of thousands of vacuum tubes. ___________ Since writing one is difficult, an operating system is an example of hardware. The delay experienced in transferring a piece of data from memory to the CPU in ___________ the Knob & Switch Computer was unrealistic since memory speed continues to outpace processor speed. ___________ The Internet was developed to reduce paper consumption by the EPA. Multiple Choice. Circle the best answer. (2 points each – 20 points total) 1. What has been called “the most important invention of the 20th century?” a. The ENIAC Computer b. Pokemon c. High-level pogramming lnguages d. The transistor 2. What is a. b. c. d. meant by the term semiconductor? A material that conducts electricity without a problem. A material that does not conduct electricity. A material that can have it's conductivity altered by the introduction of impurities. A material that conducts electricity extremely well, but only at very low temperatures. 3. An algorithm is a. a component of cache memory. b. a new type of system bus from Intel. c. a logical sequence of steps to solve a problem. d. part of the BIOS. 4. Which of the following is not a property of good design? a. The resulting program is easier to debug. Page 1 of 12 b. The resulting program will never have to be altered again. c. The resulting program is easier to write. d. The resulting program in easier to understand. 5. In which type of memory is access time independent of location? a. Sequential Access Memory. b. Random Access Memory. c. Read-Only Memory. d. Binary Memory. e. None of the above 6. Pseudocode is a. a new programming language from Microsoft. b. an English-like description of an algorithm. c. stepwise Refinement. d. poorly written JavaScript code. 7. The clock speed of a CPU is determined by a. how fast items can be transferred between the components of the CPU. b. Eastern Standard Time. c. the word size of the CPU. d. the International Standards Organization. 8. Which a. b. c. d. of the following is not a component of the von Neumann architecture: Input and output devices Memory A Compiler The Central Processing Unit 9. Which a. b. c. d. of the following is a component of a Central Processing Unit? A disk drive Main memory The data vortex An arithmetic-logic unit 10. Which of the following expressions is equivalent to if (g > 100) { document.write(“Out of range”); } if (g < 0) { document.write(“Out of range”); } a. if (g > 100 && g < 0) { document.write(“Out of range”); } b. if (g > 100 && < 0) { document.write(“Out of range”); } c. if (g > 100 || g < 0) { document.write(“Out of range”); } d. none of the above. Page 2 of 12 Short Answer (38 points total) 1. (10 points) For the following questions assume you are restricted to a fixed precision of 4 bits (i.e., numbers must consist of exactly 4 bits, no more, no less) a. Convert the following to decimal: i. 0110 ii. 1101 b. Perform the following addition in binary (show steps and final result in binary): i. 1110 + 0010 ii. Did the addition cause an overflow? Explain why or why not? 2. (10 points) a. Explain the difference between a low-level language and a high-level language. b. Would you categorize the following languages as low-level or high-level? i. machine code ii. JavaScript iii. assembly code Page 3 of 12 3. (8 points) Of the eight statements listed below, six are stages of the Program Development Life Cycle. Number the correct steps in the order they should occur. _________ Code the program. Translate the algorithm into JavaScript syntax. _________ Complete the documentation. Organize all material that describes the program for future maintenance. _________ Analyze and define the problem; i.e. understand what the program should do; do a preliminary design of the GUI. _________ Test the program and fix existing bugs. _________ Ignore the problem. Perhaps it will go away. _________ Design and Plan the solution to the problem. Formulate an algorithm. _________ Based on the design of the solution, finalize the GUI. Determine how the input will be obtained and how the output will be displayed. _________ Upload the finished program onto alpha and put a link to it from your homepage. 4. (10 points) Finish the truth table for the following circuit: A B C | Z -----------------------0 0 0 | 0 0 1 | 0 1 0 | 0 1 1 | 1 0 0 | 1 0 1 | 1 1 0 | 1 1 1 | Tracing JavaScript Code (38 points total) Page 4 of 12 1. (12 points) What is returned by the following JavaScript function function snafu(n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else if (n > 0) { return 18 - Math.pow(n, 2) / 2 - 10; } return -1; } a. if we call snafu passing in 0? b. if we call snafu passing in 4? c. if we call snafu passing in -10? 2. (14 points) Write the output of the following segment of code in the space below: var numRows, currentCols, i, j; numRows = 5; currentCols = 5; for(i = 1; i <= numRows; i = i + 1) { for(j = 1; j <= currentCols; j = j + 1) { document.write(j + " "); } document.write("<BR>"); currentCols = currentCols - 1; } 3. (12 points) Consider the following JavaScript function definitions: Page 5 of 12 function doSomething(str) { var copy; copy = ""; for (i = 0; i < str.length; i = i + 1) { copy = str.charAt(i) + copy; } return copy; } function whatDoIDo(str) { if(str.length % 2 == 0) return doSomething(str); else return str.toUpperCase(); } What is returned from the following function calls: a. doSomething(“cow”) b. whatDoIDo(“cat”) c. whatDoIDo(“deer”) Writing JavaScript Code (84 points total) 1. (22 points) Suppose we are writing a program for an amusement park that sells tickets as follows: $12.95 for children 12 or under $14.95 for seniors 65 or older $19.95 for all other park guests The park wishes to keep track of how many tickets of each type are sold during the day. Write a function called takeTickets that repeatedly prompts the ticket taker for an age until a –1 is entered. For each age entered, the appropriate text box counter should be updated (i.e., either add one to the senior, child, or adult count) and the appropriate price should be displayed in another text box. The HTML for the page would appear in the body as follows: <FORM NAME=”TicketForm”> <INPUT <INPUT <INPUT <INPUT TYPE=”text” TYPE=”text” TYPE=”text” TYPE=”text” NAME=”price” SIZE=8 VALUE=””> <BR> NAME=”seniors” SIZE=6 VALUE=”0”> <BR> NAME=”children” SIZE=6 VALUE=”0”> <BR> NAME=”adults” SIZE=6 VALUE=”0”> <!— Note: we also have a button that invokes takeTickets --> </FORM> Complete the function started for you below: Page 6 of 12 function takeTickets() { } 2. (22 points) The game of Yahtzee! involves rolling five six-sided dice. Players score points based upon patterns produced by the dice (for example, three of the same number, etc.). a. In order to play, we will have to simulate rolling five six-sided dice. Write code below that will first generate a random integer from 1 to 6 and then select a die image to display. Assume the images have names “die1.gif”, “die2.gif”, up to “die6.gif” and that the image tag in the body is given as <IMG NAME=”firstDie” SRC=”blank.gif”> Note : you only have to write code for rolling a single die. Page 7 of 12 b. A player scores a yahtzee if all five dice have the same number (e.g., five sixes appear on the dice). Complete the if condition in the following function (use as short a condition as possible): function isYahtzee(die1, die2, die3, die4, die5) { // Note that all parameters have numeric values if( ) return true; // we have a yahtzee else return false; } c. A player scores a large straight if the value of the second die is one greater than the value of the first die, the value of the third die is one greater than the value of the second die, etc. for all of the dice. For example, 2 3 4 5 6 is a large straight but 1 3 4 5 6 is not since 3 is not one greater than 1. For the following function, you may assume that the values of the parameters are ORDERED; that is, die1 <= die2 <= die3 <= die4 <= die5 Complete the if condition in the following function: function isLargeStraight(die1, die2, die3, die4, die5) { // Note that all parameters have numeric values if( ) return true; // we have a large straight else return false; } d. For this problem, you may assume that the following functions are already defined for you: isThreeOfAKind(die1, die2, die3, die4, die5) – returns true if exactly three of the dice passed into the function have the same value; otherwise, returns false. isTwoOfAKind(die1, die2, die3, die4, die5) – returns true if exactly two of the dice passed into the function have the same value; otherwise, returns false. In yahtzee, a full house occurs when exactly two of the dice have one value while the three other dice have another value. For example, 3 3 3 2 2 and 1 1 4 4 4 are both full houses. In the following code, complete the loop condition so that the loop continues until a full house is obtained or 100 rolls are completed. Complete the if condition to determine if a full house is obtained. Page 8 of 12 Remember…Complete both the loop condition and the if condition in the following code: var die1, die2, die3, die4, die5; var fullHouse, numRolls; fullHouse = false; // switch to true if we get a full house numRolls = 0; while ( ) { // Assume the dice are rolled correctly here and that // the local variables die1, die2, die3, die4, and die5 // contain the numeric values associated with the dice. if ( ) { fullHouse = true; // we have a full house } numRolls = numRolls + 1; } 3. (22 points) Write the following segments of code. a. Create a JavaScript function called findSubstringStartingAt that takes two input parameters (both strings, call them target and searchString). The function finds the location of the first occurrence of searchString in the string target and returns the rest of target starting at that position. For example, findSubstringStartingAt("yabba dabba do", "d") returns "dabba do". ( Hint: the return string can be built using two existing string methods.) You should return the null (empty) string in the case where the searchString is not found in the target. Page 9 of 12 b. Create a JavaScript function called obscureChar that takes two input parameters (both strings, call them target and searchChar) and returns the target string with each instance of the searchChar replaced by an asterisk (*). For example, obscureChar("yabba dabba do", "a") returns "y*bb* d*bb* do". You may assume that the second string is a single character. Page 10 of 12 4. (18 points) a. Write a function called minimum that takes a single parameter indicating the length of a sequence of numbers to be input by the user (that is, if 10 is passed in, your function should prompt for a number 10 times). As your function prompts for numbers, it should keep track of the smallest value entered by the user. After all data is input, the function should return this minimum. b. Suppose we wish to create an event-driven form that allows the user to specify a sequence length in a text box and then call minimum by clicking a button. The minimum value returned should be stored in a second text box. <FORM NAME=”MinForm”> <INPUT TYPE=”text” NAME=”length” SIZE=4 VALUE=””><BR> <INPUT TYPE=”text” NAME=”min” SIZE=6 VALUE=””><BR> <INPUT TYPE=”button” NAME=”findMin” VALUE=”Find Minimum” ????? > </FORM> Given the previous description of the desired behavior, indicate below what the ????? should be replaced with in the HTML above to make the web page work as desired. Page 11 of 12 Bonus Points 1. (2 points) What is your opinion of this test? 2. (1 point) Would you recommend this class to a friend? 3. (1 point) What was your favorite lab or other assignment? 4. (1 point) What was your least favorite lab or other assignment? Hope you learned something and enjoyed the course. Have a wonderful winter break! Page 12 of 12