6)
What will be the output of following program?
public class temp
{
public static void main(String args[])
{
String text="ABCD";
byte[] xByte=text.getBytes();
for(byte loop: xByte)
{
System.out.print(loop+" ");
}
}
}
- A B C D
- 41 42 43 44
- 65 66 67 68
- Error
Correct Answer: 3
65 66 67 68
Method getBytes() returns byte array of the string, string is “ABCD” so Unicode values of the string will be returned into byte array xByte (65,66,67,78). And individual value of byte array is printing through for each loop.
7)
What will be the output by following code snippet?
String text="Hello World, How are you?";
System.out.print(text.lastIndexOf('o'));
System.out.print(" "+text.lastIndexOf('o',5));
- 22 4
- 22 22
- 22 16
- None of these
Correct Answer: 1
22 4
First method text.lastIndexOf('o') will return the last index of 'o' that is 22 in the string.
Second method text.lastIndexOf('o',5) will return the last index of 'o' but this method will check till 5th index of the string (i.e. for this method string will be "Hello")
8)
What value will be printed after executing following code snippet?
String text="Hello World!";
int index= text.indexOf("llo");
System.out.println(index);
- 3
- 2
- -1
- Error
Correct Answer: 2
2
String "llo" starts with index 2 in string "Hello World!"
9)
What value will be printed after executing following code snippet?
String text="Hello World!";
int index= text.indexOf("hello");
System.out.println(index);
- 1
- 0
- -1
- Error
Correct Answer: 3
-1
String "hello" does not exist in string "Hello World!", indexOf is case sensitive so "hello" and "Hello" are different.
10)
What will be the output by following code snippet?
String Str = new String(" Include Help ");
System.out.println(Str.trim() );
- Include Help
- Include Help
- Include Help
- None of these
Correct Answer: 2
Include Help
Method trim() removes leading and preceding whitespaces.