Programming is usually divided into two types, Object Oriented Programming (OOP) and Functional Programming. In OOP, objects are created to represent real life things and they interact with each other through methods and procedures. On the contrary, functional programming uses functions instead of objects to perform tasks. These functions are pure and cannot change their input values. Let us answer some of the programming questions.
Explain why a person would choose to use OOP over FP?
An object-oriented program can be thought of as an entity that has its own data and behavior. It acts like a thing in the world and interacts with other objects by sending them messages. The main advantage of this type of programming is that it helps us organize our code in such a way that it becomes easier to understand and maintain. Also, we can create reusable components which we can call from different parts of the programs. However, there are certain disadvantages too. For example, if your program’s logic or database changes then you will have to go back and fix all those places where the function was called. This makes debugging a lot harder than in a purely procedural language.
What are some benefits of using FP vs OOP?
Functional languages are more suited for writing mathematical programs since the lack of side effects means the output will always depend on the inputs. Therefore, you do not need to worry about accidentally modifying state elsewhere in the program. You also don’t need to think about how state should be shared between different parts of the program. There is no explicit “this” concept either, so when working with collections you only care about their contents rather than worrying about what kind of container holds them. Finally, functions can easily be composed into new ones, which leads to extremely powerful abstractions.
Recursion refers to a function returning itself. A typical example of this is factorial (n). We say that recursive functions run until a base case is reached. If n happens to be zero, then factorial returns 1*2*3… *n! Otherwise, it calls itself again: factorial (factorial (n – 1)). As long as the function keeps calling itself, it keeps going up the chain, eventually reaching the base case. When the recursion stops, we return the last value it calculated.
Describe what static typing is.
Static typing is the idea that every variable in a computer program must belong to exactly one type. While this may seem limiting, it has several advantages. First, it forces programmers to make sure that variables are declared correctly before they start manipulating them. Second, it prevents many common bugs. Third, it ensures that your code won’t compile unless you follow the rules. Static typing can be used in two ways: structural typing and nominal typing. Structural typing checks that all the variables in a program have the same structure. For example, both Int32s and Double are structurally typed because they have the same set of properties. Nominal typing involves declaring each variable explicitly so that it belongs to a specific type. For example, x 5; y 7; z 9; could be written as int x, double y, float z.
Explain why dynamic typing is beneficial in real life situations.
Dynamic typing allows you to use any variable without first knowing what type it is. So, in real-life situations, we use dynamic typing because we want to keep things flexible.
Why does C++ support inheritance?
The key point of inheritance is that it lets you share types across many classes. In C++, you define a class by specifying the types of all the members of the class (including constructors), and giving the name of the class along with its parent class. If the derived class inherits from another class, the compiler automatically generates virtual methods for any member variables defined in the superclass. Inheritance lets you reuse code by storing it in a single place with just minor modifications required to adapt it to the new situation. However, there are some downsides of inheritance. Inherited data cannot be modified outside its own scope.
Even if an object isn’t meant to change, it’s still possible that other parts of the system might try to modify its values. Also, multiple inheritance can lead to ambiguities when deciding whether to call certain method or constructor implementation. These problems arise when trying to write generic algorithms.
Inheritance makes sense in general, but not always. Programming questions are so interesting. It really depends on what you’re trying to do and how much time you have to work with. Hope your programming questions were answered in this article.