Introduction to Python 3 Programming

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What happens if the code in your try block does not generate an exception and there are two blocks that handle exceptions? A. None of the exception handling code will run. B. Only the code in the first set of exception handling code will run. C. The code in both exception handling blocks will run. D. The program will crash.

A. None of the exception handling code will run.

Which of the following characters will move the output cursor to the next line? A. \n B. \r C. \t D. \a

A. \n

Which of the following methods updates a Label's font during runtime? A. config( ) B. font( ) C. update( ) D. set_font( )

A. config( )

Which of the following lines of code will create a Canvas widget that is 100 pixels wide, 300 pixels tall, and has red as the background color? A. my Canvas = Canvas(width=100, height=300, bg="red") B. my Canvas = Canvas(width=100, height=300, bg=red) C. my Canvas = Canvas(100, 300, "red") D. my Canvas = Canvas(100, 300, red)

A. my Canvas = Canvas(width=100, height=300, bg="red")

Which of the following lines of code creates a square starting at (10, 10) where each side has length 15? A. myCanvas.create_rectangle(10, 10, 25, 25) B. myCanvas.create_rectangle(10, 10, 15, 15) C. myCanvas.create_rectangle(10, 10, 15) D. myCanvas.create_square(10, 10, 15)

A. myCanvas.create_rectangle(10, 10, 25, 25)

Which of the following lines of code will create a dictionary named numbers and store the numbers 10 and 20 as the keys and the corresponding words ten and twenty as the values? A. numbers = {10:'ten', 20:'twenty'} B. numbers = [10:'ten', 20:'twenty'] C. numbers = {'ten':10, 'twenty':20} D. numbers = {10, 'ten' : 20, 'twenty'}

A. numbers = {10:'ten', 20:'twenty'}

If you open an empty data file for writing and execute the following lines of code, what would the contents of the file look like? colors = ['blue', 'white'] outfile.write("colors") outfile.writelines(colors) outfile.close( ) A. colors blue white B. colorsbluewhite C. colors bluewhite D. colors blue white

B. colorsbluewhite

Which Python keyword is used to start the definition of a function? A. define B. def C. global D. return

B. def

In exception handling, which keyword starts the block of code that contains the code to be run should an error occur? A. else B. except C. Exception D. try_again

B. except

Which of the following methods adds the individual elements of one list to another list? A. append( ) B. extend( ) C. add( ) D. insert( )

B. extend( )

Which of the following statements will determine if the value in the letter variable comes before the letter "S"? A. if letter isBefore "S": B. if letter < "S": C. if letter > "S": D. if letter != "S":

B. if letter < "S":

Which of the following lines of code will test to see if a StringVar object named self.relief is storing the value "groove"? A. if self.get(relief) == "groove": B. if self.relief.get() == "groove": C. if self.relief( ) == "groove": D. if self.relief == "groove":

B. if self.relief.get() == "groove":

Which of the following keywords is used to get access to a library file? A. using B. import C. include D. library

B. import

Which of the following lines of code will print the number of items in the list my_list? A. count(my_list) B. len(my_list) C. length(my_list) D. my_list.length

B. len(my_list)

What is the name of the library file that contains a variety of mathematical functions and constants? A. mathematical. B. math. C. math_library. D. python_math.

B. math.

Which of the following lines of code will create a Car object named, myCar? A. myCar = Car B. myCar = Car( ) C. Car my Car D. Car(myCar)

B. myCar = Car( )

Which Python keyword is used to return a value from a function back to the statement that called the function? A. result B. return C. def D. None

B. return

Which of the following lines of code will create a list variable named score and store the numeric values 10, 20, and 30? A. score = 10, 20, 30 B. score = [10, 20, 30] C. score = (10, 20, 30) D> score = ['10', '20', '30']

B. score = [10, 20, 30]

What is the result of the following line of code? print ("why " * 3) A. why B. why why why C. why * 3 D. This code will generate an error.

B. why why why

Which of the following statements is correct regarding the relationship between classes and objects? A. A class and an object both describe the exact same thing. B. An object is a blueprint that is used to create individual classes. C. A class is a blueprint that is used to create individual objects. D. Objects refer to blueprints created for real-world things, while classes refer to blueprints created for programming concepts.

C. A class is a blueprint that is used to create individual objects.

Which of the following statements regarding IDLE is correct? A. IDLE is a specific version of Python that we will use in this course. B. IDLE can only be used to create Python programs; you need another program to run them. C. IDLE is a free development environment for Python. D. IDLE is the only development environment that creates and runs Python programs.

C. IDLE is a free development environment for Python.

Which of the following statements regarding parameter lists is correct? A. If a function does not require any parameters, then parentheses are not used. B. If more than one parameter is required, each one is separated by a colon. C. The parameter list is placed in the first line of the function definition, just after the function name. D. Parameters are always listed with the data type first, followed by the variable name.

C. The parameter list is placed in the first line of the function definition, just after the function name.

Which symbols are associated with the initial creation of a Python dictionary? A. ( ) B. [ ] C. { } D. < >

C. { }

Which of the following statements about a Label widget is correct? A. Labels are used to get input data from the user. B. Once a Label's font is set, it cannot be changed. C. A Label displays the text in the title bar of a GUI application. D. A Label's text can be changed during runtime if the textvariable option is set to a control variable.

D. A Label's text can be changed during runtime if the textvariable option is set to a control variable.

How do you specify which statements are part of a Python function? A. The statements in the function are enclosed by curly braces { }. B. The function body ends with the keywords End Function. C. Each line of the function begins with the function name. D. All statements in the function are indented the same amount.

D. All statements in the function are indented the same amount.

Which of the following statements about nested ifs is correct? A. Python does not allow you to nest an if statement inside another if statement. B. Python allows you to put an if statement only inside the true path of another if statement. C. Python allows you to put an if statement only inside the false path of another if statement. D. Python allows you to put an if statement either inside the true or false path of another if statement.

D. Python allows you to put an if statement either inside the true or false path of another if statement.

What two options are used with the grid( ) layout manager to specify the location where the widget is to be placed? A. Width and height B. Left and top C. x and y D. Row and column

D. Row and column

If a language is case-sensitive, it means what? A. The capitalization of each letter of output is important. B. The capitalization of each letter of the file name is important. C. The capitalization of each letter of source code is not important. D. The capitalization of each letter of source code is important.

D. The capitalization of each letter of source code is important.

What will the following code print? word = "abcdefg" print (word[3:5]) A. cde B. cd C. def D. de

D. de

Which of the following keywords makes a multiway if statement in Python? A. if B. else_if C. elseif D. elif

D. elif

Which of the following statements will determine if the playAgain variable contains the word "yes" and the maxTries variable is less than the number 5? A. if playAgain = "yes" and maxTries < 5: B. if playAgain == "yes" and maxTries < "5": C. if playAgain == yes and maxTries < 5: D. if playAgain == "yes" and maxTries < 5:

D. if playAgain == "yes" and maxTries < 5:

Which of the following statements will print the documentation string comment that's a part of the Employee object named emp01? A. print (Employee.__class__ ) B. print (emp01.__documentation__ ) C. print (emp01.__class__ ) D. print (emp01.__doc__ )

D. print (emp01.__doc__ )

Which of the following calls to the range function will give me the even numbers from 10 to 20, including both 10 and 20? A. range(10, 20) B. range(10, 20, 2) C. range(9, 20, 2) D. range(10, 21, 2)

D. range(10, 21, 2)

Which of the following methods forces Python to write the data out to a database file immediately? A. flush( ) B. write ( ) C. iwrite( ) D. sync( )

D. sync( )

Which of the following methods gets only the values stored in the dictionary? A. items( ) B. keys( ) C. records( ) D. values( )

D. values( )

Which of the following while statements is valid and will stop the loop when the value in the variable d is no longer equal to 10? A. while d = 10 B. while d == 10 C. while d = 10: D. while d == 10:

D. while d == 10:

Which of the following operators computes the value of 5 squared? A. ** B. * C. ^ D. #

A. **

What is the name of a program that translates high-level instructions all at once, before any code is run? A. A complier B. An interpreter C. An assembler D. A machiner

A. A complier

What is the purpose of the documentation string? A. A document string is a comment used to state the general purpose of a function. B. A documentation string prints all of the details of a function on the output screen. C. A documentation string is used by the interpreter to determine the number of parameters required. D. A documentation string is needed to register the function with the Python interpreter.

A. A document string is a comment used to state the general purpose of a function.

What does the dictionary method items( ) return? A. A list containing all of the key-value pairs as tuples. B. A list containing all of the key-value pairs as lists. C. A tuple of all the keys. D. A tuple of all the values.

A. A list containing all of the key-value pairs as tuples.

What do you need to do in order to write a line of code inside a function that changes a variable outside the function? A. Be sure that the function uses the global statement to get access to this variable. B. Be sure that the variable is created with the global statement outside the function. C. Be sure the function's parameter list contains the same variable name as the variable name outside the function that you want to be changed. D. Nothing, it is not possible to change the value of a variable outside a function with code inside a function.

A. Be sure that the function uses the global statement to get access to this variable.

In Python, in order to print a literal string to the screen, what must you do to the characters that make up the string? A. Enclose them in double quotes (" "). B. Enclose them in curly braces ({ }). C. Enclose them in angle brackets (< >). D. Enclose them in square brackets ([ ]).

A. Enclose them in double quotes (" ").

If you want to write code to handle all possible exceptions, what type of object should your code handle? A. Exception B. AllException C. AllError D. ExceptionError

A. Exception

Which of the following statements about the syntax of the if structure is correct? A. Python allows you to have an if statement without an else clause. B. Python allows you to have an else clause without an if statement. C. The if statement requires a colon at the end of it, but the else clause does not. D. The else clause requires a colon at the end of it, but the if statement does not.

A. Python allows you to have an if statement without an else clause.

What is the error in the following line of code that reads the city name from the user? location = eval(input("Please enter the name of your city: ")) A. The eval function is used to convert the input data to a number, but a city name is a string. B. There is no function named input in Python 3. C. You are not allowed to pass the input function a string. D. The variable name, location, is a reserved word.

A. The eval function is used to convert the input data to a number, but a city name is a string.

When is the code in the else clause of a loop executed? A. This code is executed only if the loop exits normally. B. This code is executed only if the loop does not exist normally. C. The code is always executed. D. Never, because you cannot place an else clause after a looping structure.

A. This code is executed only if the loop exits normally.

Which of the following statements forces the computer to exit the current control structure and begin executing the code following this structure? A. break B. control C. skip D. continue

A. break

Which of the following could be the first line of code for a class called, Employee? A. class Employee: B. def class Employee: C. class Employee(self): D. def class Employee(self):

A. class Employee:

Which of the following methods changes the location of a shape on the Canvas? A. coords( ) B. move_shape( ) C. relocate( ) D. update( )

A. coords( )

Which of the following is a valid first line in the definition of a Python function? A. def myFunction( ): B. myFunction( ): C. def my Function( ) D. myFunction( )

A. def myFunction( ):

Which of the following methods registers a widget with the layout manager so that the widget is displayed on the Frame? A. grid( ) B. layout( ) C. add( ) D. register( )

A. grid( )

Using the following variables, a = 1 and b = 2, which of the following statements will evaluate to false? A. if a == 1 and b == 1: B. if a == 1 or b == 1: C. if a == 2 or b == 2: D. if a == 1 or b == 2:

A. if a == 1 and b == 1:

Which Python function is used to get string input from the user? A. input B. raw_input C. get_input D. string_input

A. input

Which of the following keywords must be included as a parameter in every class function? A. self B. class C. def D. param

A. self

Which of the following statements best describes a constructor? A. A constructor is a special variable that stores the initial values of the object. B. A constructor is a special function that sets the initial values of an object's variables. C. A constructor is a part of IDLE that builds classes. D. A constructor is a special function that displays all of the values of all variables in a particular object.

B. A constructor is a special function that sets the initial values of an object's variables.

In Python, a set of characters that are enclosed in double quotes (" ") are known as what? A. An output statement B. A literal string C. A code sequence D. A variable

B. A literal string

What happens when you attempt to sort a list containing both strings and numbers? A. The string values will come before all of the numeric values. B. A syntax error would occur. C. The values will be treated like strings and sorted accordingly. D. The numeric values will come before all of the string values.

B. A syntax error would occur.

Which of the following statements regarding data files is accurate? A. A file can be opened for either reading or writing but not both at the same time. B. By default, when the IDLE prompt is first opened, it will always look for the file in the same directory as the python.exe file. C. The writelines method is used to write a line of text to a file and then advance the cursor to the next line automatically. D. If a data file exists and you attempt to open it, it will always clear all of the data that is present.

B. By default, when the IDLE prompt is first opened, it will always look for the file in the same directory as the python.exe file.

Which of the following key combinations will stop Python in the middle of running an infinite loop? A. CTRL + X B. CTRL + C C. ALT + F D. ALT + E

B. CTRL + C

Which GUI component best allows the user to select anywhere between none and all of the different options available? A. Button B. Checkbutton C. Radiobutton D. Entry

B. Checkbutton

If you want to allow your function to be called even when some arguments are not specified, what will you need to provide? A. Keyword arguments B. Default arguments C. Code inside the function to test each of the values passed to the function. D. The keyword None in the call to the function.

B. Default arguments

Which of the following Tkinter widgets allows the user to enter text data into the program? A. Textbox B. Entry C. Button D. Checkbutton

B. Entry

What is the output from the following program? try: outfile = open('data.txt', 'r') outfile.write("hello") print ("Complete") outfile.close() except IOError: print ("Error with the file") except Exception: print ("An error occurred") print ("Have a nice day") A. Complete B. Error with the file; Have a nice day C. Error with the file; An error occurred D. An error occurred; Have a nice day

B. Error with the file; Have a nice day

What does the dictionary method update( ) do? A. It allows the programmer to change the value stored at a particular key in the dictionary. B. It allows the programmer to combine two dictionaries. C. It allows the programmer to remove all values in the dictionary that do not have a key. D. It allows the programmer to remove all values in the dictionary that do not have a value.

B. It allows the programmer to combine two dictionaries.

What term describes the unique value that dictionaries associate each record with? A. Subscript B. Key C. Value D. Index

B. Key

Which of the following terms describes the action of the interpreter to somewhat deny direct access to an object's variables? A. Encapsulation B. Name mangling C. Data hiding D. Access restriction

B. Name mangling

Which of the following statements about the padx option is correct? A. The padx option specifies the number of columns that this widget should span in the grid. B. The padx option specifies the amount of empty space between the left and right of the widget. C. The padx option specifies in which column the widget should be placed. D. The padx option specifies the width of the widget.

B. The padx option specifies the amount of empty space between the left and right of the widget.

What is the purpose of the seek method? A. The seek method allows you to search the data file for a particular string of characters. B. The seek method allows you to move to any position inside a data file. C. The seek method allows you to determine whether a file exists before you attempt to open it. D. The seek method allows you to determine the current file position as the number of bytes from the start of the file.

B. The seek method allows you to move to any position inside a data file.

Which of the following is true regarding exception handling? A. The statements that may create an exception are put in the except block, and the statements to execute should an exception occur are put in the try block. B. The statements that may create an exception are put in the try block, and the statements to execute should an exception occur are put in the except block. C. All of the statements are put inside the try block, and typically the except block is left empty. D. All of the statements are put inside the except block and typically the try block is left empty.

B. The statements that may create an exception are put in the try block, and the statements to execute should an exception occur are put in the except block.

What is the output after running the following code segment? for i in range(0, 5): print(i) A. The number 5 printed five times. B. The values increasing from 0 to 4, inclusive. C. The values decreasing from 4 to 0, inclusive. D. The values increasing from 0 to 5, inclusive.

B. The values increasing from 0 to 4, inclusive.

What parameters are required to make an oval using the create_oval method? A. The x and y locations of the oval's center and the oval's height and width. B. The x and y locations of the upper-left and lower-right corners of the oval's bounding box. C. The x and y locations of the upper-left corner of the oval and its height and width. D. The x and y locations of the oval's two foci and the distance from these foci.

B. The x and y locations of the upper-left and lower-right corners of the oval's bounding box.

What would happen if you typed the following Python statement at the Python command prompt? (i.e. print("hello")) A. You would receive a message saying that you need to save the file before running it. B. You would see the word hello on the screen. C. You would see the value of the variable named hello on the screen. D. You would see an error message because this is not legal Python syntax.

B. You would see the word hello on the screen.

Which of the following characters is used to tell Python that the remaining part of the line is a comment? A. ' B. // C. / D. #

D. #

Which of the following is the Python command prompt? A. python> B. p> C. > D. >>>

D. >>>

Which type of block contains code that you want executed whether an exception occurs or not? A. Except block B. Catch block C. Else block D. Finally block

D. Finally block

What is the purpose of the Frame's mainloop method? A. It adds other widgets to the Frame. B. It creates the Frame. C. To pause the program for a specified amount of time. D. It keeps the Frame open until the user chooses to close it.

D. It keeps the Frame open until the user chooses to close it.

What happens if your program generates an exception and there is no code written to handle it? A. Nothing happens; the exception is ignored and the program continues. B. Python gives you an error when you attempt to save the source code file. C. Python gives you a warning when the exception occurs during program execution, and the program continues running. D. Python gives you an error message when the exception occurs during program execution, and the program stops.

D. Python gives you an error message when the exception occurs during program execution, and the program stops.

Which of the following compound conditions will evaluate to true even when the value in the choice variable is "N"? A. choice == "Y" or choice == "y" B. choice == "Y" or == "y" C. choice == "Y" and choice == "y" D. choice == "Y" or "y"

D. choice == "Y" or "y"

Which of the following statements can be used inside a loop structure to skip the remaining lines of code in that iteration of the loop and start a new iteration? A. break B. else C. skip D. continue

D. continue

Which method is used to pickle an object and send the result to a data file? A. loads( ) B. load( ) C. dumps( ) D. dump( )

D. dump( )

Which of the following values is used with the create_text method's anchor option so that the point specified represents the lower-right corner of the text placement? A. nw (northwest) B. ne (northeast) C. sw (southwest) D. se (southeast)

D. se (southeast)

Which of the following operators tests for inequality in Python? A. <> B. > C. != D. ==

C. !=

How do you let Python know to execute multiple statements if the condition of an if statement is true? A. All statements that are part of the true path are enclosed in curly braces ({}). B. All statements that are part of the true path end with a colon (:). C. All statements that are path of the truth path are indented the same amount. D. Python does not allow you to execute more than one statement when the condition evaluates to true.

C. All statements that are part of the true path are indented the same amount.

What type of loop uses a variable to keep track of the number of times the loop body should execute? A. Conditional loop B. Control loop C. Counter controlled loop D. Infinite loop

C. Counter controlled loop

What does the following line of code do: my_list.insert(2, 3)? A. It inserts the values 2 and 3 to the beginning of the list. B. It appends the values 2 and 3 to the end of the list. C. It inserts value 3 at index 2 in the list. D. It inserts value 2 at index 3 in the list.

C. It inserts value 3 at index 2 in the list.

What term describes when a programmer places one if statement in the true path of another if statement? A. Inner ifs B. An if if statement C. Nested ifs D. This is not possible in Python

C. Nested ifs

A function that does not have a return statement will return which of the following? A. The function name B. The function's parameter list C. None D. null

C. None

Which GUI component works best if you want the user to choose whether a light bulb has color green or blue? A. Button B. Checkbutton C. Radiobutton D. Entry

C. Radiobutton

If you were asked to write a class named Car, which of the following would be the name of the constructor function? A. Car B. __Car__ C. __init__ D. CarConstructor

C. __init__

Which of the following statements will be part of the code that handles both an IOError and ValueError exception? A. except IOError, ValueError: B. exception (IOError, ValueError): C. except (IOError, ValueError): D. except: (IOError, ValueError)

C. except (IOError, ValueError):

Which of the following lines of code will create an object named, myfile and open an output file named, data.txt? A. myfile = open('data.txt', 'r') B. data.txt = myfile.open('w') C. myfile = open('data.txt', 'w') D. data.txt = open(myfile, 'w')

C. myfile = open('data.txt', 'w')

Which of the following lines of code will print the elements at indices 2, 3, and 4 from the my_list list? A. print (my_list[2:3] ) B. print (my_list(2:5) ) C. print (my_list[2:5] ) D. print (my_list.slice(2, 3) )

C. print (my_list[2:5] )

In order to have your program pause during execution, you use which of the following methods from the time module? A. pause( ) B. wait( ) C. sleep( ) D. hold( )

C. sleep( )

If you printed the result of a function call that returned a tuple that contains no values, what would the output look like? A. ( ) B. (, ) C. None D. 0

A. ( )

Which of the following characters encloses the values of a tuple? A. ( ) B. [ ] C. { } D. < >

A. ( )

Which file extension should you use at the end of your Python files to make the most of the IDLE environment? A. .Python B. .py C. .IDLE D. idl

B. .py

Which of the following terms describes code that is used as a blueprint to create a Python object? A. A pattern B. A class C. An instance D. A blueprint

B. A class

Which of the following lines of code can be placed inside the __init__ method to set the size of the Frame to a width of 300 and a height of 150? A. self.master.size("300x150") B. self.master.geometry("300x150") C. self.master.dimension(width = 300, height = 150) D. self.master.set(width = 300, height = 150)

B. self.master.geometry("300x150")

Which of the following methods is not available to a Python dictionary variable? A. get( ) B. update( ) C. sort( ) D. clear( )

C. sort( )

What can be said about the output from the following line of code that prints the dictionary, my_dictionary? (i.e. print(my_dictionary)) A. The key-value pairs will be arranged so that the keys are in increasing order. B. The key-value pairs will be arranged so that the values are in increasing order. C. There is no guarantee as to what order the key-value pairs will appear in the output. D. Only the values will be printed from this statement.

C. There is no guarantee as to what order the key-value pairs will appear in the output.

If you want to open your data file and automatically add data to the end of the file, which flag should you use in the open statement? A. w B. w+ C. a D. r

C. a

Which of the following flags opens a shelf file for both reading and writing? A. r+ B. w+ C. c D. None, because it is not possible to open a file for both reading and writing.

C. c

Which of the following lines of code starts a class named Car that inherits from the class Vehicle? A. class Car( ) inherits from Vehicle: B. class Car( ) : Vehicle( ): C. class Car(Vehicle): D. class Vehicle(Car):

C. class Car(Vehicle):

Which method finds out all of the attributes of an exception or any other Python object? A. information( ) B. attributes( ) C. dir( ) D. object( )

C. dir( )

Which of the following groups of code would correctly sort the items in the list my_list in decreasing order? A. my_list.sort( ) B. my_list.reverse( ) C. my_list.sort( ); my_list.reverse( ) D. my_list.reverse( ); my_list.sort( )

C. my_list.sort( ); my_list.reverse( )

Which of the following code segments will generate an error? A. x, y = 0, 0 B. sentence = "The crowd exclaimed, \"YES!\"" C. phrase = ""Thank you"" D. _number = 555

C. phrase = ""Thank you""

If you had a dictionary used to associate students' names with their test scores, which of the following lines of code would print the test score for a student named Sam? A. print (scores.get['Sam'] ) B. print (scores.get(Sam) ) C. print (scores.get('Sam') ) D. print (scores.get(90) )

C. print (scores.get('Sam') )

Which of the following lines of Python code would print out the words "Introduction to Python" on the screen? A. PRINT("Introduction to Python") B. PRINT(Introduction to Python) C. print("Introduction to Python") D. print(Introduction to Python)

C. print("Introduction to Python")


संबंधित स्टडी सेट्स

Microbiology Lab Midterm Study Guide

View Set

AP Biology Lab 2.08-2.09: Tonic Osmoregulation and Transport

View Set

1.1-Enlightenment Ideas that influenced the Founding Fathers

View Set

Flight Data Input/ Output (FIDO)

View Set

Chapter 53: Assessment and Management of Patients with Male Reproductive Disorders

View Set

Chapter 6, Formulating the Hypothesis

View Set

Research Design & Statistics: Chapters: STATS 7-9/ R.M. 7

View Set

FADAVIS Q's-Ethics & Legal Issues

View Set