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________________
Multiple choices (exactly one correct answer). (50 pts). Figure 1
What output is produced if given input 1 in Figure 1? a) 1 b) 2 c) 0 d) 4
What output is produced if given input 3 in Figure 1? a) 1 b) 256 c) 4. d) 16
What output is produced if given input 5 in Figure 1? a) 5 b) 144 c) 25 d) 256
What output is produced if given input 4 in Figure 1? a) 5 b) 144 c) 25 d) 256
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
Given the input ‘bca’, what output is produced in Figure 2? a) abc b) bca c) baa d) bcc
Given the input ‘abcd’, what output is produced in Figure 2? a) cda b) abcd c) bca d) abb
Given the input ‘bcade’ what output is produced in Figure 2? a) bca b) cadb c) ade d) cad
Given the input ‘abbad’ what output is produced in Figure 2? a) bba b) bad c) abb d) None of the above
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
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 \
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)