Java questions and answers

Java multiple choice objective type questions  for written test and answers


(1)

public class ArrayDemo {

    public static void main(String[] args) {

         long [][]arr=new long [2][];

         arr[0]=new int[2];

         arr[1]=new int[3];

         arr[0][1]=015L;

         System.out.println(arr[0][1]);

    }

}

What will be output of above program?

(a)13

(b)15

(c)0

(d) Compiler error

Answer:(d)

(2)

public class BreakDemo {

    public static void main(String[] args){

         Boolean b=21>=21;

         block1:{

             block2:{

                 block3:{

                      if(b)

                          break block2;

                      System.out.println("BLOCK3");

                 }

                 System.out.println("BLOCK2");

             }

             System.out.println("BLOCK1");

         }

            

    }

}

What will be output of above program?

(a)Infinite loop

(b)BLOCK2

   BLOCK1

(c)BLOCK1

(d)Compiler error

Answer:(C)

(3)What will be output of following program?

public class Float {

    public static void main(String[] args) {

         float f=5.3f;

         if(f==5.3)

             System.out.println("equal");

         else

             System.out.println("not equal");

            

    }

}

(a)equal

(b)not equal

(c)Run time error

(d)Compiler error

Answer: (b)

Explanation:

(4)

public class EnumTest {

    public static void main(String[] args) {

         byte a=-0x15;

         byte c=a>>2;

         System.out.print(c);

    }

}

What will output when you compile and run the above code?

(a) 84

(b) -84

(c) -5

(d)Compiler error

Answer: (d)

 (5)

public class Overloading {

    public static void main(String[] args) {

         int a=25;

         System.out.println((Long)a);

    }

}

What will be output of above program?

(a)25

(b)25l

(c)25L

(d)Compiler error

Answer: (d)

(6)

public class ArrayDemo {

    public static void main(String[] args) {

         long [][]arr=new long [2][];

         arr[0]=new long[2];

         arr[1]=new long[3];

         arr[0][1]=5L;

         System.out.println(arr[0][1]);

    }

}

What will be output of above program?

(a)5

(b)5l

(c)5L

(d) Compiler error

Answer: (a)

(7)

public class BreakDemo {

    public static void main(String[] args){

         int a=2;

         india: {

             ++a;

             if(a==3)

                 break india;

         }

             System.out.print(a);

            

    }

}

What will be output of above program?

(a)3

(b)4

(c)2

(d)Compiler error

Answer: (a)

(8)Which of the following java doesn’t support?

(a)Signed number

(b)Unsigned number

(c)Signed right shift

(d)Unsigned right shift

Answer: (b)

Explanation:

    Java language doesn’t support unsigned number because for programming purpose there is not use of concept of unsigned number .Unsigned number is used to check the singe higher order bit of numeric data . java fulfill this requirement by using concept of unsigned right shift (>>>).

(9)

public class EnumTest {

    public static void main(String[] args) {

         byte a=-15;

         int c=a>>2;

         System.out.print(c);

    }

}

What will output when you compile and run the above code?

(a)-60

(b)-3

(c)Compiler error

(d)None of these

Answer: ()

(10)

class Operator

{

    public static void main(String arg[])

    {

         int i=2;

         int j=++i + ++i + ++i;

         System.out.println(j);



    }

}

What will output when you compile and run the above code?

(a)250

(b)-123

(c)-6

(d) Compiler error

Answer: (c)


(11)

public class ArrayDemo {

    public static void main(String[] args) {

         if((int)Math.sqrt(2)==1)

             System.out.println("equal(+)");

         if((int)Math.sqrt(2)==-1)

             System.out.println("equal(-)");

    }

}

What will be output of above program?

(a) equal(+)

(b) equal(-)

(c) equal(+)

    equal(-)

(d)Compiler error

Answer: (a)

(12)What is not true about new operator?

(a) new is reserved word of java programming.

(b) It dynamically allocates memory.

(c) If new operator is not able to allocate memory for object then run  

    time exception will occur.

(d)With the help delete keyword we can release the memory spaces which

   has been allocated by new operator.

Answer: (d)

(13)What will be output of following program?

public class Float {

    public static void main(String[] args) {

         float f=5.5f;

         if(f==5.5)

             System.out.println("equal");

         else

             System.out.println("not equal");

            

    }

}

(a) equal

(b) not equal

(c) Run time error

(d)Compiler error

Answer: (a)

Explanation:

(14)

public class BitwiseOpe {

    public static void main(String[] args) {

         int a=-0x20;

         int b=a<<3;

         System.out.println(b);



    }



}

What will output when you compile and run the above code?

(a)1

(b)-256

(c)-1

(d)Compiler error

Answer: (b)

(15)

public class Overloading {

    short s=(2);

    Overloading(){

         ++s;

         ++this.s;

    }

    Overloading(Overloading t){

         ++t.s;

         ++this.s;

    }

    public static void main(String[] args) {

         Overloading t1=new Overloading();

         Overloading t2=new Overloading(t1);

         System.out.print(t2.s-t1.s);

    }

}

What will be output of above program?

(a)0

(b)-1

(c)-2

(d)Compiler error

6 comments:

confused said...

consider 12.a new can also be used in C++

and what is the difference in answer in 5.3f equal and 5.5f not equal..kindly explain

Mohit Mishra said...

Good questions!!!

I have added few C/C++ questions in

C/C++ Interview Questions

Have a look at them and reply.

Naveen said...

how did in 13th question program prints equal for 5.5 while not question for 5.3 in 3rd question

Naveen said...

how did in 13th question program prints equal for 5.5 while it prints not equal for 5.3 in 3rd question

JOHN said...

Answer for Question No-10 is 12 which is not present in the options.

Unknown said...
This comment has been removed by the author.