Showing posts with label JAVA developer. Show all posts
Showing posts with label JAVA developer. Show all posts

Friday, March 30, 2012

What is the meaning of the keyword Synchronized?

What is the meaning of the keyword Synchronized?

The keyword synchronized is used in multi-threaded applications, to guarantee consistency of the data and to avoid unexpected results.

When it is used in a method declaration, the JVM will guarantee that once a thread A starts to run this method, other threads that want to run other synchronized methods in the same object will be locked until thread A leaves the synchronized method.

E.g.:
          public synchronized int getFoo() { return foo++; }

This keyword can also be used in a synchronized statement, to improve the application performance,  as when not all of the lines of code in the method need to synchronized. It is necessary to declare which object will be synchronized (use this to synchronize the object that has the method).

E.g.:
        public int foo() {
                synchronized(this){
                      foo++;
                 }
                //do something else...
        }

Thursday, March 29, 2012

How do I prevent classes of the other packages from accessing my members in JAVA?

How do I prevent  classes of the other packages from accessing my members in JAVA?
By using the <package-private> control acces modifier in the members. What is the keyword? None!  Please check out the example:

package ric.interviewqa4java;
public class A{
    int aField;         //visible to classes of the same package
    void aMethod(){}    //the same!
}

How do you make a variable read-only in Java?

How do you make a variable read-only in Java?
By using the keyword final.

E.g.:
      public final int id = 1234;

How many bits are used to store a long variable, in Java?

How many bits are used to store a long variable, in Java?
64-bits.

Here are the primitive data types in Java and their sizes:
  • byte       8-bits
  • short    16-bits
  • int        32-bits
  • long     64-bits
  • float     32-bits
  • double 64-bits
  • char     16-bits
  • boolean  unknown (I know it seems weird why the answer is not 1-bit. If you know the reason, please add a comment!)
 
 For more details, check the reference. 

What is the ultimate superclass in Java?

What is the ultimate superclass in Java?
The class Object.  All classes in Java that don't explicitly extend another class will implicitly extend this class.

Thursday, March 22, 2012

Can you write a palindrome function?

Can you write a palindrome function?

Yes, sure! Here is a very simple one, case sensitive, without handling special characters:

package com.blogspot.interviewqa4java;

/** © 2012 interviewqa4java.blogspot.com */
public class Palindrome {
  
    public static void main(String[] args) {
        String[] words= {"ABCBA", "ABCCBA", "AAC", "ABAB"};
        for (String word : words) {
            System.out.println("Is \'"+ word + "\' a palindrome? "+ isPalindrome(word));
        }
    }

    private static boolean isPalindrome(String word) {
        for (int i = 0, j=word.length()-1; i < (word.length() / 2); i++, j--) {
            if(word.charAt(i) != word.charAt(j)){
                return false;
            }
        }
        return true;
    }
}


Console results:
Is 'ABCBA' a palindrome? true
Is 'ABCCBA' a palindrome? true
Is 'AAC' a palindrome? false
Is 'ABAB' a palindrome? false

Thursday, February 9, 2012

How do I multiply 2 big integers?

How do I multiply 2 big integers?
In the previous post I talked about "How do I add 2 big integers?". In the same scenario, consider that now you want to multiply those 2 huge integer numbers... Is the logic the same?

Yes, but the solution is not as simple. Please read the previous post to understand the logic behind the solution, then jump to the code and see my comments bellow.

[Java code snippet] Big integer product

First, note that we are iterating over both big integer representations as arrays – array1 and array2. On line 22 you can see the product of 2 single-digits items that represent the i-est and j-est digits of both big integers, respectively. 

As commented on line 25, if the product has 2 digits, the 2nd one should be stored in the next index. Since the current index is given by i+j, the next one will be i+j+1. To implement this logic, we will iterate over the array representation of this product, called prodAsArray, where k could be 0 or 1 (line 26).

When we are storing each digit on its correct position i+j+k (line 27), we have to add the it to the previous digit that might have been stored in a previous iteration. That's why I used +=. Ops, a sum operation, so the result could also have 2 digits! If it is the case, we have lines 30 to 38 to handle this, following the same logic as described before.

How do I add 2 big integers?

How do I add 2 big integers?
Say you want to add 2 integer numbers that are so huge that you can't just add them directly and you can't use an API like BigInteger; you have to implement the sum operation. What would you do?

The solution here is simple. Each huge integer number will be represented using an  int array[ ]. Each digit will be stored in a different position in the array. E.g.: 2012 --> [2, 1, 0, 2]

Considering that 2012 = (2*10^0 + 1*10^1 + 0*10^2 + 2*10^3 ),  then 2012 + 35= ((2+5)*10^0 + (1+3)*10^1 + (0+0)*10^2 + (2+0)*10^3 )   = 2047

So, all you have to do is to sum items in the same index, right?Almost! Remember to consider the carry: store it if the sum  > 9 and then use it in the next index sum. Well, enough of talking, let's see the code! First let's see the main() method and the add logic:

[Java code snippet] Big integer sum

 If you're interested in the details, take a look at the Big integer auxiliary methods, that also can be used for Big integer product, in a later post...

[Java code snippet] Big integer auxiliary methods


And here, the code so you can copy and run using Eclipse, NetBeans, etc.

package ric.interviewqa4java;

/** © 2012 interviewqa4java.blogspot.com */
public class BigInteger {
    public static void main(String[] args) {
        int bigInt1 = Integer.MAX_VALUE;
        int bigInt2 = Integer.MAX_VALUE;
   
        System.out.println(bigInt1+" + "+bigInt2+" = "+add(bigInt1, bigInt2));
    }
   
    public static String add(int bigInt1, int bigInt2) {
        //Find array length
        int length1 = intLenght(bigInt1);
        int length2 = intLenght(bigInt2);
        int arrayLength = Math.max(length1, length2);
               
        //convert numbers into array; Ex: 157 -> [7, 5, 1, 0, 0]
        int array1[] = intToArray(bigInt1, length1, arrayLength);
        int array2[] = intToArray(bigInt2, length2, arrayLength);
       
        //sum arrays
        return sumArray(array1, array2);
    }
   
    /** Solution logic here*/
    private static String sumArray(int[] array1, int[] array2) {
        int carry=0;
        int sumArray[] = new int[array1.length + 1];
       
        //sum arrays
        for (int i = 0; i < array1.length; i++) {
            sumArray[i] = (array1[i] + array2[i] + carry) % 10 ; //sum digits + carry; then extract last digit
            carry = (array1[i] + array2[i] + carry) / 10; //Compute carry
        }
        sumArray[array1.length] = carry;
        return arrayToString(sumArray);
    }
   
    /** Auxiliary methods*/
    private static int intLenght(int bigInt) {
        return Integer.toString(bigInt).length();
    }
    private static int[] intToArray(int bigInt, int bigIntLength, int arrayLength) {
       
        int array[] = new int[arrayLength ];
        for (int i = 0; i < arrayLength ; i++) {
            array[i] = ( i<bigIntLength ?
                             getDigitAtIndex(bigInt, bigIntLength - i -1) :
                             0 ); //complete the rest of the array with 0
        }
        return array;
    }
    private static int getDigitAtIndex(int longint,int index){       
        return Integer.parseInt(Integer.toString(longint).substring(index, index+1));
    }
    private static String arrayToString(int[] sumArray) {
        String sum = "";
        boolean firstNonZero = false;
        for (int i = sumArray.length-1; i >= 0 ; i--) { //from array end to beginning
           
            if(!firstNonZero && (sumArray[i]==0)){ //ignore if 1st digits are 0
                continue;
            } else{
                firstNonZero=true;
            }
            sum += sumArray[i];
            if((i%3 ==0)&&i!=0){ sum +=",";}  //formatting
        }
        String sumStr = sum.length()==0?"0":sum; // handle the 0 value (haha, input was not big!)
        return sumStr;
    }
}

Which sorting algorithm would you choose for guaranteed optimal time performance?

Which sorting algorithm would you choose for guaranteed optimal time performance?
Consider you have a huge array of n elements and you want to sort it in considering time performance, not only in the average case, but also in the worst case. Which sorting algorithm would I choose?

R: I won't go through all sort algorithms, so here my "quick answer": merge sort. Why? Because merge sort is O(n . lg n) not only for the average case, but also in the worst case. In addiction to that, merge sort doesn't replace the same array elements: it is stable.

Here is my Java code snippet. I removed unnecessary code from the picture, but it is shown bellow. Of course I just used a small array, feel free to used with a huge array!


[Java code snippet] Merge sort


package ric.interviewqa4java;

/** © 2012 interviewqa4java.blogspot.com */
public class MyMergeSort {
    static int array[]= {10,6,2,3,11,8,7,10,4,1};
   
    public static void main(String[] args) {       
        printlnArray("Original array = ");
        mergeSort(0, array.length-1);
        printlnArray("Sorted array   = ");
    }
   
    private static void mergeSort(int low, int high) {
        if(low<high){
            int middle = (low+high)/2;
            mergeSort(low, middle);
            mergeSort(middle+1, high);
            merge(low, middle, high);
        }// if i==j, the sub-list has 1 element, then it's considered sorted
    }
   
    private static void merge(int low, int middle, int high) {
       
        int tempArray[] = new int[high-low+1];
       
        //Compare members in each sublists in the the extreme left;
            //the smaller element is "removed" and
            //stored in a temporary list that will be an ordered list after the loop
        for(int i=low, j=middle+1, k=0; (i <= middle) || (j <= high); k++){
            if(i > middle){//all members in the left list were used
                tempArray[k] = array[j++];
            }
            else if (j > high){ //all member in the right list were used
                tempArray[k] = array[i++];
            }
            else if(array[i] <= array[j]){
                tempArray[k] = array[i++];
            }else{
                tempArray[k] = array[j++];
            }
        }
        //apply changes
        int index=low;
        for (Integer value : tempArray) {
            array[index++] = value;
        }
    }
   
    //Auxiliary method to print the array
    private static void printlnArray(String message) {
        StringBuffer arrayStr =  new StringBuffer(message+"[");
        for (int i = 0; i < array.length; i++) {
            arrayStr.append(array[i]);
            if(i < array.length-1){ arrayStr.append(", "); }
        }
        arrayStr.append("]");
        System.out.println(arrayStr);
    }
}

Tuesday, December 20, 2011

Differences between .equals() and ==?

What are the differences between ".equals()" and "==" (in Java)?
Both options are used to compare data.

Primitive types only accept "==": the equality operator, that compares if the primitive variables have the same value.  

Reference variables accept both. "==" compares if both are references to the same area in the memory, which means that both variables references the same object. The method ".equals()"  have the same meaning in some cases -  the class Object and its subclasses that don't override the method ".equals()" (in any inheritance level).

As this method can be overriden, we could have 2 instances of the class A, where instance1== instance2 returns false, but instance1.equals( instance2) returns true.

Wednesday, December 14, 2011

Interview Questions and Answers.. now!

As you prepare yourself for that job interview you may want to find some questions they will ask you... and, of course, the answers! I can't promise to be right always, but I will try. So please, do your job, study the topic, understand the answers, you are responsible for your success!