18CS55 ADP Question Bank Module 3, 4 and 5
18CS55 ADP Question Bank Module 3, 4 and 5
Date: 11-11-2021
1. What are Lists? List are mutable. Justify the statement with examples. [June 2018]
2. How tuples are created in Python? Explain different ways of accessing and creating them.
[June 2018]
3. Write a Python program to read all the lines in a file accepted from the user and print all email
addresses contained in it. Assume the email addresses contain only non-white space characters.
[June 2018]
4. Implement a Python program using List to store and display the average of N integers accepted from
the user. [June 2018]
5. Explain dictionaries. Demonstrate with a Python program. [June 2018]
6. Write a Python program to search for lines that start with the word 'From' and a character followed
by a two digit number between 00 and 99 followed by ':' Print the number if it is greater than zero.
Assume any input file. [June 2018]
7. Describe any two list operations and list methods. Write a python program to accept 'n' numbers
from user, find sum all even numbers and product of all odd numbers in entered list. [Dec 2018]
8. List merits of dictionary over list. Write a python program to accept USN and marks obtained,
find maximum, minimum and students USN who have scored in the range 100-85,85-75,75-60 and
below 60 marks separately. [Dec 2018]
9. Compare and contrast tupules with lists. Explain the following operations in tupules
a. Sum of two tupules
b. Slicing operators
c. Compression of two tupules
d. Assignment to variables. [Dec 2018]
10. Explain extracting data using regular expressions. Implement a python program to find for files
having ‘@’ sign between characters in a read text file. [Dec 2018]
Organizing Files
11. What is the difference between shutil.copy() and shutil.copytree()?
12. What function is used to rename files?
13. What is the difference between the delete functions in the send2trash and shutil modules?
14. ZipFile objects have a close() method just like File objects’ close() method. What ZipFile method is
equivalent to File objects’ open() method?
15. Explain the following functions with examples.
shutil.copy(source, destination)
shutil.copytree(source, destination)
shutil.move(source, destination)
os.unlink(path)
os.rmdir(path)
Debugging
16. Write an assert statement that triggers an AssertionError if the variable spam is an integer less than
10.
17. What are the two lines that your program must have in order to be able to call logging.debug()?
18. What are the five logging levels?
19. What line of code can you add to disable all logging messages in your program?
20. Explain Assertions with examples.
Module 4: Classes and objects, Classes and functions, Classes and methods
[TB 2: Chapter 15 – 17]
1. How class can be instantiated in python? Write a program to express instances as return
values to define a class RECTANGLE with members width, height and corner_x, corner_y
and member functions: to find center, area of a rectangle. [Dec 2018]
2. Explain init and str methods with an example python program. [Dec 2018]
3. Define polymorphism. Demonstrate polymorphism with function to find histogram to count
the number of times each letter appears in a word and sentence. [Dec 2018]
4. What is pure function? Write a python program to find duration of event if start and end
time is given by defining class TIME. [Dec 2018]
5. Create a student class and initialize it with name and roll number. Design methods to:
a. display: to display all information of the student.
b. setAge: to assign age to student.
c. setMarks: to assign marks to the student. [June 2018]
6. Using datetime module write a program that gets a current date and prints the day of the week.
[June 2018]
7. What are polymorphic functions? Explain with a snippet code. [June 2018]
8. What does the keyword self in python mean? Explain with an explain. [June 2018]
9. Show using a python code how init method is invoked when an object is initiated. Explain its
working. [June 2018]
10. Explain _str_ method with a python program. [June 2018]
11. Explain operator overloading with example.
12. Write a python program to define class Rectangle with members width, height and corner_x,
corner_y and member functions to find center of rectangle, and area of a rectangle.
Demonstrate usage of all the functions.
13. Write a python program
a. to find duration of event if start and end time is given, by defining class TIME.
b. to increment TIME object. [use integer to store time]
Exercise 2
Write a function named move_rectangle that takes a Rectangle and two numbers named dx and dy. It
should change the location of the rectangle by adding dx to the x coordinate of corner and adding dy to
the y coordinate of corner.
Exercise 3
Write a version of move_rectangle that creates and returns a new Rectangle instead of modifying the
old one.
Soln: ch15_e1_e2_e3.py
Exercise 2
Write a boolean function called is_after that takes two Time objects, t1 and t2, and
returns True if t1 follows t2 chronologically and False otherwise. (Avoid if statement)
Soln: ch16_e2.py
Exercise 3
Write a correct version of increment that doesn’t contain any loops.
Soln: ch16_e3.py
Exercise 4
Write a “pure” version of increment that creates and returns a new Time object rather than modifying the
parameter.
Exercise 5
Rewrite increment using time_to_int and int_to_time.
Exercise 2
Write an init method for the Point class that takes x and y as optional parameters and assigns them to
the corresponding attributes.
Exercise 3 Write a str method for the Point class. Create a Point object and print it.
Exercise 4 Write an add method for the Point class.
Exercise 5
Write an add method for Points that works with either a Point object or a tuple:
If the second operand is a Point, the method should return a new Point whose xcoordinate is the
sum of the x coordinates of the operands, and likewise for the ycoordinates.
If the second operand is a tuple, the method should add the first element of the tuple to
the x coordinate and the second element to the y coordinate, and return a new Point with the
result.
Exercise 6
Download the code from this chapter (http://thinkpython.com/code/Time2.py). Change the attributes
of Time to be a single integer representing seconds since midnight. Then modify the methods (and the
function int_to_time) to work with the new implementation. You should not have to modify the test
code in main. When you are done, the output should be the same as before.
Solution: http://thinkpython.com/code/Time2_soln.py
Module 5
Chapter 11: Web Scraping
1. Briefly describe the differences between the webbrowser, requests, BeautifulSoup, and selenium
modules.
2. What type of object is returned by requests.get()? How can you access the downloaded content as a
string value?
3. What Requests method checks that the download worked? Give example.
4. How can you get the HTTP status code of a Requests response?
5. How do you save a Requests response to a file?
6. What is the keyboard shortcut for opening a browser’s developer tools?
7. How can you view (in the developer tools) the HTML of a specific element on a web page?
8. What is the CSS selector string that would find the element with an id attribute of main?
9. What is the CSS selector string that would find the elements with a CSS class of highlight?
10. What is the CSS selector string that would find all the <div> elements inside another <div> element?
11. What is the CSS selector string that would find the <button> element with a value attribute set to
favorite?
12. Say you have a Beautiful Soup Tag object stored in the variable spam for the element <div>Hello
world!</div>. How could you get a string 'Hello world!' from the Tag object?
13. How would you store all the attributes of a Beautiful Soup Tag object in a variable named linkElem?
14. Running import selenium doesn’t work. How do you properly import the selenium module?
15. What’s the difference between the find_element_* and find_elements_* methods?
16. What methods do Selenium’s WebElement objects have for simulating mouse clicks and keyboard
keys?
17. You could call send_keys(Keys.ENTER) on the Submit button’s WebElement object, but what is an
easier way to submit a form with Selenium?
18. How can you simulate clicking a browser’s Forward, Back, and Refresh buttons with Selenium?
*****