Characters
- What is the output of this program?
public class Result
{
public static void main(String args[])
{
char chars;
chars = "hello".charAt(0);
System.out.println(chars);
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
“hello” is a String literal, method charAt() returns the character specified at the index position. Character at index position 0th is h of hello, hence chars contains h.
- What is the output of this program?
public class output
{
public static void main(String args[])
{
char ch[]={'p', '1', 'b' ,' ' ,'P' , '0'};
for (int k = 0; k < 5; ++k)
{
if(Character.isDigit(ch[k]))
System.out.println(ch[k]+" is a digit");
if(Character.isWhitespace(ch[k]))
System.out.println(ch[k]+" is a Whitespace character");
if(Character.isUpperCase(ch[k]))
System.out.println(ch[k]+" is an Upper case Letter");
if(Character.isLowerCase(ch[k]))
System.out.println(ch[k]+" is a lower case Letter");
k=k+3;
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Character.isDigit(ch[k]),Character.isUpperCase(ch[k]),Character.isWhitespace(ch[k]) 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.
- What is the output of this program?
public class Result
{
public static void main(String args[])
{
String a = "hello i love interview mania";
System.out.println(a.indexOf('i')+" "+a.indexOf('o') +" "+a.lastIndexOf('i')+" "+a.lastIndexOf('o'));
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
indexof(‘p’) and lastIndexof(‘p’) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by p in the given array.
- What is the output of this program?
public class Result
{
public static void main(String args[])
{
String ch = "Hello i love Interview mania";
int start = 13;
int end = 28;
char str[]=new char[end-start];
ch.getChars(start,end,str,0);
System.out.println(str);
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
getChars(start,end,str,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 str.
- Which of these methods can be used to convert all characters in a String into a character array?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
charAt() return one character only not array of character.