LinkedIn Python Skill Quiz

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

How is comment created?

# This is a comment

What is the correct syntax for calling an instance method on a class named Game? (Answer format may vary. Game and roll (or dice_roll) should each be called with no parameters.)

>>> dice = Game(self) >>> dice.roll(self)

What is an instance method?

Instance methods can modify the state of an instance or the state of its parent class.

What is the runtime of accessing a value in a dictionary by using its key?

O(1)

Assuming the node is in a singly linked list. what is the runtime complexity of searching for a specific node within a singly linked list?

The runtime is O(n) because in the worst case, the node you are searching for it the last node and every node in the linked list must be visited.

What would this expression return? college_years = ['Freshman', 'Sophomore', 'Junior', 'Senior'] return list(enumerate(college_years, 2019))

[(2019, 'Freshman'), (2020, 'Sophomore'), (2021, 'Junior'), (2022, 'Senior')]

What does calling namedtuple on a collection type return?

a tuple subclass with iterable named fields

Which collection type is used to associate values with unique keys?

dictionary

What is the algorithmic paradigm of quick sort?

divide and conquer

What is the correct syntax for instantiating a ew object of the type Game?

my_game = Game()

What is the term to describe this code? count, fruit, price = (2, 'apple', 3.5)

tuple unpacking

When does a for loop stop iterating?

when it has assessed each item in the iterable it is working on or a break keyword is encountered

What built-in list method would you use to remove items from a list?

.pop() method

What symbol(s) do you use to assess equality between two elements?

==

What is a base case in a recursive function?

A base case is the condition that allows the algorithm to stop recursing. It is usually a problem that is small enough to solve directly.

What statement about the class methods is true?

A class method can modify the state of the class, but they can't directly modify the state of an instance that inherits from that class.

Describe the functionality of a dequeue.

A dequeue adds items at either or both ends, and remove items at either or both ends.

Describe the functionality of a queue?

A queue adds items to either end and removes items from either end.

What is key difference between a set and a list?

A set is an unordered collection unique items. A list is an ordered collection of non-unique items.

What is the definition of abstraction as applied to object-oriented Python?

Abstraction means the implementation is hidden from the user, and only the relevant data or information is shown.

What is the runtime complexity of adding an item to a stack and removing an item from a stack?

Add items to a stack in O(1) time and remove items from a stack in O(1) time.

What is an abstract class?

An abstract class exists only so that other "concrete" classes can inherit from the abstract class.

Suppose a Game class inherits from two parent classes: BoardGame and LogicGame. Which statement is true about the methods of an object instantiated from the Game class?

An instance of the Game class will inherit whatever methods the BoardGame and LogicGame classes have.

What does a generator return?

An iterable object

What are attributes?

Attributes are a way to hold data or describe a state for a class or an instance of a class.

What is the difference between class attributes and instance attributes?

Class attributes are shared by all instances of the class. Instance attributes may be unique to just that instance

Which of the following is TRUE About how numeric data would be organised in a binary Search tree?

For any given Node in a binary Search Tree, the child node to the left is less than the value of the given node and the child node to its right is greater than the given node.

If you don't explicitly return a value from a function, what happens?

If the return keyword is absent, the function will return None.

Why would you use mixin?

If you have many classes that all need to have the same functionality, you'd use a mixin to define that functionality.

How does defaultdict work?

If you try to access a key in a dictionary that doesn't exist, defaultdict will create a new key for you instead of throwing a KeyError.

What does the built-in map() function do?

It applies a function to each item in an iterable and returns the value of that function.

What is the purpose of an if/else statement?

It executes one chunk of code if a condition is true, but a different chunk of code if the condition is false.

What is the purpose of the pass statement in Python?

It is a null operation used mainly as a placeholder in functions, classes, etc.

Which statement does NOT describe the object-oriented programming concept of encapsulation?

It only allows the data to be changed by methods.

What is the primary difference between lists and tuples?

Lists are mutable, meaning you can change the data that is inside them at any time. Tuples are immutable, meaning you cannot change the data that is inside them once you have created the tuple.

Which of these is NOT a characteristic of namedtuples?

No import is needed to use namedtuples because they are available in the standard library.

What is runtime complexity of the list's built-in .append() method?

O(1), also called constant time

What statement about static methods is true?

Static methods serve mostly as utility methods or helper methods, since they can't access or modify a class's state.

Which statement about static method is true?

Static methods serve mostly as utility or helper methods, since they cannot access or modify a class's state.

What does a class's init() method do?

The __init__ method is a constructor method that is called automatically whenever a new object is created from a class. It sets the initial state of a new object.

What happens when you use the built-in function all() on a list?

The all() function returns True if all items in the list evaluate to True. Otherwise, it returns False.

What is meant by the phrase "space complexity"?

The amount of space taken up in memory as a function of the input size

What does it mean for a function to have linear runtime?

The amount of time it takes the function to complete grows linearly as the input size increases.

What happens when you use the build-in function any() on a list?

The any() function returns True if any item in the list evaluates to True. Otherwise, it returns False.

What is the runtime complexity of searching for an item in a binary search tree?

The runtime for searching in a binary search tree is generally O(h), where h is the height of the tree.

What value would be returned by this check for equality? 5 != 6

True

Why would you use a virtual environment?

Virtual environments create a "bubble" around your project so that any libraries or packages you install within it don't affect your entire machine.

When would you use a for loop?

When you need to check every element in an iterable of known length.

Why is it considered good practice to open a file from within a Python script by using the with keyword?

When you open a file using the with keyword in Python, Python will make sure the file gets closed, even if an exception or error is thrown.

Why would you use a decorator?

You use the decorator to alter the functionality of a function without having to modify the functions code.

What would happen if you did not alter the state of the element that an algorithm is operating on recursively?

You would get a RuntimeError: maximum recursion depth exceeded.

What will happen if you use a while loop and forget to include logic that eventually causes the while loop to stop?

Your code will get stuck in an infinite loop.

What does this function print? def print_alpha_nums(abc_list, num_list): for char in abc_list: for num in num_list: print(char, num) return print_alpha_nums(['a', 'b', 'c'], [1, 2, 3])

a 1 a 2 a 3 b 1 b 2 b 3 c 1 c 2 c 3

What is a lambda function ?

a small, anonymous function that can take any number of arguments but has only expression to evaluate

Which statement accurately describes how items are added to and removed from a stack?

a stacks adds items to the top and removes items from the top.

What is the term used to describe items that may be passed into a function?

arguments

What is the correct syntax for defining a class called "Game", if it inherits from a parent class called "LogicGame"?

class Game(LogicGame): pass

What is the correct syntax for defining a class called Game?

class Game: pass

What is the correct syntax for defining an __init__() method that takes no parameters?

def __init__(self): pass

What is the most self-descriptive way to define a function that calculates sales tax on a purchase?

def calculate_sales_tax(subtotal): pass

What is the proper way to define a function?

def get_max_num(list_of_nums): # body of function goes here

What is the correct syntax of creating an instance method?

def get_next_card(self): # method body goes here

Correct representation of doctest for function in Python

def sum(a, b): """ >>> a = 1 >>> b = 2 >>> sum(a, b) 3 """ return a + b

What is the correct way to write a doctest?

def sum(a, b): """ >>> sum(4, 3) 7 >>> sum(-4, 5) 1 """ return a + b

What is the correct syntax for creating a variable that is bound to a dictionary?

fruit_info = {'fruit': 'apple', 'count': 2, 'price': 3.5}

Review the code below. What is the correct syntax for changing the price to 1.5? fruit_info = { 'fruit': 'apple', 'count': 2, 'price': 3.5 }

fruit_info ['price'] = 1.5

What is the correct syntax for adding a key called variety to the fruit_info dictionary that has a value of Red Delicious?

fruit_info['variety'] = 'Red Delicious'

What is the proper way to write a list comprehension that represents all the keys in this dictionary? fruits = {'Apples': 5, 'Oranges': 3, 'Bananas': 4}

fruit_names = [x for x in fruits.keys()]

What is the correct way to call a function?

get_max_num([57, 99, 31, 18])

Given the following three list, how would you create a new list that matches the desired output below fruits = ['Apples', 'Oranges', 'Bananas'] quantities = [5, 3, 4] prices = [1.50, 2.25, 0.89] #Desired output [('Apples', 5, 1.50), ('Oranges', 3, 2.25), ('Bananas', 4, 0.89)]

i = 0 output = [] for fruit in fruits: temp_qty = quantities[i] temp_price = prices[i] output.append((fruit, temp_qty, temp_price)) i += 1 return output

According to the PEP 8 coding style guidelines, how should constant values be named in Python?

in all caps with underscores separating words -- e.g. MAX_VALUE = 255

What data structure does a binary tree degenerate to if it isn't balanced properly?

linked list

What built-in Python data type is commonly used to represent a queue?

list (You can only build a stack from scratch.)

What built-in Python data type is commonly used to represent a stack?

list (You can only build a stack from scratch.)

What is the correct syntax for replacing the string apple in the list with the string orange?

my_list[1] = 'orange'

What is the correct syntax for creating a variable that is bound to a set?

myset = {0, 'apple', 3.5}

Which choice is the most syntactically correct example of the conditional branching? Q19

num_people = 5 if num_people > 10: print("There is a lot of people in the pool.") elif num_people > 4: print("There are some people in the pool.") elif num_people > 0: print("There are a few people in the pool.") else: print("There is no one in the pool.")

Which choice is the most syntactically correct example of the conditional branching?

num_people = 5 if num_people > 10: print("There is a lot of people in the pool.") elif num_people > 4: print("There are some people in the pool.") else: print("There is no one in the pool.")

What is the correct way to run all the doctests in a given file from the command line?

python3 <filename>

What is the purpose of the "self" keyword when defining or calling instance methods?

self refers to the instance whose method was called.

What is one of the most common use of Python's sys library?

to capture command-line arguments given at a file's runtime

When would you use a while loop?

when you want some code to continue running as long as some condition is true


Kaugnay na mga set ng pag-aaral

Таблица умножения на русском языке / Times Tables / English /

View Set

D216 Unit 6: Forms of Business Organization (14%)

View Set

Chapter 12 Cardiovascular system Mid term

View Set

Care of Dental Appliances and Prostheses

View Set

Biologie - Ultrastructura celulei

View Set