Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 6 pages
Exam (elaborations)

ICS 31 Study Guide Questions and Answers

Document preview thumbnail
Preview 2 out of 6 pages

ICS 31 Study Guide Questions and AnswersWhat is one similarity between print() and input()? * Both generate a string output * Both are built-in or standard library functions When ever the Python interpreter needs to evaluate a name (of a variable, function, ...), it searched for the name in order: 1) 2) 3) 4) 1) local 2) Global 3)Module Import 4 ) Built in Explain the difference between 'import math' and 'from math import sqrt' or 'from math import*' 'Import math' and 'From math import' import all the functions/objects of the module while 'from module import sqrt' imports the specific function/object. "From math import' and 'From math import sqrt' allow the object to be referred to without including the module from which it was imported, which might lead to pollution of the name space. What is the difference between print() and return? * print(): prints the specified message to the screen * return : is used to end the execution of the function call and "returns" the result (value of the expression following the return keyword) to the caller * print example *return example Which operator has the lowest precedence? Why? count %= count - offset + 2 answer: (%=) explanation: Since the modulo operator is chained with the assignment operator(which has the lowest precedence), the %= will execute last. Do the keys in a dictionary best model a list or a set? + 5 pts [List or Set] Set + 5 pts [Uniqueness] Keys must be unique + 5 pts [Order] Keys and sets are unordered. + 5 pts [Mutability] Keys are immutable, and elements of a set have to be immutable Give two examples of functionality/applications that you can do with a dictionary you can't do with a list. + 2.5 pts Relationships between keys and values + 2.5 pts Addressing values by key rather than index Given a value for a dictionary, is there a built-in way to lookup the corresponding key? Explain your answer. + 4 pts There is NOT a built-in way. + 4 pts Keys are unique while values are not. + 4 pts Many keys might have the same associated value. Why does python provide so many different ways to structure & store data? What would happen if Python didn't provide data structure? +4 pts Answered first question: many ways to associate/store data +4 pts Programs would have an unmanageable number of variables Give two examples of functionality/applications that you can do with a dictionary you can't do with a list. + 2.5 pts Relationships between keys and values + 2.5 pts Addressing values by key rather than index + 2.5 pts Keys are unique + 2.5 pts Dictionaries are unordered When can you change the field of a namedtuple and not change the original object's id? Write a program to demonstrate. + 4 pts Answer: when the field being changed is a mutable object + 4 pts Example: nt = namedtuple('nt','immutable mutable') e = nt('s',[0]) d(1) Why is it good advise to use parentheses with Boolean expressions? *overrides operator precedence * for readability *you can use an example with the "and" and "or" operators to increase the precedence of the or operator What is the difference between a function that returns None and a value returning method? *a value returning method has a return statement, a function that returns None doesn't exist *result of a value returning method can be used in expressions(more commonly) Arguement the value that is sent to the function when its called Parameter a variable listed inside the parenthesis in a function's definition Why is it good advice to use parentheses with Boolean expressions? Give an example where it is necessary. + 4 pts override operator precedence + 4 pts For readability of a very long and unreadable boolean expression + 4 pts Example with both "and" and "or" operators where parentheses are used to increase the precedence of the "or" operato Why are the relational operators called "relational"? + 8 pts Compare two values to determine how they relate to one another (, , =, =, ==, !=) + 8 pts Makes a binary statement ranking two values Explain the differences between positive and negative indexing in Python. + 2.5 pts Start Index: positive uses '0', negative is -1 + 2.5 pts Incrementing: positive use +1, negative uses -1 + 2.5 pts Last Index: positive = len(lst)-1, negative = -len(lst) What is the main difference between looping-by-value and looping-by-index(as discussed during lecture)? Using code, give an example to describe your answer. Difference: Loop-By-Value 4 pts 2.1 Difference + 2 pts DIFFERENCE: Looping-by-Index allows direct access to containers original contents to be modified. 2.2 Example + 1 pts EXAMPLE: show loop-by-index: Header: for .... range(len())/enumerate __or__ while index len() + 1 pts EXAMPLE: show loop-by-index: Body/Change: lst[index] += 1 __or__ lst[index] += 1; index += 1 - 0.5 pts Your loop by value is a loop by index. + 0 pts Incorrect What is the problem with the program below? How would you fix it? numbers = list(range(lO)) for i in range(len(numbers)): if numbers[i] %2 == 0: del numbers[i] + 4 pts Problem Detection: deleting from a list while iterating over it + 2 pts Partially correct problem detection + 2 pts Solution: make a copy of the list + 2 pts Solution: use remove instead of delete with an index from list copy The Python code below is supposed to double the value of each element in a list. What are the problems with the program below? How would you fix it? data = [ 4 . 14, 7 . 2 3, 8 . 2 2] for i in range(data): i *= 2 + 8 pts Correct + 4 pts Problem Detection: deleting from a list while iterating over it + 4 pts Partially correct problem detection + 2 pts Solution: make a copy of the list + 2 pts Solution: use remove instead of delete with an index from list copy Write a function named, duplicate_ date, that takes three integers as parameters, representing a 2-digit month, 2-digit day, and 4-digit year. The function returns True when the combined value of the month-day, or daymonth are the same as the year, and False otherwise. Example: duplicate_date (10 ,25, 1025) returns True duplicate_date (25, 10, 1025) returns True duplicate_date (11, 22, 3333) returns False Write your program in a file named, duplicate_ & upload. Coding: duplicate_date + 0 pts Completely wrong + 1 pts return statement + 1 pts return true or false + 3 pts correct calculations + 2 pts one if/else or 'or' (for 'daymonth' or 'monthday') + 3 pts if/else or 'or' for both 'daymonth' and 'monthday' Write a function, get_longest_run(num_list), that returns the starting index in the array of a run of maximum size. A run is defined as the repeated occurrence of the same value in two or more consecutive positions in the array. If there are no runs return -1. get_longest_run([1, 2, 2]) - 1 get_longest_run([1, 2, 2, 99, 99, 99, 7]) - 3 get_longest_run([1, 11, 6, 7, 2]) - -1 getLongestRun + 10 pts Correct + 1 pts Loop through all elements + 2 pts Keep track of maximum run index + 2 pts Detect start/end of run + 2 pts Initialize run counter & incrementing + 2 pts Keep track of current run value + 1 pts Correct return value(-1 for no run) What is the difference between an argument & a parameter? Argument vs Parameter + 8 pts A parameter is initialized through an argument's value(passed through/into), an argument is the value passed through/into the function + 8 pts Not Expected: Correct answers pertaining to Scope/Mutability/ an argument lives outside the function's scope + 4 pts Partially Correct based on above statements + 3 pts Defined terms: parameter & argument, but did not explain a difference. + 2 pts Correctly defined one + 1 pts Not blank, but incorrect statements Compare & contrast how function parameters & local variables are similar/different. + 2 pts (SIMILARITY) Local in Scope + 2 pts (SIMILARITY) Scope extends from def to end of function + 2 pts (DIFFERENCE) Initialized in args vs explicitly What is the difference between a function that returns None and a value-returning function? None vs Value + 8 pts A value returning method has a return statement, a function that returns None does not + 8 pts Result of a value returning method can be used in expressions (more commonly) + 4 pts Partially Correct based on above statements + 1 pts Not blank, but incorrect statements

Content preview

ICS 31 Study Guide Questions and
Answers
What is one similarity between print() and input()? - answer * Both generate a string
output
* Both are built-in or standard library functions

When ever the Python interpreter needs to evaluate a name (of a variable, function, ...),
it searched for the name in order:
1)
2)
3)
4) - answer 1) local
2) Global
3)Module Import
4 ) Built in

Explain the difference between 'import math' and 'from math import sqrt' or 'from math
import*' - answer 'Import math' and 'From math import*' import all the
functions/objects of the module while 'from module import sqrt' imports the specific
function/object. "From math import*' and 'From math import sqrt' allow the object to be
referred to without including the module from which it was imported, which might lead to
pollution of the name space.

What is the difference between print() and return? - answer * print(): prints the
specified message to the screen
* return : is used to end the execution of the function call and "returns" the result (value
of the expression following the return keyword) to the caller
* print example
*return example

Which operator has the lowest precedence? Why?
count %= count - offset + 2 - answer answer: (%=)
explanation: Since the modulo operator is chained with the assignment operator(which
has the lowest precedence), the %= will execute last.

Do the keys in a dictionary best model a list or a set? - answer + 5 pts [List or Set]
Set
+ 5 pts [Uniqueness] Keys must be unique
+ 5 pts [Order] Keys and sets are unordered.
+ 5 pts [Mutability] Keys are immutable, and
elements of a set have to be immutable

, Give two examples of functionality/applications that you can do with a dictionary you
can't do with a list. - answer + 2.5 pts Relationships between keys and values
+ 2.5 pts Addressing values by key rather than index

Given a value for a dictionary, is there a built-in way to lookup the corresponding key?
Explain your answer. - answer + 4 pts There is NOT a built-in way.
+ 4 pts Keys are unique while values are not.
+ 4 pts Many keys might have the same associated value.

Why does python provide so many different ways to structure & store data? What would
happen if Python didn't provide data structure? - answer +4 pts Answered first
question: many ways to associate/store data
+4 pts Programs would have an unmanageable number of variables

Give two examples of functionality/applications that you can do with a dictionary you
can't do with a list. - answer + 2.5 pts Relationships between keys and values
+ 2.5 pts Addressing values by key rather than index
+ 2.5 pts Keys are unique
+ 2.5 pts Dictionaries are unordered

When can you change the field of a namedtuple and not change the original object's id?
Write a program to demonstrate. - answer + 4 pts Answer: when the field being
changed is a mutable object
+ 4 pts Example: nt = namedtuple('nt','immutable mutable')
e = nt('s',[0])
e.mutable.append(1)

Why is it good advise to use parentheses with Boolean expressions? - answer
*overrides operator precedence
* for readability
*you can use an example with the "and" and "or" operators to increase the precedence
of the or operator

What is the difference between a function that returns None and a value returning
method? - answer *a value returning method has a return statement, a function that
returns None doesn't exist
*result of a value returning method can be used in expressions(more commonly)

Arguement - answer the value that is sent to the function when its called

Parameter - answer a variable listed inside the parenthesis in a function's definition

Why is it good advice to use parentheses with Boolean expressions? Give an example
where it is necessary. - answer + 4 pts override operator precedence
+ 4 pts For readability of a very long and unreadable boolean expression

Document information

Uploaded on
January 3, 2025
Number of pages
6
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$13.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Pogba119
3.9
(14)
Sold
62
Followers
2
Items
5513
Last sold
5 days ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions