Pretty simple question. What would happen if you try to execute the
below program?

public Class States{

private String state;

     public States(String state){

           this.state = state;

     }

public String getState(){

     return state;

}

public void setState(String state){

     this.state = state;

}

public void clearState(){

     this = null;

}

public static void main(String args[]){

     States s = new States(“Active”);

     s.clearState();

}

}

Consider the below classes.

public class AH<E> {

    private E a;

    public void setA (E x) {

        a = x;

    }

    public E getA () {

        return a;

    }

}

public class A {

}

public class C extends A {

}

public class D extends A {

}

For the following code snippets, identify whether the code:

*    fails to compile,
*    generates an error at runtime, or
*    none of the above (compiles and runs without problem.)

a. AH<A> h = new AH<C>();
b. AH<D> h = new AH<A>();
c. AH<?> h = new AH<C>();
   h.setA(new C());
d. AH h = new AH();
   h.setA(new D());

e. AH<? extends A> h = new AH<C>();

  h.setA(new C());

Consider the below generic method:

public static <U> void fillBoxes(U u, List<Box<U>> boxes) {
        for (Box<U> box : boxes) {
            box.add(u);
        }
    }

Also, this snippet,

Crayon red = ...;
List<Box<Crayon>> crayonBoxes = ...;

Now,

What is the way to invoke the generic method?

a. Box.<Crayon>fillBoxes(red, crayonBoxes);
b. Box.fillBoxes(red, crayonBoxes);

Is it possible in java to define a class with in an Interface?  If yes,
what is the scope of that class? If No, Why is it not allowed in Java?
Why will this be useful for anyone?

Given an Main program, how do you make sure that some piece of code is
executed even if the user interrupts the execution of the program by
closing or pressing CTRL + C , say in the command prompt.

Can classes implementing Singleton pattern be extended?  When is it
possible to have multiple instances of Singleton objects assuming that
the singleton has been implemented with threads safety?

What is Multiton pattern?

How do you write a java program that is run on a command prompt and
which displays a progress as:

Status x of 100%

Without going to the nextline. i.e only x keeps changing from 1 to 100
on the same line.

Say in the finalize() method, the objects reference is re-instantiated..
Will the object be never reclaimed by GC, as, each time finalize() is
run, a reference to it is restored.

What happens when we have two mains, one with String[] and another with
Integer[] argument. A compile time error or compiles ok, but runtime
error? What if there is only one main with Integer[] arguments? Will
there be a ClasscastException if anything other than integer is entered
from command prompt as arguments?

Is it possible to know number of times the Garbage Collector has been
run, and if so, the elapsed time when it was run last time?

How do you find absolute path of the Class currently in execution? The
Class may be a part of a jar, in which case, the path to the jar file
location is needed on individual systems.

How do you write a standalone java program, which can be run, with no
main method in it?

I want

1.    The output of the below program. I know you can execute it and
see, but keep it as last option. Try to analyze the problem and
determine the output.
2.    Why is the resultant output that way and what is the moral of
the story?

//Parent.java

import java.util.Date;

public class Parent {

      public Parent(){

            printTodaysDate();

      }

      public void printTodaysDate(){

            System.out.println(new Date());

      }

}

//Child.java

import java.util.Date;

public class Child extends Parent{

      private Date date;     

      public Child(){

            date = new Date(2000,12,31);

      }

      public void printTodaysDate() {

            yadiYadiDaPrintSomeDate();

      }

      public void yadiYadiDaPrintSomeDate(){

            System.out.println(date);

      }

      /**

       * @param args

       */

      public static void main(String[] args) {

            Child d = new Child();

            d.printTodaysDate();// What does this print

      }

}

What is the problem with the below immutable class? Though I say
immutable it is still possible to change the state of an object of this
class. How?

public final Class StoreDate{

            private Date storeDate;

            StoreDate(Date date){

                        storeDate = date;

            }

            public Date getDate(){

                        return new Date(storeDate.toString())

            }

}

Below is a snippet of a java program. I would like to know the output
and the reason for the same which is printed at the end. If there does
exist a problem, then what is the problem and its solution? If there is
no problem, good, we are going the right way.. :-) [PJ]

//router.properties

ProcessCreditOrder=firstDest

avm:ProcessValuation=secondDest

//Tester.java

public class Tester {

      /**

       * @param args

       */

      public static void main(String[] args) {

            Properties properties = new Properties();

            try {

//No Problem, router.properties was found

properties.load(Tester.class.getClassLoader().getResourceAsStream("route
r.properties"));
System.out.println(properties.getProperty("ProcessCreditOrder"));

System.out.println(properties.getProperty("avm:ProcessValuation"));
} catch (FileNotFoundException e) {

                  e.printStackTrace();

            } catch (IOException e) {

                  e.printStackTrace();

            }

      }

}

There exist entities Person, Employee and Manager. Where Employee
inherits from Person and Manager inherits from Employee.

How can you take a person object and make it an employee object, and how
can you take an employee object and make it a manager object(Say, the
person becomes an employee, and then gets a promotion to manager)?

No, the solution is not about creating a new Employee object and get/set
from a Person object to set the properties.. :-)

How do you guarantee that the garbage collector can reclaim a particular
object when JVM is running very low on memory? Say, for example an image
or a file object is loaded into memory, and is frequently being used.
But, at the time of crunching situation in memory, it can be released to
increase the available memory.

There is a small code snippet below. Obviously, you can execute and see
the result, I would request to you to try to answer it before executing
it. What will be the output of the below, obviously in a well written
class, with public static void main?

  int[] intArray = null;

        System.out.println(String.valueOf( intArray ));

        char[] charArray = null;

        System.out.println(String.valueOf( charArray ));

What is the output of below program, why and what is the concept guiding
the output?

public class Test {
  public static void main(String[] args) {
    try {
      String str = null;
      doSomething(str);
      System.out.println("1");
    } catch (RuntimeException e) {
      System.out.println("2");
    }
  }
  public static void doSomething(String str) {
    try {
        System.out.println(str.length());
    } finally {
      return;
    }
  }
}

What is Class data sharing?

An object or a set of objects have to be notified that the data or the
state of some other object has changed and it can proceed to do some
logic. How can you implement this in java using the features provided in
java language?

How many concrete implementations of the java.util.Calendar are released
in your JDK?

1.    ONE: the Gregorian Calendar
2.    TWO: the Gregorian Calendar and a new one from Japan
3.    THREE: because there is secret Calendars somewhere
4.    None of the above

How do you store user level data on system for a stand alone java
application? 

Say, the user has configured some preferences, and we have to save it so
that the next time the user of that login(system login) uses it, he can
view those changes. No login provided for the application.

No need to do it by writing it to a property or some other file.. :-)

I here will post some questions and the answers that I could find out for the same. Please feel free to comment with suggested answers. This blog is dedicated for discussion and new thoughts. Please email new questions at willProvideEmailSoon@gmail.com. I would post it on the blog.