19
Introduction to Computers -3 rd exam- 授授授授 授授授

Introduction to Computers -3 rd exam-

Embed Size (px)

DESCRIPTION

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!”); - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to Computers -3 rd   exam-

Introduction to Computers-3rd exam-

授課教授:李錫智

Page 2: Introduction to Computers -3 rd   exam-

1. [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!

Page 3: Introduction to Computers -3 rd   exam-

• [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!”);

Page 4: Introduction to Computers -3 rd   exam-

2. [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 !

Page 5: Introduction to Computers -3 rd   exam-

• [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 + “!”);

Page 6: Introduction to Computers -3 rd   exam-

3. [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 + “!”);

Page 7: Introduction to Computers -3 rd   exam-

• [3] What will the response be if grade is set to 70 before the first line of the code?

ANS:Execution ends with C !

Page 8: Introduction to Computers -3 rd   exam-

• [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 + “!”);

Page 9: Introduction to Computers -3 rd   exam-

4. [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

Page 10: Introduction to Computers -3 rd   exam-

5. [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)”.

Page 11: Introduction to Computers -3 rd   exam-

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);

Page 12: Introduction to Computers -3 rd   exam-

6. [10] Please write a program in Javascript to 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 ;}

Page 13: Introduction to Computers -3 rd   exam-

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 />”);

Page 14: Introduction to Computers -3 rd   exam-

7. [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 5LOAD R1 6ADD R2 R0 R1STORE 7 R2HALTSuppose 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

Page 15: Introduction to Computers -3 rd   exam-

8. [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/R3B Bus = R0/R1/R2/R3ALU = + / - / & / |C Bus = R0/R1/R2/R3ALU Switch = open/closedMMIn Switch = open/closedMMOut Switch = open/closedC Switch = open/closed

Page 16: Introduction to Computers -3 rd   exam-

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 = closedC Bus = R0 MMIn Switch = open

C Switch =closedALU Switch = open

MM Bus = address 6 MMOut Switch = closedC Bus = R1 MMIn Switch = open

C Switch =closedALU Switch = open

Page 17: Introduction to Computers -3 rd   exam-

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 = closedB Bus = R2 MMIn Switch = closedALU = A & B C Switch = openMM Bus = address7

Page 18: Introduction to Computers -3 rd   exam-

9. [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 R0HALT

• [5] Triple the content of R0 and store the result in R2.ANS:ADD R2 R0 R0ADD R2 R2 R0HALT

• [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 7ADD R2 R0 R0STORE 8 R2HALT

Page 19: Introduction to Computers -3 rd   exam-

10. [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 30LOAD R1 31ADD R2 R0 R0ADD R2 R2 R0SUB R2 R2 R1SUB R2 R2 R1STORE 32 R2HALT