CS 110 Exam 2 Fall 2011 Name (print): Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than the professor/proctor for ANY reason in ANY language in ANY manner. This exam is closed notes, closed books, and no calculator. Turn all mobile devices off and put them away now. You cannot have them on your desk. Write neatly and clearly indicate your answers. What I cannot read, I will assume to be incorrect. Stop writing when told to do so at the end of the exam. I will take 5 points off your exam if I have to tell you multiple times. Academic misconduct will not be tolerated. Suspected academic misconduct will be immediately referred to the Emory Honor Council. Penalties for misconduct will be a zero on this exam, an F grade in the course, and/or other disciplinary action that may be applied by the Emory Honor Council. Time: This exam has 11 questions on 9 pages including the title page. Please check to make sure all pages are included. You will have 75 minutes to complete this exam. I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Emory community. I have also read and understand the requirements and policies outlined above. Signature: Page: 2 3 4 5 6 7 8 9 Total Points: 10 6 7 7 15 10 10 10 75 Score: 1
1. Fill in the Blank. Answer the following questions about the following HTML/Javascript webpage. <html> <head> <title>my Webpage</title> <script type="text/javascript"> function makenum() { p = Math.random() * 2; p = Math.round(p); p = Math.pow(100,p); document.getelementbyid( numdiv ).innerhtml = p; } </script> </head> <body> <input type="button" value="calculate" onclick="makenum();"> <div id="numdiv"></div> </body> </html> (a) (1 point) What is the name of the function defined in this file? (b) (1 point) How many variables are defined in the function? (c) (1 point) List the names of the variable(s) defined in the function. (a) makenum (b) 1 (d) (2 points) How many Javascript statements are in the webpage? (c) p (e) (2 points) List three functions that are called/invoked in the webpage. (d) 5 (e) random, round, pow (e) getelementbyid, makenum (f) (3 points) Write an appropriate comment which you could place at the beginning of the function to describe how it works. Use the appropriate JavaScript syntax for comments. Solution: //Page generates a random number between 0 and 2 (both inclusive) and raises 100 to that power. So the webpage will eventually display either 100 0, 100 1, or 100 2 (1, 100, 1000). 2
2. (3 points) Is the following statement True or False? Explain your answer. When using a binary search method, the items in our list must always be sorted in increasing order. Solution: False. It s true that items must be sorted in some order, but it doesn t have to be increasing order. Items could be sorted in decreasing order and 3. (3 points) Suppose you have been given an O(N) algorithm that averages student grades, where N is the number of grades. If it takes 1 minute to average 100 grades using the algorithm, how long would it take to average 200 grades? 400 grades? Justify your answers. Solution: Approximately 2 min. for 200 and 4 min. for 400 grades. Since the algorithm is linear, we expect it to scale based on the size of the input. If we were to draw a graph including the line defined by the datapoints (0,0) and (100,1), the line would run through the points (200,2) and (400,4) giving us 2 and 4 minutes. 3
4. (3 points) In your own words, describe what Big-Oh notation is and how it is used in Computer Science. Solution: Big-Oh notation is used to give computer scientists a way to compare algorithms. Since a simple time measurement (such as 1m6sec or.03245sec) on a computer is highly dependent on HW and SW configurations, it isn t enough to simply compare these measurements. Big-Oh gives us a way to compare algorithms regardless of HW, SW, or input size. 5. (4 points) Explain the relationship between machine language, assembly language, and a high-level language. Solution: Machines only understand machine language. Therefore, all instructions must be translated into machine language/binary. For early computers, this is how they were programmed. Assembly language was developed to make it easier to write machine languages. It used some words/abbreviations, but the programmer still had to have intimate knowledge of the hardware. HLL were developed to make programming more closely match the way humans think. However, HLL must be translated into machine languages by an interpretter/compiler in order to run on a machine. 4
6. The video we watched in class discussing the OLPC movement demonstrated many aspects of the digital divide. (a) (2 points) What is the digital divide? Solution: Idea that access to technology/internet isn t evenly distributed amongst nationalities, geographical regions, genders, etc. (b) (3 points) Give three examples from this video of how the OLPC movement is designed to bridge the digital divide. Solution: Note: for full credit, you needed to give the context. For example, saying giving computers to kids isn t correct. Giving computers to rich kids in the US isn t an example of bridging the digital divide. Arguably, it s widening the dig. divide. Some examples of correct answers: Getting power to village often requires (expensive) generator and (expensive and hard to obtain) fuel. Computers have hand-cranks for power generation in power deficient areas. Children are provided w/ computers so they can teach others. This makes best use of limited money and man-power for instruction in areas where teaching resources/knowledge may be scarce. Computers are designed to be used out of doors in full/harsh sunlight and be extremely durable. Computers must not break easily since they cannot be replaced in remote areas. 7. (2 points) Give 2 examples of devices in your everyday life that use embedded processors. Solution: Answers vary. 5
8. Write the value of the variables a, b, and c (or x, y, and z in part b) after each Javascript statement executes. Write if the variable does not have a value assigned to it. Indicate whether a value is a string or a number through the use of quotation marks. When you finish every box should have something written in it. (a) (8 points) a b c a = 22; 22 b = a + 6; 22 28 c = a + 6 ; 22 28 226 a = c + 10 / 5; 2262 28 226 b = b * 2 / 8; 2262 7 226 a = parsefloat( 25.534 ); 25.534 7 226 a = Math.round(a); 26 7 226 c = c + b; 26 7 2267 (b) (7 points) x y z x = 6 + 3 * 2; 12 z = 2; 12 2 y = 10 + x/z; 12 16 2 y = x + z + 10 ; 12 1410 2 y = 10 + (x/z); 12 16 2 y = x*z + 20/10; 12 26 2 z = 2 + 3 * Math.round(2.456); 12 26 8 6
9. (10 points) Code Writing. Complete the webpage below by writing a function described below. The HTML has been given for you and should not be modified. Write a function named MPG which calculates the miles per gallon a specific model of car gets using the data entered by the user. An example output to be displayed on the webpage is something like, My Volkswagen gets 18.2 mpg. <html> <head> <title>miles Per Gallon</title> <script type="text/javascript" function MPG() { n1 = parsefloat(document.getelementbyid( milesdriven ).value); n2 = parsefloat(document.getelementbyid( gals ).value); mpg = n1/n2; document.getelementbyid( output ).innerhtml = My + document.getelementbyid( model ).value + gets + mpg + mpg. ; } </script> </head> <body> <input type="text" size=4 id="milesdriven"> <input type="text" size=4 id="gals"> <input type="text" size=10 id="model"> <input type="button" value="calculate MPG" onclick="mpg();"> <div id="output"></div> </body> </html> Solution: +1 function declaration/name +3 values retrieved from user input correctly (x3) +2 strings/values converted to numbers correctly (x2) +1 MPG calculation done correctly +2 correct output string generated and assigned to innerhtml +1 correct JavaScript syntax (paretheses, semicolons, etc) 7
10. (10 points) Code Writing. Complete the webpage below by writing a function described below. The HTML has been given for you and should not be modified. Write a function named BMI which calculates a user s Body Mass Index, a common measurement of obesity. This formula is expressed mathematically as: BMI = (weight in lbs 703) height in inches 2 The user enters their height in feet and inches in the webpage as well as their weight. The page should display something like, Your BMI is 32.3. <html> <head> <title>bmi Calculation</title> <script type="text/javascript" function BMI() { n1 = parsefloat(document.getelementbyid( feet ).value); n2 = parsefloat(document.getelementbyid( inches ).value); n3 = parsefloat(document.getelementbyid( weight ).value); num = n3 * 703; denom = Math.pow((n1*12 + n2),2); bmi = num/denom; document.getelementbyid( output ).innerhtml = Your BMI is + bmi; } </script> </head> <body> <input type="text" size=4 id="feet"> <input type="text" size=4 id="inches"> <input type="text" size=4 id="weight"> <input type="button" value="calcuate BMI" onclick="bmi();"> <div id="bmiout"></div> </body> </html> Solution: +1 function declaration/name +3 values retrieved from user input correctly and converted to numbers(x3) +3 BMI computed correctly +2 correct output string generated and assigned to innerhtml +1 correct syntax 8
11. (10 points) Suppose you have an old mechanical typewriter, with a page width of 60 characters. Given any message with a length less than or equal to 60, your job is center the message on a sheet of paper. Describe an algorithm to accomplish this task. Solution: Example answer (there are others which are also correct): 1. Count number of characters in your message (note, characters, not letters). 2. Subtract that number from 60. 3. Divide that number of 2 (let s call this number X). 4. If number is a decimal number, round X down (or up). 5. Space in X times and type your message. Note that if X was rounded in step 4, you would end up with different numbers of spaces on either side of your message. Ex: ------------- message ------------- 19 spaces 21 char msg 20 spaces Whatever your solution was, it had to be correct and specific. 9