Top 5 Object Oriented Design Interview Questions for Programmers, Software Engineers

Testing the design skill of a candidate is quite common on programming job interviews. Questions like, how do you design a vending machine or a traffic controller system is very popular on object oriented programming language interviews e.g. Java and C++. Designing a software of System requires good experience and knowledge about tools of object-oriented design and analysis. You should not only be good at OOP fundamentals like Abstraction, Inheritance, Polymorphism, Encapsulation, Coupling, Cohesion, Association, Aggregation, or Composition but also on several important object-oriented design principles like SOLID principles. A good knowledge of when to use a class over an interface is also important for designing a maintainable software or Java application. Now, questions are how do you develop such skills? You need to practice and practice hard. This is why I am sharing some frequently asked object oriented design questions from Java programming interviews here.

How to sort array in descending order - Java Example

It's easy to sort an object array in decreasing or reverse order, just provide the Comparator with opposite order. You can even use Collections.reverseOrder() if you want to sort array in the decreasing order, which returns a reverse Comparator to sort objects in the order opposite of their natural ordering defined by the compareTo() method. Unfortunately, for a primitive array, there is no direct way to sort in descending order. The Arrays.sort() method which is used to sort a primitive array in Java doesn't accept a boolean to sort the primitive array in reverse order. You might have seen the error "no suitable method found for sort(int[],comparator<object>)" which occurs when programmers try to call the Arrays.sort() method by passing reverse Comparator defined by Collection.reverseOrder(). That will work fine with Integer array but will not work with an int array. The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place as shown here. This is also true for two-dimensional primitive arrays.

Common reasons of java.lang.ArrayIndexOutOfBoundsException in Java

The ArrayIndexOutOfBoundsException, also known as java.lang.ArrayIndexOutOfBoundsExcepiton is one of the most common errors in Java program. It occurs when Java program tries to access an invalid index e.g. an index which is not positive or greater than the length of an array. For example, if you have an array of String e.g. String[] name = {"abc"} then trying to access name[1] will give java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 error because index 1 is invalid here. Why? because index in Java array starts with zero rather than 1, hence in an array of just one element the only valid index is index zero.  This was one of the most common scenarios which cause several million of ArrayIndexOutOfBoundsException daily. Sometimes it just a plain programming error but sometimes if an array is populated outside and if there is an error on feed than also you get java.lang.ArrayIndexOutOfBoundsException in Java.