CS 221 Final Exam Fall 2009 Answers

 

NAME:______________________________________________  Section:_______

-5 points if section (or lab time) not supplied

Closed book, notes, no calculators can be used

 66 questions, 1.5 points per question

Answers follow the questions, with the source of the question in the textbook:

mc = multiple choice question

tf  = true/false question

cp = checkpoint question

 

 

1. Where does a computer store a program and the data that the program is working with while the program is running?

a.

main memory

b.

CPU

c.

disk

d.

microprocessor

e.

none of the above

A  mc 5   (source is multiple choice question 5)

 

2. The ______________ coding scheme contains a set of 128 numeric codes that are used to represent characters in the computer memory.

a.

Unicode

b.

ASCII

c.

ENIAC

d.

Binary numbering

e.

Twos complement

B mc 14

 

3. The computer hardware that actually executes the program machine instructions is the:

a.

Main memory

b.

Operating system

c.

Disk

d.

Microprocessor

e.

None of the above

D  mc 7

 

4. The computer hardware uses the _____________ numbering system.

a.

hexadecimal

b.

binary

c.

octal

d.

decimal

e.

None of the above

B mc 12  cp 1.14

 

5. A ________ is enough memory to store a letter or a small number.

a.

bit

b.

byte

c.

transistor

d.

switch

e.

none of the above

B mc 10  cp 1.12

 

6. Computer hardware can only execute instructions that are written in__________.

a.

C++

b.

machine language

c.

JAVA

d.

Python

e.

all of the above

B mc 21

 

7. Python is a(n) ______________ language meaning it both translates and executes the instructions in a high-level program.

a.       compiler

b.      assembler

c.       interpreter

d.      translator

e.       none of the above

C cp 1.25

 

 

8.  TRUE FALSE: A bit is made up of 8 bytes.

FALSE  mc 11

 

 

       9. TRUE FALSE: Windows Vista, Linux, and Mac OSX are all examples of operating systems.

TRUE t/f 9

 

10.What will the following code display:

val = 99

print ‘Value: ‘, ‘val

 

a.

Value: 99

b.

Value: , 99

c.

Value: val

d.

None of the above, the statements are in error

C  cp 2.14

 

11.After the execution of the following statement, the variable price will reference the value: 

price = int(45.98)

 

a.

45

b.

46

c.

45.9

d.

45.98

e.

None of the above

A    cp 2.19

 

12.Which of the following statements will cause an error?

a.

number1 = 17

b.

Number-1 = 17

c.

number 1 = 17

d.

b and c

e.

all are correct

D     cp 2.11 tf 3

 

13.After execution of this statement, what is the value of x?

x = float(4 +16 *2  -5)

 

a.

31

b.

31.0

c.

35

d.

35.0

e.

none of the above

B   cp 2.19

 

14.After the execution of this statement, what is the value of x?

   x = 11/2-2

a.

3 

3

3.5

c.

5.5

d.

Nothing – the statement is in error

    A  

 

Cp 2.20  tf  2

15.This built-in function can be used to read a string that has been typed on the keyboard:

 

 

 

a.       get_num()

b.      keyboard()

c.       input()

d.      raw_input()

e.       none of the above

D  mc  18

 

 

16.What statement(s) will assign the name Smith to the variable name?

a.name = Smith

b.name = “Smith”

c.name = ‘Smith’

d.either b or c

e.all of the above

D  mc 9

 

 

 

17.What value will be assigned to result?

result = 12 %  5-1

a.       1

b.      1.5

c.       2

d.      2.5

e.       None of the above, but the statement is correct

f.        None of the above because the statement has a syntax error

 

A  cp 2.21

 

18.What will be the value of answer?

answer = float(9) / 2  - float(9/2)  

a.       0

b.      0.0

c.       1

d.      1.0

e.       None of the above

  E   cp 2.19

 

 

 

19.TRUE/FALSE: 99bottles is a legal variable name. _____FALSE  cp 2.11  tf 4

 

 

20.TRUE/FALSE: A byte is a name that references a value in the computer’s memory in a Python program. _____FALSE  mc 7

 

21.TRUE/FALSE: If you print a variable that has not been assigned a value, the number 0 will be displayed. ______FALSE  tf 5

 

22.TRUE/FALSE: Variable names are case insensitive, Sales and sales refer to the same variable. ______FALSE cp  2.12

 

23.TRUE/FALSE: The float() built-in function can be used to convert an int value to a float. _____TRUE  mc 19

 

1.        2.

 

 

 

24. A(n) ________________ variable can be shared by all functions.

 

 

 

a.       global variable

b.      local variable

c.       argument variable

d.      hidden variable

e.       none of the above

A   mc  7

 

 

   25.A variable’s __________ is the part of a program in which the variable may be accessed.

a.

global

                               

b.

argument

 

c.

scope

 

d.

local

 

e.

None of the above

C cp 3.11

 

 

 

26.TRUE/FALSE: Local variables in different functions can have the same name. _____TRUE   cp 3.12

 

 

 27.TRUE/FALSE: A statement in one function can access a local variable in a different function. _______FALSE tf 7

 

        

 28. TRUE/FALSE: Python, like most languages, allows you to write functions that accept multiple arguments. _________ TRUE tf 8

 

2.        2.

 

29.What does the follow code display?

def main():

            x = 1

            y = 2

            swap(x,y)

            print x, y

def swap(a , b):

            temp = a

            a = b

            b = temp

main()

 

_________ 1  2    aw 4

 

30.What does the follow code display?

def main():

            n1 = 1

            n2 = 2

n3 =  3

            display(n3,n2,n1)

def display(n1,n2,n3):

            print n1,n2,n3

main()

 

_________ 3  2  1    aw 3

31.When  using the _______ operator, one or both subexpressions must be true for the compound expression to be true.

a.

or

b.

and

c.

not

d.

Any of the above

e.

None of the above

A   mc 10

 

32.Which  of the following is the correct if clause to determine whether y is greater than 1 to (but not including) 10?

a.

if  1 < y 10:

b.

if  1 < y or y > 9:

c.

if  1 > y and 10 > y:

d.

if  y > 1 and y < 10:

e.

if  y > 1 or y < 10:

 

ANS:  D  cp 4.16

 

33.What is the result of the following Boolean expression, if x is 2, y is 1, and z is 2?

x < y or z <= x

a.

true

b.

false

c.

8

d.

5

e.

None of the above

 

ANS:  A  cp 4.16

          

34.What is the result of the following Boolean expression, if x is 5, y is 5, and z is 5?

x <= y and z > x

a.

true

b.

false

c.

8

d.

5

e.

None of the above

 

ANS:  B  cp 4.16

       

35.   What is the result of the following Boolean expression, if x is 5, y is 3, and z is 8?

not (x < y or z > x) and y < z

a.

true

b.

false

c.

8

d.

5

e.

None of the above

 

ANS:  B  cp 4.16

    

       

          

   36.   Which of the following is the correct if clause to use to determine whether choice is other than 10?

a.

if choice != 10:

b.

if choice != 10

c.

if choice <> 10:

d.

if choice <> 10

e.

None of the above

 

ANS:  A   cp 4.16

          

   37.   When using the _______ operator, both subexpressions must be true for the compound expression to be true.

a.

or

b.

and

c.

not

d.

Any of the above

e.

None of the above

 

ANS:  B  cp 4.15

          

 

        

38. TRUE/FALSE:  Python allows you to compare strings, but it is not case sensitive.

 

ANS:  F  cp 4.11 4.12

 

       

 

 

39. TRUE/FALSE:  Python uses the same symbols for the assignment operator and the equality operator (comparing two values in an if statement for equality).

 

ANS:  F   mc 4

 

 

Assume the variables a = 1, b = 2, and c = 3. Supply the result of these conditions (T for true, F for false)

40.       3 <= c and a > 3           F

 

41.       1 != b and a != 1         F

 

42.       not (a >1)                    T

cp 4.16

 

43.       a == 2 or b > 1            T

 

44. What is printed:____________________

Firstname = ‘Paul P’

Secondname = ‘Paul D’

if Firstname < Secondname:

   print Firstname

elif Firstname > Secondname:

   print Secondname

else:

   print “equal”  

Paul D  cp 4.12

 

 

    

 

          

 

45.   When will the following loop terminate?

while keep_on_going != 999 :

a.

When keep_on_going refers to a value less than 999

b.

When keep_on_going refers to a value greater than 999

c.

When keep_on_going refers to a value equal to 999

d.

When keep_on_going refers to a value not equal to 999

e.

None of the above

 

ANS:  C    cp 5.5 5.6

 

 

 

 46. True/False: In Python, an infinite loop occurs usually when the computer accesses the wrong memory address.

 

ANS:  F   cp 5.6  5.7

 

 

 

 

 

47. What is printed:____________________________ 4 2        

count = 4

while count > 0:

   print count

   count = count -2

cp 5.5  5.6

 

48. What is printed:____________________________ 6    

i = 0

count = 0

while i < 3:

  j = 0

  while j < 2:

      j = j + 1

      count = count + 1

  i = i + 1

print count

 

 

49. What is printed:____________________________ 9   

count = 0

sum = 0

for number in [3,2,6,9]:

   if count % 2 == 0:

     sum = sum + number

   count = count + 1

print sum

cp 5.8

 

 

 

 

50. What is printed:____________________________ 10    

count = 2

sum = 0

for number in range(2):

    while count  > number:

          sum +=5

          count -=1

print sum 

 cp 5.5 5.6   5.9  

 

51. If Price is equal to 12, what is the value of Price after this statement:_________2

Price /= 5

cp 5.17

 

 

 

 

 

52.      What is the result of the following statement?

x = random.randrange(5, 15) * 2

a.

A random integer from 5 through  15, multiplied by 2, assigned to the variable x

b.

A random integer from 5 through  14, multiplied by 2, assigned to the variable x

c.

A random integer from 5 through  15, step by 2, assigned to the variable x

d.

Cannot be determined

e.

None of the above

 

ANS:  B   cp 6.5

 

 

 

53.   Given the following function definition, what would the statement print magic(3) display?

                                                            def magic(num):

                                                                        return  num * 2 + 10

a.

16

 

b.

36

 

c.

Statement will cause a syntax error

 

d.

Statement will cause a run-time error

 

e.

None of the above

 

 

ANS:  A  cp 6.10

 

 

 

54. True/False: Many games use dice (a pair of cubes each called a die with numbers 1-6 on each face). To simulate a roll of a die, the following statement can be used:

         Number_on_die = randint(1,6)

 

ANS:  T   cp 6.6 

 

 

          55. True/False: Python can return multiplevalues from a function.

 

ANS:  T tf 4

 

 

 

      56. True/False: To get the total number of iterations of a nested loop, add the number of iterations of all the loops.

 

           ANS:     F      tf 6

 

           57.   True/False: The trigonometric math function sin is passed the angle in radians, and returns the sine value of that angle.

 

           ANS:     T   cp 6.14

 

 

 

      58.      Which method will return an empty string when it has attempted to read beyond the end of the file?

a.

read

 

b.

getline

 

c.

input

 

d.

readline

 

e.

None of the above

 

 

ANS:  D  cp 7.13

 

 

59.       In order to handle the case when you open a file for reading, but the file does not exist:

a.

Add try/except statements

 

b.

Put an if statement after the open to detect it

 

c.

Check if the file exists before the open statement

 

d.

Either a or b will work

 

e.

None of the above will work, the program always terminates

 

 

ANS:  A  cp 7.18

 

60.  Which statement can be used to loop until the file (identified by file object DATA) reaches end of file:

a.

for line in DATA:

 

b.

while DATA != ‘’:

 

c.

while DATA != EOF:

 

d.

Either a or b

 

e.

None of the above

 

 

ANS: A   cp 7.12  7.13

 

 

 

61.True/False: The following can be used to write the variable n1  to a file with the file object name OUTPUT (assume the file already has been opened for writing):

n1   = 3.4

OUTPUT.write(n1)

   ANS:   F  cp 7.12

 

 

 

 

62.This is the first index of a string or a list:

a.    -1

b.    0

c.     1

d.    The size of the list minus 1

B  mc 3

 

 

63.A(n) ______________ exception occurs if you try to use an index out of the range for a list.

a.    ValueError

b.    IOError

a.    OutOfBoundsError

b.    ListAccessError

c.    Non of the above

E  mc 5

 

64.TRUE/FALSE: To change the first character of the string variable name you can use these statements:

name = “Paul”

name[0] =’S’

False  t/f  1

 

65.TRUE/FALSE: To change the last value of the list numbers you can use these statements:

numbers = [1,2,3,4]

numbers[-1] = 5

true mc 4

 

66.What does the following code print:______________2 3     cp 8.19

numbers = [1,2,3,4]

extract = numbers[1:3]

print extract