Java Language | 30 Minute‐Test 5


Instruction

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

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 obj = new box();

             obj.width = 10;

             obj.height = 2;

             obj.length = 10;

             int y = obj.width * obj.height * obj.length;

             System.out.print(y);

        }

    }

A.
12
B.
200
C.
400
D.
100

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

None.

12.

What is the output of this program?

    class box {

        int width;

        int height;

        int length;

        int volume;

        void volume() {

             volume = width*height*length;

        }

    }

        class Output {

        public static void main(String args[])

        {

            box obj = new box();

            obj.height = 1;

            obj.length = 5;

            obj.width = 5;

            obj.volume();

            System.out.println(obj.volume);

                }

    }

A.
0
B.
1
C.
25
D.
26

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

13.

What is the output of this program?

    class box {

        int width;

        int height;

        int length;

        int volume;

        void finalize() {

            volume = width*height*length;

            System.out.println(volume);

        }

        protected void volume() {

            volume = width*height*length;

            System.out.println(volume);

       }

    } 

       class Output {

        public static void main(String args[])

        { 

           box obj = new box();

           obj.width=5; 

           obj.height=5; 

           obj.length=6; 

           obj.volume();  

      }   

  }

A.
150
B.
200
C.
Run time error
D.
Compilation error

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

None.

14.

What is the output of this program?

   class overload {

        int x; double y;

        void add(int a , int b) {

            x = a + b;

        }

        void add(double c , double d){

            y = c + d;

        }

        overload() {

            this.x = 0;

            this.y = 0;

        } 

           } 

       class Overload_methods { 

       public static void main(String args[])  

      {  

          overload obj = new overload();

               int a = 2;

            double b = 3.2;

            obj.add(a, a);

            obj.add(b, b);

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

             }

   }

A.
6 6
B.
6.4 6.4
C.
6.4 6
D.
4 6.4

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

For obj.add(a,a); ,the function in line number 4 gets executed and value of x is 4. For the next function call, the function in line number 7 gets executed and value of y is 6.4

15.

What is the output of this program?

    class static_out {

        static int x; static int y;

        void add(int a, int b){

            x = a + b;

            y = x + b;

        }

    }

        class static_use {

        public static void main(String args[])

        {

            static_out obj1 = new static_out();

            static_out obj2 = new static_out();

               int a = 2;

            obj1.add(a, a + 1);

            obj2.add(5, a);

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

             }

   }

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

16.

What is the output of this program?

    class static_out {

        static int x; static int y;

        void add(int a , int b){

            x = a + b;

            y = x + b; 

       }

    }  

      class static_use {

        public static void main(String args[])

        { 

           static_out obj1 = new static_out();

            static_out obj2 = new static_out();

               int a = 2;

            obj1.add(a, a + 1);

            obj2.add(5, a);

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

             } 

 }

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

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.length());

        }

    }

A.
9
B.
10
C.
11
D.
12

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

18.

What is the output of this program?

    class A {

        public int i;

        private int j;

    }   

    class B extends A {

        void display() {

            super.j = super.i + 1;

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

        }

    }   

    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.
Runtime Error
D.
Compilation Error

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.

19.

What is the output of this program?

   final class A {

         int i;

    }   

    class B extends A {

        int j;

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

    }   

    class inheritance {

        public static void main(String args[])

        {

            B obj = new B();

            obj.display();    

        }

   }

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

class A has been declared final hence it cannot be inherited by any other class. Hence class B does not have member i, giving compilation error.

20.

What is the output of this program?

   class A {

  int i;

  int j;

        A() {

            i = 1;

            j = 2;

        }

   }

   class Output {

        public static void main(String args[])

        {

             A obj1 = new A();

             A obj2 = new A();

       System.out.print(obj1.equals(obj2));

        }

   }

A.
false
B.
true
C.
1
D.
Compilation Error

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

obj1 and obj2 are two different objects. equals() is a method of Object class, Since Object class is superclass of every class it is available to every object.

21.

What is the output of this program?

class String_demo {

        public static void main(String args[])

        {

            int ascii[] = { 65, 66, 67, 68};

            String s = new String(ascii, 1, 3);

            System.out.println(s);

        }

   }

A.
ABC
B.
BCD
C.
CDA
D.
ABCD

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

ascii is an array of integers which contains ascii codes of Characters A, B, C, D. String(ascii, 1, 3) is an constructor which initializes s with Characters corresponding to ascii codes stored in array ascii, starting position being given by 1 & ending position by 3, Thus s stores BCD.

22.

What is the output of this program?

    class output {

        public static void main(String args[])

        {

            char c[]={'a', '1', 'b' ,' ' ,'A' , '0'};

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

            {

                   if(Character.isDigit(c[i]))

                       System.out.println(c[i]+" is a digit");

                   if(Character.isWhitespace(c[i]))

                       System.out.println(c[i]+" is a Whitespace character");

                   if(Character.isUpperCase(c[i]))

                       System.out.println(c[i]+" is an Upper case Letter");

                   if(Character.isLowerCase(c[i]))

                       System.out.println(c[i]+" is a lower case Letter");

               i=i+3;

            }

        }

    }

A.
a is a lower case Letter
is White space character
B.
b is a lower case Letter
is White space character
C.
a is a lower case Letter
A is a upper case Letter
D.
a is a lower case Letter
0 is a digit

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i]) are the function of library java.lang. They are used to find weather the given character is of specified type or not. They return true or false i:e Boolean variable.

23.

What is the output of this program?

    class output {

        public static void main(String args[])

        {

           String s1 = "Hello";

           String s2 = s1.replace('l','w');

           System.out.println(s2);

        }

    }

A.
hello
B.
helwo
C.
hewlo
D.
hewwo

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

replace() method replaces all occurrences of one character in invoking string with another character. s1.replace(‘l’,’w’) replaces every occurrence of ‘l’ in hello by ‘w’, giving hewwo.

24.

What is the output of this program?

    class output {

        public static void main(String args[])

        {

             StringBuffer c = new StringBuffer("Hello");

             StringBuffer c1 = new StringBuffer(" World");

             c.append(c1);

             System.out.println(c);

        }

    }

A.
Hello
B.
World
C.
Helloworld
D.
Hello World

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.

25.

What is the output of this program?

    class output {

        public static void main(String args[])

        {

           StringBuffer s1 = new StringBuffer("Hello");

           s1.setCharAt(1,'x');

           System.out.println(s1);

        }

    }

A.
xello
B.
xxxxx
C.
Hxllo
D.
Hexlo

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

26.

What is the output of this program?

    class output {

        public static void main(String args[])

        {

           String s1 = "Hello";

           String s2 = new String(s1);

           String s3 = "HELLO";

           System.out.println(s1.equals(s2) + " " + s2.equals(s3));

        }

    }

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

27.

What is the output of this program?

    package pkg;

    class display {

        int x;

        void show() {

            if (x > 1)

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

        }

    }

    class packages {

        public static void main(String args[]) {

            display[] arr=new display[3];

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

                arr[i]=new display();

            arr[0].x = 0;     

            arr[1].x = 1;

            arr[2].x = 2;

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

                arr[i].show();

         }

    }

Note : packages.class file is in directory pkg;

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

28.

What is the output of this program?

    interface calculate {

        void cal(int item);

    }

    class display implements calculate {

        int x;

        public void cal(int item) {

            x = item * item;           

        }

    }

    class interfaces {

        public static void main(String args[]) {

            display arr = new display;

            arr.x = 0;     

            arr.cal(2);

            System.out.print(arr.x);

        }

    }

A.
0
B.
2
C.
4
D.
None of the mentioned

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

None.

29.

What is the output of this program?

    class isinfinite_output {

        public static void main(String args[]) {

            Double d = new Double(1 / 0.); 

            boolean x = d.isInfinite();

            System.out.print(x);

        }

    }

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

isInfinite() method returns true is the value being tested is infinitely large or small in magnitude. 1/0. is infinitely large in magnitude hence true is stored in x.

30.

What is the output of this program?

    class Output {

        public static void main(String args[]) {

            Integer i = new Integer(257); 

            byte x = i.byteValue();

            System.out.print(x);

        }

    }

A.
0
B.
1
C.
256
D.
257

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

i.byteValue() method returns the value of wrapper i as a byte value. i is 257, range of byte is 256 therefore i value exceeds byte range by 1 hence 1 is returned and stored in x.

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