QUESTION1
You have the following code in a file called Test.java

    class Base{
        public static void main(String[] args){
        System.out.println("Hello");
        }
    }

public class Test extends Base{}

What will happen if you try to compile and run this?
1. It will fail to compile.
2. Runtime error
3. Compiles and runs with no output.
4. Compiles and runs printing "Hello"

ANS : 4
This will compile and print "Hello". The entry point for a standalone java program is the main method of the class that is being run. The java runtime system will look for that method in class Test and find that it does have such a method. It does not matter whether it is defined in the class itself or is inherited from a parent class.

QUESTION2
What is the result of trying to compile and run the following code.

    public final static void main(String[] args){
        double d = 10.0 / -0;
        if(d == Double.POSITIVE_INFINITY)
            System.out.println("Positive infinity");
        else
            System.out.println("Negative infinity");
    }

1. output Positive infinity
2. output Negative infinity
3. Will fail to compile
4. Runtime exception

ANS : 1
There is no such thing as a positive or negative zero. Hence the result is always positive infinity.

QUESTION3

What is the result that will be printed out ?

    void aMethod()
    {
        float f = (1 / 4) * 10;
        int i = Math.round(f);
        System.out.println(i);
    }

1. 2
2. 0
3. 3
4. 2.5
5. 25

ANS : 2
The result of 1/4 will be zero because integer divion is carried out on the operands. If you need to obtain a fractional value you need to use either a float or double literal as in 1F / 4F.

QUESTION4
Which of the following are valid declarations?

    Note : None of the literals used here
    contain the character O they are all zeroes.

1. int i = 0XCAFE;
2. boolean b = 0;
3. char c = `A`;
4. byte b = 128;
5. char c = "A";

ANS : 1,3
1. is correct as it is a valid hexadecimal number. 2. is wrong because you can only assign the values true and false to them 3 is wrong because 128 is beyond the range of a byte. 5 is wrong because "A" is not a char it is a String.

QUESTION5
What is the result of trying to compile and run this program.

    public class Test{   
        public static void main(String[] args){
        int[] a = {1};
        Test t = new Test();
        t.increment(a);
        System.out.println(a[a.length - 1]);
        }
        void increment(int[] i){
        i[i.length - 1]++;
        }
     }

1. Compiler error.
2. Compiles and runs printing out 2
3. Compiles and runs printing out 1
4. An ArrayIndexOutOfBounds Exception at runtime

ANS : 2
You are passing a reference to an array as the argument to the method. The method may not modify the passed object reference but it can modify the object itself.