Feb 23, 2014

Top 20 Java technical interview questions and answers for experienced Java developers - part 2

Java Interview Questions and Answers

1-5 for experienced developers 6-10 for experienced developers 11-16 for experienced developers 17-20 for experienced developers

Q6. How would you go about designing a car parking station?

Map out the requirements:
  • The car park needs to cater for different types of car parks like regular, handicapped, and compact.
  • It should keep track of empty and filled spaces.
  • It should also cater for valet parking.
Map out the classes that would be required. Use a UML class diagram. Here are some points to get started.
  • A CarPark class to represent a parking station.
  • A ParkingSpace can be an abstract class or an interface to represent a parking space, and RegularParkingSpace, HandicappedParkingSpace, CompactParkingSpace, etc are subtypes of a ParkingSpace. This means a RegularParkingSpace is a ParkingSpace.
  • A CarPark has a (i.e. composition) finite number of ParkingSpaces. A CarPark also keeps track of all the parking spaces and a separate list of all the vacant parking spaces.
  • A Vehicle class uses a (i.e. delegation) ParkingSpace.  The Vehicle class will hold attributes using enum classes like  VehicleType and ParkingType. The vehicle types could be Compact, Regular, and Handicapped. The parking types could be Self or Valet. Depending on the requirements, the self or valet types could be designed as subtypes of the Vehicle class.

Q7: Can you design the classes that represent a restaurant?
A7: Very popular. Good understanding of OO design is vital to job interview success. Learn the SOLID design principles, know why you favor composition over inheritance, and don't under estimate the power of coding to interface.
  • Java OO Interview Questions and Answers. A step-by-step tutorial explaining how you would go about designing your classes. The "Core Java Career Essentials" PDF covers more examples on SOLID design principles with working code.

How would you go about identifying thread safety issues in an application? How would you go about identifying memory leaks issues? How would you go about identifying performance issues?, etc to judge your experience. I am yet to work on a project that didn't face problems relating to thread safety, memory leaks, and performance issues. Hence, it really pays to market your skills and experience in fixing issues relating to these common challenges.

Q8. How will you go about fixing memory leaks in Java?
A8. Again, a very common problem, and a thorough answer will go a long way in securing your next Java job.

Q9: How will you go about fixing performance issues in Java?
  • Detecting performance issues in Java. A basic tutorial covering -- how to detect performance issues.
  • Also, refer to the JMeter tutorials in this blog as to learn how you can put load on your application to simulate concurrent users.

Q10: How will you go about detecting and fixing thread-safety issues in Java?
A10: Debugging concurrency issues and fixing any thread starvation, dead lock, and contention requires skills and experience to identify and reproduce these hard to resolve issues. Here are some techniques to detect concurrency issues.
  • Manually reviewing the code for any obvious thread-safety issues. There are static analysis tools like Sonar, ThreadCheck, etc for catching concurrency bugs at compile-time by analyzing their byte code.
  • List all possible causes and add extensive log statements and write test cases to prove or disprove your theories.
  • Thread dumps are very useful for diagnosing synchronization problems such as deadlocks. The trick is to take 5 or 6 sets of thread dumps at an interval of 5 seconds between each to have a log file that has 25 to 30 seconds worth of runtime action. For thread dumps, use kill -3 in Unix and CTRL+BREAK in Windows. There are tools like Thread Dump Analyzer (TDA), Samurai, etc. to derive useful information from the thread dumps to find where the problem is. For example, Samurai colors idle threads in grey, blocked threads in red, and running threads in green. You must pay more attention to those red threads.
  • There are tools like JDB (i.e. Java DeBugger) where a “watch” can be set up on the suspected variable. When ever the application is modifying that variable, a thread dump will be printed.
  • There are dynamic analysis tools like jstack and JConsole, which is a JMX compliant GUI tool to get a thread dump on the fly. The JConsole GUI tool does have handy features like “detect deadlock” button to perform deadlock detection operations and ability to inspect the threads and objects in error states. Similar tools are available for other languages as well.

Q. What is the difference between mutex and semaphores? What problem do they solve?

Example 1: A trading system creates a buy order and places it in a queue, and a separate consumer thread picks up the order and sends it to the stock exchange. A typical producer and consumer scenario.

Example 2: If you want to recursively traverse through nested folders and spawn a number of worker threads to move the html files found to a destination folder and increment the semaphore permits with a release( ) method call. A separate thread will wait with the acquire(numberOfPermits) to acquire all the released permits before start archiving (i.e. zipping up) those files.

Example 3: A counter that keeps track of the number of active logged in users by incrementing the count when users log in and decrementing the count when the users log out.

Mutex and semaphores are used to solve producer and consumer synchroniztion scenarios outline above.. 

Q. Can you write code to demonstrate deadlock in Java?
A. Creating a deadlock in Java

Q. Can you give some examples of thread racing conditions you had experienced?
A. Thread racing conditions and atomic operations. 

Java Interview Questions and Answers

1-5 for experienced developers 6-10 for experienced developers 11-16 for experienced developers 17-20 for experienced developers

Labels: , ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home