Functions Flashcards

1
Q

What is the super() function in Python?

A

The super() function in Python is used to call a method of the parent class from a subclass. It is used to access the parent class’s method, even if the subclass has overridden the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When would you use super() in Python?

A

You would use super() in Python when you need to call a method of the parent class from a subclass. This is useful when the subclass has overridden the method, but you still need to access the parent class’s method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does super().\_\_init\_\_() do in Python?

A

super().\_\_init\_\_() is used to call the \_\_init\_\_ method of the parent class from a subclass. It is used to properly initialize the parent class and ensure that all its required attributes and methods are properly set up.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are access modifiers in object-oriented programming?

A

Access modifiers in object-oriented programming are keywords that are used to control the visibility of class members (i.e., fields and methods). They determine whether a class member can be accessed from outside the class, or only within the class itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the three main access modifiers in object-oriented programming?

A

The three main access modifiers in object-oriented programming are: public, private, and protected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the public access modifier do in object-oriented programming?

A

The public access modifier in object-oriented programming makes a class member visible to all other classes, both within and outside the same package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the private access modifier do in object-oriented programming?

A

The ‘private’ access modifier makes a class member visible only within the same class. A private class member cannot be accessed from subclasses or other classes in the same package.

In other words, private members are completely hidden from any code outside of the class in which they are defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the protected access modifier do in object-oriented programming?

A

The protected access modifier restricts access to members (fields, properties, and methods) to the class itself and its subclasses, also known as its derived classes, but not to the instances of the class or other classes outside the inheritance hierarchy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the default access modifier in Java?

A

The default access modifier in Java is package-private (also called default). This means that a class member is visible within the same package, but not outside the package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a function in programming?

A

A function in programming is a block of code that performs a specific task. It can be called from other parts of a program to perform the task it was designed for, and can take inputs and return outputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a method in object-oriented programming?

A

A method in object-oriented programming is a function that is associated with a specific object or class. It can be called on an instance of an object or on the class itself, and can access the object’s data and behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a constructor in object-oriented programming?

A

A constructor in object-oriented programming is a special method that is used to initialize an object when it is created. It has the same name as the class, and is called automatically when a new object is created. Constructors can be used to set initial values for the object’s fields, and to perform any necessary setup or validation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between a function and a method in programming?

A

The main difference between a function and a method in programming is that a method is associated with an object or class, while a function is not. Methods can access the data and behavior of an object, while functions can only access the data that is passed to them as arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between a constructor and a regular method in object-oriented programming?

A

The main difference between a constructor and a regular method in object-oriented programming is that a constructor is called automatically when a new object is created, while a regular method must be called explicitly. Constructors also have the same name as the class, while regular methods can have any name. Constructors are used to initialize objects when they are created, while regular methods are used to perform tasks or calculations on objects that already exist.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is object-oriented programming (OOP)?

A

Object-oriented programming (OOP) is a programming paradigm that is based on the concept of objects, which can contain data and behavior. It allows programmers to create reusable code that can be easily maintained and modified.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a class in object-oriented programming?

A

A class in object-oriented programming is a blueprint for creating objects. It defines the properties (fields) and behavior (methods) of the objects that will be created from it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is an object in object-oriented programming?

A

An object in object-oriented programming is an instance of a class. It contains data (the values of the class’s fields) and behavior (the methods that can be called on the object).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is inheritance in object-oriented programming?

A

Inheritance in object-oriented programming is a mechanism that allows a class to be based on another class, inheriting its properties and behavior. The new class is called the subclass, and the original class is called the superclass. The subclass can add new fields and methods, or modify the behavior of the superclass’s methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is polymorphism in object-oriented programming?

A

Polymorphism in object-oriented programming is the ability of an object to take on many forms (i.e., to be treated as an object of multiple classes). It allows different objects to be treated in the same way, regardless of their specific class or type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a for loop?

A

A for loop is a type of loop in programming that is used to iterate over a sequence of values. It allows a block of code to be executed a fixed number of times, based on the length of the sequence being iterated over.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is a while loop?

A

A while loop is a type of loop in programming that is used to repeat a block of code as long as a specified condition is true. It allows a block of code to be executed an indefinite number of times, until the condition becomes false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is a do-while loop?

A

A do-while loop is a type of loop in programming that is similar to a while loop, but guarantees that the code block will be executed at least once, even if the condition is false to begin with.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is a nested loop?

A

A nested loop is a loop that is contained within another loop. It allows a block of code to be executed for each combination of values from the two loops, resulting in a set of all possible combinations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the best type of loop to use when you need to iterate over a sequence of values, such as the elements of an array or a list?

A

The best type of loop to use when you need to iterate over a sequence of values is a for loop. A for loop allows a block of code to be executed a fixed number of times, based on the length of the sequence being iterated over.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the best type of loop to use when you need to repeat a block of code as long as a specified condition is true?

A

The best type of loop to use when you need to repeat a block of code as long as a specified condition is true is a while loop. A while loop allows a block of code to be executed an indefinite number of times, until the condition becomes false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the best type of loop to use when you need to guarantee that a block of code will be executed at least once, even if the condition is false to begin with?

A

The best type of loop to use when you need to guarantee that a block of code will be executed at least once, even if the condition is false to begin with, is a do-while loop. A do-while loop is similar to a while loop, but guarantees that the code block will be executed at least once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the best type of loop to use when you need to iterate over the elements of a collection, such as an array or a list?

A

The best type of loop to use when you need to iterate over the elements of a collection is a for loop. A for loop is a type of loop that is used to iterate over the elements of a collection and allows a block of code to be executed for each element in the collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the best type of loop to use when you need to generate a set of all possible combinations of values from two loops?

A

The best type of loop to use when you need to generate a set of all possible combinations of values from two loops is a nested loop. A nested loop is a loop that is contained within another loop, and allows a block of code to be executed for each combination of values from the two loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What’s the syntax for declaring a function in Python?

A

To declare a function in Python, you use the def keyword followed by the function name and its parameters in parentheses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is a tuple in Python?

A

In Python, a tuple is an ordered, immutable collection of values. It is similar to a list, but you cannot add or remove elements from a tuple after it has been created. Tuples are defined using parentheses and commas.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is a dictionary in Python?

A

In Python, a dictionary is an unordered collection of key-value pairs. It is similar to a hash table or associative array in other languages. Dictionaries are defined using curly braces and colons.

32
Q

What is list comprehension in Python?

A

List comprehension in Python is a concise way to create lists from other iterables, such as lists or tuples. It allows you to create a new list by applying an expression to each element of the original list that meets a certain condition.

33
Q

What is a function in python?

A

In Python, a function is a block of code that performs a specific task and can be called from other parts of a program. It does not have any association with an object or class. A method, on the other hand, is a function that is associated with an object or class. It can be called on an instance of an object or on the class itself, and can access the object’s data and behavior.

34
Q

Which keyword is used to declare a function in Python.

A

The def keyword is used to declare a function in Python.

35
Q

A \_\_\_\_\_\_ is a collection of key-value pairs in Python.

A

A dictionary is a collection of key-value pairs in Python.

36
Q

\_\_\_\_\_\_ is a way to create lists in Python by applying an expression to each element of an original list that meets a certain condition.

A

List comprehension is a way to create lists in Python by applying an expression to each element of an original list that meets a certain condition.

37
Q

A \_\_\_\_\_\_ is an ordered, immutable collection of values in Python.

A

A tuple is an ordered, immutable collection of values in Python.

38
Q

A \_\_\_\_\_\_ is a function that is associated with an object or class in Python.

A

A method is a function that is associated with an object or class in Python.

39
Q

A \_\_\_\_\_\_ is a block of code that performs a specific task in Python.

A

A function is a block of code that performs a specific task in Python.

40
Q

The \_\_\_\_\_\_ function can be used to return multiple values from a function in Python.

A

The return function can be used to return multiple values from a function in Python.

41
Q

The \_\_\_\_\_\_ operator is used to access an element of a list or tuple in Python.

A

The square bracket ([]) operator is used to access an element of a list or tuple in Python.

42
Q

The \_\_\_\_\_\_ keyword is used to define a class in Python.

A

The class keyword is used to define a class in Python.

43
Q

A \_\_\_\_\_\_ is a template for creating objects in Python.

A

A class is a template for creating objects in Python.

44
Q

The \_\_\_\_\_\_ statement is used to skip the rest of a loop iteration in Python.

A

The continue statement is used to skip the rest of a loop iteration in Python.

45
Q

The \_\_\_\_\_\_ function is used to sort a list in Python.

A

The sorted() function is used to sort a list in Python.

46
Q

The \_\_\_\_\_\_ keyword is used to create a new instance of a class in Python.

A

The \_\_init\_\_() keyword is used to create a new instance of a class in Python.

47
Q

The \_\_\_\_\_\_ operator is used to concatenate two strings in Python.

A

The + operator is used to concatenate two strings in Python.

48
Q

The \_\_\_\_\_\_ function is used to remove an item from a list in Python.

A

The remove() function is used to remove an item from a list in Python.

49
Q

The \_\_\_\_\_\_ statement is used to define a block of code that will be executed if an exception is raised in Python.

A

The try...except statement is used to define a block of code that will be executed if an exception is raised in Python.

50
Q

The \_\_\_\_\_\_ function is used to convert a string to a list in Python.

A

The list() function is used to convert a string to a list in Python.

51
Q

The \_\_\_\_\_\_ operator is used to access a key-value pair in a dictionary in Python.

A

The square bracket ([]) operator is used to access a key-value pair in a dictionary in Python.

52
Q

The \_\_\_\_\_\_ function is used to convert a list to a set in Python.

A

The set() function is used to convert a list to a set in Python.

53
Q

Which of the following is a built-in data type in Python?
a) function
b) class
c) module
d) dictionary

A

d) dictionary

Functions, classes, and modules are not built-in data types in Python, but dictionaries are. Functions are used to perform specific tasks, classes are used to define objects and their behavior, and modules are used to organize and reuse code.

54
Q

What is the difference between a tuple and a list in Python?
a) A tuple is immutable and a list is mutable.
b) A tuple can only contain integers and a list can contain any data type.
c) A tuple is ordered and a list is unordered.
d) A tuple is a scalar data type and a list is a vector data type.

A

a) A tuple is immutable and a list is mutable.

In Python, a tuple is an ordered, immutable collection of values, whereas a list is an ordered, mutable collection of values. You cannot add or remove elements from a tuple after it has been created, but you can modify the elements of a list.

55
Q

What is the output of the following code?
my_list = [1, 2, 3, 4, 5]
new_list = [x * 2 for x in my_list if x % 2 == 0]
print(new_list)

A

a) [2, 4, 6, 8, 10]
b) [4, 8]
c) [1, 2, 3, 4, 5]
d) [2, 3, 4, 5, 6, 7, 8, 9, 10]

56
Q

Which of the following best describes a class member in Python?
a) A function that is defined inside a class.
b) An instance variable that is defined inside a class.
c) A class variable that is defined outside any method inside a class.
d) A special type of object that inherits from a class.

A

c) A class variable that is defined outside any method inside a class.

In Python, a class member is a variable or method that belongs to a class and not to any specific instance of the class. A class variable is a variable that is defined outside any method inside a class, and it is shared by all instances of the class. An instance variable, on the other hand, is a variable that is defined inside a method of a class, and it is unique to each instance of the class.

57
Q

What is a package in Python?
a) A collection of related modules.
b) A built-in function that performs a specific task.
c) A special type of object that inherits from a class.
d) An instance of a class that represents a file.

A

a) A collection of related modules.

In Python, a package is a collection of related modules that are organized in a directory hierarchy. Packages allow you to group related functionality together, making it easier to organize and reuse your code.

58
Q

What is a module in Python?
a) A collection of related packages.
b) A built-in function that performs a specific task.
c) A special type of object that inherits from a class.
d) A file that contains Python code.

A

d) A file that contains Python code.

In Python, a module is a file that contains Python code. Modules can define functions, classes, and variables, which can be imported and used in other modules or scripts.

59
Q

What is a file object in Python?
a) An instance of a class that represents a file.
b) A variable that stores the path to a file.
c) A module that provides functions for working with files.
d) A package that contains functions for working with files.

A

a) An instance of a class that represents a file.

In Python, a file object is an instance of a class that represents a file. File objects are used to read from and write to files, and provide methods like read(), write(), and close().

60
Q

What is the purpose of the open() function in Python?
a) To create a new instance of a class.
b) To create a new object of a specific data type.
c) To open a file and return a file object.
d) To import a module or package.

A

c) To open a file and return a file object.

In Python, the open() function is used to open a file and return a file object. The file object can then be used to read from or write to the file. The open() function takes two arguments: the file name and the mode, which specifies whether the file should be opened for reading, writing, or both.

61
Q

You are defining a class that will be used in multiple modules in your project. Which access modifier should you use for the class?
a) public
b) protected
c) private

A

a) public

If you want a class to be accessible from other modules in your project, you should use the public access modifier. Public members can be accessed from any module that imports the class. Protected and private members, on the other hand, are only accessible from within the same class or its subclasses.

62
Q

You are defining an instance variable in a class that should not be modified from outside the class. Which access modifier should you use for the variable?
a) public
b) protected
c) private

A

c) private

If you want an instance variable to be private and not modifiable from outside the class, you should use the private access modifier. Private members are denoted by a double underscore (\_\_) prefix, and can only be accessed from within the same class. Public and protected members can be accessed from outside the class and can be modified directly.

63
Q

You are defining a method in a class that should only be used by subclasses. Which access modifier should you use for the method?
a) public
b) protected
c) private

A

b) protected

If you want a method to be accessible to subclasses but not to other modules or classes, you should use the protected access modifier. Protected members are denoted by a single underscore (_) prefix, and can be accessed from within the same class or its subclasses. Public members can be accessed from any module or class, and private members can only be accessed from within the same class.

64
Q

You are defining a module that contains functions that should only be used within the module. Which access modifier should you use for the functions?
a) public
b) protected
c) private

A

c) protected

Using the protected access modifier would allow the functions to be accessible to other classes within the same module, while still preventing them from being used outside the module.

65
Q

________ members are accessible from anywhere, both within and outside of the class.

A

Public members are like items you would lend out to anyone, and in coding, public members are accessible from anywhere, both within and outside of the class.

66
Q

_________ members are accessible from within the class and any subclasses, but not from outside the class.

A

Protected members are like items you would only lend out to people you trust, and in coding, protected members are accessible from within the class and any subclasses, but not from outside the class.

67
Q

_________ members are only accessible from within the same class, and not from any subclasses or other classes outside the class.

A

Private members are like items you would never share with anyone, and in coding, private members are only accessible from within the same class, and not from any subclasses or other classes outside the class.

68
Q

What is a subclass in object-oriented programming?

A

A subclass is a class that inherits attributes and behaviors from a superclass or parent class.

69
Q

How do you create a subclass in Python?

A

To create a subclass in Python, use the following syntax:

class MySubclass(MyClass):
def __init__(self, arg1, arg2, arg3):
super().__init__(arg1, arg2)
self.arg3 = arg3

70
Q

What is a superclass in object-oriented programming?

A

A superclass is a class that is used as a blueprint for creating a subclass, and from which the subclass inherits attributes and behaviors.

71
Q

How do you access the methods of a superclass in a subclass?

A

To access the methods of a superclass in a subclass, use the super() function, which allows you to call the superclass method from within the subclass. For example:

72
Q

What is method overriding in object-oriented programming?

A

Method overriding is the process of creating a subclass method with the same name as a superclass method, but with a different implementation.

73
Q

How do you override a method in a subclass in Python?

A

To override a method in a subclass in Python, use the same method name as the superclass method, but with a different implementation.

74
Q

What is method overloading in object-oriented programming?

A

Method overloading is the process of creating multiple methods with the same name but different arguments or parameters.

75
Q

Does Python support method overloading? If not, how can you achieve similar functionality?

A

Python does not support method overloading in the traditional sense, but you can achieve similar functionality using default values for method parameters or using variable-length argument lists.