SEC290 Final Exam Study Guide. Week 1-7

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

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

any number of times

What does '==' mean?

equal to

What does the format specifier '{:20}' do for the display of a string value?

it indicates that the field should be at least 20 characters wide and left justified

How is user input provided to the program?

it is returned by the input() function and stored in a variable using the assignment operator

What does the for statement do at the beginning of each iteration of the loop?

it takes the next item in the sequence and stores it in the for loop variable.

What does '<' mean?

less than

If no parameter is provided to split() what happens?

split() defaults to using a space as the delimiter for dividing the string.

What does split() do?

split() slices a string into many pieces.

What does the strip method do?

strip() removes unwanted characters from the beginning and end of a string.

What command reads just 5 characters from a text file?

stuff = text_file.read(5)

What does the statement 'guess == 5' do?

tests for equality, resulting in True when guess is equal to 5

What is a for loop?

the for loop enables a program to repeat a block of code multiple times.

What does print(some_var.upper()) display?

the string contained in some_var in all upper case characters

What does 'num >= 5' test for?

the value in num is greater than or equal to 5

What does immutable mean?

unchangeable

In order to provide documentation about the code inside modules you create

use docstrings.

When is the constructor for an instance of a class called?

when an instance of the class is instantiated.

In Python, which of the symbols below is used for multiplication?

*

Which symbol is used as the string concatenation operator?

+

How many times can a while loop run?

0 or more times.

The Python programming language became available in what year?

1991

Which of the following is not a legal variable name.

1st_lap

What is a 'try block'?

A block of code in which an exception is expected to be thrown.

What is an 'except block'?

A block of code to handle an exception that has been caught

How many times does a for loop run?

A for loop runs once for each item in the sequence included in the for statement.

Which of the following is an example of a sequence that might be used in a for statement?

A list

What is true about a list?

A list is a variable that may contain more than one value at the same time.

What is a sequence?

A sequence is an ordered collection of items.

What is a string?

A sequence of characters

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

An exception is thrown

What is it called when the condition for a while statement is always True?

An infinite loop.

An object is

An instance of a class

Which statement is true about the number of values that can be passed to the print statement?

Any number of values may be passed to a print statement as long as they are separated by commas.

How are elements accessed within a list?

By appending square brackets to the list name and including an index value within the square brackets.

After a function calculates a value, how does it return that value to main program where it was called?

By including that value in a return statement.

How is the body of a function defined?

By indenting the lines of code immediately following the 'def' statement

How is a function called?

By providing the name of the function followed by a set of parentheses in which there may be zero or more arguments.

How are values in a dictionary accessed?

By putting the key inside square brackets, like a list.

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

By putting the key inside square brackets, like a list.

How can data be written to the end of a text file?

By setting the mode parameter in the open statement to "a".

How are values added to a dictionary?

By using square brackets, a key and the assignment operator.

How can an item be deleted from a dictionary?

By using the del statement.

How can elements be deleted from a list?

By using the del statement.

How can you read a text file one line at a time?

By using the file object variable as the sequence in a for loop command.

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

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

How can you read an entire file at once into a list?

By using the readlines() command.

How can a particular value be deleted from a list.

By using the remove() list method.

"As opposed to programs you write yourself in Python, commercial off the shelf programs"

Can only do what they were designed to do

Which of the following statements is true about functions?

Functions are a means to re-use code. Using functions can help make your programs modular. Functions make maintenance easier Correct! All of the above!!

What are global variables?

Global variables are variables that are accessed at the main program level.

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 does the 'and' Boolean operator do?

It compares two conditions. Only if both are True is the result True

What does the 'not' Boolean operator do?

It flips the Boolean value of an expression from True to False or False to True.

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 is a list index?

It is the offset of an element from the beginning of the list. The first item in the list has an index of 0.

What does the term, "throw an 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.

How can you slice a tuple?

Just as you would with a list or string, by placing two integers separated by a colon inside square brackets. The first integer represents the starting index and the second integer represents one more than the ending index.

What code should be placed inside a try block?

Just the code that could be expected to throw an exception.

What do text files consist of?

Lines of text characters (e.g. ASCII or UTF-8) ending in a line break

Where in a program should a function be defined?

Near the top of the program

Which programming design approach visualizes things with attributes and actions that can manipulate these attributes?

Object Oriented Design

What happens after a function has been called and the code in the function body has been executed?

Program execution returns to where the function was called.

Which of the following statements is true about "private" instance variables that cannot be accessed except from inside an object in Python.

Python doesn't support "private" instance variables.

Which of the following statements about Python is true?

Python supports both procedural and object oriented programming.

Which technique for reading a file is the most appropriate when working with very large text files?

Read one line at a time in a for loop.

Assuming myRectangle is an instance of the Rectangle class, what happens when del(myRectangle) is called?

Rectangle's __del__(self) method is called.

What happens if you write lines of text to a file without including the line breaks?

The file will consist of a single long line of text.

What is the syntax of the for loop statement?

The for keyword is followed by a for loop variable, the in operator, a sequence and a colon

How do the lines of code in the body of a function get executed?

The function must be called within the program

Where should the initial condition for a while loop be set?

The initial condition needs to be set up before the while loop statement.

After a line is read from a text file, what is the file position?

The next line in the file to be read.

What must happen before a text file can be read or written.

The open() command must be called

What command is used to open a text file?

The open() command opens a text file.

What is true about the parameters of a function?

The order in which the parameters are defined matters.

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 list method deletes an element from a list and returns its value?

The pop() list method.

What happens if your program does not catch an exception?

The system catches the exception and the program terminates immediately

Assuming break statements are not used, what has to happen for a while loop to exit?

The while loop condition must become False.

What happens if the condition in a while statement is False?

The while loop exits.

What is a run time exception?

This is an error that is detected while the program is running, such as an attempt to divide by zero.

In a Python program, how do you convert data entered at the keyboard into a floating point number?

Use the float() function.

In a Python program, how do you convert data entered at the keyboard into a whole number?

Use the int() function.

How do you access a value within a dictionary?

Using square brackets and a key.

What are parameters?

Variables in the function definition that are used to pass values to the function.

Given that some_var has the string "Whatever" in it, what does print(some_var[0]) display?

W

When is the body of a function executed?

When the function is called

When is a for loop the most appropriate choice when code in your program needs to be repeated?

When the number of times the loop needs to run is known in advance.

Which of the following variable names does not follow best practices.

Width

What is an important requirement of Secure Programming?

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

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

Yes

What can happen if you don't indent your code properly in Python?

Your code may run but produce unexpected results.

Which of the methods below is a destructor?

__del__(self)

Which of the methods below is a constructor?

__init(self)__

What identifies the start of a format specifier the curly braces of an f-string?

a colon

A text file consists of

a sequence of lines.

In terms of programming as it is explained in this session's presentation, what is a path?

a set of instructions that the computer will actually follow (or execute)

When a line is written to a text file

a trailing line break must be included.

Thonny can best be described as

an Integrated Development Environment for Python

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

an integer, floating point number, a string(any of these)

How are letters stored in memory?

as ones and zeros using ASCII encoding

In order to provide a default value for a function

assign the default value to the parameter in the function definition.

In other programming languages, the dictionary data structure is referred to as a(an)

associative array, hash hash table, all of the above!!!!!

What statement can be used to immediately exit a for or while loop?

break

The int() function is an example of a

built-in function

How can an entire text file be read into a string?

by using the read() command with no parameters.

Which of the following statements correctly shows the first line of a class definition for Rectangle?

class Rectangle:

What statement can be used to skip the remaining lines of code in a for or while loop and return to the top of the loop?

continue

In order create a module

create a program file with a '.py' extension, just like any other Python program.

Dictionaries are created using

curly braces

What keyword is used to create a function?

def

Which of the following function definitions is syntactically correct?

def my_function(parm1, parm2):

Assuming that you have a Rectangle class and an instance of this class named first_rectangle, what does the following statement do?first_rectangle.width = 500

first_rectangle's width attribute is set to 500.

Which is true about the use of the global statement?

global statements should be avoided because using them can lead to hard-to-find bugs in your program.

What does '>' mean?

greater than

Given that some_var has the string "Whatever" in it, what does print(some_var[1]) display?W

h

Syntax highlighting in an IDE such as Thonny

helps identify syntax problems using colors

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

import random

Lines read from a text file

include trailing line breaks.

What is the purpose of curly braces inside an f-string?

includes a variable name for string interpolation

Where is the best place to initialize attributes for an instance of a class?

inside the __init__ method.

What does int(g) mean?

int converts the value in g from a string to a whole number.

What does the '^' symbol do in a format string specifier?

it causes the text value to be displayed center justified

What does the '>' symbol do in a string format specifier?

it causes the text value to be displayed right justified

How would you remove an unwanted newline character from the end of a string in a variable named 'line'?

line = line.strip()

Indents in Python code

matter

______ manipulate the attributes of an object.

methods

Keys in a dictionary

must be unique

Which of the following statements creates an instance of the Rectangle class?

myRectangle = Rectangle()

Assuming mySquare is an object of the Rectangle class and has an attribute named width, which statement below sets the value of the width to 4?

mySquare.width = 4

When restoring a data structure from a JSON file with a file object of 'fp', which command should be used? Assume the data structure's name is my_data.

my_data = json.load(fp)

Which of the following is the correct syntax for creating a tuple with a single value?

my_tuple = ("Harry",)

Given the following string, which contains an item number, brand, model and price separated by tab characters, how would you split the data into the associated variables?

num, brand, model, price = line.split("\t")

Data is represented in computer memory as

ones and zeros

With positional parameters

order matters.

What is the name of the function used to display text to the user?

print()

Which statement below displays the value of the class attribute numRectangles of the Rectangle class?

print(Rectangles.numRectangles)

The Python split method can be used to

separate values in a string into individual variables

What does line.split(";")

separates the string contained in line at every occurrence of semi-colon

What does 'num = 5' do?

sets the value of num to 5

In order to import a module you have created

simply use the name of the program file in the import statement, without the '.py' extension.

Which of the following is not a legal variable name.?

six-of-one

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)

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)

What is a logic error?

when the program compiles and runs but the result is wrong

What command can be used to automatically close a file after it has been read or written?

with

String methods are

"like functions, but associated with a particular string"

What symbol is used to create a comment in Python?

#

In Python, which of the following symbols is used for modulo?

%

What is the file extension typically used with JSON files?

'.json'

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.

How can a file be opened for writing?

A file can be opened for writing by including a mode parameter set to "a" or "w" in the open() command.

How does Python know which code needs to be executed on the true path of an if/else statement?

All the code that is indented under the 'if' and before the 'else' is in the true path

With respect to functions, what best describes the term 'argument'?

An 'argument' is a value placed between the parentheses in a function call.

What happens when an event disrupts the normal flow of a program?

An exception is thrown

How can elements be added to a list?

By using the append() method

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.

What should you do after you are done reading or writing the file?

Close the file.

What does JSON stand for?

JavaScript Object Notation

Header comments

Often provide useful information about the author of the program, the date and its purpose

The design approach in which commands operate on data, such as through function calls, but there is no direct relationship between between the data itself and the functions is called

Procedural Programming.

What value is returned from a function that does not have a return statement?

The None value, which is a keyword in Python indicating the absense of a value.

How is the body of a for loop defined?

The body of a for loop is defined by an indented block of code following the for statement.


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

Quizlet Nursing Care of Children

View Set

The Handmaid's Tale Quizlet for Test

View Set

MRKTING EXAM 2, MKT EXMA 3 CH. 12 -16, MKT Exam 4 CH. 17 - 22

View Set

Jane Austen Mastery Test (All are right except 1st question)

View Set