Monday, 30 May 2016

1. Write code in Java to Convert numbers to words

public class NumbersAndWords
{
  public void convertToWord(int number,String word)
  {
 StringBuffer convertedWord = new StringBuffer();
String one[] = { " ", " one", " two", " three", " four", " five",
" six", " seven", " eight", " Nine", " ten", " eleven",
" twelve", " thirteen", " fourteen", "fifteen", " sixteen",
" seventeen", " eighteen", " nineteen" };

String ten[] = { " ", " ", " twenty", " thirty", " forty", " fifty",
" sixty", "seventy", " eighty", " ninety" };

if (number > 19)
{
convertedWord.append(ten[number / 10] + " " + one[number % 10]);
} else
{
convertedWord.append(one[number]);
}
    if(number > 0)
    convertedWord.append(word);
    System.out.println(convertedWord.toString());
  }

  public static void main(String[] args)
  {
    int number=1223456;
 
   if(number>0)
   {
      NumbersAndWords a = new NumbersAndWords();
      a.convertToWord(((number/100000)%100)," lakh");
      a.convertToWord(((number/1000)%100)," thousand");
      a.convertToWord(((number/100)%10)," hundred");
      a.convertToWord((number%100)," ");
    }
  }
}

Friday, 26 February 2016

Exception Handling in Java

Exceptional handling is one of the best useful features provided by Java.
Exception can be any error that may occur during the execution of the program.
Example: When user tries to withdraw amount more than his account balance, an error occurs.
In Such situations instead of ending the program abnormally, Exception handling is used to throw the valid error messages to the end user.
The reasons for an exception can be mainly categorized into three categories:
1.       User(client) error
2.       Developer error
3.       Hardware error.
Exceptions are mainly divided into three categories
1.       Checked Exceptions
2.       Un checked Exceptions
3.       Error
Below is the hierarchy of exceptions. Throwable is the root class for all exceptions.

Checked Exceptions:  
v  Checked Exceptions are the exceptions which are checked by compiler. I.e. our program will not compile if do not handle these checked Exceptions.
v  All checked Exceptions are inherited from Exception class
v  Checked exceptions can be handled either by using try-catch block. Or by using throws keyword
v  Checked Exceptions are also called as Compile time Exceptions
Example:
In the below example user is trying to read a file called ‘chckedException.txt’ in D drive. There is a chance giving wrong location for the file.

File file=new File ("D: // chckedException.txt”);
 FileReader fr = new FileReader (file);

To handle such situations, java throws a compile time exception called FileNotFoundException which should be handled by the developer while developing the code.
Developer can use either try catch block or can choose to skip the exception handling by just throwing it using throws keyword.

Solution 1:
public void readFile()
{
Try
{
File file=new File ("D: // chckedException.txt”);
 FileReader fr = new FileReader (file);
}
Catch(FileNotFoundException ex)
{
System.out.println(“File does not exist at the given location”);
}
}
Solution 2
public void readFile() throws Exception
{
File file=new File ("D: // chckedException.txt”);
 FileReader fr = new FileReader (file);

}



Thursday, 14 January 2016

Core Java Interview questions

Hello guys, in this post I will try to cover as many interview questions as possible.These are most commonly asked interview questions that can be useful for freshers as well as experienced people.

In this post I will try to concentrate maximum on core Java questions only. 

1. What is the difference between String, StringBuffer and StringBuilder?

String Object is immutable. I.e. if we create a String object, we cannot change it at the same memory location. 
If we change the string object, it creates a new memory location and copies the content into that new location.

Example 

String s1 = new String ("Java interview")

Suppose that Address of s1 is 200 

if we say s1 = s1 +" questions”

Then it will create a new memory location (say at 300) and copies the content to new location.


I.e. address of s1 will change to 300. Please click on the image below to maximize it.

StringBuffer :

StringBuffer is mutable. I.e. if we create a string buffer object, then we can modify it at the same location.

StringBuffer sb = new String Buffer (“Java interview”);

Suppose that address of sb is 200

If we say
Sb.sppend(“questions”)

Then its value will be changed to Java Interview Questions and address will be same 200.



  String Builder :

  1.  String Builder is Non-synchronized version of String buffer. i.e String Buffer is synchronized where as String builder is non-synchronized. 
  2. StringBuilder is much faster than StringBuffer as it doesnt have to deal with synchronization methods and locks
  3. so if you are not going to use threading in java then using StringBuilder will improve the efficiency of the program.
  4. StringBuffer is old class in java where as StringBuilder was introduced from java 1.5 version.

1.     Explain about finalize method in JAVA.

v  Finalize method is a protected method declared in Object class
v  It is by default available to all classes defined in Java.
v  This method is called by the garbage collected before destroying an object.
v  We can override this method in our class. When we override, it is our responsibility to call the finalize method of the super class.
v  Below is the syntax of finalize method.


Protected void finalize() throws Throwable;
 

Search This Blog