Java Language | 10 Minute‐Test 8


Instruction

  • Total number of questions : 10.
  • Time alloted : 10 minutes.
  • Each question carry 1 mark.
  • No Negative marks
  • DO NOT refresh the page.
  • All the best :-).

1.

What will be the output of these statement?

class output {

        public static void main(String args[])

        {

            double a, b,c;

            a = 3.0/0;

            b = 0/4.0;

            c=0/0.0;

            System.out.println(a);

            System.out.println(b);

            System.out.println(c);

        }

             }

A.
INFINITY
B.
0.0
C.
NaN
D.
all of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

For floating point literals, we have constant value to represent (10/0.0) infinity either positive or negative and also have NaN (not a number for undefined like 0/0.0), but for the integral type, we don’t have any constant that’s why we get an arithmetic exception.

2.

What is the output of this program?

    class mainclass {

        public static void main(String args[])

        {

            boolean var1 = true;

      boolean var2 = false;

      if (var1)

          System.out.println(var1);

      else

          System.out.println(var2);

       }

    }

A.
0
B.
1
C.
true
D.
false

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

3.

What is the output of this program?

    class variable_scope {

        public static void main(String args[])

        {

            int x;

            x = 5;

            {

          int y = 6;

          System.out.print(x + " " + y);

            }

            System.out.println(x + " " + y);

        }

    }

A.
5 6 5 6
B.
5 6 5
C.
Runtime error
D.
Compilation error

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x.

4.

What is the output of this program?

    class A {

        final public int calculate(int a, int b) { return 1; }

    }

    class B extends A {

        public int calculate(int a, int b) { return 2; }

    }      public class output {

        public static void main(String args[])

        {

            B object = new B();

            System.out.print("b is " + b.calculate(0, 1));

          }

    }

A.
b is : 2
B.
b is : 1
C.
Compilation Error.
D.
An exception is thrown at runtime.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The code does not compile because the method calculate() in class A is final and so cannot be overridden by method of class b.

5.

What is the output of this program?

    class evaluate {

        public static void main(String args[])

            {

          int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};

          int n = 6;

                n = arr[arr[n] / 2];

          System.out.println(arr[n] / 2);

            }

    }

A.
3
B.
0
C.
6
D.
1

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Array arr contains 10 elements. n contains 6 thus in next line n is given value 2 printing arr[2]/2 i:e 2/2 = 1.

6.

What is the output of this program?

    class increment {

        public static void main(String args[])

        {

                     int g = 3;

             System.out.print(++g * 8);

        }

    }

A.
25
B.
24
C.
32
D.
33

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.

7.

What is the output of this program?

    class leftshift_operator {

        public static void main(String args[])

        {

                     byte x = 64;

             int i;

             byte y;

             i = x << 2;

             y = (byte) (x << 2)

             System.out.print(i + " " + y);

        }

    }

A.
0 64
B.
64 0
C.
0 256
D.
256 0

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

None.

8.

What is the output of this program?

    class ternary_operator {

        public static void main(String args[])

        {

                     int x = 3;

             int y = ~ x;

             int z;

             z = x > y ? x : y;

             System.out.print(z);

        }

    }

A.
0
B.
1
C.
3
D.
-4

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

9.

What is the output of this program?

class Output {

        public static void main(String args[])

        {

                 int x=y=z=20;

         }

    }

A.
compile and runs fine
B.
20
C.
run time error
D.
compile time error

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

None.

10.

What is the output of this program?

    class jump_statments {

        public static void main(String args[])

        {

                     int x = 2;

             int y = 0;

             for ( ; y < 10; ++y) {

                 if (y % x == 0)

                     continue;

                   else if (y == 8)

                      break;

                 else

                    System.out.print(y + " ");

             }

        }

    }

A.
1 3 5 7
B.
2 4 6 8
C.
1 3 5 7 9
D.
1 2 3 4 5 6 7 8 9

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Whenever y is divisible by x remainder body of loop is skipped by continue statement, therefore if condition y == 8 is never true as when y is 8, remainder body of loop is skipped by continue statements of first if. Control comes to print statement only in cases when y is odd.


Submit your test now to view the Results and Statistics with answer explanation.