Java Language | 10 Minute‐Test 27


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 String_demo {

        public static void main(String args[])

        {

            char chars[] = {'a', 'b', 'c'};

            String s = new String(chars);

            String s1 = "abcd";

            int len1 = s1.length();

            int len2 = s.length();

            System.out.println(len1 + " " + len2);

        }

   }

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

None.

2.

What is the output of this program?

    class String_demo {

        public static void main(String args[])

        {

            char chars[] = {'a', 'b', 'c'};

            String s = new String(chars);

            System.out.println(s);

        }

   }

A.
a
B.
b
C.
c
D.
abc

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “abc”.

3.

What is the output of this program?

    class output {

        public static void main(String args[])

        {

           String s1 = "Hello World";

           String s2 = s1.substring(0 , 4);

           System.out.println(s2);

        }

   }

A.
Hell
B.
Hello
C.
Worl
D.
World

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

substring(0,4) returns the character from 0 th position to 3 rd position.

4.

What is the output of this program?

    class output {

        public static void main(String args[])

        {

           StringBuffer s1 = new StringBuffer("Hello");

           StringBuffer s2 = s1.reverse();

           System.out.println(s2);

        }

    }

A.
Hello
B.
olleH
C.
HelloolleH
D.
olleHHello

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

reverse() method reverses all characters. It returns the reversed object on which it was called.

5.

What is the output of this program?

    class output {

        public static void main(String args[])

        {

           StringBuffer s1 = new StringBuffer("Hello World");

           s1.insert(6 , "Good ");

           System.out.println(s1);

        }

   }

A.
HelloGoodWorld
B.
HellGoodoWorld
C.
HellGood oWorld
D.
Hello Good World

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The insert() method inserts one string into another. It is overloaded to accept values of all simple types, plus String and Objects. Sting is inserted into invoking object at specified position. “Good ” is inserted in “Hello World” T index 6 giving “Hello Good World”.

6.

In the below code, which code fragment should be inserted at line 3 so that the output will be: “123abc 123abc”?

  1. StringBuilder sb1 = new StringBuilder("123");
  2. 2 String s1 = "123";
  3. 3 // insert code here
  4. 4 System.out.println(sb1 + " " + s1);

 

A.
sb1.append(“abc”); s1.append(“abc”);
B.
sb1.append(“abc”); s1.concat(“abc”);
C.
sb1.concat(“abc”); s1.append(“abc”);
D.
sb1.append(“abc”); s1 = s1.concat(“abc”);

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

append() is stringbuffer method and concat is String class method. append() is stringbuffer method and concat is String class method.

7.

What is the output of this program?

    package pkg;

    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.

8.

What is the output of this program?

    interface calculate {

        void cal(int item);

    }

    class displayA implements calculate {

        int x;

        public void cal(int item) {

            x = item * item;           

        }

    }

    class displayB implements calculate {

        int x;

        public void cal(int item) {

            x = item / item;            

        }

    }

    class interfaces {

        public static void main(String args[]) {

            displayA arr1 = new displayA;

            displayB arr2 = new displayB;

            arr1.x = 0;

            arr2.x = 0;     

            arr1.cal(2);

            arr2.cal(2);

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

        }

    }

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

class displayA implements the interface calculate by doubling the value of item, where as class displayB implements the interface by dividing item by item, therefore variable x of class displayA stores 4 and variable x of class displayB stores 1.

9.

What is the output of this program?

    class isNaN_output {

        public static void main(String args[]) {

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

            boolean x = d.isNaN();

            System.out.print(x);

        }

    }

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

isisNaN() method returns true is the value being tested is a number. 1/0. is infinitely large in magnitude, which cant not be defined as a number hence false is stored in x.

10.

What is the output of this program?

    class Output {

        public static void main(String args[]) {

            Integer i = new Integer(257); 

            float x = i.floatValue();

            System.out.print(x);

        }

    }

A.
0
B.
1
C.
257
D.
257.0
Submit your test now to view the Results and Statistics with answer explanation.