Java Language | 10 Minute‐Test 7


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 is the output of this program?

    class average {

        public static void main(String args[])

        {

            double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};

            double result;

            result = 0;

            for (int i = 0; i < 6; ++i)

                result = result + num[i];

      System.out.print(result/6);

 

        }

    }

A.
16.34
B.
16.566666644
C.
16.46666666666667
D.
16.46666666666666

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

2.

What is the output of this program?

    class mainclass {

        public static void main(String args[])

        {

            char a = 'A';

            a++;

      System.out.print((int)a);

        }

    }

A.
66
B.
67
C.
65
D.
64

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

ASCII value of ‘A’ is 65, on using ++ operator character value increments by one.

3.

What is the output of this program?

    class array_output {

        public static void main(String args[])

        {

               int array_variable [] = new int[10];

      for (int i = 0; i < 10; ++i) {

                array_variable[i] = i/2;

                array_variable[i]++;

                System.out.print(array_variable[i] + " ");

                i++;

            }

         }

    }

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.

4.

What is the output of this program?

    class conversion {

        public static void main(String args[])

        {

            double a = 295.04;

            int  b = 300;

            byte c = (byte) a;

            byte d = (byte) b;

            System.out.println(c + " "  + d);

        }

    }

A.
38 43
B.
39 44
C.
295 300
D.
295.04 300

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Type casting a larger variable into a smaller variable results in modulo of larger variable by range of smaller variable. b contains 300 which is larger than byte’s range i:e -128 to 127 hence d contains 300 modulo 256 i:e 44.

5.

What is the output of this program?

    class multidimention_array {

        public static void main(String args[])

        {

            int arr[][] = new int[3][];

            arr[0] = new int[1];

            arr[1] = new int[2];

            arr[2] = new int[3];

                     int sum = 0;

      for (int i = 0; i < 3; ++i)

          for (int j = 0; j < i + 1; ++j)

                    arr[i][j] = j + 1;

      for (int i = 0; i < 3; ++i)

          for (int j = 0; j < i + 1; ++j)

                    sum + = arr[i][j];

      System.out.print(sum);

                }

    }

A.
11
B.
10
C.
13
D.
14

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains 1 element, 2nd row contains 2 elements and 3rd row contains 3 elements. each element of array is given i + j value in loop. sum contains addition of all the elements of the array.

6.

What is the output of this program?

    class Modulus {

        public static void main(String args[])

        {

                 double a = 25.64;

             int  b = 25;

             a = a % 10;

             b = b % 10;

             System.out.println(a + " "  + b);

        }

    }

A.
5.640000000000001 5
B.
5.640000000000001 5.0
C.
5 5
D.
5 5.640000000000001

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Modulus operator returns the remainder of a division operation on the operand. a = a % 10 returns 25.64 % 10 i:e 5.640000000000001. Similarly b = b % 10 returns 5.

7.

What is the output of this program?

    class bitwise_operator {

        public static void main(String args[])

        {

             int a = 3;

             int b = 6;

             int c = a | b;

             int d = a & b;

             System.out.println(c + " "  + d);

        }

    }

A.
7 2
B.
7 7
C.
7 5
D.
5 2

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if any bit of the two operands in 1.

8.

What is the output of this program?

    class bool_operator {

        public static void main(String args[])

        {

                 boolean a = true;

             boolean b = !true;

             boolean c = a | b;

             boolean d = a & b;

             boolean e = d ? b : c;

             System.out.println(d + " " + e);

        }

    }

A.
false false
B.
true ture
C.
true false
D.
false true

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Operator | returns true if any one operand is true, thus ‘c = true | false’ is true. Operator & returns a true if both of the operand is true thus d is false. Ternary operator ?: assigns left of ‘:’ if condition is true and right hand of ‘:’ if condition is false. d is false thus e = d ? b : c , assigns c to e , e contains true.

9.

What is the output of this program?

    class operators {

        public static void main(String args[])

        {

                 int x = 8;

             System.out.println(++x * 3 + " " + x);

        }

    }

A.
24 8
B.
24 9
C.
27 8
D.
27 9

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Operator ++ has higher precedence than multiplication operator, *, x is incremented to 9 than multiplied with 3 giving 27.

10.

What is the output of this program?

    class comma_operator {

        public static void main(String args[])

        {

                 int sum = 0;

             for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)

                 sum += i;

     System.out.println(sum);

        }

    }

A.
5
B.
6
C.
14
D.
compilation error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Using comma operator , we can include more than one statement in the initialization and iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value – 0,1,2,3,4 & j gets the values -0,1,2,3,4,5.


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