Docsity

Exam 2 Solution - Introduction to Computer Programming | CSC 1310, Exams of Computer Science

Toggle sidebar
bg1
Csc 1310 Test 2
Spring 2009
Name_________________ Date________________
1. Multiple choices (exactly one correct answer). (50 pts).
Figure 1
1) What output is produced if given input 1 in Figure 1?
a) 1
b) 2
c) 0
d) 4
2) What output is produced if given input 3 in Figure 1?
a) 1
b) 256
c) 4.5
d) 16
3) What output is produced if given input 5 in Figure 1?
a) 5
b) 144
c) 25
d) 256
4) What output is produced if given input 4 in Figure 1?
a) 5
b) 144
c) 25
d) 256
5) For the program in Figure 1, what will happen if we change the code from count < 8 to
count <= 8?
a) error, division by 0
b) no change
c) go through the loop one more time
d) infinite loop
pf3
pf4
pf5

Partial preview of the text

Download Exam 2 Solution - Introduction to Computer Programming | CSC 1310 and more Exams Computer Science in PDF only on Docsity!

Csc 1310 Test 2

Spring 2009 Name_________________ Date________________

  1. Multiple choices (exactly one correct answer). (50 pts). Figure 1
  1. What output is produced if given input 1 in Figure 1? a) 1 b) 2 c) 0 d) 4
  2. What output is produced if given input 3 in Figure 1? a) 1 b) 256 c) 4. d) 16
  3. What output is produced if given input 5 in Figure 1? a) 5 b) 144 c) 25 d) 256
  4. What output is produced if given input 4 in Figure 1? a) 5 b) 144 c) 25 d) 256
  5. For the program in Figure 1, what will happen if we change the code from count < 8 to count <= 8? a) error, division by 0 b) no change c) go through the loop one more time d) infinite loop

Figure 2

  1. Given the input ‘bca’, what output is produced in Figure 2? a) abc b) bca c) baa d) bcc
  2. Given the input ‘abcd’, what output is produced in Figure 2? a) cda b) abcd c) bca d) abb
  3. Given the input ‘bcade’ what output is produced in Figure 2? a) bca b) cadb c) ade d) cad
  4. Given the input ‘abbad’ what output is produced in Figure 2? a) bba b) bad c) abb d) None of the above
  5. Which of the following is a true statement(s)? a) The program always stops for all the valid inputs b) It may cause infinite loop for some valid inputs c) Both of a) and b) d) None of the above
  1. Consider the function to swap values of two variables. Can we just simply assign values each other by the following codes? Why or why not? If not, give the correct program. (15pts) def swap(a, b): a = b b = a return a, b a = 3 b = 5 swap(a, b) No. For the example above, the values of both a and b are 5 after the execution of function swap. The correct program: temp =a a=b a=temp \
  1. Please write a python code that users can input a number (height of the graph), and Python will print out the following graph (the input number is 7). (15 pts) num = int(raw_input(“Please input the height of the graph: ”)) for i in range(num): print ' ' * i + '*' * (2 *(num - i) - 1)