python data structures coursera week 1 quiz answers

Chapter 6 Quiz

1. What does the following Python Program print out?

str1 = โ€œHelloโ€
str2 = โ€˜thereโ€™
bob = str1 + str2
print(bob)
  • 0
  • Hello
  • Hello

    there

  • Hellothere

2. What does the following Python program print out?

x = โ€™40โ€™
y = int(x) + 2
print(y)
  • int402
  • 42
  • 402
  • x2

3. How would you use the index operator [] to print out the letter q from the following string?

x = โ€˜From marquard@uct.ac.zaโ€™
  • print(x[q])
  • print(x[9])
  • print(x[8])
  • print x[-1]
  • print(x[7])

4. How would you use string slicing [:] to print out 'uct' from the following string?

x = โ€˜From marquard@uct.ac.zaโ€™
  • print(x[14+17])
  • print(x[15:3])
  • print(x[14:17])
  • print(x[14:3])
  • print(x[14/17])
  • print(x[15:18])

5. What is the iteration variable in the following Python code?

for letter in โ€˜bananaโ€™ :
print(letter)
  • letter
  • print
  • for
  • โ€˜bananaโ€™
  • in

6. What does the following Python code print out?

print(len(โ€˜bananaโ€™)*7)
  • 42
  • banana banana banana banana banana banana banana
  • -1
  • banana7

7. How would you print out the following variable in all upper case in Python?

greet = โ€˜Hello Bobโ€™
  • print(greet.toUpperCase());
  • print(uc($greet));
  • puts(greet.ucase);
  • print(greet.upper())

8. Which of the following is not a valid string method in Python?

  • twist()
  • startswith()
  • join()
  • split()

9. What will the following Python code print out?

data = โ€˜From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008โ€™
pos = data.find(โ€˜.โ€™)
print(data[pos:pos+3])
  • .ma
  • mar
  • uct
  • Sat

10. Which of the following string methods removes whitespace from both the beginning and end of a string?

  • strip()
  • wsrem()
  • strtrunc()
  • rltrim()

Assignment 6.5

6.5 Write code using find() and string slicing (see section 6.10) to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out.

line = โ€˜X-DSPAM-Confidence: 0.8475โ€™

space_pos = line.rfind(โ€˜ โ€˜)

number_str = line[space_pos + 1:]

number = float(number_str)

print(number)

Leave a Reply