Java questions for freshers

Java written test questions and answers for freshers




(1)

public class SwitchCase {

    public static void main(String[] args) {

         int a=12;

         switch(a){

         case 014:

             System.out.print("I know java");

             break;

         case 12:

             System.out.print("I don't know java");

         default:

             System.out.print("I also know c++");

         }

    }

}

What will be output of above program?

(a) I don't know java

(b) I don't know java

    I also know c++

(c) I know java

(d)Compiler error

Answer: (d)

(2)

public class ClassDemo {

    public static void main(String[] args){

         ClassDemo o=new ClassDemo();

         String str=o.__();

         System.out.print(str);

    }

    String __()

    {

         return "I know c#";

    }

}

What will be output of above program?

(a) I know c#

(b) I

(c) null

(d)Compiler error

Answer: (a)

(3)

public class Literal {

    public static void main(String[] args) {

         long a=25L;

         float b=25.0f;

         if(a==b)

             System.out.println(a+b);

         else

             System.out.println(b-a);

        

            

    }

}

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

(a)50.0

(b)0.0

(c)Run time error

(d) Compiler error

Answer: (a)

Explanation:

(4)

public class Literal {

    public static void main(String[] args) {

         char a ='\\';

         System.out.println(a+"I know java"+a);

    }

}

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

(a) \\I know java\\

(b) \I know java\

(c) I know java

(d) Compiler error

Answer:  (b)

Explanation:

(5)

public class BitwiseOpe {

    public static void main(String[] args) {

         int a=-10;

         int b=a>>>1;

         System.out.println(b);



    }



}

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

(a)-1254

(b)-10

(c) 2147483643

(d)Compiler error

Answer: (c)

(6)

public class SwitchCase {

    public static void main(String[] args) {

         int a=10;

         switch(a++){

         case 10:

             switch(a--){

             default:System.out.print("Exit");

             case 10:

             }

         default:System.out.print(a);

            

         }

    }

}

What will be output of above program?

(a) 11

(b) 10

(c) Exit10

(d)Compiler error

Answer: (c)

(7)

public class ClassDemo {

    public static void main(String[] args){

         ClassDemo o=new ClassDemo();

         double d=o.$cal$();

         System.out.print(d);

    }

    Long $cal$()

    {

         long l=10;

        short s=2;

         Short S=1;

         return ~s+++l>>>S;

    }



What will be output of above program?

(a)4.0

(b)3.0

(c)16.0

(d)Compiler error

Answer: (b)

(8)

public class Literal {

    public static void main(String[] args) {

         char a ='"';

         char b='\"';

         System.out.println(a+"I know java"+b);

    }

}

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

(a) \"I know java"

(b) "I know java

(c) "I know java"

(d) Compiler error

Answer:   (c)

Explanation:

(9)

public class BitwiseOpe {

    public static void main(String[] args) {

         byte a=-10;

         int b=a>>>1;

         System.out.println(b);



    }



}

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

(a)-1254

(b)-10

(c) 2147483643

(d)Compiler error

Answer: (c)


(10)Which of the following false java statement?

(a)A class cannot be private.

(b)A class cannot be protected.

(c)A class cannot be final.

(d)A cannot be static.

Answer: (c)


(11)

Which of the following is not correct?

(a)Duplicate case is not possible.

(b)Nested switch case is possible.

(c)Switch case statement is more efficient than equivalent if else ladder.

(d)A switch statement without any case is not possible.

Answer: (d)

(12)

public class ClassDemo {

    public static void main(String[] args){

         ClassDemo o=new ClassDemo();

         double d=o._cal_(15,2,2);

         System.out.print(d);

    }

    Long _cal_(int p,Integer q,int r)

    {

         long l=p;

         int s=q;

         Integer S=r;

         return ~~~s+++l>>>S;

    }

}

What will be output of above program?

(a)3.0

(b)1.0

(c)48.0

(d)Compiler error

Answer: (a)

(13)

public class Literal {

    public static void main(String[] args) {

         char a ='\u0041';

         System.out.println(a);

    }

}

(a)41

(b)’41’

(c)A

(d) Compiler error

Answer:  (c)

Explanation:

(14)

public class BitwiseOpe {

    public static void main(String[] args) {

         byte a=-1;

         int b=a>>5;

         int c=a>>2;

         System.out.println(b+c);

         }

    }

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

(a)-1

(b)-2

(c)0

(d)Compiler error

Answer: (b)

(15)

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)15

(b)12

(c)9

(d)6

No comments: