Tower of Hanoi in Java Using Recursion

What is Tower of Hanoi ?

Tower of Hanoi is also called as Tower of Brahma or Lucas Tower. It is one of the most popular problem which makes you understand the power of recursion.  Tower of Hanoi is a mathematical puzzle which consist of 3 poles and number of discs of different sizes. Initially all the discs will be places in the single pole with the largest disc at the bottom and smallest on the top. We need to move all the disc from the first pole to the third pole with the smallest disc at the top and the largest at the bottom under the below conditions

  1. Only one disc can be moved at a time.
  2. Larger disc cannot be placed on a smaller disc.

[Read more…]

Check if a number is even or odd without using modulo or division operators in Java

Write a program to Check if a number is even or odd is one of  basic question which will be asked in the interview for fresher, anyone can solve this but always this question comes with a twist given at the end stating that we should not use the modulo or division operator in Java. Before getting into the actual code to begin with lets use the modulo and division operation to check if the given number is odd or even. [Read more…]

Top 17 Core Java Interview Questions on Constructors

1. What is a Constructor in Java?

Constructor is just like a method in Java that is used to initialize the state of an object and will be invoked during the time of object creation.

2. What are the Rules for defining a constructor?

  1. Constructor name should be the same as the class name
  2. It cannot contain any return type
  3. It can have all Access Modifiers are allowed (private , public, protected, default)
  4. It Cannot have any Non Access Modifiers (final ,static, abstract, synchronized)
  5. No return statement is allowed
  6. It can take any number of parameters
  7. Constructor can throw exception, we can have throws clause

[Read more…]

Top 14 Java Interview Questions on Static keyword

1. What is static keyword in Java?

Static is a Non Access Modifier. Static can be applied to variable, method, nested class and initialization blocks (static block).

2. What is a static variable ?

  • A Static variable gets memory allocated only once during the time of class loading.
  • All the instance of the class share the same copy of the variable, a static variable can be accessed directly by calling “<<ClassName>>.<<VariableName>>” without need to create instance for the class.
  • value of a static variable will be common for all instances

[Read more…]

Polymorphism in Java – Method Overloading and Overriding

Polymorphism is the ability to take more than one form. Polymorphism is one of the most important concept in OOPS ( Object Oriented Programming Concepts). Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

In Java, there are 2 ways by which you can achieve polymorphic behavior

1. Method Overloading

2. Method Overriding [Read more…]

How to make a class Immutable in Java

Immutable class is the one whose state cannot be changed after it is created. The best example for this is String,Integer class in Java, once instantiated the value never be changed.

Immutable class has lots of advantages such as they can be used for the caching purposes as you don’t need to worry about the value changes. Immutable class is thread-safe and hence you don’t need to worry about synchronization issues and can be easily used in the multi-threaded environment. [Read more…]

How HashMap works in Java

How a HashMap Works internally has become a popular question in almost all the interview. As almost everybody knows how to use a HashMap or the difference between HashMap and Hashtable. But many fails when the question is how does a hashmap internally works.

So the answer to the question how does a hashmap works is that is it works based on the hashing principle but it is not as simple as it sounds. Hashing is the mechanism of assigning the unique code to a variable or attribute using some algorithm to enable easy retrieval. A true hashing mechanism should always return the same hashCode() when it is applied to the same object. [Read more…]