Python Week 4 Quiz Questions

¡Supera tus tareas y exámenes ahora con Quizwiz!

The randint function is part of the random module. Given the following import statement, how would you call randint? from random import randint

value = randint(1,10)

How are values in a dictionary accessed?

By putting the key inside square brackets, like a list

How are values added to a dictionary?

By using square brackets, a key and the assignment operator

How can you determine if a key exists in the dictionary?

By using the in operator to test for existence of the key

What can you do to prevent run-time exceptions from crashing your program?

Implement try/except blocks anywhere a runtime exception might be possible

What is the purpose of the 'else' clause in a try/except construct?

It holds code that should only execute when an exception has not been thrown

What does the term "throw a exception" refer to?

It means that the normal execution of code has been disrupted by an error condition

What does the keys() dictionary method do?

It produces an object with a list of keys in the dictionary

What code should be placed inside a try block?

Just the code that could be expected to throw an exception

What happens if a key already exists in the dictionary and an attempt is made to store a new value using the same key?

The original value is replaced by the new value

How can an except block be defined to capture specific exceptions?

The particular kind of runtime exception, such as ValueError, can be specified in the except statement

what happens if your program does not catch an exception?

The system catches the exception and the program terminates immediately

How do you access a value within a dictionary?

Using square brackets and a key

What kind of exception is thrown when a value cannot be converted, such as trying to use int() to convert "Harry" to a number?

ValueError

How can the items() dictionary method be used in a for loop statement?

With each iteration of the for loop, the key is copied into one for loop variable and the value is copied into another

What is an important requirement of Secure Programming?

Writing programs that perform input validation and that catch and handle runtime exceptions

If an exception is not thrown in a try block and the optional else clause is omitted, what happens next?

all of the code in the try block is executed and the except block is skipped

How many times can the same value appear in a dictionary?

any number of times

Which of the following data types may be values in a dictionary?

any of these

What is one way in which a hacker can compromise a system via a programming application?

cause the application to crash by entering invalid input

If you only wanted to use the randint function of the random module and you did not want to have to include the random module name when calling randint, which import statement would you use?

from random import randint

if you wanted to refer to the randint function of the random module as rint, which import statement would you use?

from random import randint as rint

If you want to have access to all of the functions within the random module, what command would you use?

import random

what code should be place inside an except block?

just the code to handle an exception should one be thrown

Keys in a dictionary

must be unique

How much of the code in the except block is executed if no exception is thrown?

none of the code in the except block executes

The randint function is part of the random module. Given the following import statement, how would the randint function be called: import random

value = random.randint(1,10)

Does all the code in a try block execute if no exception is thrown?

yes

What is the syntax for items within a dictionary?

A collection of key/value pairs inside curly braces, each separated by a comma. With each key/value pair, the key comes first, then a colon, then a value

What happens if you try to access a value in the dictionary using a key that does not exist?

An exception is thrown

Dictionaries are created using

Curly braces

If an exception is thrown in a try block, what happens next?

Execution jumps immediately to the except block

How does the dictionary data structure get its name?

Just as a word is paired with a definition in a physical dictionary, a key and a value are paired in the dictionary data structure


Conjuntos de estudio relacionados

Verifying Trigonometric Identities

View Set

INT MKT Ch 4 Cultural Dynamics in Assessing Global Markets

View Set

Chapter 7 - Social and Emotional Development in Infants and Toddlers

View Set

AP US Government and Politics Midterm

View Set

Sentence Patterns and Types Language Arts 700

View Set

Bi 112 Ch 15 Questions (2nd Batch)

View Set