Python Sample Final Exam Review PDF

Summary

This document contains a sample final exam review for a Python course. The exam covers topics such as true or false questions, multiple choice questions, and basic web scraping concepts. Solutions are included for each problem.

Full Transcript

Web Mining and Social Network analysis Final Exam Review – Sample Exam Python A. True or False 1. In Python, math expressions are always evaluated from left to right, no matter what the operators are. ANS: False 2. A good...

Web Mining and Social Network analysis Final Exam Review – Sample Exam Python A. True or False 1. In Python, math expressions are always evaluated from left to right, no matter what the operators are. ANS: False 2. A good way to repeatedly perform an operation is to write the statements for the task once and then place the statements in a loop that will repeat as many times as necessary. ANS: True 3. Python allows you to pass multiple arguments to a function. ANS: True 4. The following code will display 'yes + no': mystr = 'yes' yourstr = 'no' mystr = mystr + yourstr print(mystr) ANS: False 5. The following code is correct if clause to determine whether choice is anything other than 10? if choice != 10: ANS: True B. Multiple Choice 6. The __________ function reads a piece of data that has been entered at the keyboard and returns that piece of data, as a string, back to the program. a. input() b. output() c. eval_input() d. str_input() ANS: A 1 7. What is the output of the following command, given that value1 = 2.0 and value2 = 12? print(value1 * value2) a. 24 b. value1 * value2 c. 24.0 d. 2.0 * 12 ANS: C 8. In Python, a comma-separated sequence of data items that are enclosed in a set of square brackets is called a. sequence b. variable c. value d. list ANS: D 9. What will be displayed after the following code is executed? total = 0 for count in [1,2,3]: total = total + count print(total) a. 1 3 6 b. 5 c. 1 4 d. 6 ANS: D 10. What will be the output after the following code is executed? def pass_it(x, y): z = str(x) + ", " + str(y) num1 = 4 num2 = 8 answer = pass_it(num1, num2) print(answer) a. 4, 8 b. 8, 4 2 c. 48 d. None ANS: D 11. What are the valid index values for extracting 'New York'from the following string? city = 'New York city' a. city[0:8] b. city[:8] c. city[0:-5] d. All of the above. ANS: D 12. What is the number of the first index in a dictionary? a. 0 b. 1 c. the size of the dictionary minus one d. Dictionaries are not indexed by number. ANS: D 13. What is the value of the variable phones after the following code executes? phones = {'John' : '5555555', 'Julie' : '5557777'} phones['John'] = '5556666' a. {'John' : '5555555', 'Julie' : '5557777'} b. {'John' : '5556666', 'Julie' : '5557777'} c. {'John' : '5556666'} d. This code is invalid. ANS: B 3 Web Scraping html_code = """ Food My Favorite Food Items This page lists all tasty food items. Cauliflower Fritters Deep fried cauliflower with a glaze of soy sauce Chickpea Curry Boiled chickpeas cooked in a tangy and spicy tomato sauce """ import pandas as pd from bs4 import BeautifulSoup soup = BeautifulSoup(html_code,'html.parser') 1.Which one of the following codes extracts the tag from html_code? a. soup.h1 b. soup.body.h1 c. soup.find('h1') d. soup.find(id="top") e All of the above Answer: E 2.The following tag extracts the first tag with class equal to "food" and id equal to "top"? soup.find('h1', id="top", class="food" ) a. True b. False 4 Answer: False 3. Which one of the following codes extracts the value of the class attribute of all the tags in html_code? A. for p in soup.find("div").find_all("p"): print(p.attrs['class']) B. for p in soup.find_all("div").find_all("p"): print(p.attrs['class']) C. for p in soup.find("div").find("p"): print(p.attrs['class']) D. for p in soup.find_all("div").find("p"): print(p.attrs['class']) Answer: A 4. Which one of the following codes doesn't print the tag object of the tag that says, "Cauliflower Fritters"? A. soup.find_all("p") B. soup.find_all(class_="food", limit=1) C. soup.find(id="content").find("p") D. soup.div.p E. soup.find_all("p", string="Cauliflower Fritters") Option B 5. How many list items will be there in pTags? pTags = soup.body.find_all("p", recursive=False) print(len(pTags)) A. 4 B. 5 C. 0 D. 1 Option D 6. What is the output produced by the following statement? print(soup.find("h1", attrs ={"id":"top" ,"class":"sister"})) 5 A. [] B. None C. My Favorite Food Items D. [My Favorite Food Items] Option B **********Imagine that we wrote the following code to extract the data from html_code: import pandas as pd from bs4 import BeautifulSoup soup = BeautifulSoup(html_code,'html.parser') divTags = soup.find("div", id="content") # line 1 pfood = divTags.find_all("p", class_="food") # line 2 pdesc = divTags.find_all("p", class_="desc") # line 3 food_list = [] # line 4 desc_list=[] # # line 5 for p in pfood: # line 6 food = p.get_text().strip() # line 7 food_list.append(food) # line 8 for d in pdesc: # Line 9 desc = d.get_text().strip() # Line 10 desc_list.append(desc) # Line 11 data1 = {'food':food_list,'desc':desc_list} print(data1) df = pd.DataFrame(data1) print(df) 7. Let's say that we want to remove line 1. Still, we want the program to produce the same dataframe df. Replacing "divTags" with "soup" in lines 2 and 3 should work. A. True B. False Answer: True 8. Imagine that we wanted to handle errors in case a food description is not on the website. The following replacement for lines 9 to 11 works to produce the same dataframe df. 6 for d in pdesc: try: desc = d.get_text().strip() desc_list.append(desc) except: desc= "NA" desc_list.append(desc) A. True B. False Answer: True 9. If we added print(p) within the "for" loop at line 6, print(p)will display ______ in each iteration. (a) Contents (i.e., the actual data) (b) List Object (c) Tag Object (d) None of the above Option C 10. The following code extracts the first tag in markup1. markup1='Page 1Page 1' soup1 = BeautifulSoup(markup1,"html.parser") print(soup1.find_all(data-cap="error")) A. True B. False Answer: False 11. A data scientist started writing code to search the following books on amazon.com. data science finance web mining scraping Amazon’s URL follows the following pattern for searching. https://www.amazon.com/s?k=life+lessons https://www.amazon.com/s?k=management+information+systems https://www.amazon.com/s?k=business Given this pattern, the data scientist wrote the following "for" loop to create links? Does this work in creating a list of search URLs? Mark True if "Yes", else mark "False" 7 search_strings = ["data science","finance","web+mining+scraping"] search_strings_list=[] for search in search_strings: created_link = "https://www.amazon.com/s?k=" + search_string search_strings_list.append(created_link) print(created_link) A. True B. False Answer: False 12. A tag’s children are available in a list called A..descendants B..contents C..children D..nested_tags Answer: B 2. The following tag extracts the first tag with class equal to "food" and id equal to "top"?4 15. 8

Use Quizgecko on...
Browser
Browser