Object Oriented Interview Questions

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, OOP interview Questions 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.

OOP interview Questions In Java, they are designed to evaluate a candidate’s knowledge and proficiency in using OOP concepts in real-world scenarios. This article provides some commonly asked OOP interview Questions in Java that can help Java developers prepare for interviews and thoroughly understand OOP 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 a constructor in Java?
  • Why do we need a default constructor in Java classes?
  • What is the value returned by the 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 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:

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

What is Encapsulation?
Ans: Encapsulation is the technique of wrapping up data and its associated methods into a single unit.

What is Inheritance?
Ans: Inheritance is a technique for one object to acquire the properties of another object. This supports 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 for using 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 reduce complexity by allowing the same interface 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 Exposes 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, the 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 (e.g., the interface), whereas encapsulation (information hiding) prevents the client from seeing the view where the abstraction’s behavior is implemented.
  • Abstraction solves the problem on the design side, while Encapsulation is the implementation.
  • Encapsulation is the deliverable 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 behaviour of similar classes are defined in their common class.

What is a Base Class?
Ans: The base class is the most generalized in a class structure. Most applications have such root classes. In Java, the object is the base class for all the classes.

What is a 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 a 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 class name and have no return type.

Does Java support Destructor?
Ans: In Java, there is no concept of destructors. The JVM handles this using the Garbage Collection.

What is meant by Binding?
Ans: Binding denotes the association of a name with a class.

What is Static Binding?
Ans: A Static Binding is a binding in which the class association is made during compile time. It is also called an 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 Late Binding.

Define Modularity.
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 to provide some higher level of behavior.

In Java, How can objects be completely encapsulated?
Ans: In Java, to completely encapsulate an object, all its instance variables should be declared private, and the public getter and setter methods should be provided for accessing them.

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 behaviour 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 create an instance of that class. That object will have its state and access to all of the behaviours 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.
  • Behaviour (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 an independent language on the platform.

What do you mean by platform independence?
Ans: Platform independence means that we can write and compile the Java code on one platform (e.g., Windows) and execute the class on any other supported platform (e.g., Linux, Solaris, etc.).

What is a JVM?
Ans: A JVM is a Java Virtual Machine, which is a runtime environment for the compiled Java class files.

Is JVM’s platform independent?
Ans: JVMs are not platform-independent. They are platform-specific runtime implementations provided by the vendor.

What is the difference between a JDK and a JVM?
Ans: JDK is the Java Development Kit for development purposes and includes the execution environment. However, 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 their usage.

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 defines where the system can find the executable (. exe) files, and classpath specifies the location of .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 JVM calls the main() method even before the class is instantiated; hence, it is declared static.

What is the argument of the main() method?
Ans: The main() method accepts an array of String objects as an argument.

Can a main() method be overloaded?
Ans: Yes. You can have any number of main() methods with different method signatures and implementations in the class.

Can a main() method be declared final?
Ans: Yes. Any inheriting class cannot 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 the void should always come before the 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: The 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.

How is the ternary operator 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 be evaluated 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 loop iteration 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 of all of the expressions 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 selects 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 differences between procedural and object-oriented programs:

  • In procedural programming, the logic follows certain procedures, and the instructions are executed one after another, whereas, In an OOP program, a unit of the program is an object, which is nothing but a combination of data and
    code.
  • In procedural programming, data is exposed to the whole program, whereas in the OOPs program, it is accessible within the object, which, in turn, ensures the security of the code.

Ref: article

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment