Java Language | 20 Minute‐Test 1


Instruction

  • Total number of questions : 20.
  • Time alloted : 20 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.

11.

Which of the following statements is correct?

A.
Public method is accessible to all other classes in the hierarchy
B.
Public method is accessible only to subclasses of its parent class
C.
Public method can only be called by object of its class.
D.
Public method can be accessed by calling object of the public class.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

None.

12.

What is the output of this program?

    class equality {

        int x;

        int y;

        boolean isequal(){

            return(x == y);

          }

    }

        class Output {

        public static void main(String args[])

        {

            equality obj = new equality();

            obj.x = 5;

            obj.y = 5;

            System.out.println(obj.isequal());

        }

    }

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

None.

13.

What is the output of this program?

class San{

     San()throws IOException

     {

      }

 }

class Foundry extends San{

     Foundry()

     {

      }

     public static void main(String[]args)

     {

      }

}

A.
compile time error
B.
run time error
C.
compile and runs fine
D.
unreported exception java.io.IOException in default constructor

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

If parent class constructor throws any checked exception, compulsory child class constructor should throw the same checked exception as its parent, otherwise code won’t compile.

14.

What is the output of this program?

    class overload {

        int x; int y;

        void add(int a){ 

           x =  a + 1; 

       }    

    void add(int a , int b){  

          x =  a + 2;   

     }    

        }      

  class Overload_methods {    

    public static void main(String args[])  

      {      

      overload obj = new overload();    

           int a = 0;    

        obj.add(6, 7);  

          System.out.println(obj.x);   

          } 

 }

A.
6
B.
7
C.
8
D.
9

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

15.

What is the output of this program?

    class access{

        public int x; private int y;

        void cal(int a, int b){

            x =  a + 1;

            y =  b;

        }

           void print() {

            system.out.println(" " + y);

             }

    }

       class access_specifier {

        public static void main(String args[])

        {

            access obj = new access();

               obj.cal(2, 3);

            System.out.println(obj.x);

            obj.print();

             }

   }

A.
2 3
B.
3 3
C.
Runtime Error
D.
Compilation Error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

None.

16.

What is the output of this program?

    class access{

       static int x;

       void increment(){

           x++;

       } 

       }

       class static_use {

        public static void main(String args[])

        {

            access obj1 = new access();

            access obj2 = new access();

            obj1.x = 0;

               obj1.increment();

            obj2.increment();

            System.out.println(obj1.x + " " + obj2.x);

            }

   }

A.
1 2
B.
1 1
C.
2 2
D.
Compilation Error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

All objects of class share same static variable, all the objects share same copy of static members, obj1.x and obj2.x refer to same element of class which has been incremented twice and its value is 2.

17.

What is the output of this program?

    class string_class {

        public static void main(String args[])

        {

            String obj = "I LIKE JAVA";

               System.out.println(obj.charAt(3));

        }

    }

A.
I
B.
L
C.
K
D.
E

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

charAt() is a method of class String which gives the character specified by the index. obj.charAt(3) gives 4th character i:e I.

18.

What is the output of this program?

    class A {

        int i;

    }   

    class B extends A {

        int j;

        void display() {

            super.i = j + 1;

            System.out.println(j + " " + i);

        }

    }   

    class inheritance {

        public static void main(String args[])

        {

            B obj = new B();

            obj.i=1;

            obj.j=2;  

            obj.display();    

        }

   }

A.
2 2
B.
3 3
C.
2 3
D.
3 2

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None

19.

What is the output of this program?

class Alligator

 {

  public static void main(String[] args)

   {

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

   int [][]y = x;

   System.out.println(y[2][1]);

   }

 }

A.
2
B.
3
C.
7
D.
Compilation Error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Both x,and y are pointing to the same array

20.

What is the output of this program?

    abstract class A {

        int i;

        abstract void display();

    }   

    class B extends A {

        int j;

        void display() {

            System.out.println(j);

        }

    }   

    class Abstract_demo {

        public static void main(String args[])

        {

            B obj = new B();

            obj.j=2;

            obj.display();   

        }

   }

A.
0
B.
2
C.
Runtime Error
D.
Compilation Error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

class A is an abstract class, it contains a abstract function display(), the full implementation of display() method is given in its subclass B, Both the display functions are the same. Prototype of display() is defined in class A and its implementation is given in class B.

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