Google

Mar 11, 2014

Top 15 Core Java pre-interview technical written test questions and answers -- part 2

Extends Java written test pre-interview Questions and Answers 1-3.

Q4: Given the following class with an equals(...) method


package com.writtentest4;

public class Xyz {
 
 int a, b;

 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  
  if (obj == null)
   return false;
  
  if (getClass() != obj.getClass())
   return false;
  
  Xyz other = (Xyz) obj;
  
  if (b != other.b)
   return false;
  
  return true;
 }
 
}


Which of the following hashCode( ) method implementation is correct.

a) public int hashCode() { return a;}
b) public int hashCode() { return a+b;}
c) public int hashCode() { return a*b;}
d) public int hashCode() { return b;}


A4: The answer is d. As per the equals(..) and hashCode( ) contract, whatever fields used in equals(..) must be used in hashCode( ); Since only field "b" is used in equals(...), the hashCode( ) should also use only "b". Can't use "a" because it will break the contract. Refer to the Object class api methods hashCode( ) & equals(..) for the contract.


Q5. An IllegalArgumentException is thrown from

a) JVM when wrong method arguments are passed.
b) pre-condition check on a public method.
c) pre-condition check from a private method.
d) pre-condition check from a public or private method.

A5. The answer is b. You need to understand 2 things here.

1. Firstly, common exceptions classes in Java are:

Runtime exceptions thrown by JVM 



  • ArrayIndexOutOfBoundsException: when your code uses an array index, which is outside the array's bounds.
  • NullPointerException: when your code attempts to use a null reference where an object reference is required.
  • ClassCastException: when an attempt is made to cast an object reference fails.
  • NoClassDefFoundError: thrown by the JVM or class loader when the definition of a class cannot be found. In JDK 1.7 the error message is simplified to "Error: Could not find or load main class xxx" instead of a detailed stack trace.

Runtime exceptions  thrown programmatically

  • NumberFormatException: when an attempt is made to convert a string to a numeric type, but the string does not have the appropriate format. 
  • IllegalArgumentException: to indicate that a method has been passed an illegal or inappropriate argument. For example, a null object or a percentage  value less than 0 or greater than 100. This extends "IllegalStateException".
  • IllegalStateException: when a method is invoked and the program is not in an appropriate state for that method to perform its task. This typically happens when a method is invoked out of sequence, or perhaps a method is only allowed to be invoked once and an attempt is made to invoke it again.


All the above exceptions thrown by either JVM or programmatically indicate a bug in your application and need to be fixed.

2. Design by contract allows you to write fail-fast programs at runtime. The fail-fast programs need to check the validity of the arguments (pre-condition) passed into "public" method.  The public methods exposed and anyone could call the "public" methods with invalid arguments. On the other hand, private methods are under your sole control and it is appropriate to assert the pre-conditions as opposed to throwing an IllegalArgumentException.


Q6. What method do you invoke on the thread class to pause until a signal is received from the other threads?
A6. wait( ) or wait(2000). 

The wait( ), notify (), and notifyAll( ) methods are used to provide an efficient way for threads to communicate with each other. This communication solves the ‘consumer-producer problem’. This problem occurs when the producer thread is completing work that the other thread (consumer thread) will use. Refer Java Multithreading Interview Questions and Answers


Q7. What do you do when an ArrayIndexOutOfBoundsException is thrown?

a) Catch it and do nothing.
b) Catch it and log the full stack trace.
c) Don't do anything as it is a Runtime exception it will bubble all the way up.
d) All of the above.

A7. The answer is c.

The a) is an anti-pattern to to hide exceptions.
The b) is not a best practice to unnecessarily catch and then log. The ArrayIndexOutOfBoundsException indicates a bug in the application, hence it must be fixed and not logged. It will hide the bug unless someone is monitoring the logs or you have Nagios based alert setup to raise alerts and help desk tickets.

You may also like


Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home