Which methods can be used to convert all characters in a string into character array?

This section of our 1000+ Java MCQs focuses on character extraction of Java Programming Language.

1. Which of these method of class String is used to extract more than one character at a time a String object?
a] getchars[]
b] GetChars[]
c] Getchars[]
d] getChars[]
View Answer

Answer: d
Explanation: None.

2. Which of these methods is an alternative to getChars[] that stores the characters in an array of bytes?
a] getBytes[]
b] GetByte[]
c] giveByte[]
d] Give Bytes[]
View Answer

Answer: a
Explanation: getBytes[] stores the character in an array of bytes. It uses default character to byte conversions provided by the platform.

3. In the following Java code, what can directly access and change the value of the variable name?

  1.  package test;
  2.  class Target 
  3.  {
  4.   public String name = "hello";
  5.  }

a] any class
b] only the Target class
c] any class in the test package
d] any class that extends Target
View Answer

Answer: c
Explanation: Any class in the test package can access and change name.

4. What will be the output of the following Java code?

  1. public class Boxer1 
  2. {
  3.     Integer i;
  4.     int x;
  5.    public Boxer1[int y] 
  6.    {
  7.         x = i+y;
  8.         System.out.println[x];
  9.    }
  10.    public static void main[String[] args] 
  11.    {
  12.        new Boxer1 [new Integer[4]];
  13.    }
  14. }

a] The value “4” is printed at the command line
b] Compilation fails because of an error in line
c] A NullPointerException occurs at runtime
d] An IllegalStateException occurs at runtime
View Answer

Answer: d
Explanation: Because we are performing operation on reference variable which is null.

5. Which of these methods can be used to convert all characters in a String into a character array?
a] charAt[]
b] both getChars[] & charAt[]
c] both toCharArray[] & getChars[]
d] all of the mentioned
View Answer

Answer: c
Explanation: charAt[] return one character only not array of character.

6. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main[String args[]]
  4.         { 
  5.            String c = "Hello i love java";
  6.            int start = 2;
  7.            int end = 9;
  8.            char s[]=new char[end-start];
  9.            c.getChars[start,end,s,0];
  10.            System.out.println[s];
  11.         }
  12.     }

a] Hello, i love java
b] i love ja
c] lo i lo
d] llo i l
View Answer

Answer: d
Explanation: getChars[start,end,s,0] returns an array from the string c, starting index of array is pointed by start and ending index is pointed by end. s is the target character array where the new string of letters is going to be stored and the new string will be stored from 0th position in s.
Output:

$ javac output.java
$ java output
llo i l

7. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main[String args[]]
  4.         {
  5.             String a = "hello i love java";
  6.             System.out.println[a.indexOf['i']+" "+a.indexOf['o'] +" "+a.lastIndexOf['i']+" "+a.lastIndexOf['o']];
  7.         }
  8.     }

a] 6 4 6 9
b] 5 4 5 9
c] 7 8 8 9
d] 4 3 6 9
View Answer

Answer: a
Explanation: indexof[‘c’] and lastIndexof[‘c’] are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
Output:

$ javac output.java
$ java output
6 4 6 9

8. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main[String args[]]
  4.         {
  5.             char c[]={'a', '1', 'b' ,' ' ,'A' , '0'};
  6.             for [int i = 0; i 

Chủ Đề