If you plan to work in software development and engineering, get prepared for some tough questions related to the field. Nothing is more nerve-wracking than a tech interview. You’ll likely sit in front of multiple people who will judge your answers as well as your personality. Most people recommend studying before your interview. Although you can’t prepare for all questions an interviewer can ask, you can prepare yourself for some common questions. Here are some OOP interview questions you’ll likely encounter in your next interview.
1) What is a class?
A class is the basic part of OOP objects. The class encapsulates a logical structure. The class contains the properties and methods that make your class function. One advantage of OOP is the ability to inherit one class from another.
2) What are properties and methods of a class?
Properties define the “nouns” of your classes. They let the programmer determine a look and feel of the object. For instance, if you have a class that defines a house, the class properties would describe the house’s color, size, and address. The class methods are the “verbs.” Methods define the actions carried out by your classes.
3) What is private and public when defining methods or properties in classes?
The two major types of inheritance accessibility definitions are public and private. Private definitions restrict the method or property to the internal class. This means that external classes are not able to “see” the private definitions. You use these private methods and properties to manipulate data within the class. Public makes the methods and properties available to other classes. For instance, if you want to create a car class, you want other classes to be able to move the car. The car’s “move” method would be public, so other classes can move the car. However, you would not want other classes to be able to change aspects about the car’s engine. You would then keep engine properties private.
4) What is inheritance?
Inheritance is the concept that allows one class to take on the methods and properties of another class. In the example of the car, you probably have a car frame that defines the basic structure for the car. The body of the car would inherit the main frame of the car, because it uses the basic car frame as a model. The same is true for your classes. You build a hierarchy for your classes that inherit each other.
5) What is a class constructor?
A constructor is used when you instantiate a class. The class constructor must have the same name as the class. You can have several overloaded constructors or a constructor with no parameters. You use constructors to initialize certain properties in your class.
6) What are overloaded methods?
Overloaded methods are the same method name that just contains a different set of parameters. It means you can have the same name for your methods just differentiated by parameters. For instance, you have a car “move” method. However, you might want to move the car right and forward or left and forward. You could use the same move method just with different parameters, and these are called “overloaded” methods.
7) What is an abstract class?
An abstract class cannot be instantiated, but it can be inherited. This means that your inherited class can use the abstract class methods and properties, but the abstract class cannot be called directly. Abstract classes are good for defining global definitions for your inherited classes, so you only need to define these definitions once.
8) What is instantiation?
Instantiation is how you create a class object. The class object is instantiated using the constructor whether that constructor has no parameters or several parameters. You need to instantiate a class before you can use it. After you instantiate your class, you can use its properties and methods. After you are finished with the class and it is out of scope, you use the class destructor to destroy it and remove it from memory.
9) What are parameters called by value versus reference?
When you pass parameters to your methods, you send the methods parameters by value by default. This means that the value of the past variable only changes within the function. When the function is finished executing, the value does not stay constant. However, when you pass a parameter by reference, you actually pass a pointer to the function. A pointer is a reference to a value in memory. When you manipulate values in memory, they are global, so not only change the value within the method, you also change the physical value in memory. When the method is finished executing, the value is also changed globally, so the value is changed permanently outside of the method.
10) What does it mean to override a method?
When you inherit a class, you can override the parent class’ methods. When you override a method, you inherit its function, but the class inheriting the parent’s class method overrides parameters and functionality, so you can redefine how the method acts.
11) What is exception handling?
Exception handling is a type of error correction that traps “mistakes” in your code. You want users to come across issues in a graceful exception. This graceful exception traps errors, so the program doesn’t crash. The error correction is useful when a connection to a database occurs or you accidentally configure two values incorrectly. The error handling exception shows an error to the user and allows the user to continue using the program without it shutting down.
12) What is the “this” object?
The “this” reference refers to the current instance of the class. You typically use “this” to reference an internal property or method of the class. You will see “this” in various programming languages. Basically, “this” refers to the currently instantiated object.
13) What is a pointer?
Pointers are typically used in C++, but they are also relevant in other programming languages. Pointers are the actual memory location of a value. Because a pointer is an actual memory physical location, when you change a value in a pointer, you change the value in memory so it is global. When you globally change a value, it changes for all classes, methods and any code that uses that value.
14) What are static functions?
Static functions and classes require no instantiation. It means that you can use these classes, functions, and parameters at-will without creating and instantiating the object. Static functions are beneficial when you need quick execution of functions that don’t need other parts of the containing class. You cannot use non-static classes with static functions.
While this is definitely not a complete list of questions, they give you an overall view of OOP interview question possibilities. If you are rusty on OOP theory, these questions will help you when you face the interview panel. It’s typical that you’ll have multiple people interviewing you. Don’t get intimidated or nervous. As long as you know OOP and how to structure your code, you will do fine in the interview. For most people, the personality part of an interview is more difficult than the technical questions.
The best way to get to know OOP programming is creating a program and practicing. You can do this with any OOP language.