MULTIPLE CHOICE. Chapter Seven

Similar documents
Exam 3. 1 True or False. CSCE 110. Introduction to Programming Exam 3

Working with Sequences: Section 8.1 and 8.2. Bonita Sharif

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

String Processing CS 1111 Introduction to Programming Fall 2018

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 8: More About Strings. COSC 1436, Summer 2018 Dr. Zhang 7/10/2018

Announcements COMP 141. Writing to a File. Reading From a File 10/18/2017. Reading/Writing from/to Files

Announcements COMP 141. Accessing Characters Review. Using len function 10/30/2017. Strings II. Reminders: Program 6 - due 11/2

Introduction to String Manipulation

Chapter 6: Files and Exceptions. COSC 1436, Spring 2017 Hong Sun 3/6/2017

SPRING COMP 141 MIDTERM 2 PRACTICE PROBLEMS

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Strings are actually 'objects' Strings

Chapter 6: Files and Exceptions. COSC 1436, Summer 2016 Dr. Ling Zhang 06/23/2016

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Python Tutorial. Day 1

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

Script language: Python Data structures

Introductory Linux Course. Python I. Martin Dahlö UPPMAX. Author: Nina Fischer. Dept. for Cell and Molecular Biology, Uppsala University

COLLEGE OF ENGINEERING, NASHIK-4

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

UNIVERSITÀ DI PADOVA. < 2014 March >

Unit 3 Fill Series, Functions, Sorting

6. Data Types and Dynamic Typing (Cont.)

Unit 3 Functions Review, Fill Series, Sorting, Merge & Center

Introduction to Python for Plone developers

Advanced Python. Executive Summary, Session 1

Strings, Lists, and Sequences

Fundamentals of Python: First Programs. Chapter 4: Strings and Text Files

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

Tips and Tricks to loading data using the Data Import Specification

Introduction to: Computers & Programming: Strings and Other Sequences

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Introduction to: Computers & Programming: Strings and Other Sequences

Babu Madhav Institute of Information Technology, UTU 2015

The Big Python Guide

UNIVERSITY OF TORONTO SCARBOROUGH. December 2017 EXAMINATIONS. CSCA20H3 Duration 3 hours. Examination Aids: Instructor: Bretscher

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Standard prelude. Appendix A. A.1 Classes

Sequences: Strings, Lists, and Files

Topic 7: Lists, Dictionaries and Strings

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Language Reference Manual

1 Strings (Review) CS151: Problem Solving and Programming

Introductory Linux Course. Python I. Pavlin Mitev UPPMAX. Author: Nina Fischer Dept. for Cell and Molecular Biology, Uppsala University

Python Strings. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. September 25, 2012

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

2. Explain the difference between read(), readline(), and readlines(). Give an example of when you might use each.

ECS15, Lecture 13. Midterm solutions posted. Topic 4: Programming in Python, cont. Staying up to speed with Python RESOURCES. More Python Resources

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

18.1. CS 102 Unit 18. Python. Mark Redekopp

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Chapter 10: Creating and Modifying Text Lists Modules

Sprite an animation manipulation language Language Reference Manual

SMURF Language Reference Manual Serial MUsic Represented as Functions

CSC148 Fall 2017 Ramp Up Session Reference

PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Python Intro GIS Week 1. Jake K. Carr

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

CMPT 125: Lecture 3 Data and Expressions

Chapter 5 BET TER ARRAYS AND STRINGS HANDLING

CSc 120. Introduc/on to Computer Programing II. 01- b: Python review. Adapted from slides by Dr. Saumya Debray

Data Structures. Lists, Tuples, Sets, Dictionaries

Composite)Types. Types)Of)Variables. Addresses)And)References)(2) Addresses)And)References. Small)Example)Programs)Using)Strings

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Unit 2. Srinidhi H Asst Professor

Some material adapted from Upenn cmpe391 slides and other sources

- c list The list specifies character positions.

Question 1. Part (a) Part (b) December 2013 Final Examination Marking Scheme CSC 108 H1F. [13 marks] [4 marks] Consider this program:

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python Companion to Data Science

Genome 373: Intro to Python II. Doug Fowler

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

CSc 120 Introduction to Computer Programing II Adapted from slides by Dr. Saumya Debray

Variables, Constants, and Data Types

APPENDIX E SOLUTION TO CHAPTER SELF-TEST CHAPTER 1 TRUE-FALSE FILL-IN-THE-BLANKS

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Rhythm Reference Manual

Previously. Iteration. Date and time structures. Modularisation.

Programming Fundamentals and Python

ISE 101 Introduction to Information Systems. Lecture 3 Objectives: While loops Strings

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Using Lists (Arrays) Notes

CIS192 Python Programming

String Objects: The string class library

Text. Text Actions. String Contains

PRELIMINARY APPLE BASIC USERS MANUAL OCTOBER Apple Computer Company. 770 Welch Rd., Palo Alto, CA (415)

Introduction to Python

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

Programming in Python

COMP1730/COMP6730 Programming for Scientists. Strings

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Transcription:

Chapter Seven MULTIPLE CHOICE 1. Which of these is associated with a specific file and provides a way for the program to work with that file? a. Filename b. Extension c. File object d. File variable 2. What do you call the process of retrieving data from a file? a. Retrieving data b. Reading data c. Input data d. Get data 3. What happens when a piece of data is written to a file? a. Data is copied from a variable in RAM to a file. b. Data is copied from a variable in the program to a file. c. Data is copied from the program to a file. d. Data is copied from a file object to a file. 4. Which step creates a connection between a file and a program? a. Open the file. b. Read the file. c. Process the file. d. Close the file. 5. How many types of files are there? a. One b. Two c. Three d. Four 6. A(n) access file is also known as a direct access file. a. sequential b. random c. numbered d. text 7. What type of file access jumps directly to any piece of data in a file without reading the data that came before it? a. Sequential b. Random c. Number d. Text 8. Which mode specifier will open a file but will not let you change the file or write to it?

a. w b. r c. a d. 'e' 9. Which mode specifier will erase the contents of a file if it already exists and create it if it does not exist? a. w b. r c. a d. e 10. Assume that the customer file references a file object, and the file was opened using the w mode specifier. How would you write the string Mary Smith to the file? a. customer_file.write( Mary Smith ) b. customer.write( w, Mary Smith ) c. customer.input( Mary Smith ) d. customer.write( Mary Smith ) 11. When a file has been opened using the r mode specifier, which method will return the file s contents as a string? a. write b. input c. get d. read 12. Which method could be used to strip specific characters from the end of a string? a. estrip b. rstrip c. strip d. remove 13. Which method could be used to convert a numeric value to a string? a. str b. value c. num d. chr 14. Which method will return an empty string when it has attempted to read beyond the end of a file? a. Read b. Getline c. input d. readline 15. What statement can be used to handle some of the run-time errors in a program? a. exception statement b. try statement c. try/except statement

d. exception handler statement TRUE/FALSE 1. True/False: 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. True/False: When a piece of data is read from a file, it is copied from the file into the program. 3. True/False: Closing a file disconnects the communication between the file and the program. 4. True/False: Python allows the programmer to work with text and number files. 5. True/False: In Python, there is nothing that can be done if the program tries to access a file to read that does not exist. 6. True/False: The ZeroDivisionError exception is raised when the program attempts to perform a division by zero. 7. True/False: An exception handler is a piece of code that is written using the try/except statement. 8. True/False: If the last line in a file is not terminated with a \n, the readline method will return the line without a \n. 9. True/False: 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. True/False: It is possible to create a while loop that determines when the end of a file has been reached. FILL IN THE BLANK 1. When a program needs to save data for later use, it writes the data in a(n). 2. Programmers usually refer to the process of data in a file as writing data to the file. 3. The term file is used to describe a file to which data is written. 4. The term file is used to describe a file from which the program gets data. 5. A(n) file contains data that has been encoded as text, using a scheme such as ASCII. 6. A(n) file contains data that has not been converted to text. 7. A(n) access file retrieves data from the beginning of the file to the end of the file.

8. A filename is a short sequence of characters that appear at the end of a filename preceded by a period. 9. A(n) gives information regarding the line number(s) that caused an exception. 10. A(n) includes one or more statements that can potentially raise an exception.

Chapter Eight MULTIPLE CHOICE 1. What are the data items in the list called? a. data b. elements c. items d. values 2. Which list will be referenced by the variable number after the execution of the following code? number = range(0, 9, 2) a. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] b. [1, 3, 5, 7, 9] c. [2, 4, 6, 8] d. [0, 2, 4, 6, 8] 3. What would you use if an element is to be removed from a specific index? a. del statement b. remove method c. index method d. slice method 4. What is the first negative index in a list? a. 0 b. -1 c. -0 d. Size of the string or list minus one 5. What method can be used to place an item in the list at a specific index? a. append b. index c. insert d. Add 6. What would be the value of the variable list after the execution of the following code? list = [1, 2] list = list * 3 a. [1, 2] * 3 b. [3, 6] c. [1, 2, 1, 2, 1, 2] d. [[1, 2], [1, 2], [1, 2]] 7. What would be the value of the variable list after the execution of the following code? list = [1, 2, 3, 4] list[3] = 10 a. [1, 2, 3, 10] b. [1, 2, 10, 4] c. [1, 10, 10, 10]

d. invalid code 8. What method or operator can be used to concatenate lists? a. * b. + c. % d. concat 9. What would be the value of the variable list2 after the execution of the following code? list1 = [1, 2, 3] list2 = list1 list1 = [4, 5, 6] a. [1, 2, 3] b. [4, 5, 6] c. [1, 2, 3, 4, 5, 6] d. invalid code 10. What would be the value of the variable list2 after the execution of the following code? list1 = [1, 2, 3] list2 = [] for element in list1 list2.append(element) list1 = [4, 5, 6] a. [1, 2, 3] b. [4, 5, 6] c. [1, 2, 3, 4, 5, 6] d. invalid code 11. When working with multiple sets of data, one would typically use a(n). a. list b. tuple c. nested list d. Sequence 12. The primary difference between a tuple and list is that. a. when creating a tuple you don t use commas to separate elements b. a tuple can only include string elements c. a tuple cannot include lists as elements d. once a tuple is created, it cannot be changed 13. What is the advantage of using tuples over lists? a. Tuples are not limited in size. b. Tuples can include any data type as an element. c. Processing a tuple is faster than processing a list. d. There is no advantage. 14. What method can be used to convert a list to a tuple? a. append b. tuple c. insert d. list

15. What method can be used to convert a tuple to a list? a. append b. tuple c. insert d. list TRUE/FALSE 1. True/False: Invalid indexes do not cause slicing expressions to raise an exception. 2. True/False: Lists are dynamic data structures such that items may be added to them or removed from them. 3. True/False: Arrays, which most other programming languages allow, have much more capabilities than list structures. 4. True/False: A list cannot be passed as an argument to a function. 5. True/False: The remove method removes all occurrences of the item from a list. 6. True/False: The sort method rearranges the elements of a list so they appear in ascending or descending order. 7. True/False: The first step in calculating the average of the values in a list is to get the total of the values. 8. True/False: Indexing starts at 1, so the index of the first element is 1, the index of the second element is 2, and so forth. 9. True/False: The index 1 identifies the last element in a list. 10. True/False: In slicing, if the end index specifies a position beyond the end of the list, Python will use the length of the list instead. FILL IN THE BLANK 1. A(n) is an object that holds multiple items of data. 2. Each element in a tuple has a(n) that specifies its position in the tuple. 3. The built-in function returns the length of a sequence. 4. Tuples are sequences, which means that once a tuple is created, it cannot be changed. 5. A(n) is a span of items that are taken from a sequence.

6. Lists are, which means their elements can be changed. 7. The method is commonly used to add items to a list. 8. The exception is raised when a search item is not in the list being searched. 9. The method reverses the order of the items in the list. 10. The function returns the item that has the lowest value in the sequence. Chapter Nine MULTIPLE CHOICE 1. What are the valid indexes for the string New York? a. 0 through 7 b. 0 through 8 c. -1 through -8 d. -1 through 6 2. What will be assigned to s_string after the execution of the following code? special = 1357 Country Ln. s_string = special[ :4] a. 7 b. 1357 c. 1357 d. Invalid code 3. What will be assigned to s_string after the execution of the following code? special = 1357 Country Ln. s_string = special[4: ] a. 1357 b. Country Ln. c. Country Ln. d. Invalid code 4. What will be assigned to the string variable even after the execution of the following code? special = 0123456789 even = special[0:10:2] a. 13579 b. 2468 c. 02468 d. Invalid code 5. What will be assigned to s_string after the execution of the following code? special = 1357 Country Ln. s_string = special[-3: ] a. 531 b. Ln.

c. Ln. d. 7531 6. If the start index is the end index, the slicing expression will return an empty string. a. equal to b. less than c. greater than d. not equal to 7. Which of the following string methods can be used to determine if a string contains only \n characters? a. ischar() b. isalpha() c. istab() d. isspace() 8. What is the return value of the string method lstrip()? a. The string with all whitespace removed b. The string with all leading spaces removed c. The string with all leading tabs removed d. The string with all leading whitespaces removed 9. What will be assigned to the string variable pattern after the execution of the following code? i = 3 pattern = z * (5*i) a. zzzzzzzzzzzzzzz b. zzzzz c. Error: * operator used incorrectly d. The right side of the * must be an integer. 10. Which list will be referenced by the variable list_strip after the execution of the following code? list_string = 03/07/2008 list_strip = list_string.split( / ) a. [ 3, 7, 2008 ] b. [ 03, 07, 2008 ] c. [ 3, /, 7, /, 2008 ] d. [ 03, /, 07, /, 2008 ] 11. What is the first negative index in a string? a. 0 b. -1 c. -0 d. Size of the string minus one 12. Which method would you use to determine whether a substring is present in a string? a. endswith(substring) b. find(substring) c. replace(string, substring)

d. startswith(substring) 13. Which method would you use to determine whether a substring is the suffix of a string? a. endswith(substring) b. find(substring) c. replace(string, substring) d. startswith(substring) 14. What is the value of the variable string after the execution of the following code? string = 'abcd' string.upper() a. 'abcd' b. 'Abcd' c. 'ABCD' d. Invalid code 15. What is the value of the variable string after the execution of the following code? string = 'Hello' string += ' world' a. 'Hello' b. ' world' c. 'Hello world' d. Invalid code TRUE/FALSE 1. True/False: Invalid indexes do not cause slicing expressions to raise an exception. 2. True/False: Indexing works with both strings and lists. 3. True/False: 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. 4. True/False: The index 1 identifies the last character in a string. 5. True/False: In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead. 6. True/False: An expression of the form string[i] = 'i' is a valid expression. 7. True/False: If the + operator is used on strings, it produces a string that is the combination of the two strings used as its operands. 8. True/False: When accessing each character in a string, such as for copying purposes, you would typically use a while loop. 9. True/False: 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.

10. True/False: The strip() method returns a copy of the string with all leading whitespace characters removed, but does not remove trailing whitespace characters. FILL IN THE BLANK 1. Each character in a string has a(n) which specifies its position in the string. 2. A(n) exception will occur if you try to use an index that is out of range for a particular string. 3. Strings are, which means that once a string is created, it cannot be changed. 4. A(n) is a span of characters that are taken from within a string. 5. The operator can be used to determine whether one string is contained in another string. 6. The method returns true if the string contains only numeric digits. 7. When the operand on the left side of the * symbol is a string and the operand on the right side is an integer, it becomes the operator. 8. The method returns the list of the words in the string. 9. The method returns a copy of the string with all alphabetic letters converted to lower case. 10. The third number in string slicing brackets represents the value.