Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Showing posts with label experienced java interview. Show all posts
Showing posts with label experienced java interview. Show all posts

Sunday

FizzBuzz sample Program in Java 8

public static String fizzBuzzInJava8(int number) 
{
 String result = Optional.of(number) .map(n -> (n % 3 == 0 ? "Fizz" : ""+ (n % 5 == 0 ? "Buzz" : "")) .get();

 return result.isEmpty() ? Integer.toString(number) : result; 
}

Tuesday

Infosys Sr System Engineer Java Interview Questions 2015

Infosys Sr System Engineer Java Interview Questions 2015


I am 3 years experienced Java Developer from Trivandrum. I attended the Infosys Interview and a panel of 2 people interviewed me. The Questions asked are as Follows.


  1. Introduce Yourself: Give a introduction about yourself,mosltly talk about your career and project related, avoid talking about your own family and hobbies.
  2. Asked about my current project.
  3. What is String in Java?
  4.  Why String is Immutable?
  5. Infosys Interview Experience
  6. What is Collection?
  7. How HashMap works?
  8. Why Map is not a Collection?
  9. Difference between String and StringBuffer?
  10. Which Oracle version Using?
  11. Asked to write a query based on GroupBy function?

Please share your experience to help others.

Infosys Offer Letter

Thursday

Java Comparator sample program example

Java Comparator sample program example

import java.util.Comparator;


public class Employee {

private String empName;
private String department;
private int age;
public Employee(String empName,String department,int age) {
this.empName = empName;
this.department = department;
this.age = age;
}

public static class OrderByName implements Comparator<Employee>
{

@Override
public int compare(Employee e1, Employee e2) {
// TODO Auto-generated method stub
return e1.empName.compareTo(e2.empName);
}

}

public static class OrderByDepartment implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2) {
// TODO Auto-generated method stub
return e1.department.compareTo(e2.department);
}
}

public static class OrderByAge implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2) {
// TODO Auto-generated method stub
return e1.age>e2.age?1:(e1.age<e2.age?-1:0);
}
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the department
*/
public String getDepartment() {
return department;
}
/**
* @param department the department to set
*/
public void setDepartment(String department) {
this.department = department;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}

}



import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Sample {

    public static void main(String args[])
    { 
   Employee e1 = new Employee("vaisakh","IT",21);
   Employee e2 = new Employee("rajkumar","EEE",22);
   Employee e3 = new Employee("binu","MECH",20);
   
   List<Employee> list = new ArrayList<Employee>();
   list.add(e1);
   list.add(e2);
   list.add(e3);

   Collections.sort(list, new Employee.OrderByName());
   System.out.println("Ordering based on name "+list);
   Collections.sort(list, new Employee.OrderByDepartment());
   System.out.println("Ordering based on department "+list);
   Collections.sort(list, new Employee.OrderByAge());
   System.out.println("Ordering based on age "+list);
    }
}

Java Program to Find the Fibonacci series of a number

Java Program to Find the Fibonacci series of a number

public class Sample {

public static int findFibbonacci(int fibbNum)
{
if(fibbNum==1 || fibbNum==2)
{
return 1;
}
int f1=1;int f2=1;int fib=1;
for(int i=3;i<=fibbNum;i++)
{
fib=f1+f2;
f1=f2;
f2=fib;
}
return fib;
}
    public static void main(String args[])
    {
 
    for(int i=1;i<10;i++)
    {
    System.out.println(findFibbonacci(i));
    }
    }
}


Java Program to Replace a Character from a String by index position

Java Program to Replace a Character from a String by index position

public class Sample {

public static String replaceChar(int index,String str,char replaceChar)
{
char[] charStr = str.toCharArray();
charStr[index] = replaceChar;
return String.valueOf(charStr);
}
    public static void main(String args[])
    {
   System.out.println(replaceChar(6,"malayalam",'b'));
    }
}

Sunday

SOLID Principle in OOPS

SOLID Principle in OOPS Programming

The SOLID principles are useful to build a neat an good software. These are the programming and design principles of programming.
SOLID===>

  1. Single Responsibility Principle
  2. Open-Closed Principle
  3. Liskov Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion principle
1. Single Responsibility Principle(SRP):
                                                                   Each and Every Class should have a single responsibility. The Class should be changed for one and only reason.

2. Open-Closed Principle(OCP):
                                                     A Class should be Open for extension and close for modification.

3.Liskov Substitution Principle(LSP):                   
                                                     Objects of parent class can be replaced by objects of child class, without altering the properties of the program.

4.  Interface  Segregation Principle(ISP):
                                                          Program should generate client specific interfaces rather than general purpose interfaces.

5. Dependency Inversion principle(DIP):
                                                         A Class should depend on Abstractions rather than Concretions.




Static Blocks in Java

3. Static blocks in Java:
                                       Static blocks are called before the main block is called.It can initialize the static data members.

Example:

public class HelloWorld{

static double pi = 3.14;

 static{System.out.println("static block is invoked");}  
public static void main(String[] args) {
System.out.println("main block");
}

Static Methods in Java

2. Static Methods:
                              Static Methods belongs to the Class rather than the object.Static methods can be invoked without the need of objects.Statics variables can be modified by static methods.

Example:


public class HelloWorld{
static double pi = 3.14;
     static void calc(double a) {
pi = a;
System.out.println("Value pi is: "+a);
}
public static void main(String[] args) {
HelloWorld.calc(55.3);//accessing the static method
}
}

Saturday

Method Overloading in java

Method Overloading:

                                   Method Overloading is achieved by same method name but differ in parameters. The main usage of Method Overloading is consider you have to multiply some numbers. The methods are multiply(int,int) and multiply(double,int). Here the method name is same but arguments types are different.One for two int variables and the other for int and double. By giving the same name the programmer is easy identification that these methods are for multiplying numbers.

Method Overloading is again classified into two types.
  1. differ in number of arguments
  2. differ in the datatype of arguments.
Differ in Number of Arguments:
                                                     In this type of overloading the number of arguments in the method changes.That is the multiplication example seen previously.

Example Program by Changing the number of arguments:

package com.jobberindia;
import java.io.File;
import java.util.*;
public class HelloWorld {

void multiply(int a,int b)
{
int c = a*b;
System.out.println("After multiplication of "+a+" and "+b+"is: "+c);
}
void multiply(int a,int b,int c)
{
int d = a*b*c;
System.out.println("After multiplication of "+a+","+b+" and "+c+"is: "+d);
}
public static void main(String[] args) {
HelloWorld hw =  new HelloWorld();
hw.multiply(5, 3);
hw.multiply(5, 6, 7);
}
}

Output:
After multiplication of 5 and 3is: 15
After multiplication of 5,6 and 7is: 210

Differ in changing the data type of arguments:
                                                                           Another way to achieve Method Overloading is by changing the data type of arguments. The same multiplication example by changing the data type of arguments is shown below.

Example program by changing the data type of arguments:

package com.jobberindia;
import java.io.File;
import java.util.*;
public class HelloWorld {

void multiply(int a,int b)
{
int c = a*b;
System.out.println("After multiplication of"+a+" and "+b+"is: "+c);
}
void multiply(int a,double b)//one int and other double value
{
double c = a*b;
System.out.println("After multiplication of"+a+" and "+b+"is: "+c);
}
public static void main(String[] args) {
HelloWorld hw =  new HelloWorld();
hw.multiply(5, 3);
hw.multiply(5, 6.6);
}
}

   

Thursday

How to check a file is hidden in Java

How to check a file is hidden in Java

In Java to check whether a file is hidden in location we can use the below mentioned program. It checks whether the specified file is hidden or not.


It uses the isHidden() method on File object.

Possible Exceptions are IOException and SecurityException.


package com.jobberindia;
import java.io.File;
import java.util.*;
public class HelloWorld {

public static void main(String[] args) {

    File file = new File("c:/filename.txt");

    if(file.isHidden()){
    System.out.println("The file is hidden");
    }else{
    System.out.println("The file is not hidden");
    }
}
}



Wednesday

What is Map in Java?

What is Map in Java?

Other than List and Set Map is used to Store Key and Value Pair.The key is unique and values are duplicate allowed. Map implement the collection Interface. It allows only one null value which is stored in the zeroth index.

These are the some of the classes implements the Map<K,V> Interface.  HashMap, Hashtable, IdentityHashMap, LinkedHashMap.

Example:

public class HelloWorld {

public static void main(String[] args) {
List<String> list = new ArrayList<String>();
       list.add("Hello");
       list.add("World");
       list.add("Hello1");
       list.add("World2");
       Map<Integer,String> map = new HashMap<Ineger,String>();
     
       for(int i=0;i<list.size();i++)
       {
       map.put(i, list.get(i));
       }
     
        System.out.println("Data stored in mainlist "+map.get(2));

}


}

What is Set in Java?

What is Set in Java?

Set implements the Collection<E>, Iterable<E> Interfaces.

public interface Set<E>
extends Collection<E>
Set in Java allows to store objects by storing them in each index. Other than Set Set doesn't allows Duplication of elements.To store an element in Set use the add() method.
Example:

package com.jobberindia;

import java.util.*;

public class HelloWorld {

public static void main(String[] args) {

        Set<String> set = new TreeSet<String>();
        set.add("Hello");
        set.add("World");
        set.add("Hello1");
        set.add("World2");
        set.add("Hello3");
        set.add("World4");
        System.out.println("Data stored in mainlist "+set);

}

}



Some Classes implementing List Interface are TreeSet,HashSet etc.

What is List in Java?


What is List in Java? 

List implements the Collection<E>, Iterable<E> Interfaces.

public interface List<E>
extends Collection<E>.
List in Java allows to store objects by storing them in each index. Other than Set List allows Duplication of elements.To store an element in List use the add() method and to get a value use get() method.

Some Classes implementing List Interface are ArrayList,LinkedList etc.

List throws these Exceptions:


UnsupportedOperationException
ClassCastException
NullPointerException
IllegalArgumentException
IndexOutOfBoundsException

Example:

package com.jobberindia;
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();
        list.add("Hello");
        list.add("World");
        list.add("Hello1");
        list.add("World2");
        System.out.println("Data stored in mainlist "+list);
System.out.println("Size of list before removing "+list.size());
ArrayList<String> subList = new ArrayList<String>(list.subList(1, 2));
System.out.println("Data stored in sublist "+subList);
System.out.println("Size of list before removing "+subList.size());
}

}

Difference between List and Set and Map

Difference between List and Set and Map

List,Set,Map are the basic collection interfaces, although Map doesn't inherit Collection Interface, it belongs to the Collection in Java.

What is List in Java? 

List implements the Collection<E>, Iterable<E> Interfaces.

public interface List<E>
extends Collection<E>.


List in Java allows to store objects by storing them in each index. Other than Set List allows Duplication of elements.To store an element in List use the add() method and to get a value use get() method.

Some Classes implementing List Interface are ArrayList,LinkedList etc.

List throws these Exceptions:




UnsupportedOperationException
ClassCastException
NullPointerException 
IllegalArgumentException
IndexOutOfBoundsException 
Example:
package com.jobberindia;
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();
        list.add("Hello");
        list.add("World");
        list.add("Hello1");
        list.add("World2");
        System.out.println("Data stored in mainlist "+list);
System.out.println("Size of list before removing "+list.size());
ArrayList<String> subList = new ArrayList<String>(list.subList(1, 2));
System.out.println("Data stored in sublist "+subList);
System.out.println("Size of list before removing "+subList.size());
}

}
What is Set in Java?

Set implements the Collection<E>, Iterable<E> Interfaces.

public interface Set<E>
extends Collection<E>

Set in Java allows to store objects by storing them in each index. Other than Set Set doesn't allows Duplication of elements.To store an element in Set use the add() method.

Example:



package com.jobberindia;
import java.util.*;
public class HelloWorld {

 public static void main(String[] args) {
        Set<String> set = new TreeSet<String>();

        set.add("Hello");
        set.add("World");
        set.add("Hello1");
        set.add("World2");
        set.add("Hello3");
        set.add("World4");
        System.out.println("Data stored in mainlist "+set); 
 }
}

Some Classes implementing List Interface are TreeSet,HashSet etc.

What is Map in Java?

Other than List and Set Map is used to Store Key and Value Pair.The key is unique and values are duplicate allowed. Map implement the collection Interface. It allows only one null value which is stored in the zeroth index.
These are the some of the classes implements the Map<K,V> Interface.  HashMapHashtableIdentityHashMapLinkedHashMap.
Example:
public class HelloWorld {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("Hello1");
list.add("World");
Map<Integer,String> map = new HashMap<Ineger,String>();
list.add("World2");
map.put(i, list.get(i));
for(int i=0;i<list.size();i++)
{ }
}
System.out.println("Data stored in mainlist "+map.get(2)); }
Difference between List VS Set VS Map Interface:
  1. List allows duplicate values.
  2. List allows any number of null values.
  3. Set doesn't allows duplicate values.
  4. Set allows one null value.
  5. Map allows one null key value.
  6. List maintains the insertion order.
  7. Set and Map doesn't maintain the insertion order.
  8. Map stores key and value pair.
Based on your usage select these interfaces carefully.


How to get sublist from an ArrayList in Java

How to get sublist from an ArrayList in Java


We already studied about ArrayList in Java. Now we can see how to sublist an ArrayList. the scenario occurs when a ArrayList contains data that is to be displayed into two areas.
For this we use subList(arg1,arg2);
Example:
/** * author: JOBBERINDIA
 */
package com.jobberindia;
import java.util.*;
/**
 * @author user
 *
 */
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();
     
        list.add("Hello");
        list.add("World");
        list.add("Hello1");
        list.add("World2");
        list.add("Hello3");
        list.add("World4");
     
        System.out.println("Data stored in mainlist "+list);
System.out.println("Size of list before removing "+list.size());
ArrayList<String> subList = new ArrayList<String>(list.subList(1, 2));
System.out.println("Data stored in sublist "+subList);
System.out.println("Size of list before removing "+subList.size());
}
}


OutPut:
Data stored in mainlist [Hello, World, Hello1, World2, Hello3, World4]Size of list before removing 6Data stored in sublist [World]
Size of list before removing 1
Possible Errors are IndexOutOfBoundException and IllegalArgumentException.

Monday

ArrayList in Java with example Collections framework

ArrayList in Java with example Collections framework

ArrayList which implements the List Interface which belongs to the Collections Framework. ArrayList is very fast and ease of use. So developers prefer ArrayList over Arrays in java.

Advantages:


  • Other than Arrays ArrayList can increase or decrease its size based on the data size.
  • Fast so developers prefer ArrayList
Example:



/**
 * author: JOBBERINDIA
 */
package com.jobberindia;

import java.util.*;
/**
 * @author user
 *
 */
public class HelloWorld {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();
        
        list.add("Hello");
        list.add("World");
System.out.println("Size of list before removing"+list.size());
list.remove("Hello");
System.out.println("Size of list "+list.size());
}

}

Output: - 

Size of list before removing2
Size of list 1

Important Methods belongs to ArrayList:

  1.                 list.add(arg0);
  2. list.add(arg0, arg1);
  3. list.addAll(arg0);
  4. list.addAll(arg0, arg1);
  5. list.clear();
  6. list.contains(arg0);
  7. list.containsAll(arg0);
  8. list.containsAll(arg0);
  9. list.equals(arg0);
  10. list.get(arg0);
  11. list.size();
  12. list.isEmpty();
  13. list.lastIndexOf(arg0);
  14. list.remove(arg0);
  15. list.removeAll(arg0);
  16. list.toArray();
  17. list.toString();

Sunday

Can we overload and override static methods in java

Can we overload and override static methods in java

Overloading

Overloading is the one which same method name but the number of arguments and the type of arguments and the order of arguments can be changed.The return Type is same.

Overriding

Overriding is the process by which overriding the method in parent class by its subclass.

For the above question the answer is YES

public final class Test {


public static void far() {
        System.out.println("aaaaaa");
    }
    public static void far(int a) { 
        System.out.println("bbbbbbbb");
    }
    public static void main(String args[])
    { 
        Test.far();
        Test.far(10);
    }
    

}

But for Overriding the answer is NO

we can't override static methods.

static blocks in java

static blocks in java

static blocks are executed before the constructor is executed. The static block is executed only one, during the initialization of object or access the static member of that class for the first time.

public final class Test {


static int i;
    int j;
   
    // start of static block
    static {
        i = 10;
        System.out.println("static block called ");
    }
    // end of static block
    public static void main(String args[]) {
   
        // Although we don't have an object of Test, static block is
        // called because i is being accessed in following statement.
        System.out.println(Test.i);
    }
}


Output:
static block called 
10

public final class Test {


static int i;
    int j;
    Test(){
        System.out.println("Constructor called");
    }
    // start of static block
    static {
        i = 10;
        System.out.println("static block called ");
    }
    // end of static block
    public static void main(String args[]) {
   
    Test t1 = new Test();
    }
}



Output:
static block called
Constructor called

Final Arrays in java

Final Arrays in java

Normally in java a FINAL variables can't be changed. But array values can be changed. In java Array is a object.

public final class Test {


void comparaeAll()
{
 String[] a2 = {"sdafs"};
final String[] a1 = { "dsfasd", "sdfsd" };
a1 = a2; // Error, you can't do that. a1 is final
a1[0] = "over";  //good final has nothing to do with it
}

}

Thus FINAL array is not immuable

Arrays in java

Arrays in java

Arrays are one of the important feature of java. Arrays are used to store some objects that commonly share some properties. Arrays Provides lot of functionalists. for more about arrays see


  1. Arrays
  2. Arrays
  3. Arrays
  4. Arrays

public final class Test {

void comparaeAll()
{
int arr[] = {10, 20, 30, 40, 50};
      for(int i=0; i < arr.length; i++)
      {
            System.out.print(" " + arr[i]);              
      }
}
}