Java Questions and Answers Part-16

1. Which of these is an incorrect statement?
a) String objects are immutable, they cannot be changed
b) String object can point to some other reference of String variable
c) StringBuffer class is used to store string in a buffer for later use
d) None of the mentioned

Answer: c
Explanation: StringBuffer class is used to create strings that can be modified after they are created.

2. What will be the output of the following Java program?
class String_demo
{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
System.out.println(s);
}
}
a) a
b) b
c) c
d) abc

Answer: d
Explanation: String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “abc”.
output:
abc

3. What will be the output of the following Java program?
class String_demo
{
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68};
String s = new String(ascii, 1, 3);
System.out.println(s);
}
}
a) ABC
b) BCD
c) CDA
d) ABCD

Answer: b
Explanation: ascii is an array of integers which contains ascii codes of Characters A, B, C, D. String(ascii, 1, 3) is an constructor which initializes s with Characters corresponding to ascii codes stored in array ascii, starting position being given by 1 & ending position by 3, Thus s stores BCD.
output:
BCD

4. What will be the output of the following Java program?
class String_demo
{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
String s1 = "abcd";
int len1 = s1.length();
int len2 = s.length();
System.out.println(len1 + " " + len2);
}
}
a) 3 0
b) 0 3
c) 3 4
d) 4 3

Answer: d
Explanation:
output:
4 3

5. 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()

Answer: d
Explanation: getChars()

6. 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()

Answer: a
Explanation: getBytes() stores the character in an array of bytes. It uses default character to byte conversions provided by the platform.

7. 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

Answer: c
Explanation: Any class in the test package can access and change name.

8. 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

Answer: d
Explanation: Because we are performing operation on reference variable which is null.

9. 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

Answer: c
Explanation: charAt() return one character only not array of character.

10. 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

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:
llo i l