Test #3 November 5, 2015

CPSC 1040 Test #3 Name: _______________________________ November 5, 2015 Closed notes, closed laptop, calculators OK. Please use a pencil. 100 point...
Author: Annabel Lee
4 downloads 0 Views 555KB Size
CPSC 1040 Test #3

Name: _______________________________ November 5, 2015

Closed notes, closed laptop, calculators OK. Please use a pencil. 100 points, 5 point bonus. Maximum score 105. Weight of each section in parentheses. If you need more space, use the back of the sheet. A. (15 points) True or False. Put your answer in the space provided. One has been done for you. 1. __F__ If a file with the specified name already exists when the file is opened, and the file is opened in 'w' mode, then an alert will appear on the screen. 2. _____ When a piece of data is read from a file, it is copied from the file into the program. 3. _____ Closing a file disconnects the communication between the file and the program. 4. _____ Python allows the programmer to work with text and number files. 5. _____ In Python, there is nothing that can be done if the program tries to access a file to read that does not exist. 6. _____ The ZeroDivisionError exception is raised when the program attempts to perform a division by zero. 7. _____ An exception handler is a piece of code that is written using the try/except statement. 8. _____ If the last line in a file is not terminated with a \n, the readline method will return the line without a \n. 9. _____ Strings can be written directly to a file with the write method, but numbers must be converted to strings before they can be written. 10. _____ It is possible to create a while loop that determines when the end of a file has been reached. 11. _____ Invalid indexes do not cause slicing expressions to raise an exception. 12. _____ Lists are dynamic data structures such that items may be added to them or removed from them. 13. _____ Arrays, which most other programming languages allow, have much more capabilities than list structures. 14. _____ A list cannot be passed as an argument to a function. 15. _____ The remove method removes all occurrences of the item from a list. 16. _____ The sort method rearranges the elements of a list so they appear in ascending or descending order. 17. _____ The first step in calculating the average of the values in a list is to get the total of the values. 18. _____ Indexing starts at 1, so the index of the first element is 1, the index of the second element is 2, and so forth. 19. _____ The index – 1 identifies the last element in a list. 20. _____ In slicing, if the end index specifies a position beyond the end of the list, Python will use the length of the list instead.

21. _____ Invalid indexes do not cause slicing expressions to raise an exception. 22. _____ Indexing works with both strings and lists. 23. _____ Indexing of a string starts at 1, so the index of the first character is 1, the index of the second character is 2, and so forth. 24. _____ The index – 1 identifies the last character in a string. 25. _____ In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead. 26. _____ An expression of the form string[i] = 'i' is a valid expression. 27. _____ If the + operator is used on strings, it produces a string that is the combination of the two strings used as its operands. 28. _____ When accessing each character in a string, such as for copying purposes, you would typically use a while loop. 29. _____ If a whole paragraph is included in a single string, the split() method can be used to obtain a list of the sentences included in the paragraph. 30. _____ The strip() method returns a copy of the string with all leading whitespace characters removed, but does not remove trailing whitespace characters. B. ASCII Table (10 points) 1.

(10) Consider the ASCII Table below. For the Hex (hexadecimal) column, ignore the “0x” prefix. That means that the letter ‘a’ has Hex code 61 and the number 9 has hex code 39. The Hex encoding of a mystery string is shown on the right. Complete the entries by showing the equivalent encodings in Decimal, Octal, and Character. Hexadecimal: 476f6f64204a6f6221 Octal: __________________________ Decimal: ___________________________ Character: ___________________________

C. Python Programming (40 points) 1.

Consider the following Python statements that create a two-dimensional list object called values. rows = 5 cols = 5 values = [[ 1, 2, 3, 4, [ 6, 7, 8, 9, [11, 12, 13, 14, [16, 17, 18, 19, [21, 22, 23, 24,

5], 10], 15], 20], 25]]

\ \ \ \

(5) Write a for loop that prints the diagonal elements (i.e., 1, 7, 13, 19, 25). Your loop should involve the use the variable rows or the variable cols or both.

(5) Write a for loop that takes the sum of the values in the third row (i.e., 11, 12, 13, 14, 15). Your loop should involve the use the variable rows or the variable cols or both.

2.

(10) What does the following section of Python code print out? mystring = 'jklmnopqrstu' print(mystring[2:8]) print(mystring[6:]) print(mystring[:6]) print(mystring[:]) if mystring.isdigit(): print(mystring, 'contains only digits.') else: print(mystring, 'contains characters other than digits.') ch = 'w' ch2 = ch.upper() print(ch, ch2)

3.

(10) What is printed by the following Python program? def main(): for count in range(1, 7): print('*' * count) for count in range(5, 0, -1): print('+++' * count) main()

4.

(10) Write a section of Python code that opens a file called (input_text) for reading, reads each line and prints each line that contains the string “Clemson”. At the end, your code should close the file. Your program need not test for exceptions.

D. Number Conversion (10 points) Convert the numbers shown in the table from one base to another and fill in the missing entries. Decimal

Binary

Octal

Hexadecimal

__125____

________________

_____________

___________

_______

___10110101_____

_____________

___________

_______

________________

_____763_____

___________

_______

________________

_____________

____4FB____

E.

1.

Useful  Formulas.    (15  points)  Simplify  each  of  the  following.  Use  G,  K,  T,  Z,  M,  P,  Y  and  E  as   appropriate.    Show  clearly  how  you  arrived  at  your  answer.    Do  not  put  your  solution  work  on  the   scratch  page.  All  logarithms  are  base  2.  You  may  use  a  calculator.       log ((QZ * CX) / (VY * LD)) =

2.

23 + 24 + 25 + … + 876 =

3.

81 + 100 + 121 + 144 + … + 992 =

4.

((230) / (128M)) * (235) / ((229)*(256K)) =

5.

((K/M*M)/(E*Z/T))*245 =

F. Permutations and Combinations. (10 points) Show clearly how you arrived at your answers. 1.

How many permutations of 5 different characters are there, chosen from the letters A through K?

2.

If 29 people shake hands so that each person shakes every other person’s hand exactly once, how many handshakes take place?

3.

A password consists of three letters of the alphabet (both lowercase and uppercase letters are allowed) followed by three digits chosen from 0 to 9 followed by two special symbols take from a set of five symbols. Repeats are allowed. How many different possible passwords are there?

G. (5 points) Bonus Question. Unicode. Consider the following section of the Unicode table. The Unicode value of the digit 0 is 0030. For the character ‘?’, it is 003F. For the letter ‘M’ it is 004D. Get it? Then answer the questions on the right using this table alone. _______ What is the encoding of the character ‘k’? _______ How many bits are required to encode a character? _______ How many characters can be encoded using this scheme? Translate the following Unicode string: 0047 006F 0074 0020 0069 0074 0021 ___________________________

String Methods Method Description isalnum() Returns true if the string contains only alphabetic letters or digits and is at least one character in length. Returns false otherwise. isalpha() Returns true if the string contains only alphabetic letters, and is at least one character in length. Returns false otherwise. isdigit() Returns true if the string contains only numeric digits and is at least one character in length. Returns false otherwise. islower() Returns true if all of the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter. Returns false otherwise. isspace() Returns true if the string contains only whitespace characters, and is at least one character in length. Returns false otherwise. (Whitespace characters are spaces, newlines (\n), and tabs (\t). isupper() Returns true if all of the alphabetic letters in the string are uppercase, and the string contains at least one alphabetic letter. Returns false otherwise. lower() Returns a copy of the string with all alphabetic letters converted to lowercase. Any character that is already lowercase, or is not an alphabetic letter, is unchanged. lstrip() Returns a copy of the string with all leading whitespace characters removed. Leading whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the beginning of the string. lstrip(char) The char argument is a string containing a character. Returns a copy of the string with all instances of char that appear at the beginning of the string removed. rstrip() Returns a copy of the string with all trailing whitespace characters removed. Trailing whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the end of the string. rstrip(char) The char argument is a string containing a character. The method returns a copy of the string with all instances of char that appear at the end of the string removed. strip() Returns a copy of the string with all leading and trailing whitespace characters removed. strip(char) Returns a copy of the string with all instances of char that appear at the beginning and the end of the string removed. upper() Returns a copy of the string with all alphabetic letters converted to uppercase. Any character that is already uppercase, or is not an alphabetic letter, is unchanged. endswith(substring) The substring argument is a string. The method returns true if the string ends with substring. find(substring) The substring argument is a string. The method returns the lowest index in the string where substring is found. If substring is not found, the method returns _1. replace(old, new) The old and new arguments are both strings. The method returns a copy of the string with all instances of old replaced by new. startswith(substring) The substring argument is a string. The method returns true if the string starts with substring.

List Methods Adds item to the end of the list. Returns the index of the first element whose value is equal to item. A ValueError exception is raised if item is not found in the list. insert(index, item) Inserts item into the list at the specified index. When an item is inserted into a list, the list is expanded in size to accommodate the new item. The item that was previously at the specified index, and all the items after it, are shifted by one position toward the end of the list. No exceptions will occur if you specify an invalid index. If you specify an index beyond the end of the list, the item will be added to the end of the list. If you use a negative index that specifies an invalid position, the item will be inserted at the beginning of the list. sort() Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value). remove(item) Removes the first occurrence of item from the list. A ValueError exception is raised if item is not found in the list. reverse() Reverses the order of the items in the list. append(item) index(item)