Java, OOP questions

By | May 7, 2016

Question 01: What are the characteristics of Object-Oriented programming language?

  • Encapsulation – Encapsulation is capturing data and keeping it safely and securely from outside interfaces.
  • Inheritance- This is the process by which a class can be derived from a base class with all features of the base class and some of its own. This increases code reusability.
  • Polymorphism- This is the ability to exist in various forms.
  • Abstraction- The ability to represent data at a very conceptual level without any details.

Question 02: Can you override the static method in Java? if I create the same method in the subclass is it a compile-time error?

  • No, you can not override the static method in Java but it’s not a compile-time error to declare exactly the same method in a subclass. That is called method hiding in Java.

Question 03: What is the difference between Abstraction and Encapsulation in Java?

  • Abstraction represent taking out the behavior from How exactly it implemented, one example of abstraction in Java is an interface.
  • Encapsulation means hiding details of implementation from the outside world.

Question 04: What is method overloading?

  • Method Overloading means to have two or more methods with the same name in the same class with different arguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type.

Question 05: What is method overriding?

  • Method overriding occurs when sub class declares a method that has the same type of arguments as a method declared by one of its superclasses. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type.

Question 07: Can you use this() and super() both in the constructor?

  • No, because this() or super() must be the first argument.

Question 08: What is an immutable object in Java?

  • A Java object is considered immutable when its state cannot change after it is created. The use of immutable objects is widely accepted as a sound strategy for creating a simple, reliable code. Immutable objects are particularly useful in concurrent applications. String is immutable object type.

Question 09: How to create an immutable object in Java?

  • To create an object immutable, you need to make the class final and all its members final so that once objects get created no one can modify its state. You can achieve the same functionality by making members non-final but private and not modifying them except in the constructor.

Question 10: What is the difference between String/StringBuilder/StringBuffer?

  • StringBuffer and StringBuilder objects are mutable whereas String class objects are immutable.
  • StringBuffer class implementation is synchronized while StringBuilder class is not synchronized.

Question 11: Which two methods you need to implement for key Objects in HashMap?

  • In order to use any object as Key in HashMap, it must implement the equals() and hashcode() method in Java.

Question 12: What is the difference between HashMap/Hashtable?

  • HashMap is roughly equivalent to Hashtable, except that HashMap is unsynchronized and permits null key/value.

Question 13: What is the difference between Vector and ArrayList?

  • Both implements java.util.List interface. Vector is synchronized while ArrayList is not synchronized.

Question 14: What is the difference between Enumeration and Iterator?

  • The functionality of the Enumeration interface is duplicated by the Iterator interface. Iterator has a remove() method while Enumeration doesn’t. Enumeration acts as a Read-only interface because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like adding and removing the objects. So Enumeration is used when ever we want to make Collection objects as Read-only.

Question 15: What is java.util.concurrent BlockingQueue?

  • Blocking Queue is a type of Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

Question 16: Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list?

  • LinkedList.

Question 17: What is the difference between ArrayList and LinkedList?

  • ArrayList uses primitive object array for storing objects whereas LinkedList is made up of a chain of nodes. Each node stores an element and the pointer to the next node. A singly linked list only has pointers to next. A doubly linked list has a pointer to the next and the previous element. This makes walking the list backward easier.
  • Adding and deleting at the start and middle of the ArrayList is slow, because all the later elements have to be copied forward or backward. (Using System.arrayCopy()) Whereas Linked lists are faster for inserts and deletes anywhere in the list, since all you do is update a few next and previous pointers of a node.

Question 18: What is the use of finally block? Is finally block in Java guaranteed to be called? When finally block is not called?

  • Finally is the block of code that executes always. The code in the finally block will execute even if an exception has occurred. Finally block is NOT called in the following conditions:
    1. If the JVM exits while the try or catch code is being executed, then the finally block may not execute. This may happen due to System.exit() call.
    2. if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
    3. If an exception is thrown in finally block and not handled then the remaining code in finally block may not be executed.

Question 19: Explain different way of creating Thread?

  • A Java thread could be implemented by using Runnable interface or by extending the Thread class. The Runnable is more advantageous when you are going for multiple inheritances.

Question 20: What is the difference between Thread.start() & method?

  • If we directly call method it will be executed in the same thread, so does not solve the purpose of creating a new thread.

Question 21: What is the difference between a class method and an instance method?

Class vs. Instance

Question 22: What are the differences between Design Patterns and Design Principles?

  • Design Pattern is a general reusable solution to a commonly occurring problem within a given context.
  • Design Principles a set of guidelines that helps us to avoid having a bad design.

Question 23: Do you know any OOP Design principles?

  • List out some principles such as the Single Responsibility Principle, Open-Closed Principle, Acyclic Dependencies Principle

Question 24: What are the SOLID design principles?

  • Single responsibility, Open-closed, Liskov substitution, Interface Segregation, and Dependency inversion
Category: Q&A

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.