Functions Flashcards
What is the super()
function in Python?
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.
When would you use super()
in Python?
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.
What does super().\_\_init\_\_()
do in Python?
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.
What are access modifiers in object-oriented programming?
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.
What are the three main access modifiers in object-oriented programming?
The three main access modifiers in object-oriented programming are: public
, private
, and protected
.
What does the public
access modifier do in object-oriented programming?
The public
access modifier in object-oriented programming makes a class member visible to all other classes, both within and outside the same package.
What does the private
access modifier do in object-oriented programming?
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.
What does the protected
access modifier do in object-oriented programming?
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.
What is the default access modifier in Java?
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.
What is a function in programming?
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.
What is a method in object-oriented programming?
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.
What is a constructor in object-oriented programming?
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.
What is the difference between a function and a method in programming?
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.
What is the difference between a constructor and a regular method in object-oriented programming?
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.
What is object-oriented programming (OOP)?
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.
What is a class in object-oriented programming?
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.
What is an object in object-oriented programming?
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).
What is inheritance in object-oriented programming?
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.
What is polymorphism in object-oriented programming?
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.
What is a for loop?
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.
What is a while loop?
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.
What is a do-while loop?
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.
What is a nested loop?
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.
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?
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.
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?
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.
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?
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.
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?
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.
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?
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.
What’s the syntax for declaring a function in Python?
To declare a function in Python, you use the def
keyword followed by the function name and its parameters in parentheses.
What is a tuple in Python?
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.