358). 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()
Show Answer
Correct Answer: getChars()
Notes:
Answer: d
Explanation: None.
359). 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()
Show Answer
Correct Answer: getBytes()
Notes:
Answer: a
Explanation: getBytes() stores the character in an array of bytes. It uses default character to byte conversions provided by the platform.
360). In the following Java code, what can directly access and change the value of the variable name?
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
Show Answer
Correct Answer: any class in the test package
Notes:
Answer: c
Explanation: Any class in the test package can access and change name.
361). 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
Show Answer
Correct Answer: An IllegalStateException occurs at runtime
Notes:
Answer: d
Explanation: Because we are performing operation on reference variable which is null.
362). Which of these methods can be used to convert all characters in a String into a character array?
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]charAt()
[B] both getChars() & charAt()
[C]both toCharArray() & getChars()
[D] all of the mentioned
Show Answer
Correct Answer: both toCharArray() & getChars()
Notes:
Answer: c
Explanation: charAt() return one character only not array of character.
363). 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
Show Answer
Correct Answer: llo i l
Notes:
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
364). 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
Show Answer
Correct Answer: 6 4 6 9
Notes:
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
365). 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 < 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] 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
Show Answer
Correct Answer: a is a lower case Letter A is an upper case Letter
Notes:
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
366). 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);
}
}
Show Answer
Correct Answer: e
Notes:
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