Python Programming: An Introduction to Computer ?· Python Programming: An Introduction to Computer…

Download Python Programming: An Introduction to Computer ?· Python Programming: An Introduction to Computer…

Post on 29-Jun-2018

213 views

Category:

Documents

0 download

TRANSCRIPT

  • Python Programming: An Introduction to Computer Science (2nd Ed.)

    Chapter 5 Questions1. A Python string literal can be indicated by enclosing text in:

    1. single quotes (e.g., 'example') 2. double quotes (e.g., "example") 3. either single or double quotes 4. all of the above

    2. The Python function for getting string input interactively from the user is 1. input 2. stdin 3. getString 4. read

    3. What is printed by the following Python fragment?

    s = "John Doe" print(s[1])

    1. J 2. e 3. John 4. o

    4. What is printed by the following Python fragment?

    s = "John Doe" print(s[-1])

    1. J 2. e 3. John 4. o

    5. What is printed by the following Python fragment?

    s = "John Doe" print(s[1:3])

    1. Jo 2. Joh 3. oh 4. ohn

    6. What is printed by the following Python fragment?

    s1 = "hello" s2 = "polly" print(s1 * 2 + s2)

    1. hellohellopolly 2. hello2polly

  • 3. hello hello polly 4. Nothing (the fragment is incorrect Python code)

    7. What is the output from the following program, if the input is "Spam And Eggs" (typed without the quotes)?

    import stringdef main(): msg = input("Enter a phrase: ") for w in msg.split(): print(w[0],end=" ")main()

    1. SAE 2. S A E 3. S S S 4. None of the above

    8. What is the output of this program fragment?

    for x in "Mississippi.split("i"): print(x, end=" ")

    1. Msssspp 2. M ssissippi 3. Mi ssi ssi ppi 4. M ss ss pp

    9. An acronym is a word formed by taking the first letter of each word in a phrase. For example, RAM is an acronym for random access memory. Notice that the acronym is in uppercase letters. The following fragment of code is meant to compute the acronym of a phrase:

    acronym = ""for w in phrase.split(): # missing line hereprint(acronym)

    What should the missing line be? 1. print(w[0].upper()) 2. acronym = w[0] 3. acronym = w[0].upper() 4. acronym = acronym + w[0].upper()

    10.ASCII and Unicode are 1. standardized encodings of written characters as numeric codes 2. encryption systems for keeping information private 3. ways of representing numbers using binary 4. computer languages used in natural language processing

    11.One difference between strings and lists in Python is that 1. strings are sequences, but lists aren't 2. lists can be indexed and sliced, but strings can't 3. lists are mutable, but strings aren't 4. strings can be concatenated, but lists can't

    12.The following program fragment is to find the sum of the Unicode values for the characters in a string. What is the missing line in this program?

    phrase = input("Enter a prhase: ")UniSum = 0for ch in phrase:

  • # missing line hereprint UniSum

    1. UniSum = UniSum + ch 2. UniSum = chr(ch) 3. UniSum = UniSum + chr(ch) 4. UniSum = UniSum + ord(ch)

    13.What is the result of evaluating the expression chr(ord('A'+2))? 1. 'A2' 2. 'C' 3. 67 4. Error

    14.In a public key encryption system, the decryption key is 1. publically available 2. kept private 3. the same as the encryption key 4. unkown

    15.Suppose day, month, and year are int variables containing the current day, month, and year numbers. Which of the following expressions would produce a properly formatted date in month/day/year format?

    1. str(month)+"/"+str(day)+"/"+str(year) 2. "{0}/{1}/{2}".format(month,day,year) 3. "{0}/{1}/{2}".format(str(month),str(day),str(year)) 4. All of the above

    16.What is the result of evaluating this expression: "{0}.{1:02}".format(13,8)? 1. "13. 8" 2. "13.80" 3. "13.08" 4. "13.8 "

    17.How many lines of output are generated by this statement:

    print("Hello\nGoodbye\n")

    1. 1 2. 2 3. 3 4. 4

    18.Which of the following commands would be used to open a file called "foobar.dat" so that it's contents could be read into memory?

    1. open = infile("foobar.dat","r") 2. infile = open("foobar.dat","w") 3. infile = open("foobar.dat","r") 4. data = foobar.read()

    19.Suppose you want to display the contents of a text file to the screen exactly as it appears in the file. Which of the following program fragments is correct?

    1. for line in infile.readlines(): print(line)

    2. for line in infile: print(line)

    3. print(infile.read()) 4. All of the above are correct

  • 20.A batch-oriented program is one that 1. does its input and output through files 2. uses multi-line strings 3. takes a long time to run 4. processes usernames

Recommended

View more >