
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
PYTHON LEARNING EXAM WITH VERIFIED SOLUTIONS 2026 #5
Typology: Exams
1 / 40
This page cannot be seen from the preview
Don't miss anything!

































The Internet of Things is made up of a growing number of devices that collect and transfer data over the Internet without human involvement. - correct answer True Which of the following is not a primary contributor to data science? A)domain expertise B)computer science C)procurement D)statistics - correct answer c)procurement Which of the following is not a good description of the goal of data science? A)Turn data into information, and information into knowledge. B)Use data to answer real-world questions. C)Protect data against invalid access. D)Use data to gain insight, and use insight to take action. - correct answer c)Protect data against invalid access. What does "cleaning" data mean? A)Truncating long floating-point values to a reasonable number of decimal places. B)Removing data that does not support the desired conclusion. C)Preparing data for analysis by improving its structure and organization. D)Presenting data in clear graphs and diagrams. - correct answer c)Preparing data for analysis by improving its structure and organization. The data science process is linear. - correct answer False Python is probably the most popular programming language for data science. - correct answer True Module - correct answer a single Python file that contains a group of related functions, classes, and variables. Example: the math module- contains many functions that perform basic mathematical operations.
Package - correct answer a group of modules; a folder containing modules and even other packages; a package is just a special kind of module that can contain other modules. Example: the tkinter package contains several modules that all contribute to the development of graphical user interfaces (guis). Ex: Python Standard library Import - correct answer you must import a module, or particular elements of a module, in order to use them except for built-in Script - correct answer main driver of the program; a program is not only composed of the script code that drives it, but is also made up of the functions that are used from imported modules Module vs. Script - correct answer both contain Python code: file names that have a .py extension. Script- can be thought of as the main driver of a program Module- is thought of as a library, containing functions and other elements that are imported and used in a script. A module can import other modules as needed. Namespace - correct answer items in the module allow two items to have the same name as long as they are in different modules. Functions of the math module do not have to be imported - correct answer False The only functions that don't need to be imported is the built-ins An import statement finds a module and binds it to a name you can use in a program - correct answer True Refer to the function in the module by using a dot operator Ex: import package.module Two functions can have the same name if they are in different modules - correct answer True Modules provide a unique namespace in which to define functions and classes An entire module can be imported using one import statement - correct answer True
A)conversions.KILOMETERS_PER_MILE B)tkinter.NE C)math.pi D)math.e - correct answer a)conversions.KILOMETERS_PER_MILE Conditional expression - correct answer is an expression that produces one of two possible values depending on a boolean condition. Syntax: Expr1 if boolean-expr else expr Conditional expression vs. If-else statement - correct answer An expression produces a value that, to be useful, you must do something with (print it, assign it to a variable, etc.). A statement stands on its own. The behavior of an if statement depends on the statements that are controlled by the condition. If the value of weight is 7, what does the following statement print? Print('The weight is ' + ('even' if weight % 2 == 0 else 'odd')) A)The weight is even B)The weight is odd C)The weight is void D)The weight is NAN - correct answer b)The weight is odd Which assignment statement is functionally equivalent to the following if statement? If num1 > total: Num1 = total Else: Num1 = 0 A)num1 = total if num1 > total else 0 B)num1 = 0 if total > 0 else total C)num1 = 0 if num1 > total else total D)num1 = total if num1 > 0 else 0 - correct answer a)num1 = total if num1 > total else 0 If the value of num is 7 and the value of max is 10, what is the value of num after the following statement is executed? Num = max if max < num else max - num
D)10 - correct answer a) What does the following statement print? Print('Invalid' if count <= 0 else count) A)Zero, or 'Invalid' if count is negative. B)Zero, or 'Invalid' if count is positive. C)The value of count, or 'Invalid' if count is negative. D)The value of count, or 'Invalid' if count is positive. - correct answer c)The value of count, or 'Invalid' if count is negative. Membership operators - correct answer The in and not in operators produce a boolean (True or False) result; Be used to check the type of a variable (or, to be more precise, the type of the object to which the variable refers) Ex: 'isinstance' (built-in) returns True if the first argument is of the type specified by the second argument If isinstance(x, str): Print('x is in string') Identity operators - correct answer The is and is not operators are used to check if two variables refer to the same object Checking identity is different than checking if two variables hold the same value. If x == y is true, then they refer to objects that contain the same value- doesn't mean they necessarily refer to the same object; if x is y is true, then it will also be true that x == y. The in and is operators both produce boolean results - correct answer True The in operator can be used to check the contents of a character string or a list - correct answer True If a == b is true, then a is b will also be true? - correct answer False They could be referring to diff. Objects w/ the same value If a is b is true, then a == b will also be true? - correct answer True
Which of the following operations cannot be performed on tuples? A)indexing B)slicing C)element removal D)concatenation - correct answer True Which of the following methods can be called on a tuple? A)extend B)index C)append D)reverse - correct answer b)index Which of the following is NOT a good reason to use a tuple rather than a list? A)Accessing tuple elements is faster than accessing list elements. B)It establishes that the data should not be changed. C)Tuples can hold more elements than lists. D)Some tuples can be used as keys in a dictionary. - correct answer c)Tuples can hold more elements than lists Not true- there is no limit to the # of elements in a tuple or a list Target element - correct answer The goal of a search among a possibly large list of elements, might also not be in the list Linear search - correct answer searches a list by starting at the beginning and looking at each element one at a time until the target is found or the end of the list is reached Binary search - correct answer requires the elements in the list to be sorted; more efficient- we can quickly reduce the group of viable candidates in the list where the target might still be found Begins by examining the value in the middle of the list, then eliminates half of the data from consideration after only one comparison. Then jumps again to the middle of the remaining viable candidates and examine that element Implementing binary search - correct answer It can accept arguments as a list of values to search and a target value to find; The efficiency of a binary search depends on being able to "jump" to the middle element. List indexing lets us do just that, whereas a linked list data structure does not.
However, a binary search can be performed on linked data if it is organized into a binary search tree. There is no explicit binary search function in the library, the bisect module contains functions that can be helpful; bisect module is used to find the proper place in a list where a value can be inserted in order to keep the list sorted. A binary search works best on an unsorted list of values. - correct answer False A binary search only works if the number of elements to search is odd. - correct answer False A binary search can be performed on any number of elements When performing a binary search, how many comparisons are required to find a value in a list of N values, in the worst case? A) B)log2n + 1 C)N / 2 D)N2 - correct answer b)log2n + 1 How many comparisons are necessary to find the value 86 in the following list using a binary search? 13 25 28 30 39 43 44 58 66 70 78 81 86 88 95 A) B) C) D)5 - correct answer c) How many comparisons are necessary to find the value 43 in the following list using a binary search? 13 25 28 30 39 43 44 58 66 70 78 81 86 88 95 A) B) C) D)6 - correct answer b) Elements examined in order: 58, 30, 43 One-dimensional list - correct answer A list with one index
A example of a ragged list is a two-dimensional list where each row has a different number of columns. - correct answer True Ragged lists have at least one dimension of non-uniform length The maximum number of dimensions a Python list can have is 3. - correct answer False The concept of multidimensional list can be extended to any # of dimensions Dictionary - correct answer an object that lets you use a key to look up a value; you provide the word (the key), and the dictionary provides the definition (the value); unordered collection of keys and corresponding values; stores key/value pairs such that a value can be found efficiently given its key The keys must be unique, and can be immutable data type (ex: int/ str). The values don't have to be unique and can be of any type. Multiple keys could map to the same value. The type of a dictionary is dict, which is a Python built-in data type. Syntax: dictionaries - correct answer create: curly braces ({}) Separate an initial list of key/value pairs: (:) Retreive: square brackets ([key of the value]) Change: specify the new value for an existing key using an assignment statement, as the old key is replaced by the new value New entry: use a new key Remove entries: Particular entry- pop method (.pop( ) )/ keyerror (N/A) Entry/ entire dict.- del... Have to specify key or else removes the whole dict. All entries except the dict.- clear method (.clear( ) ) Dict - correct answer built-in function dict- which is the object constructor for the dictionary type. The dict function returns a new dictionary based on the arguments provided. Check key's existence - correct answer avoid generating a keyerror, you can verify that a particular key is present in the dictionary using the in operator before trying to access its value; also use the not in operator on a dictionary, which returns True if the key is not in the dictionary.
Iterating through dictionary - correct answer use for loop to iterate through a dictionary and access all of its members. If you iterate over the dictionary itself, the keys are obtained one at a time Dictionary methods - correct answer items(): returns a list containing a tuple for each key/value pair Get(key): returns the value of the specified key Keys(): returns a list containing the dictionary's keys Values(): returns a list of all the values in the dictionary. All values stored in a Python dictionary must be unique. - correct answer False A dictionary key must be a character string - correct answer False A dictionary is designed for efficient value retrieval - correct answer True Using square brackets to retrieve a value will result in a keyerror if the key is not in the dictionary - correct answer True A for loop can be used to traverse a dictionary's keys, but not its values - correct answer False The items method returns a list of key/value tuples for all entries in a dictionary - correct answer True Why use a dictionary? - correct answer 1)Look up values 2)Count values of various types 3)Store a record of related values Which of the following is NOT an appropriate use case for a Python dictionary? A)Representing an ordered sequence of values. B)Counting the frequency of values. C)Looking up a value. D)Representing a record of related values. - correct answer a)Representing an ordered sequence of values. A dictionary is bidirectional — values can be used to look up keys and vice versa. - correct answer False Which of the following statements is true when using a dictionary for counting?
Remove (keyerror w/o an item): .remove() Discard(no effect w/o an item): .discard() *sets do not allow duplicate elements. Any attempt to add an element that already exists in the set will be ignored Frozenset - correct answer immutable version of a set. Once a frozen set is created, its elements cannot be changed. Any attempt to change the contents of a frozen set will result in an error. Set operators - correct answer the equality and relational operators can be used on sets, returning boolean results: S1 == s S1 != s S1 <= s S1 >= s One set (s1) is a subset of another (s2) if all elements in s1 are contained in s2. If s contains additional elements (not in s1), then s1 is a proper subset of s2. Similar definitions apply for the terms superset and proper superset. Other operators can be used to perform the classic set operations union, intersection, and difference, which produce new sets: S1 | s2 (union) S1 & s2 (intersection) S1 - s2 (difference) S1 ^ s2 (symmetric difference: defined as the set of elements that are in one set or the other, but not both) When printed, a set containing strings will list the elements in alphabetical order. - correct answer False Checking to see if an element is in a set is an efficient operation in Python. - correct answer True What set is stored in set3 by the following code? Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7, 8, 9} Set3 = set1 - set A){6, 7, 8, 9} B){1, 2} C){1, 2, 3, 4, 5}
D){1, 2, 6, 7, 8, 9} - correct answer b){1, 2} What set is stored in set3 by the following code? Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7, 8, 9} Set3 = set1 | set A){3, 4, 5} B){1, 2, 3, 4, 5, 6, 7, 8, 9} C){6, 7, 8, 9} D){1, 2} - correct answer b){1, 2, 3, 4, 5, 6, 7, 8, 9} What set is stored in set3 by the following code? Set1 = {1, 2, 3, 4, 5} Set2 = {3, 4, 5, 6, 7, 8, 9} Set3 = set1 & set A){6, 7, 8, 9} B){1, 2} C){1, 2, 3, 4, 5, 6, 7, 8, 9} D){3, 4, 5} - correct answer d){3, 4, 5} Comprehension - correct answer a compact programming expression used to create a new collection based on the elements of a sequence; uses a for loop to iterate over a sequence of values and determine value added in the new collection A syntactic convenience- same results can always be produced using more verbose, less pythonic code (like using separate for loops and append method), etc. Ex: a list comprehension is used to create a new list based on a sequence such as a range, string, or another list Comprehension filters - correct answer optional if clause can be added to a comprehension to filter the elements based on a boolean condition; For each element, a value is only added to the resulting collection if the condition is True List syntax: ['expression' for 'item' in 'sequence' if 'condition'] Nested, set, dictionaries, and generator comprehension - correct answer nested comprehension creates a lists of lists (ex: two dimensional): Ex: matrix = [[x + y for x in range(5)] for y in range(5)] print(matrix) [[0, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8]]
A)['m', 's', 's', 's', 's', 'p', 'p'] B){'m', 'i', 's', 'p'} C){'m', 's', 'p'} D)['M', 'i', 's', 's', 'i', 's', 's', 'i', 'p', 'p', 'i'] - correct answer c){'m', 's', 'p'} It's a set containing unique lowercase letters besides the letter 'i' A generator expression has the same basic syntax as a list comprehension. - correct answer True Object - correct answer a program component that represents something, physical or otherwise, and performs related tasks. All Python values are objects, including integers, strings, lists, etc. The operations you can perform on those objects depend on the type, or class, of the object. An object is an instance of a class Class - correct answer A class describes an object. It is the type of the object. The term class comes from the word classification. A class describes a group of similar objects. Instance variables (fields) - correct answer this means that for each object or instance of a class, the instance variables are different. Unlike class variables, instance variables are defined within methods. The behaviors are defined by functions. Attributes - correct answer describes the object; characteristics ; example, you may want to keep track of a car's make, model, color, current speed, and current direction. Behaviors - correct answer An object's behavior is the list of services that object will perform. These behaviors are represented by methods that are invoked through the object. Ex: A car's behaviors might include accelerating, stopping, changing gears, and turning. Behaviors often affect attributes. A car that accelerates will affect its current speed, for instance. Identity - correct answer distinguish one object from another, and to access it when needed. In a program, the variable that refers to an object serves as the object's identity. State - correct answer represented by the values of its instance data. Example: the state of a particular Car object might be that it's a green Honda CRV traveling northeast at 55 miles per hour. Another car's state might be that it's a black Ford Focus traveling south at 40 miles per hour.
Analogies of class to object relationship - correct answer - A class is like a cookie cutter and the objects are the cookies.
D)A random number from 1 to 6 will be printed. - correct answer b)Die value: 1 What is the output of the following code? Die = Die(20) Die.roll() Die.roll() Print(die.value) A) B) C) D)A random number between 0 and 19. E) A random number between 1 and 20. - correct answer e) A random number between 1 and 20. Each call to the roll method assigns a random number to the die in the range 1 to the number of sides (20 in this case). *20 is inclusive due to randint; randrange is not inclusive of last parameter and it allows the step argument randrange([start], stop[, step]) Inheritance - correct answer defines an is-a relationship. The subclass is a more specific version of the superclass; the object-oriented programming technique that allows one class to be derived from another. The new class is created by extending the definition of an existing class. This is a form of software reuse. Whenever you're considering deriving one class from another, make sure the is-a relationship applies. Ex: A Dog is a Mammal. Superclass - correct answer A superclass is sometimes called a parent class or a base class; the existing class Subclass - correct answer A subclass might be called a child class or a derived class; the new class; The subclass inherits the elements of the superclass, and can then go on to add data and methods that make it distinct from the superclass. The subclass can also redefine (override) inherited methods in favor of its own. Multiple Inheritance - correct answer Python supports multiple inheritance, which means a class can be derived from more than one superclass. Multiple parent classes are separated by commas in the class header of the derived class:
Class Copier(Scanner, Printer): Pass What happens when both parents define a method or variable with the same name? The answer is found in the way references get resolved. If a variable named machine refers to a Copier object, then a call to machine.reset() must be resolved to a particular method. First, the Copier class is checked. If it has its own reset method (maybe overriding a parent's version), then that is the method that gets invoked. Inheritance, really, is about how methods are found in a class hierarchy, rather than how they are "passed down" to child classes. Ex: a scanner and a printer are two useful devices that perform particular jobs. If we were representing these devices in software, we might define separate classes with the following inheritance relationship: The Copier class inherits the methods and data from both classes, then adds any that are unique to copiers. Which two terms mean the same thing? A)superclass and parent class B)child class and superclass C)base class and child class D)parent class and subclass - correct answer a)superclass and parent class Deriving one class from another establishes what kind of relationship between the two classes? A)aggregation B)uses C)has-a D)is-a - correct answer d)is-a The subclass is-a more specific version of its superclass. A subclass is a more specific version of its superclass. - correct answer True Which Python function can be used to call a method of a superclass? A)super B)parent C)self D)my - correct answer a)super