CS110 Exam 2

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

=' assigns whatever the expression on the right evaluates to to the variable on the left. '==' compares the right and left sides to see if they are the same object. 'is' compares the values on the right and left sides to see if they are the same. True False

FALSE

All lists (not tuples or dictionaries) can be sorted. True False

FALSE

For if - elif -else structure purposes, if you use a number instead of a boolean, 1 is considered True and everything else is False. True False

FALSE

Given: myString = "coronavirus", then myString[len(myString)] would return 's' True False

FALSE

Given:myTuple = ("a", "b", "c", "d", "e", "f")myTuple[4] will return "d". True False

FALSE

If you have multiple elif clauses in an if - elif - else structure, once a match is found the other clauses are still tested to see if they also match. True False

FALSE

The comparison operators that can give us True or False values are ==, !=, >, >=, <, <=, is, not, and, and or. True False

FALSE

The end value in a Python range is inclusive. True False

FALSE

The keyword 'break' inside a loop tells the program to skip the rest of the body and loop back to the top of the loop to begin the next iteration. True False

FALSE

The keyword 'continue' is used with 'break' to indicate where the program should resume after encountering 'break'. True False

FALSE

A tuple is mutable, meaning you can change its contents without having to specify a complete new tuple. True False

FALSE it is immutable, or unchangeable

Given: for x in range (5): print("Hi!") the output will be: (A) Hi! Hi! Hi! Hi! (B) Hi! Hi! Hi! Hi! Hi! (C) Hi! Hi! Hi! Hi!

Hi! Hi! Hi! Hi!

A tuple, list, or dictionary may be created with no elements inside. True False

TRUE

Boolean 'not' is the equivalent of arithmetic exponent and comes before 'and' (Boolean multiplication) followed by 'or' (Boolean addition) True False

TRUE

Comments delimited by """ can be used by the help function to document user-created functions. True False

TRUE

Comments may begin with a '#' and continue to the end of the line. If comments begin and end with """, they may span several lines. True False

TRUE

For an if - elif - else structure, there may be multiple elif clauses but the else is optional. True False

TRUE

Not covered in the video, but earlier in the semester we saw that if - elif - else clauses could contain nested if - elif - else structures. True False

TRUE

Processing functions return some value, which may be captured in a variable. True False

TRUE

Python is interpreted in the command shell and the files can be run as interpreted scripts. But Python scripts may also be compiled, if desired. True False

TRUE

The for structure in python takes a range in the form (start, end, increment) where the start and increment are optional. True False

TRUE

The keyword 'continue' inside a loop tells the program to skip the rest of the loop body and loop back to the beginning to start the next loop iteration. True False

TRUE

The primes() function contained a nested for loop that had a break statement inside an if statement. There was also an else clause that was not lined up with the if but instead was lined up with the for. The code is shown below:term-37 def primes(y): """ Starting with 2 up to but not including the number passed in, prints if the number is prime or the first factor if it is not.""" for n in range(2, y): for x in range(2, n): if n % x == 0: print(n, 'equals', x, '*', n//x) break else: # inner loop fell through print(n, 'is a prime number') Here is the T/F question: An else clause paired with a for loop only executes if the for loop does not execute a break statement. True False

TRUE

You can decrement in a for structure as long as you make sure the start value is larger than the end value. True False

TRUE

You can define a range used by a for structure by using a list. True False

TRUE

You can refer to elements in a string by index, just like you would for a tuple or list. True False

TRUE

myList.append(myValue) adds myValue to the right end of myList and myValue = myList.pop() removes a value from the right end of myList and stores it in myValue. True False

TRUE

programming language such as Python is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. [Recall "types" refer to a variety of basic data types: numbers (e.g. floating point or integers), strings (e.g. characters), or lists.] True False

TRUE

Given: def square10(): return 10 * 10 identify the correct statement. This is not a useful function because it is not modular and reusable. It is a non-processing function. A function name cannot have numbers in it. It cannot be a function because it has no parameters.

This is not a useful function because it is not modular and reusable.

Given: for range (5): print("Hi!") the output will be: Hi!Hi!Hi!Hi!Hi! Hi!Hi!Hi!Hi! an error because there is no variable in the for statement.

an error because there is no variable in the for statement.

Given:myString = "coronavirus"print(myString[5:7]) would print: vi vir av avi

av

Which is the correct statement? (A) Variables may change type and the type is determined by the value stored in the variable. (B) The type of a variable must be declared before it is used, but can be changed during runtime. (C) Variables have no type. (D) A variable's type is determined the first time it is used, after which it may not be changed.

(A) Variables may change type and the type is determined by the value stored in the variable.

In Python, everything is (A) an object. (B) a variable. (C) a class. (D) a method.

(A) an object.

The method dir() is available for any variable and (A) lists the methods available to the variable object based on the class it is derived from. (B) lists the help documentation for the object. (C) lists the characteristics (data) of the object. (D) lists the contents of the path where the variable is found.

(A) lists the methods available to the variable object based on the class it is derived from.

a = 5b = 13c = 55d = 22e = 17x = d < 12 or (c > b and not(5 != a) or (e < 20 and e < c)) print(x) prints: (A) False (B) True (C) None (D) File "<pyshell>", line 1x = d < 12 or (c > b and not(5 != a) or (e < 20 and e < c) ^SyntaxError: invalid syntax

(B) True

To create a list, the variable is assigned with ____ surrounding the elements. (A) < > (B) [ ] (C) { } (D) ( )

(B) [ ]

Given:myString = "coronavirus"print(myString[5:]) would print: (A) a (B) avirus (C) v (D) virus

(B) avirus

Dictionary elements consist of key and value pairs denoted by which following example? (Note, key does not have to be a character string, as in the video. A key can be any immutable type, such as a string, number, or a tuple that only contains immutable types. Value can contain any object type.) (A) myDictionary = {[1 : "one"], [2 : "two"], [3 : "three"]} (B) myDictionary = {1 : "one", 2 : "two", 3 : "three"} (C) myDictionary = {(1 : "one"), (2 : "two"), (3 : "three")} (D) myDictionary = {1 ; "one", 2 ; "two", 3 ; "three"}

(B) myDictionary = {1 : "one", 2 : "two", 3 : "three"}

Given:myList = [1, "a", 2, "b", 3, "c", 4, "d"]myValue = myList.remove(4)The result of the second line of code is (A) myList becomes [ 1, "a", 2, 3, "c", 4, "d"] and myValue = None (B) myList becomes [ 1, "a", 2, "b", 3, "c", "d"] and myValue = None (C) myList becomes [ 1, "a", 2, "b", 3, "c", "d"] and myValue = 4 (D) myList becomes [ 1, "a", 2, 3, "c", 4, "d"] and myValue = "b"

(B) myList becomes [ 1, "a", 2, "b", 3, "c", "d"] and myValue = None

a = 5b = 13c = 55d = 22e = 17x = d < 12 or c > b and not(5 != a or e < 20) and e < c print(x) prints: (A) True (B) None (C) False (D) File "<pyshell>", line 1x = d < 12 or c > b and not(5 != a or e < 20) and e < c^SyntaxError: invalid syntax

(C) False

To create a dictionary, the variable is assigned with ____ surrounding the elements. (A) < > (B) [ ] (C) { } (D) ( )

(C) { }

To create a tuple, the variable is assigned with ____ surrounding the elements. (A) < > (B) [ ] (C) { } (D) ( )

(D) ( )

a = 5b = 13c = 55d = 22e = 17x = d + 12 + c * b / (5**a - e - 20) * e / c print(x) prints: (A) None (B) 5.145557224750518e+24 (C) 11.046981818181818 (D) 34.071567357512954

(D) 34.071567357512954

Given:str1 = "bird"str2 = "cattle"str3 = str2[:3] + str1 str3 contains: (A) birdcat (B) cat bird (C) cattbird (D) catbird

(D) catbird

For the following while loop, match the code lines to the loop part: 1 count = 1 2 while count < 20: 3 count += 1 4 print(count) 5 print("That's all!")

1 initialize 2 test 3 change 4 body of loop 4 implicit loopback

Given: def double(inValue): return inValue * 2 identify the correct statements. 1.double() is a processing function. 2.double() is a non-processing function. 3.double(abs(-10)) is invalid because you can't use a function (abs()) as a parameter. 4.After the statement x = double(5), x would contain None. 5.double(abs(-10)) would return 20. 6.After the statement x = double(5), x would contain 10. 7.double(abs(-10)) would print "The double of 10 is 20.

double() is a processing function. double(abs(-10)) would return 20. After the statement x = double(5), x would contain 10.

Given: def rtTriangle(sideA, SideB): hyp = sqrt(sideA * sideA + sideB * sideB) angleA = atan(sideA/sideB) angleB = atan(sideB/sideA) return [hyp, angleA, angleB] identify the correct statement: rtTriangle() violates the rule that a function should only do a single task. rtTriangle() is not modular or reusable. rtTriangle() is a non-processing function. rtTriangle() illegally tries to return multiple values.

rtTriangle() violates the rule that a function should only do a single task.


Conjuntos de estudio relacionados

Intro to Computer Forensics Test 2

View Set

(Gillesania)ALGEBRA SETS 7, 8, 9

View Set

Chapter 23 "The Great Depression"

View Set

Chapter 4: Patient Safety and Quality Improvement

View Set

Purchasing and Buyer Behavior Exam 3 Review: Connect Quizzes

View Set

Ch. 12: Food, Soil, and Pest Management

View Set