Sunday, December 6, 2015

Java Interview questions - Beginner level - 1

What is a final variable and effectively final variable in Java ? And give an example ?

final variable :  A variable or parameter whose value is never changed after it is initialized is final.

effectively  final variable : A variable or parameter is not declared as final and still the whose value is never changed after it is initialized is effectively final.

Why is the below program never generate a NullPointerException even the instance is null ? 

  Test t = null;

   public static void someMethod() {

There is no need for an instance while invoking static member or method.

Since static members belongs to class rather than instance.

A null reference may be used to access a class (static) variable without causing an exception.

What is the output of the below lines of codes ? 



Why you are not allowed to extends more than one class in Java where as you are allowed to implement multiple inheritance?

In case of extends Ambiguity problems may raise where as in case of interfaces, single method implementation in one class servers for both the interfaces.

 int a = 1L; Won't compile and int b = 0; b += 1L; compiles fine. Why ? 

When you do += that's a compound statement and Compiler internally casts it. Where as in first case the compiler straight way shouted at you since it is a direct statement.

Why it is printing true in the second and false in the first case?? 

public class Test
    public static void main(String[] args)
        Integer a = 1000, b = 1000;
        System.out.println(a == b);

        Integer c = 100, d = 100;
        System.out.println(c == d);



The second output is true though we are comparing the references, because the JVM tries to save memory, when the Integer falls in a range (from -128 to 127). At point 2 no new reference of type Integer is created for 'd'. Instead of creating new object for the Integer type reference variable 'd', it only assigned with previously created object referenced by 'c'. All of these are done by JVM.

What is the access level of default modifier in Java 

Default access modifier is package-private - visible only from the same package

Write a Program to check below the given 2 Strings are Anagrams or Not ?
For ex, below 2 strings are anagrams
String s1="home";
String s2="mohe";

Write program to reverse String("Java Programming")without using Iteration and Recursion?

Give me a real world example, where I have to choose ArrayList and Where I have to choose a LinkedList ?

What is the difference between a Iterator and a ListIterator ? 

What is the advantage of generic collection?

When can an object reference be cast to an interface reference?

Post a Comment