1 / 19

Introduction to Computers -3 rd exam-

Introduction to Computers -3 rd exam-. 授課教授:李錫智. [10] Suppose we have the following lines of code: if (grade < 60) { alert(“Sorry! You failed.”); } else { alert(“ Congratulations ! You passed.”); } alert(“Execution ends!”);

Télécharger la présentation

Introduction to Computers -3 rd exam-

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computers-3rd exam- 授課教授:李錫智

  2. [10] Suppose we have the following lines of code: if (grade < 60) { alert(“Sorry! You failed.”); } else { alert(“Congratulations! You passed.”); } alert(“Execution ends!”); • [3] What will the response be if grade is set to 65 before the first line of the code? ANS: Congratulations! You passed. Execution ends!

  3. [7] Please rewrite the code without using “else” or ”elseif”. Note that the execution result should be totally identical to that of the original code. ANS: if (grade < 60) { alert(“Sorry! You failed.”); } if (grade >= 60) { alert(“Congratulations! You passed.”); } alert(“Execution ends!”);

  4. [10] Suppose we have the following lines of code: if (temperature <= 50) { if(windSpeed<= 3) { windChill= temperature; } else { windChill= 20 + temperature + 0.5*windSpeed; } } else { windChill= temperature; } alert(“Execution ends with “ + windChill + “!”); • [3] What will the response be if temperature and windSpeed are set to 40 and 10, respectively, before the first line of the code? ANS: Execution ends with 65 !

  5. [7] Please rewrite the code without using “else” or ”elseif”. Note that the execution result should be totally identical to that of the original code. ANS: if (temperature <= 50) { if(windSpeed<= 3) { windChill= temperature; } if(windSpeed> 3) { windChill= 20 + temperature + 0.5*windSpeed; } } if (temperature > 50) { windChill= temperature; } alert(“Execution ends with “ + windChill + “!”);

  6. [10] Suppose we have the following lines of code: if (grade < 60) { letterGrade= “F”; } else if (grade < 70) { letterGrade= “D”; } else if (grade < 80) { letterGrade= “C”; } else if (grade < 90) { letterGrade= “B”; } else { letterGrade= “A”; } alert(“Execution ends with “ + letterGrade + “!”);

  7. [3] What will the response be if grade is set to 70 before the first line of the code? ANS: Execution ends with C !

  8. [7] Please rewrite the code without using “else” or ”elseif”. Note that the execution result should be totally identical to that of the original code. ANS: if (grade < 60) { letterGrade= “F”; } if (grade < 70 && grade >= 60) { letterGrade= “D”; } if (grade < 80 && grade >= 70) { letterGrade= “C”; } if (grade < 90 && grade >= 80) { letterGrade= “B”; } if (grade >= 90) { letterGrade= “A”; } alert(“Execution ends with “ + letterGrade + “!”);

  9. [10] Please answer the following questions: • [5] Suppose we have a bit string “10000000110000000000000000000000” which represents a real number in IEEE Single-Precision Floating-Point Representation (32 bits). What is the real number? ANS: (-1.1)2*2-126 • [5] Please express 42 in IEEE Single-Precision Floating-Point Representation (32 bits). Show the resulting 32 bits. ANS: 01000010001010000000000000000000

  10. [10] Suppose you have the following code: ro1 = RandomInt(1,6); ro2 = RandomInt(1,6); while(ro1+ro2 != 7) { ro1 = RandomInt(1,6); ro2 = RandomInt(1,6); } document.write(“You rolled: “ + ro1 + “ and “ + ro2); Please rewrite the above code using a Boolean flag “sevenFlag” such that the while statement becomes “while(sevenFlag == false)”.

  11. ANS: sevenFlag= false; while(sevenFlag== false) { ro1 = RandomInt(1,6); ro2 = RandomInt(1,6); if (ro1+ro2 == 7){ sevenFlag= true; } } document.write(“You rolled: “ + ro1 + “ and “ + ro2);

  12. [10] Please write a program in Javascriptto roll a dice 100 times and show the number of occurrences of each side. ANS: i= 100; o1 = 0; o2 = 0; o3 = 0; o4 = 0; o5 = 0; o6 = 0; while( i > 0 ){ roll = RandomInt(1,6); if( roll = 1 ){ o1 = o1 + 1 ; } else if( roll == 2 ){ o2 = o2 + 1 ; }

  13. else if( roll == 3 ){ o3 = o3 + 1 ; } else if( roll == 4 ){ o4 = o4 + 1 ; } else if( roll == 5 ){ o5 = o5 + 1 ; } else if( roll == 6 ){ o6 = o6 + 1 ; } i = i – 1 ; } document.write(“Number of occurrences of 1: “ + o1 + “<br />”); document.write(“Number of occurrences of 2: “ + o2 + “<br />”); document.write(“Number of occurrences of 3: “ + o3 + “<br />”); document.write(“Number of occurrences of 4: “ + o4 + “<br />”); document.write(“Number of occurrences of 5: “ + o5 + “<br />”); document.write(“Number of occurrences of 6: “ + o6 + “<br />”);

  14. [10] Let the assembly instructions be “ADD [REG] [REG] [REG]”, “SUB [REG] [REG] [REG]”, “LOAD [REG] [MEM]”, “STORE [MEM] [REG]”, “MOVE [REG] [REG]”, and “HALT”. Suppose we have the following code: LOAD R0 5 LOAD R1 6 ADD R2 R0 R1 STORE 7 R2 HALT Suppose the location of memory address 5 contains 25 and the location of memory address 6 contains 37. What happens after the execution of the above code? ANS: the location of memory address 7 contains 62

  15. [10] Please explain clearly how CPU executes the program of Problem 7. Specify how the knobs and switches operate. Assume that the program begins at memory address 0. You can refer to Figure 1. A Bus = R0/R1/R2/R3 B Bus = R0/R1/R2/R3 ALU = + / - / & / | C Bus = R0/R1/R2/R3 ALU Switch = open/closed MMIn Switch = open/closed MMOut Switch = open/closed C Switch = open/closed

  16. ANS: in the 1st step, copy the data from address 5 into a register, say R0 in the 2nd step, similarly copy the data from address 6 into a register, say R1 • MM Bus = address 5 MMOut Switch = closed • C Bus = R0 MMIn Switch = open C Switch =closed • ALU Switch = open • MM Bus = address 6 MMOut Switch = closed • C Bus = R1 MMIn Switch = open • C Switch =closed • ALU Switch = open

  17. in the 3rd step, the contents of R0 and R1 are added and the result is sent directly to R2 in the 4rd step, R2 is stored to address 7 • A Bus = R2 ALU Switch = closed • B Bus = R2 MMIn Switch = closed • ALU = A & B C Switch = open MM Bus = address7

  18. [15] Please write a program in assembly language to • [5] Double the content of R0 and store the result in R2. ANS: ADD R2 R0 R0 HALT • [5] Triple the content of R0 and store the result in R2. ANS: ADD R2 R0 R0 ADD R2 R2R0 HALT • [5] Double the content of the location of memory address 7 and store the result in the location of memory address 8. ANS: LOAD R0 7 ADD R2 R0 R0 STORE 8 R2 HALT

  19. [10] You want to calculate 3x-2y. You write a program in assembly language to do it. Suppose the x value is stored in the location of memory address 30, the y value is stored in the location of memory address 31, and the result is going to be stored in the location of memory address 32. Please show your program. ANS: LOAD R0 30 LOAD R1 31 ADD R2 R0 R0 ADD R2 R2 R0 SUB R2 R2 R1 SUB R2 R2 R1 STORE 32 R2 HALT

More Related