Intro to Python midterm

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following is a relational operator? not and <= /

<= Exactly! That operator tests if one value is less than or equal to another.

int

A whole number.

tuple

An immutable list of values.

A Python program must be enclosed in curly braces { } to be executed.

False

What is the role of the seed value of a random number generator? It's a number that is the basis of the calculated pseudorandom numbers. It's the minimum value of any range produced by the generator. It's the maximum value that can be generated. It's the maximum number of values that can be produced by the generator.

It's a number that is the basis of the calculated pseudorandom numbers. Excellent! It's the starting point of a series of complex calculations.

What is syntax coloring?

Showing certain elements of program code in different colors

Which of the following statements is true? The break statement terminates the current iteration of the loop. The break statement terminates the loop completely. The break statement causes an error to be generated. The break statement breaks out of the outermost loop.

The break statement terminates the loop completely. Right! The loop body is exited immediately and no more iterations will be performed.

What does the term case sensitive mean?

The difference between uppercase and lowercase letters matters

granularity

The level at which an instruction is expressed.

The in and is operators both produce boolean results. True False

True Well done! One checks membership (containment) and the other checks identity.

swap values

# Swap the element at index 4 (64) with the element at index 10 (72) values[4], values[10] = values[10], values[4]

If the variable machine refers to the string 'computer', what does the expression machine[0:len(machine):2] produce? 'uter' 'cmue' 'optr' 'comp'

'cmue' Right! The slice extracts every other character in the entire string.

Algorithm

A step-by-step procedure for solving a problem.

Which of the following does the input function NOT do when it is called? Convert the user input to an integer. Wait for the user to type information and press Enter. Print a message if provided. Return the value typed by the user.

Convert the user input to an integer. Well done! The input function does not convert data.

Which of the following would cause a runtime error? Dividing by zero. Misspelling a keyword. Leaving a quotation mark off of the end of a character string. Computing the wrong result.

Dividing by zero. Excellent! Since dividing by zero is an undefined operation, the program halts execution.

A Python program must compiled into an executable form before it can be run.

False

Python 3 is backwards compatible to Python 2.

False

The Python programming language was so named after a snake was discovered in the creator's office.

False

In Python, the assignment statement is also an expression. True False

False Correct! The assignment operator does not return a value.

You must use a print statement to display the result of an expression in the Python shell. True False

False Correct! The expression is evaluated and the result is displayed immediately.

If A is false, then the B operand in the expression A or B is not evaluated. True False

False Exactly! Since A is false, the truth value of an or expression won't be known until B is evaluated.

Python variables that represent integers are NOT object reference variables. True False

False - Right! ALL Python variables are object references.

Why don't we use natural languages (like English) to program a computer? Natural languages are semantically ambiguous. It's often hard to understand people when they speak. There are too many words and phrases to understand in natural languages. Natural languages don't have syntax rules.

Natural languages are semantically ambiguous. Well done! A statement in a natural language can often have mutliple valid meanings.

What output does the following code produce? print('Total: ' + 100 + 20) Total: 120 Total: 10020 Total: 1000200 No output. This line would produce an error.

No output. This line would produce an error. Excellent! You can't concatenate a string and an integer in Python.

Python embodies elements of which programming paradigm?

Procedural programming Object-oriented programming Functional programming

Which of the following is NOT true regarding the return statement? The last statement in a function must be a return statement. The keyword return is usually followed by an expression. A function can have more than one return statement. When a return statement executes, the function stops executing immediately.

The last statement in a function must be a return statement. Exactly! This is often the case, but is not required.

What is a program's flow of control? The order in which loop bodies are executed. The order in which statements are executed. The order in which functions are called. The order in which interface events are processed.

The order in which statements are executed. Right! Many programming elements affect the flow of control.

What is the syntax of a language? A specification that lists a program's requirements. The rules that determine how words and symbols can be combined. A list of errors that can occur in a program. The meaning of a language element.

The rules that determine how words and symbols can be combined. Excellent! A program must be syntactically valid.

Python is a general-purpose programming language, appropriate for solving problems in many areas of computing. True

True

The output of a Python program run in Thonny appears in the shell window.

True

Thonny displays code using syntax highlighting.

True

Thonny has a package manager that lets you install and update external Python packages.

True

A function parameter ceases to exist when the function returns to the caller. True False

True Exactly! A parameter is a temporary variable.

After a function finishes executing, control returns to the place the function was called. True False

True Excellent! Control returns when a return statement is executed or when the end of the function is reached.

The stop value passed as an argument to the range function is NOT included in the range produced. True False

True Right! The range runs up to but doesn't include the stop value.

A with statement will automatically close an open file when processing is complete. True False

True Yes! Output files, especially, need to be closed correctly or data may be lost.

The open function can be used to open an input file or create an output file. True False

True Yes! The file mode determines what happens when the file is opened.

Using a while loop to check each input value for correctness is a form of input validation. True False

True Yes! The loop won't allow processing to proceed until a valid value is entered.

Which of the following is NOT a principle embraced by the Python programming language? Beautiful is better than ugly. Readability counts. Simple is better than complex. Complex is better than complicated. Verbose is better than succinct. Explicit is better than implicit. Verbose is better than succinct

Verbose is better than succinct

If a variable called jedi refers to the string 'Yoda', what is the result of the expression jedi * 3? YodaYodaYoda YYYooodddaaa 12 The expression will generate an interpreter error.

YodaYodaYoda Excellent! The repetition operator repeats the string a specified number of times.

What does list contain after the following code is executed? list = [77, 41, 92, 30, 38, 12, 63] list.sort() list.insert(2, 88) list.pop(5) [12, 30, 88, 38, 63, 77, 92] [12, 88, 30, 38, 63, 77, 92] [92, 77, 88, 63, 41, 30, 12] [12, 30, 88, 38, 41, 77, 92]

[12, 30, 88, 38, 41, 77, 92] Right! That's the list that would result from those operations.

Computing the wrong answer is an example of what kind of error? interface error logic error runtime error syntax error

logic error You got it! An error caused by faulty logic is called, cleverly enough, a logic error.

What output is produced by the following code? print('oak', 'elm', 'pine', end='!', sep='#')

oak#elm#pine!

Which category of data types represents an ordered collection of values? boolean numeric sequence dictionary

sequence Yes! Character strings and lists are both sequences.

If a variable called word refers to the string 'committee', what is the result of the expression word.strip('m')? 'committee' 'coittee' 'comittee' 'co'

'committee' Correct! The strip method only strips leading or trailing characters.

If the variable num contains a positive integer, what are the only two possible results of the expression num % 2? 0 and 2 0 and 1 1 and 2 -1 and 1

0 and 1 Excellent! 2 either divides the number evenly or there is 1 left over.

Which of the following is NOT a valid Python identifier?

1stPlace - can't begin with digit

If a variable called address refers to the string '123 Main Street', what is the result of the expression address.find('3')? -1 2 3 That expression would cause an error.

2 You got it! Since string indexes start at 0, the character '3' is at index 2.

What is the result of the expression 43 % 5? 3 4 8 8.6

3 Correct! There are 3 leftover after the division is performed.

Given the following line of code, what is the value of the expression nums.index(80, 2)?nums = [30, 80, 40, 80, 20, 20, 70, 30, 80] 1 2 3 4

3 Exactly! That's the index of the first occurrence of 80 when starting the search at index 2.

What is the result of the expression 7 // 2? 0 1 3 3.5

3 Well done! The fractional part of the result is truncated.

What is the Python Standard Library? A collection of software tools that support the development of Python programs. A set of officially sanctioned books about Python. A collection of programming components that can be used in any Python program. The set of documents that define the Python language.

A collection of programming components that can be used in any Python program. Right! It comes with any Python installation.

What is ASCII? A technique for ordering Unicode characters. A subset of Unicode containing primarily English characters and basic symbols. A technique for representing Unicode characters in memory. A set of non-printable Unicode characters. None of the above.

A subset of Unicode containing primarily English characters and basic symbols. Excellent! ASCII stands for American Standard Code for Information Interchange.

What is the Python shell? An integrated development environment for developing Python programs. An alternative to IDLE or Thonny. A window in which Python statements and expressions are executed immediately. A help system for Python programming.

A window in which Python statements and expressions are executed immediately. Good! It's a tool that is part of any Python development environment.

Given the following line of code, which expression would produce the substring 'snapper'?young = 'whippersnapper' young[7:14] young[7:] young[-7:] young[7:len(young)] All of the above. None of the above.

All of the above Right! They all result in the same slice.

dict

An unordered collection of key/value pairs.

set

An unordered collection of unique values.

How do you read an integer from a text file? By calling the read_int method of the file object. By calling the read_num method of the file object. By reading it as a string and converting it to an integer. By reading it as a floating-point value and truncating it.

By reading it as a string and converting it to an integer. You got it! All data read from a text file is initially read as character strings.

The expression x ^ y raises the value x to the power y. True False

False Exactly! The Python exponentiation operator is **.

The assignment operator has higher precedence than the arithmetic operators.

False Exactly! The assignment operator has a lower precedence than the arithmetic operators.

The effect of the setheading depends on the current heading of the turtle. True False

False Exactly! When setting the heading explicitly, the current heading doesn't matter.

An assignment statement affects a program's flow of control. True False

False Excellent! An assignment statement does not determine which statement is executed next.

The input function will produce an error if the value read is not numeric. True False

False Excellent! It's type conversion functions such as int and float that produce an error if necessary.

The break statement is essentially the same as a GOTO statement in older programming languages. True False

False Excellent! No, a break statement is an improvement over the GOTO, in that it only affects the current loop.

A character encoding is the list of all characters used by a programming language. True False

False Excellent! That's a character set. A character encoding defines how characters are stored in memory.

he read method of a file object reads the contents of a file one line at a time. True False

False Excellent! The read method reads the entire contents of a file, or a specific number of characters.

Python is dynamically typed, meaning each variable can only store one type of value. True False

False Good! Python is dynamically typed, but that means type checks are performed at runtime. A Python variable can store different types of values over time.

There is no way to stop an infinite loop. True False

False Good! The program must be interrupted by pressing Ctrl-C or Ctrl-Break or by closing the run window.

A turtle draws a filled circle using the fill_circle command. True False

False Good! There is no such turtle command.

All elements in a Python list must have the same data type. True False

False Right! A list can contain elements with differing types.

The write method automatically adds line numbers to the output. True False

False Well done! It doesn't do that.

A linear search examines every element in the list. True False

False Well done! Once the target element is found, there is no need to keep searching the list.

The less than operator (<) should not be used to compare floating point values. True False

False Well done! There's no problem using < or > to compare floating point values. The danger comes from comparing them for equality.

If both operands to the remainder operator (%) are positive, a divisor of n will produce a result in the range 1 to n. True False

False Yes! It will produce a result in the range 0 to n-1.

All Python data types are immutable. True False

False Yes! Some Python types are mutable, such as str and list.

The sample function in the random module takes a sample with replacement. True False

False Yes! The sample function takes a sample without replacement, which means all elements in the sample will be unique.

The write method adds a newline character to the end of the output. True False

False You got it! You can add newline characters to the output wherever appropriate.

In dynamically typed languages, variables are declared to store a specific type of data. True False

False Right! Variables don't have to be declared in dynamically typed languages.

Running a program that contains errors will cause the Thonny development environment to terminate.

False-program terminates not Thonny

Who developed the Python programming language?

Guido van Rossum

Thonny is best described as which of the following? Integrated Development Environment (IDE)

Integrated Development Environment (IDE)

The best way to read a file line by line is to use a for loop to iterate over the file object itself. True False

True Excellent! The code is more elegant and doesn't load the entire file contents into memory at once.

In some cases, a sentinel value can be used to signal the end of input. True False

True You got it! Each input value is compared to the sentinel value to know when the loop should terminate.

A character string can be used as the condition of an if statement. True False

True Excellent! Any object can be used as a condition. An empty string evaluates to False and any other string evaluates to True.

The result of a relational operator can be assigned to a variable. True False

True Good! A variable can store a True or False value.

The input function always returns the read data as a character string. True False

True You got it! You have to convert it to another type if needed.

The value assigned to a variable could be a floating point number. True False

True - Excellent! Both integers and floating point numbers can be stored in variables.

If either or both operands to the division operator (//) are floating-point values, then the result will be a floating-point value. True False

True - Good! And if both are integers, it performs integer division.

The Python turtle graphics module is based on the programming language Logo developed in the 1960s. True False

True - Right! And Logo was based on a physical robot.

The math module contains a constant that represents the value pi to several digits. True False

True - Right! It also has a constant for e, the base of the natural log, among others.

You cannot change the contents of Python character string once it has been created. True False

True - Right! Python strings are immutable.

The position of a circle depends on the current heading of the turtle. True False

True - Right! The circle is drawn to the left of the turtle.

What does list contain after the following code is executed? list = [82, 27, 66, 18, 40, 93, 85, 29] list.append(50) list.insert(3, 99) list.pop() [82, 27, 66, 99, 18, 40, 93, 85, 29] [27, 66, 99, 18, 40, 93, 85, 29, 50] [82, 27, 99, 66, 18, 40, 93, 85, 29] [50, 82, 27, 99, 66, 18, 40, 93, 85]

[82, 27, 66, 99, 18, 40, 93, 85, 29] Correct! The call to pop removes the item that was appended.

Which of the following statements is true?

a print statement is a call to a function

What output does the following code produce? print('apple', 'banana')

apple banana

For Python, the term data type can be used interchangeably with what other term? sequence index object class

class Correct! A class is used to define an object, and all values are objects in Python.

The Python type conversion functions are actually what kind of functions? iterators object constructors predicate functions user-defined types

object constructors You got it! The process of "conversion" is really creating a new object based on another.

Which statement would produce the following output? "Oom Pah Pah Oom Pah Pah," that's how it goes. print('\qOom Pah Pah Oom Pah Pah,\q that's how it goes.') print('\nOom Pah Pah Oom Pah Pah,\n that's how it goes.') print(""Oom Pah Pah Oom Pah Pah," that's how it goes.") print("\"Oom Pah Pah Oom Pah Pah,\" that's how it goes.")

print("\"Oom Pah Pah Oom Pah Pah,\" that's how it goes.") You got it! The \" escape sequences are needed to print the quotation marks.

What do the characters in a Unicode escape sequence represent? the set of characters terminating in a particular Unicode character the hexadecimal code for a particular Unicode character a range of Unicode characters the ASCII code for the equivalent Unicode character

the hexadecimal code for a particular Unicode character Good! They are the code of a single character expressed as a hexadecimal number.

Which of the following identifiers follows the convention for naming Python variables? TOTAL_VALUE totalValue Total_Value total_value

total_value You got it! A variable name should use lowercase letters and an underscore to separate words.


Ensembles d'études connexes

Constitutional Law I: Federalist Papers

View Set

Independent Samples & Paired Samples t-tests

View Set

Substance abuse/ Chapter 2- How is a drug made illegal?

View Set

Muscular System - Appendicular Muscles Lab Quiz

View Set

Pathophysiology: Chapter 33- Disorders of Thermoregulation

View Set