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? Answer: d 2. Which of these methods is an alternative to getChars[] that stores the characters in an array of bytes? Answer: a 3.
In the following Java code, what can directly access and change the value of the variable name?
a] getchars[]
b] GetChars[]
c] Getchars[]
d] getChars[]
View
Answer
Explanation: None.
a] getBytes[]
b] GetByte[]
c] giveByte[]
d] Give Bytes[]
View Answer
Explanation: getBytes[] stores the character in an array of bytes. It uses default character to byte conversions provided by the platform. package test;
class Target
{
public String name = "hello";
}
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?
public class Boxer1
{
Integer i;
int x;
public Boxer1[int y]
{
x = i+y;
System.out.println[x];
}
public static void main[String[] args]
{
new Boxer1 [new Integer[4]];
}
}
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?
class output
{
public static void main[String args[]]
{
String c = "Hello i love java";
int start = 2;
int end = 9;
char s[]=new char[end-start];
c.getChars[start,end,s,0];
System.out.println[s];
}
}
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?
class output
{
public static void main[String args[]]
{
String a = "hello i love java";
System.out.println[a.indexOf['i']+" "+a.indexOf['o'] +" "+a.lastIndexOf['i']+" "+a.lastIndexOf['o']];
}
}
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?
class output
{
public static void main[String args[]]
{
char c[]={'a', '1', 'b' ,' ' ,'A' , '0'};
for [int i = 0; i