Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

PLCY UNC 110 COMP 110: Final Exam 2 Study Guide (Questions with Answers), Exams of Advanced Education

This study guide provides a comprehensive set of questions and answers covering key concepts from plcy unc 110 comp 110, focusing on unit testing, environment diagrams, loops, dictionaries, modules, and error handling in python. The guide is designed to help students prepare for the final exam by reinforcing their understanding of these fundamental programming concepts.

Typology: Exams

2024/2025

Available from 12/12/2024

Martin-Ray-1
Martin-Ray-1 🇺🇸

5

(8)

6.3K documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PLCY UNC 110 COMP 110: Final Exam 2
Study (Questions with Answers.)
One benefit of unit testing is that once you have written your test
functions it is less cumbersome to rerun multiple tests than testing
manually in the REPL. - Answers. ✔✔True
One benefit of unit testing is that when a test fails the output is more
helpful in showing you where exactly a test case is failing. - Answers.
✔✔True
The assert keyword should be followed by a boolean expression. This
expression should evaluate to True if the unit of functionality you are
testing is working correctly, as expected. - Answers. ✔✔True
When using pytest, a popular unit testing framework, what convention
must the file name of the file containing your test functions follow? -
Answers. ✔✔Ends with _test.py
When using pytest, what convention must the names of the test
functions inside the test file follow in order to be discovered as test
cases? - Answers. ✔✔begins with test_
Suppose your test subject(s) (the function(s) you are working on) are
contained in a directory named lessons and a file named subjects.py.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download PLCY UNC 110 COMP 110: Final Exam 2 Study Guide (Questions with Answers) and more Exams Advanced Education in PDF only on Docsity!

PLCY UNC 110 COMP 110: Final Exam 2

Study (Questions with Answers.)

One benefit of unit testing is that once you have written your test functions it is less cumbersome to rerun multiple tests than testing manually in the REPL. - Answers. ✔✔True One benefit of unit testing is that when a test fails the output is more helpful in showing you where exactly a test case is failing. - Answers. ✔✔True The assert keyword should be followed by a boolean expression. This expression should evaluate to True if the unit of functionality you are testing is working correctly, as expected. - Answers. ✔✔True When using pytest, a popular unit testing framework, what convention must the file name of the file containing your test functions follow? - Answers. ✔✔Ends with test.py When using pytest, what convention must the names of the test functions inside the test file follow in order to be discovered as test cases? - Answers. ✔✔begins with test Suppose your test subject(s) (the function(s) you are working on) are contained in a directory named lessons and a file named subjects.py.

Imagine the function you are working on is named example_function. How would you import the function into your tests file? - Answers. ✔✔from lessons.subjects import example_function Suppose your test functions are contained in a directory named lessons and a file named subjects_test.py. How would you run pytest from the terminal? - Answers. ✔✔python - m pytest lessons/subjects_test.py You should write many individual tests that probe specific, individual use and edge cases rather than very few complex tests that probe many use/edge cases at the same time. - Answers. ✔✔True The same variable name cannot have different meanings in different contexts within a single Python program. - Answers. ✔✔false In an environment diagram, a function call frame is its own context for the names of parameters and variables defined within the function. - Answers. ✔✔True What would be the printed result of evaluating the following Python code listing? x: int = 0

In the evaluation of this code, how many contexts/frames is the identifier x declared in? In other words, how many separate variables share the name x? What is y's value in the frame for the call to f? - Answers. ✔✔ 3 1 2 Which of the following names is likely to be a named constant, just by looking at its capitalization? - Answers. ✔✔FREE_RADICALS After a named constant is initialized, you are encouraged to reassign new values to it as you see fit, just like a variable. - Answers. ✔✔False A magic number is always a prime number. - Answers. ✔✔False To be able to assign a new value to a global variable from within a function definition, you must use the global keyword to declare your intent to modify a specific global variable. Otherwise, you will establish a local variable within the context of the function call frame. - Answers. ✔✔True

Consider the following syntax and assume you are using a for.. in to loop through a list of items. For x in y:

...

What is y in this example? - Answers. ✔✔The list of items Consider the following syntax and assume you are using a for..in to loop through a list of items. for x in y:

...

What is x in this example? - Answers. ✔✔The name of the variable of an individual item inside the loop body A while loop offers more control over iteration through a list than a for.. in loop. - Answers. ✔✔True For any task a for..in loop can achieve, writing a while loop to achieve the same result will typically result in less code for you to write. - Answers. ✔✔False

Which statement would fill in the blank to make the while loop's y equivalent to the for.. in loop's y? - Answers. ✔✔y: int = ys[i] How does Python know the data type of the variable name of a for.. in loop when it is used inside of the for..in loop body? - Answers. ✔✔It is inferred based on the data type of the items of the list. Dictionaries do not allow you to decide the type of your keys. - Answers. ✔✔False Dictionary keys "map to" or are associated with values. - Answers. ✔✔True In the following type declaration, what is the key type? dict[int, str] - Answers. ✔✔int In the following type declaration, what is the value type? dict[str, float] - Answers. ✔✔float

To insert a new key-value pair into a dictionary, you must use the append method of a dict. - Answers. ✔✔False Dictionary literals are surrounded in which type of delimiter? - Answers. ✔✔Curly Braces Suppose you have the following dictionary: courses: dict[int, str] = dict() courses[110] = "Intro to Programming" courses[210] = "Data Structures" How would you access or look-up the string "Intro to Programming"? In other words, which of the following expressions would evaluate to the string "Intro to Programming"? - Answers. ✔✔courses[110] When you remove a key-value pair using the pop method, which do you provide as an argument? - Answers. ✔✔The key of the key-value pair to pop You can directly and efficiently lookup a key by its value in a dictionary.

  • Answers. ✔✔False Which of the following tests if the key "Duke" exists in the dictionary schools and evaluates to a bool? - Answers. ✔✔"Duke" in schools

In Python, what kind of error do you get if you attempt to access a key that is not in a dictionary? - Answers. ✔✔KeyError The last line of an error message in Python tells you what? - Answers. ✔✔The type of error The second to last two lines of the traceback of an error message in Python tells you what? - Answers. ✔✔The file, line number, and statement where the error occurred. It is also possible to loop over the keys of a dictionary using a for in loop. In this follow-along section, you should comment out the line of code that contained the error, as follows:

print(schools["UNCC"])

Then, below this print statement, add the following code:

Example looping over the keys of a dict for key in schools: print(f"Key:

{key} - > Value: {schools[key]}") When you run your program after adding the snippet above, what is the first line of output that results from the print in the for..in loop? - Answers. ✔✔Key: UNC - > Value: 19400

T/F: If you change the for..in example to use the following variable name in the for..in loop, the example still behaves exactly the same. for school in schools: print(f"Key: {school} - > Value: {schools[school]}") - Answers. ✔✔True In the example of 21.2, where the type of schools is dict[str, int], the variable named school implicitly has what data type? - Answers. ✔✔str In the example of 21.2, where the type of schools is dict[str, int], the expression schools[school] implicitly has what data type? - Answers. ✔✔int Which syntax will import the module named helpers from the package named lessons? - Answers. ✔✔from lessons import helpers When an import statement imports another module for the first time, what happens at a high level? - Answers. ✔✔The entire imported module is evaluated Suppose a module named foo is imported from a packaged named bar. In the scope (frame) where foo is imported, foo is bound to the globals frame of the module foo after the entire module evaluates. - Answers. ✔✔True