Java Language | 20 Minute‐Test 3


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 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.

2.

What is the output of this program?

    class booloperators {

        public static void main(String args[])

        {

            boolean var1 = true;

      boolean var2 = false;

      System.out.println((var2 & var2));

        }

    }

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

boolean ‘&’ operator always returns true or false. var1 is defined true and var2 is defined false hence their ‘&’ operator result is false.

3.

Which of these is incorrect string literal?

A.
“Hello World”
B.
“Hello\nWorld”
C.
“\”Hello World\””
D.
“Hello
world”

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

all string literals must begin and end in same line.

4.

What is the output of this program, if we run as “java main_arguments 1 2 3”?

    class main_arguments {

        public static void main(String [] args)

        {

            String [][] argument = new String[2][2];

            int x;

            argument[0] = args;

            x = argument[0].length;

            for (int y = 0; y < x; y++)

                System.out.print(" " + argument[0][y]);

                      }

    }

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

In argument[0] = args;, the reference variable arg[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements.

5.

What is the output of this program?

    class array_output {

        public static void main(String args[])

        {

            char array_variable [] = new char[10];

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

                array_variable[i] = 'i';

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

            }

        }

    }

A.
1 2 3 4 5 6 7 8 9 10
B.
0 1 2 3 4 5 6 7 8 9 10
C.
i j k l m n o p q r
D.
i i i i i i i i i i

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

None.

6.

Can 8 byte long data type be automatically type cast to 4 byte float data type?

A.
true
B.
false
C.
true and false
D.
true or false

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Both data types have different memory representation that’s why 8-byte integral data type can be stored to 4-byte floating point data type.

7.

What is the output of this program?

    class rightshift_operator {

        public static void main(String args[])

        {

             int x;

             x = 10;

             x = x >> 1;

             System.out.println(x);

        }

    }

A.
10
B.
5
C.
2
D.
20

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Right shift operator, >>, devides the value by 2.

8.

What is the output of this program?

    class Output {

        public static void main(String args[])

        {

                 int x , y = 1;

             x = 10;

             if (x != 10 && x / 0 == 0)

                 System.out.println(y);

             else

                 System.out.println(++y);

        }

    }

A.
1
B.
2
C.
Runtime error owing to division by zero in if condition.
D.
Unpredictable behavior of program.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error.

9.

Which of these lines of code will give better performance?
1. a | 4 + c >> b & 7;
2. (a | ((( 4 * c ) >> b ) & 7 ))

A.
1 will give better performance as it has no parentheses.
B.
2 will give better performance as it has parentheses.
C.
Both 1 & 2 will give equal performance.
D.
Dependent on the computer system.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Parentheses do not degrade the performance of the program. Adding parentheses to reduce ambiguity does not negatively affect your system.

10.

What is the output of this program?

class Output {

        public static void main(String args[])

        {

               final int a=10,b=20;

          while(a<b)

          {

           System.out.println("Hello");

          }

          System.out.println("World");

         }

    }

A.
Hello
B.
run time error
C.
Hello world
D.
compile time error

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Every final variable is compile time constant.

11.

What is the output of this program?

    class box {

        int width;

        int height;

        int length;

    }     class mainclass {

        public static void main(String args[])

        {

                    box obj1 = new box();

            box obj2 = new box();

            obj1.height = 1;

            obj1.length = 2;

            obj1.width = 1;

            obj2 = obj1;

            System.out.println(obj2.height);

        }

    }

A.
1
B.
2
C.
Runtime error
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

When we assign an object to another object of same type, all the elements of right side object gets copied to object on left side of equal to, =, operator.

12.

In the below code, which call to sum() method is appropriate?

class Output {

         public static int sum(int ...x)

        {

             return;

        }

        static void main(String args[])

        {

                 sum(10);

             sum(10,20);

             sum(10,20,30);

             sum(10,20,30,40);

        }

}

A.
only sum(10)
B.
only sum(10,20)
C.
only sum(10) & sum(10,20)
D.
all of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

sum is a variable argument method and hence it can take any number as argument.

13.

Which of the following statements are incorrect?

A.
default constructor is called at the time of object declaration
B.
Constructor can be parameterized
C.
finalize() method is called when a object goes out of scope and is no longer needed
D.
finalize() method must be declared protected

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

finalize() method is called just prior to garbage collection. it is not called when object goes out of scope.

14.

What is the output of this program?

    class test {

        int a;

        int b;

        void meth(int i , int j) {

            i *= 2;

            j /= 2;

        }

              }

        class Output {

        public static void main(String args[])

        {

            test obj = new test();

      int a = 10;

            int b = 20;

                         obj.meth(a , b); 

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

               }  

   }

A.
10 20
B.
20 10
C.
20 40
D.
40 20

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Variables a & b are passed by value, copy of their values are made on formal parameters of function meth() that is i & j. Therefore changes done on i & j are not reflected back on original arguments. a & b remain 10 & 20 respectively.

15.

Which of these access specifier must be used for class so that it can be inherited by another sub class?

A.
public
B.
private
C.
protected
D.
None of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

None.

16.

What is the output of this program?

    class Output {

        public static void main(String args[])

        {

            int arr[] = {1, 2, 3, 4, 5};

            for ( int i = 0; i < arr.length - 2; ++i)

                System.out.println(arr[i] + " ");

        }

    }

A.
1 2
B.
1 2 3
C.
1 2 3 4
D.
1 2 3 4 5

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

arr.length() is 5, so the loop is executed for three times.

17.

What is the output of this program?

    class string_class

{

        public static void main(String args[])

        {

            String obj = "hello";

            String obj1 = "world";

               String obj2 = obj;

            obj2 = " world";

            System.out.println(obj + " " + obj2);

        }

    }

A.
hello hello
B.
world world
C.
hello world
D.
world hello

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

18.

What is the output of this program?

    class A {

        public int i;

        public int j;

        A() {

            i = 1;

            j = 2;

  }

    }   

    class B extends A {

        int a;

        B() {

            super();

        }

    }   

    class super_use {

        public static void main(String args[])

        {

            B obj = new B();

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

        }

   }

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.

19.

What is the output of this program?

  class Abc

  {

      public static void main(String[]args)

      {

          String[] elements = { "for", "tea", "too" };

          String first = (elements.length > 0) ? elements[0]: null;

      }

  }

A.
Compilation error
B.
An exception is thrown at run time
C.
The variable first is set to null
D.
The variable first is set to elements[0].

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The value at the 0th position will be assigned to the variable first.

20.

What is the output of this program?

    class Output {

        public static void main(String args[])

        {

             Object obj = new Object();

       System.out.print(obj.getclass());

        }

    }

A.
Object
B.
class Object
C.
class java.lang.Object
D.
Compilation Error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

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