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 < 5; ++i)
  7.             {
  8.                    if(Character.isDigit(c[i]))
  9.                        System.out.println(c[i]+" is a digit");
  10.                    if(Character.isWhitespace(c[i]))
  11.                        System.out.println(c[i]+" is a Whitespace character");
  12.                    if(Character.isUpperCase(c[i]))
  13.                        System.out.println(c[i]+" is an Upper case Letter");
  14.                    if(Character.isLowerCase(c[i]))
  15.                        System.out.println(c[i]+" is a lower case Letter");
  16.                i=i+3;
  17.             }
  18.         }
  19.     }

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 an upper case Letter

d)

    a is a lower case Letter
    0 is a digit

View Answer

Answer: 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.
Output:

$ javac output.java
$ java output
a is a lower case Letter
A is an Upper Case Letter

  

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

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             char ch;
  6.             ch = "hello".charAt(1);
  7.             System.out.println(ch);
  8.         }
  9.    }

a) h
b) e
c) l
d) o
View Answer

Answer: b
Explanation: “hello” is a String literal, method charAt() returns the character specified at the index position. Character at index position 1 is e of hello, hence ch contains e.
output:

$ javac output.java
$ java output 
e

Sanfoundry Global Education & Learning Series – Java Programming Language.

Next Steps:

  • Get Free Certificate of Merit in Java Programming
  • Participate in Java Programming Certification Contest
  • Become a Top Ranker in Java Programming
  • Take Java Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

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

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

How do I convert a string to an array of arrays?

There are four ways to convert a String into String array in Java:.
Using String. split() Method..
Using Pattern. split() Method..
Using String[ ] Approach..
Using toArray() Method..

Which methods can be used to convert all characters?

upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.

Which method converts string into array?

String class split(String regex) can be used to convert String to array in java. If you are working with java regular expression, you can also use Pattern class split(String regex) method. Let's see how to convert String to an array with a simple java class example.

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

Which of these methods can be used to convert all characters in a String into a character array? Explanation: charAt() return one character only not array of character.