Friday, November 18, 2011

Java Experienced Interview Questions

Core Java

1) Difference between abstraction and encapsulation? Give some examples from your project.

2) There are two interfaces A and B, interface A has a variable i with value 10 and interface B also has variable i with value 20. There is a class that implements both interfaces in the constructor of the class there is statement System.out.println(i); what will be the output of this.

Code:
interface A {
public static final int i = 10;
}

interface B {
public static final int i = 10;
}


public class Test implements A,B
{
Test()
{
System.out.println(i);
}
}


what will be the output?

3) What are inner classes?

4) How will you initialize a regular inner class, method local class and static inner class?

5) How will you create Custom Exception?

6) Can I create custom exception by extending throwable class.

7) What is the property of Set interface.

8) Can we have constructor of abstract class. If yes then why we can not initialize abstract class using new keyword?

9) For a class Person I have overridden equals and hashcode methods of object class and for different objects of Person I am returing same hashcode every time like

public int hashCode()
{
return 1;
}

and I have added 4 different objects of Person class in HashMap then what will be the size of map and what will happen if we call get method of Map and passed object of Person in that?

10) What will be your approach to identify circular reference in bean classes using reflection?

11) Difference between HashTable and HashMap.

12) Difference between checked and un-checked exceptions.

13) Write code to create singleton class.

14) What is Generics.

15) What is static.

16) What will be the output of the following program

class Test {

static{
System.out.println("Static");
}
Test()
{
System.out.println("Constructor");
}
public static void main(String[] args) {
new Test();
new Test();
}
}

17) What will be the output of the following program and why


class Test {

public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
Integer j1 = 128;
Integer j2 = 128;
if(i1 == i2) {
System.out.println("i1 == i2");
}
else {
System.out.println("i1 != i2");
}

if(j1 == j2) {
System.out.println("j1 == j2");
}
else {
System.out.println("j1 != j2");
}

}
}


18) How HashMap handle collission in HashCode?

19) Comparison is done by equals method then why we need hashcode or what is the purpose of hashcode in collections?

20) Connection is an Interface and we can not initialize interface in java and DriverManager is a class we call static method getConnection of DriverManager then how does DriverManager.getConnection(....) give connection object?

21) What will be out put of the following program

class A {
public A(int a)
{
System.out.println("Constructor of A");
}
}

class B {
public B(int b) {
System.out.println("Constructor of B");
}
}

class C {
public static void main(String[] args) {
B b = new B(10);
A a = new A(20);
}
}

22) In Java, how will you declare a variable x such that the following two conditions are satisfied?
x = ++x
x = x + 1 is

23) What will happen if we try to call static method from instance of class using .(dot) operator?

24) What will be the output of the following program

class Test {
private static int a = 10;
public static void staticThis()
{
System.out.println(this.a);
}
public static void main(String[] args) {
Test.staticThis();
}
}

25) What will be the output of the following program

class Test {
public static void main(String[] args) {
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}

26) Consider two simple JAVA programs where input parameters for one of the programs depend upon the output of the other program. In such scenario, will multithreading help in improving performance ?



No comments:

Post a Comment

Spring Boot Config Server and Config Client.

 In Spring cloud config we can externalise our configuration files to some repository like GIT HUT, Amazon S3 etc. Benefit of externalising ...