Object Oriented Interview Questions: Object-Oriented Programming (OOP) is a fundamental concept in Java programming. It creates reusable code by organizing data and functions into classes and objects. As such, OOPs Interview Questions in Java are essential to Java developer interviews. These interview questions assess a candidate’s understanding of the principles of OOP and their practical application in Java, including inheritance, encapsulation, abstraction, and polymorphism.
OOPs Interview Questions In Java are designed to evaluate a candidate’s knowledge and proficiency in using OOP concepts in real-world scenarios. In this context, this article provides some commonly asked OOPs Interview Questions in Java that can help Java developers prepare for interviews and thoroughly understand OOPs concepts in Java programming.
Object Oriented Programming Interview Questions
- What are the main principles of Object Oriented Programming?
- What is the difference between Object Oriented Programming language and Object-Based Programming language?
- In Java, what is the default value of an object reference defined as an instance variable in an Object?
- Why do we need constructor in Java?
- Why do we need default constructor in Java classes?
- What is the value returned by Constructor in Java?
- Can we inherit a Constructor?
- Why constructors cannot be final, static, or abstract in Java?
OOPs Interview Questions For Fresher
What is Object-Oriented Programming?
Ans: OOP is a method of programming in which the programs are organized as cooperative collections of objects. Each object is an instance of a class, and each class belongs to a hierarchy. Object-oriented programming organizes a program around its data, i. e. the objects and a set of well-defined interfaces to that data. An object-oriented program can be characterized as data controlling access to the code.
What are the Objects?
Ans: Everything in Java is an object. An object is a collection of data and actions that make up a programming entity. A ‘car’ object, for example, might have some data (i.e. ‘speed,’ ‘direction,’ ‘lights on,’ ‘current fuel,’ etc.) and some actions it can take (‘turn right,’ ‘turn lights on,’ ‘accelerate,’ etc.)
What are the basic principles of OOPS?
Ans: The basic principles of Object-Oriented Programming Structure (OOPS) are:
What is Encapsulation?
Ans: Encapsulation is the technique to wrap-up data and its associated methods into a single unit.
What is Inheritance?
Ans: Inheritance is a technique to make one object acquiring the properties of another object. This supports the hierarchical classification. An object can inherit its general attributes from its parent. A new sub-class inherits all of the attributes of all of its ancestors.
- A class that is inherited is called the superclass.
- The class that inherits properties from its superclass is called a subclass.
- Inheritance is done by using the keyword extends.
- The two most common reasons to use inheritance are: To promote code reuse and to use polymorphism.
What is Polymorphism?
Ans: Polymorphism is a feature that allows a single interface to be used for the general class of actions. The specific action is determined by the exact nature of the situation. In general, polymorphism means one interface with multiple methods.
Polymorphism allows us to design a generic interface for a group of related activities. This helps in reducing complexity by allowing the same interface to be used to specify a general class of action. It is the compiler’s job to select the specific action (that is the method) as it applies to each situation.
What is Abstraction?
Ans: Abstraction is an essential element of Object-Oriented Programming which manages the complexity.
Definition: It Exposing the necessary/essential details and common characteristics, postponing other details.
How does Java implement polymorphism?
Ans: Polymorphism manifests itself in Java in the form of multiple methods having the same name:
- In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods).
- In other cases, multiple methods have the same name, same return type, and the same formal argument list (overridden methods).
What are the different forms of Polymorphism?
Ans: There are two forms of polymorphism: Compile-time polymorphism and Runtime polymorphism. Compile-time polymorphism is also called method overloading. Run-time polymorphism is done by using inheritance and interface.
Note: From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in Java:
- Method overloading,
- Method overriding through inheritance and
- Method overriding through the Java interface.
What is the difference between Abstraction and Encapsulation?
Ans: The following are the differences between Abstraction and Encapsulation:
- Abstraction focuses on the outside view of an object (i.e., the interface), whereas encapsulation (information hiding) prevents the client from seeing the view where the behavior of the abstraction is implemented.
- Abstraction solves the problem on the design side while Encapsulation is the implementation.
- Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer’s needs.
What is the Instance?
Ans: An instance is also called an object. It has state, behavior, and identity. The structure and behavior of similar classes are defined in their common class.
What is a Base Class?
Ans: The base class is the most generalized class in a class structure. Most applications have such root classes. In Java, the object is the base class for all the classes.
What is Subclass?
Ans: The Subclass is a class that inherits from one or more classes.
What is a Superclass?
Ans: The Superclass is a class from which another class inherits its properties.
What is Constructor?
Ans: The constructor is an operation that creates an object and/or initializes its state. Constructor declarations look like method declarations, except that they use the name of the class and have no return type.
Does Java support Destructor?
Ans: In Java, there is no concept of destructors. It is taken care of by the JVM using Garbage Collection.
What is meant by Binding?
Ans: Binding denotes the association of a name with a class.
What is Static Binding?
Ans: Static Binding is a binding in which the class association is made during compile time. This is also called as Early Binding.
What is Dynamic Binding?
Ans: Dynamic Binding is a binding in which the class association is not made until the object is created at execution time. It is also called as Late Binding.
Ans: Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.
What is Collaboration?
Ans: Collaboration is a process whereby several objects cooperate in providing some higher level of behavior.
In Java, How can objects are completely encapsulated?
Ans: In Java, to completely encapsulate an object completely, all the instance variables of it should be declared as private, and the public getter and setter methods should be provided for accessing the instance variables.
What is the difference between Aggregation and Composition?
Ans: The following are the differences between Aggregation and Composition:
- Aggregation is an association in which one class belongs to a collection. This is a part of a whole relationship where a part can exist without a whole. For example, a line item is a whole, and the product is a part. If a line item is deleted, then the corresponding product need not be deleted. So aggregation has a weaker relationship.
- The composition is an association in which one class belongs to a collection.
This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted, then all parts are deleted. For example, an order is a whole, and line items are parts. If an order is deleted, then all its corresponding line items should also be deleted. So composition has a stronger relationship.
What is a Java Program?
Ans: A Java program is mostly a collection of objects talking to other objects by invoking each other’s methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of objects of many different types.
- Class: A template that describes the kinds of state and behavior that the objects of its type support.
- Object: At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its state, and access to all of the behaviors defined by its class.
- State (instance variables): Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to the object’s instance variables make up the object’s statement.
- Behavior (methods): When a programmer creates a class, she creates methods for that class. Methods are where the class’ logic is stored. Methods are where the real work gets done. They are where algorithms
get executed, and data gets manipulated.
What is the most important feature of Java?
Ans: Java is a platform independent language.
What do you mean by platform independence?
Ans: Platform independence means that we can write and compile the Java code in one platform (e.g., Windows) and can execute the class in any other supported platform (e.g., Linux, Solaris, etc.)
What is a JVM?
Ans: JVM is a Java Virtual Machine, which is a run time environment for the compiled Java class files.
Is JVM’s platform-independent?
Ans: JVM’s are not platform-independent. JVM’s are platform specific run time implementation provided by the vendor.
What is the difference between a JDK and a JVM?
Ans: JDK is the Java Development Kit, which is for development purposes, and it includes the execution environment also. But JVM is purely a run time environment, and hence, you will not be able to compile your source files using a JVM.
What is a pointer and does Java support pointers?
Ans: A pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues; hence, Java doesn’t support the usage of pointers.
Is Java a pure object-oriented language?
Ans: Java uses primitive data types and hence is not a pure object-oriented language.
Are arrays primitive data types?
Ans: In Java, Arrays are objects.
What is the difference between Path and Classpath?
Ans: Path and Classpath are operating system level environment variables. The path is used to define where the system can find the executable (. exe) files, and classpath is used to specify the location .class files.
Should a main() method be compulsorily declared in all Java classes?
Ans: No, not required. The main() method should be defined only if the source class is a Java application.
What is the return type of the main() method?
Ans: The main() method doesn’t return anything hence declared void.
Why is the main() method declared static?
Ans: The main() method is called by the JVM even before the instantiation of the class; hence, it is declared as static.
What is the argument of the main() method?
Ans: The main() method accepts an array of String objects as argument.
Can a main() method be overloaded?
Ans: Yes. You can have any number of main() methods with different method signature and implementation in the class.
Can a main() method be declared final?
Ans: Yes. Any inheriting class will not be able to have its own default main() method.
Does the order of public and static declaration matter in the main() method?
Ans: No. It doesn’t matter, but void should always come before main().
What is a native method?
Ans: A native method is a method that is implemented in a language other than Java.
What are the order of precedence and associativity, and how are they used?
Ans: Order of precedence determines the order in which operators are evaluated in expressions. Associativity determines whether an expression is evaluated left-to-right or right-to-left.
Why isn’t there operator overloading?
Ans: Because C++ has proven by example, that operator overloading makes code almost impossible to maintain.
Is null a keyword?
Ans: The null value is not a keyword.
Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
Ans: The digits 0 through 9 may not be used as the first character of an identifier, but they may be used after the first character of an identifier.
How does the ternary operator be written; x: y? Z or x? y: z?
Ans: The ternary is written x? y: z.
How is rounding performed under the integer division?
Ans: Rounding is performed by truncating the fractional part of the result. This is also known as rounding toward zero.
What restrictions are placed on the values of each case of a switch statement?
Ans: During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.
What is the difference between a while statement and a do-while statement?
Ans: A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do-while statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do-while statement will always execute the body of a loop at least once.
What is the difference between the prefix and postfix forms of the ++ operator?
Ans: The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value.
What is the difference between an ‘if’ statement and a switch statement?
Ans: The ‘if’ statement uses a boolean expression to decide which alternative should be executed first. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.
What is the difference between procedural and object-oriented programs?
Ans: The following are the difference between procedural and object-oriented programs:
- In procedural programming, the logic follows certain procedures and the instructions are executed one after another, whereas, In OOP program, a unit of program is an object, which is nothing but a combination of data and
- In procedural programming, data is exposed to the whole program, whereas in the OOPs program, it is accessible within the object and which in turn assures the security of the code.