• No results found

(3) You may use one 8.5” x 11” note sheet during the exam

N/A
N/A
Protected

Academic year: 2021

Share "(3) You may use one 8.5” x 11” note sheet during the exam"

Copied!
11
0
0

Loading.... (view fulltext now)

Full text

(1)

Name:

Section: Date:

INSTRUCTIONS:

(1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

(2) This exam booklet contains 26 questions, each of which will be weighted equally at 5 points each. A separate 20-point question is on a separate sheet stapled to the booklet. The total points for the exam is 150 points.

(3) You may use one 8.5” x 11” note sheet during the exam. No other reference materials or electronic devices may be used during the examination, i.e. no cal- culators, cell phones, mp3 players, etc.. Paper dictionaries are allowed.

(4) Questions will not be interpreted during the examination.

(5) You should choose the single best alternative for each question, even if you believe that a question is ambiguous or contains a typographic error. If a question has more than one best answer, credit will be given for any of the correct answers.

Provide only one answer.

(6) Please fill in the requested information at the top of this exam booklet.

(7) Use a #2 pencil to encode answers on the OMR form.

(8) Please encode the following on the OMR form:

– Last name and first initial – MSU PID

– Exam form (1 A)

(9) Please sign the OMR form.

(10) Only answers recorded on your OMR form for multiple choice questions will be counted for credit. Completely erase any responses on the OMR form that you wish to delete.

(11) You must turn in this exam booklet, the OMR form, your note sheet, and your scrap paper when you have completed the exam. Be sure your name is on any of these items that you would like to have returned.

(12) When leaving, please be courteous to those still taking the exam.

(2)

KEY

1 2 3 4 5 6 7 8 9 10 11 12 13

B A C A D A B A E D A A A

14 15 16 17 18 19 20 21 22 23 24 25 26

B D A C D B A D C A B C B

(3)

(1) After the assignments x = 27 and y = 12, what is returned by x/y?

(a) 2 (b) 2.25

(c) 3 (d) 3.0

(e) None of the above.

(2) After the assignments x = 27 and y = 12, what is returned by x//y?

(a) 2 (b) 2.25

(c) 3 (d) 3.0

(e) None of the above.

(3) After the assignments x = 27 and y = 12, what is returned by x%y?

(a) 2 (b) 2.25

(c) 3 (d) 3.0

(e) None of the above.

(4) After the assignments x = 27 and y = 12, what is returned by not x <= y < x + y?

(a) True (b) False

(c) 1 (d) 0

(e) An error.

(5) Which of the following assertions about the effect of int(my_var) is correct?

(a) It modifies the value of my_var, truncating it to make it an integer.

(b) It returns True if my_var is an integer; and False, otherwise.

(c) It produces an error if my_var is not an integer.

(d) It returns the integer representation of the value associate with my_var, or an error.

(e) None of the above.

(6) Which of the following assertions is incorrect?

(a) A Python statement may return a value.

(b) A Python expression may return a value.

(c) A Python statement may produce a side effect.

(d) A Python expression may produce a side effect.

(e) None of the above (i.e., all are correct).

(4)

if x >= 0:

print (1) elif x < 20:

print(2) else:

print(3) print(4)

Figure 1

(7) In Figure 1, for what integer values of x will 1 be among the values printed?

(a) x < 0 (b) x >= 0

(c) x >= 20 (d) All values of x

(e) None of the above.

(8) In Figure 1, for what integer values of x will 2 be among the values printed?

(a) x < 0 (b) x >= 0

(c) x < 20

(d) All values of x (e) None of the above.

(9) In Figure 1, for what integer values of x will 3 be among the values printed?

(a) x < 0 (b) x >= 0

(c) x < 20

(d) All values of x (e) None of the above.

(10) In Figure 1, for what integer values of x will 4 be among the values printed?

(a) x < 0 (b) x >= 0

(c) x >= 20 (d) All values of x

(e) None of the above.

(11) After the assignments a = True and b = True, what is returned by (not a or b) and (a or not b)?

(a) True (b) False

(c) 1 (d) 0

(e) An error.

(5)

(12) What is returned by ’A A Milne’ < ’A Linkletter’?

(a) True (b) False

(c) ’A A Milne’

(d) An error.

(e) None of the above.

(13) What is returned by ’A’ < ’A Linkletter’?

(a) True (b) False

(c) ’A’

(d) An error.

(e) None of the above.

(14) After the assignment signal = ’abracadabra’, what is returned by signal[:5]?

(a) ’abraca’

(b) ’abrac’

(c) ’c’

(d) An error.

(e) None of the above.

(15) After the assignment signal = ’abracadabra’, what is returned by signal[len(signal)]?

(a) ’a’

(b) ’abracadabra’

(c) 11

(d) An error.

(e) None of the above.

(16) After the assignment signal = ’abracadabra’, what is returned by signal[-1:]?

(a) ’a’

(b) ’arbadacarba’

(c) ’’

(d) An error.

(e) None of the above.

(17) After the assignment s = ’slam’, which of the following code fragments prints scam? (Recall that Python requires a semi-colon between statements that appear on one line. I am using that syntax to save space.)

(a) s[1] = ’c’; print(s)

(b) s.replace(’l’, ’c’); print(s)

(c) s = s[:s.find(’l’)] + ’c’ + s[s.find(’l’)+1:]; print(s) (d) All of (a)–(c).

(e) None of (a)–(c) .

(6)

in str = input(’Enter a string: ’) a = ’’

d = 0 r = 1

for c in ’abcdefghijklmnopqrstuvwxyz’:

if c in in str:

a = c + a else:

d = d + 1 r += 2

print(a) # Line 1

print(d) # Line 2

print(r) # Line 3

Figure 2

(18) Given the input ’Frick & Frack’ what output is produced by Line 1 of Figure 2?

(a) FrickFrack (b) kcarkcir

(c) acikr (d) rkica

(e) None of the above.

(19) Given the input ’Frick & Frack’ what output is produced by Line 2 of Figure 2?

(a) 5 (b) 21

(c) 10 (d) 86

(e) None of the above.

(20) Given the input ’Frick & Frack’ what output is produced by Line 3 of Figure 2?

(a) 3 (b) 27

(c) 53 (d) 2

(e) None of the above.

(21) Which of the following assertions about functions is correct?

(a) The suite in the definition of a function must contain at least one return statement.

(b) A function must return a value.

(c) A function invocation must contain at least one argument.

(d) A function with no return statement must not be invoked on the right side of an assignment statement.

(e) None of the above.

(7)

(22) Given the input ’2’, what is printed by the following code?

n = int(input("Enter an integer: ")) for i in range(n):

print ("{:d} ->{:>2d}".format(i,i**3), end=’; ’) print("{:d} ->{:>2d}".format(n, n**3))

(a) 0 -> 0.00; 1 -> 1.00; 2 -> 8.00 (b) 0 -> 0; 1 -> 1; 8 -> 2

(c) 0 -> 0; 1 -> 1; 2 -> 8 (d) 0 -> 0; 1 -> 1;

2 -> 8

(e) None of the above.

s = input(’Enter a string: ’) w = ’’

for c in s:

if c in "0123456789":

#REPLACE else:

w = w + c print(w)

Figure 3

(23) What replacement statement for the comment (#REPLACE) will cause the program in Figure 3 to print the prefix up to the first digit in the input string? For example, on input ’aaa3bb3c1’, print ’aaa’ (without the quotes).

(a) break (b) continue

(c) return (w)

(d) Any of the above would work.

(e) None of the above.

(24) What replacement statement for the comment (#REPLACE) will cause the program in Figure 3 to print the string produced from the input string by removing all digits? For example, on input ’aaa3bb3c1’, print ’aaabbc’ (without the quotes).

(a) break (b) continue

(c) return (s.replace(c,’’)) (d) Any of the above would work.

(e) None of the above.

(8)

number = int(input("Enter a positive number: ")) while number > 1:

if (number % 2 == 1):

number = number * 3 + 1 else:

number = number//2 print(number)

if number == 1:

break else:

print("The end.")

Figure 4

(25) Given the input ’8’, what output is produced by the program in Figure 4?

(a) An error.

(b) The end.

(c) 4 2 1 (d) 4 2 1

The end.

(e) None of the above.

(26) Given the input ’-1’, what output is produced by the program in Figure 4?

(a) An error.

(b) The end.

(c) No output is produced.

(d) -1

(e) None of the above.

(9)

This page left intentionally blank.

(10)

Name AND Section:

Written #1: (15 points) Write correct Python code to meet the following specifications. Write all of your answer neatly on this sheet. Indicate inden- tation with care. Do not use lists. Do not use the swapcase string method.

No error checking is needed.

Repeatedly prompt for a sentence (string) or for ’q’ to quit. Upon input of a sentence s, print the string produced from s by converting each lower case letter to upper case and each upper case letter to lower case. All other characters are left unchanged. For example:

>>> =========================== RESTART ===========================

>>>

Please enter a sentence, or ’q’ to quit: This is the Bomb!

tHIS IS THE bOMB!

Please enter a sentence, or ’q’ to quit: What’s up Doc???

wHAT’S UP dOC???

Please enter a sentence, or ’q’ to quit: q

You may use the upper and lower (string) methods in your solution to this problem. Here are some examples of their use:

>>> s = "How are you?"

>>> s.upper()

’HOW ARE YOU?’

>>> s.lower()

’how are you?’

(11)

Written #2: (5 points) Write a Python function to do the conversion de- scribed in the previous problem. Write all of your answer neatly on this sheet. Indicate indentation with care. Do not use lists. Do not use the swapcase string method. No error checking is needed.

More specifically, define function convert(s) so it returns the string produced by converting each lower case letter in s to upper case and each upper case letter in s to lower case. All other characters are left unchanged. For example:

>>> =========================== RESTART ===========================

>>>

>>> convert("Sam I am!")

’sAM i AM!’

Your function must not input any values from the user or print any output;

a main program would be responsible for that. For example, your function should work properly with the following main program:

# sample main program that invokes convert sentence = input("Please input a sentence: ") converted_sentence = convert(sentence)

print(converted_sentence)

References

Related documents

The participants recognised the crucial role of certain IT, such as, cloud computing, social media, and shared servers in allowing SEs to store, retrieve and share

 Names of program and class variables may be in upper or lower case, but it is a good programming practice to use all lower case names for Java primitive data types and first

 [A-Za-z] matches any upper or lower case letter.  [A-Za-z0-9] matches any upper or lower case letter or any

Pushbutton 1S1 is activated and pneumatic proximity switch 1B1 is attenuated; the condition at the first dual-pressure valve, 1V2 (AND), thus becomes TRUE and 5/2-way double

Alphabetic energies will be up to 5 characters (upper and lower case) in either E-min or E-max, or may be considered to be concatinated across both fields.. If all upper case, then

Practice letter formation and handwriting by tracing upper and lower case letters, work on letter recognition, and teach beginning sound phonemic awareness skills.. How are

If you want to exit this section without actually signing off the Stock Return Note, type Exit (it can also be written all in upper case or all in lower case; any other format and

In this manual, the term “Switch” (first letter upper case) refers to your 16-Port 10/100Mbps Fast Ethernet Switch, and “switch” (first letter lower case) refers to other Ethernet