Java MCQs 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) any class Answer: c 4. What will be the output of the following Java code? a) The value “4” is printed at the command line Answer: d 5. Which of these methods can be used to convert all characters in a String into a character array? Answer: c 6. What will be the output of the following Java code? a) Hello, i love java 7. What will be the output of the following Java code? a) 6 4 6 9 8. What will be the output of the following Java code? a) b) c) d)
a) getchars()
b) GetChars()
c) Getchars()
d) getChars()
Clarification: None.
a) getBytes()
b) GetByte()
c) giveByte()
d) Give Bytes()
Clarification: 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";
}
b) only the Target class
c) any class in the test package
d) any class that extends Target
Clarification: Any class in the test package can access and change name.
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));
}
}
b) Compilation fails because of an error in line
c) A NullPointerException occurs at runtime
d) An IllegalStateException occurs at runtime
Clarification: Because we are performing operation on reference variable which is null.
a) charAt()
b) both getChars() & charAt()
c) both toCharArray() & getChars()
d) all of the mentioned
Clarification: charAt() return one character only not array of character.
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);
}
}
b) i love ja
c) lo i lo
d) llo i l
Clarification: 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
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'));
}
}
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9
Clarification: 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
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 is a lower case Letter
is White space character
b is a lower case Letter
is White space character
a is a lower case Letter
A is an upper case Letter
a is a lower case Letter
0 is a digit
Clarification: 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?
-
class output -
{ -
public static void main(String args[])
-
{ -
char ch;
-
ch = "hello".charAt(1);
-
System.out.println(ch);
-
} -
}
a) h
b) e
c) l
d) o
Clarification: “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
