DS 120 Quizzes 1-6

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Which of the following is an invalid expression: A. Variable_one += 5 B. Variable_three <=> 2 C. Variable_four %= 4 D. Variable_two ^= 4

B A, B, and C answers contain Python extended operators. The <=> expression is called a spaceship operator and is valid in some programming languages but not in Python.

Boolean not and bitwise NOT(~) operators can be used interchangeably.

False An example of a case when bitwise NOT and boolean not return different values is the following. print(bool(~True), bool(not True))

capitalize method takes string as an argument.

False Any string method follows the string and the string itself cannot be given as an argument to that method.

The following expression is evaluated from right to left: ~4*5**6

False False as ** is evaluated first, next the ~ operator is evaluated and * is evaluated at the end. Thus, it is not right to left.

Python does not allow to use float, type, sum, and int keywords as identifiers.

False False as Python built-in functions are allowed to be used as identifiers but it is not recommended to do so.

Bitwise and boolean operators can be used interchangeably.

False False as corresponding bitwise and boolean operators cannot be used interchangeably as they output different results for the same expression. For example, 1 & 2 outputs 0 but 1 and 2 outputs 2.

When an expression contains multiple operators with the same precedence (operators can repeat) then the expression is evaluated from left to right.

False False as for example 2**4**8 contains two ** operators with the same precedence but the expression is evaluated from right to left.

Two identical strings that contain non-alphanumeric characters do not have the same memory address in a given session.

False False as for example in the below case they have the same id. a="$" b="$" print(id(a)==id(b))

The id of None built-in data type is the same regardless of the session.

False False as the id of any Python object, including None, changes in different sessions.

The id of 5.0 is always the same in the same session.

False False as two different instances of any float numbers do not have the same memory address.

Non-alphanumeric characters cannot be used as identifiers.

False False as underscore (_) can be used in identifiers.

The filter function always alters the size of the list.

False If the function given as an input to filter never evaluates to False, you get the copy of the original list.

Integers and tuples are not iterable objects.

False Integers are not iterable but tuples are iterable.

List comprehension supports conditional statements containing "elif" part.

False It is not possible to write condition statements with "elif" part in a list comprehension.

A double asterisk(**) operator could be used to supply nested lists to a function as an argument.

False It is not possible. Try it yourself. In most cases, you use a dictionary for that cases.

The number of keys in dictionary comprehension is always less than or equal to the number of keys in the original dictionary.

False No, for example: a={(1,2):(3,4), (5,6):(7,8)} {m:n for i in a.items() for m, n in i}

Python allows writing "elif" statement after "else" statement.

False No, it is not possible.

It is possible to call a function without required parameters.

False No, it is not possible: def func(a): return a func() ------------------------------ TypeError Traceback (most recent call last) <ipython-input-17-c0755a86d5ca> in <module> 1 def func(a): 2 return a ----> 3 func() TypeError: func() missing 1 required positional argument: 'a'

The number of elements in a list comprehension is always equal to the number of elements in the original list.

False No, you can write filtering list comprehension the output list of which will have a shorter length than the original list.

The number of elements in the filtering list comprehension is always less than the number of elements in the original list.

False No. You can have a case where your filtering condition never evaluates to False and you get back the copy of the original list.

In a for loop pass statement does not execute the code following itself and continues to the next iteration.

False Pass statement does not interfere with the execution of any code.

"elif" keyword can be used in an inline conditional expression.

False Python inline conditional expressions does not allow the usage of "elif" keyword.

Running os.listdir() in the current working directory that contains files and folders will output only the list of folder names.

False Running os.listdir() in the current working directory that contains files and folders will output the list of folder and file names.

The expressions in the following string would be evaluated correctly:"Hi, {~50}. You are a {5+5}."

False The expression will not be evaluated correctly as there is no f before the string.

Any sequence of characters between double quotes is a valid string.

False The statement is False as "some text" some text" is not a valid string.

[(1,2,3), (1), tuple(tuple())] all the elements in the following list are tuples.

False The statement is False as (1) is integer one in the brackets. It would be a tuple in this form: (1,).

Applying + and * operations on the list does not create a new list but rather extends the original list.

False The statement is False as + and * operations always create a new list.

Changing a value in an alias of a list does not change that value in the original list.

False The statement is False as any change made to the alias of the list affects the initial list too.

append and extend methods of the list can be used interchangeably.

False The statement is False as append and extend do different operations.

Changing an element in a shallow copy of a list does not change that value in the original list.

False The statement is False as changing a mutable element of a list in the shallow copy of the list changes that element of the initial list as well.

Dictionary can have repeating keys.

False The statement is False as dictionary does not allow repeating keys.

Any sequence of characters in a raw string literal is a valid string.

False The statement is False as for example r"hello\" is not a valid string because backward slash makes the last double quote a character which causes a syntax error.

The type of formal variable-length parameter within the function is always a tuple.

False The statement is False as it can be dictionary too.

Any type of element can be used as a key for a dictionary.

False The statement is False as mutable data types cannot be used as dictionary keys.

It is possible to store duplicate strings in a set if the strings have different memory addresses.

False The statement is False as set does not allow duplicate values in it.

set is an ordered collection of unique elements.

False The statement is False as set is an unordered collection of elements.

sort() and sorted() functions will output the same result for a given list.

False The statement is False as sort() outputs None but sorted() outputs the sorted list.

The specifier in the following f-string will round 0.1+0.2 to the fifth decimal point f'{0.1+0.2: 5}.' .

False The statement is False as the character f before 5 is missing.

split method of the string returns two strings that are substrings of the original string.

False The statement is False as the output of split is a list and the number of splits is not limited to two.

str built-in data type is composed of characters and each character has char built-in data type.

False The statement is False as there is no char built-in data type in Python.

Operations with a tuple do not create a new tuple but rather modifies the original tuple.

False The statement is False as tuple is immutable and operations with it result in a new tuple.

"while True:" loop will run infinitely no matter what code is executed under it.

False The statement is False as you can have if statement with break part and the execution of the "while True" will be stopped when expression in if part evaluates to True.

{} expression produces an empty set.

False The statement is False as {} is an empty dictionary. Empty is set().

The id of 5 is always the same.

False The statement is False because the id of 5 may change from session to session.

Modifying the state of a formal parameter within a function body does not alter the actual parameter.

False The statement is False. You can check it: def func(a=[1,2]): print(id(a)) a.append(5) print(id(a)) return a print(func()) 2690114568064 2690114568064 [1, 2, 5]

The id of None is always the same.

False The statement is True because it is impossible to allocate two different objects at the same memory address at the same time.

Two integers that are equal have the same id.

False The statement is false as for integers not between [-5, 256] the IDs of equal integers will differ. a=1000; b=1000; print(id(a)==id(b))

Calling all() function on a list of empty elements will output:

False This is an axiomatic truth. No need to explain. all(['', 0, False, {}]) False

Calling any() function on a list of empty elements will output:

False This is an axiomatic truth. No need to explain. any(['', 0, False, {}]) False

list(map) is faster than a list comprehension.

False This is an axiomatic truth. No need to explain. You can test it.

os.mkdir("Data") will create a new empty folder in "Data" folder (directory).

False os.mkdir("Data") will create "Data" folder in the current working directory.

os.rmdir() deletes the items in the folder.

False os.rmdir() deletes folder (directory) given as an input if the folder is empty.

Any conditional statement (if statement) must contain "else" part.

False No, you can have if statement without else part. if 1: print("hi")

The boolean value of all floating-point numbers except 0.0 is True.

True As 0.0 is the only empty floating-point number, the statement is True.

There is no empty object that has a Boolean value not equal to False.

True By default in Python the Boolean value of any empty object is False.

The resulting list from list comprehension does not have the same id() as the original list.

True List comprehension creates a new list. Thus, the ids of the original and resulting lists are different.

split method of the string returns two strings that are substrings of the original string.

True List is mutable and it can contain elements with different data types.

The 'a is b' operator always returns True if a and b have the same memory address.

True The is operator checks whether the memory addresses of two objects match thus the statement is true.

Changing a value in a deep copy of a list does not change that value in the original list.

True The statement is True as deep copying of a list not only copies the references of the list but also underlying elements of the list.

It is possible to add multiple items with the same key to the dict.

True The statement is True as dictionary allows to add multiple items with the same key but keeps only the last one.

Any type of element can be used as a value for a dictionary.

True The statement is True as dictionary data type does not have any restrictions on its values.

Python, as a dynamically-typed language, does not require the indication of the object's data type.

True The statement is True as python is indeed dynamically-typed and as such does not require you to use identifiers.

It is not possible to construct a set of sets.

True The statement is True as set is a mutable object and you cannot add mutable objects to a set.

os.chdir(os.getcwd()) will not change the current working directory.

True The statement is True as with the above code you change from the current working directory to the current working directory.

It is possible to create a set of frozensets.

True The statement is True as you can add any immutable object to a set.

Different objects cannot use the same memory location at the same time.

True The statement is True because it is impossible to allocate two different objects at the same memory address at the same time.

Reassigning a new value to a formal parameter within a function body does not alter the actual parameter.

True The statement is True. You can check it: def func(a=1): print(id(a)) a = 5 print(id(a)) func() 140729528297248 140729528297376

**kwargs allows you to call the function with as many keyworded parameters as you wish.

True This is an axiomatic truth. No need to explain.

A conditional statement with else and multiple "elif" parts does not evaluate remaining "elif" and else parts if one of the "if" or "elif" statements evaluates to True.

True This is an axiomatic truth. No need to explain.

It is possible to add corresponding elements of 3 same-length lists containing integers, using list comprehension and zip function.

True This is an axiomatic truth. No need to explain.

It is possible to combine elements of a list of lists into 1 list (flatten a list of lists) using list comprehension.

True This is an axiomatic truth. No need to explain.

It is possible to make list comprehensions execute faster by using walrus operator.

True This is an axiomatic truth. No need to explain.

It is possible to supply a list of arguments using * operator.

True This is an axiomatic truth. No need to explain.

It is possible to use tqdm function to estimate the duration of each iteration of for loop.

True This is an axiomatic truth. No need to explain.

Local variables of a function cannot be accessed outside the function by default.

True This is an axiomatic truth. No need to explain.

Replacing return with yield keyword will always create a generator.

True This is an axiomatic truth. No need to explain.

The expression between "if" and ":" in a conditional statement always evaluates to True or False.

True This is an axiomatic truth. No need to explain.

The output of a list comprehension is always a list.

True This is an axiomatic truth. No need to explain.

iter() built-in function can only be called on iterable objects.

True This is an axiomatic truth. No need to explain.

os.chdir('..') will change the current working directory to the parent directory of the current working directory.

True This is an axiomatic truth. No need to explain.

os.remove() is used for deleting files.

True This is an axiomatic truth. No need to explain.

range, list, and set are iterable objects.

True This is an axiomatic truth. No need to explain.

Calling all() function on a list of non-empty elements will output:

True This is an axiomatic truth. No need to explain. all[1, 5.5,"ec3e"]) True

Calling any() function on a list of non-empty elements will output:

True This is an axiomatic truth. No need to explain. any([1, 5.5,"ec3e"]) True

List comprehensions are faster than their corresponding "for" loops.

True This is an axiomatic truth. No need to explain. You can test it.

Two integers with the same value in the range -5 to 256 inclusive are allocated to the same memory address in the same session.

True True as according to the Python implementation, indeed if two integers have the same value and are in [-5,256] range they will have the same id.

What is the result of the following expression: True or False and False

True True as and operator has higher precedence than or operator so (False and False) is executed first and after that True or False is executed which returns True.

Reassigning a new value to the alias of an identifier does not affect the value of the aliased identifier.

True True as reassigning a new value to the alias of the identifier breaks the reference to the aliased identifier. For example, a=1; b = a; b=5; print(a) (a will remain unaffected). a=[1.2.3] b=a a=5 print(b)

The output of the reduce function applied on a list can be the original list.

True Yes, it can be: Example 1 from functools import reduce ls = [1, 2] reduce(lambda a, b: [a]+[b], ls) >[1, 2] Example 2 from functools import reduce ls = [1,2,3,4]​ reduce(lambda a, b: ls, ls) >[1, 2, 3, 4]

Another list comprehension can be executed inside a list comprehension.

True Yes, it is possible. [[i for i in range(10)] for j in range(5)]

It is possible to modify the original list during the list comprehension's iteration process over that list.

True Yes, it is possible. ls=[1,2,3] [ls.append(i) for i in ls]

List comprehension can run infinitely.

True Yes, it is possible. ls=[1,2,3] [ls.append(i) for i in ls]

It is possible to supply a keyword argument after a non-keyword argument.

True Yes, it is possible: def func(a, b=1): return a func(2)

It is possible to define a function without a def keyword.

True Yes, you can create an anonymous(lambda) function.

Local variables of a function can be made accessible outside of the function.

True Yes, you can use global keyword to make them accessible outside of the function.

It is possible to create nested conditional statement (when a conditional statement contains another conditional statement).

True Yes, you can. if True: if True: print("bye")

break statement cannot be used directly inside a list comprehension.

True You cannot use break, continue and pass statements in any comprehension but you can define a function that has break, continue or pass statement and call it in any comprehension.

continue statement cannot be directly used inside a list comprehension.

True You cannot use break, continue and pass statements in any comprehension but you can define a function that has break, continue or pass statement and call it in any comprehension.

pass statement cannot be used directly inside a list comprehension.

True You cannot use break, continue and pass statements in any comprehension but you can define a function that has break, continue or pass statement and call it in any comprehension.

[::-1] slice will reverse the list.

True [::-1] slice means start=0, stop = end of the list, and step=-1. The latter iterates over the list from the end reversing it.


Set pelajaran terkait

Lesson 11-9: What is a generator?

View Set

Cardiovascular Eye Ear Medications

View Set

NCLEX Prep II Gastrointestinal Medications

View Set

Series 7 Kaplan Class Notes - Day 3 / Post Unit 11 Notes

View Set

Management - Final Exam Study Sheet

View Set

APES Semester 2 Final Exam Review

View Set

CH 6 - Business Strategy: Differentiation, Cost Leadership, and Blue Oceans

View Set