Log In Register

Units


Java String - Questions and Answers

 

Practice these Java String under Java Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on Java String for following purpose.


– Anyone who is wishing to increase their perfect knowledge of Java String of Java Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge Java Programming specifically Java String.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on Java String under Java Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  Java String topic of Java Programming:

 

1. What is the value returned by unction compareTo() if the invoking string is less than the string compared?

Answer: Option B

Explanation:
compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.

2. What will be the output of the following program code? public class Test{ public static void main(String args[]){ String s = "what"; StringBuffer sb = new StringBuffer("what"); System.out.print(sb.equals(s)+","+s.equals(sb)); } }

Answer: Option D

Explanation:

3.

What could be output of the following fragment of code?

public class Test{
        public static void main(String args[]){ 
		String x = "hellow";
		int y = 9;
		System.out.println(x += y);
        } 
}

Answer: Option B

Explanation:

4.  Which of these operators can be used to concatenate two or more String objects?

Answer: Option A

Explanation:
operator + is used to concatenate strings, Example String s = “i ” + “like ” + “java”; String s contains “I like java”.

5. Which of these method of class String is used to check weather a given object starts with a particular string literal?

Answer: Option A

Explanation:
Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is specialized form of method regionMatches()

6. Which of these is an oncorrect statement?

Answer: Option C

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

7. How many objects will be created? String a = new String("TestMirror"); String b = new String("TestMirror"); String c = "TestMirror"; String d = "TestMirror";

Answer: Option B

Explanation:
Object will be created each time whenever we use new keyword. So, 2 object will be created simply for the first two line and matter is with remaining two bottom line. String c="TestMirror" creates an object and store it in String pool, next time when we are writing String d="TestMirror" it will first check in String pool whether object already exists or not. Since, it is existing, no new object will be created. Hence reference "d" points to existing object "TestMirror". So ultimately 3 object wi

8.
String str1 = "Kolkata".replace('k', 'a');

In the above statement, the effect on string Kolkata is 

Answer: Option B

Explanation:

replace() method returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
Syntax: Here is the syntax of this method:
public String replace(char oldChar, char newChar)
Parameters: Here is the detail of the parameters:
oldChar -- the old character. newChar -- the new character.
Return Value: It returns a string derived from this string by replacing every occurrence of oldChar with

9. Determine output: public class Test{ public static void main(String args[]){ String str = null; if(str.length() == 0){ System.out.print("1"); } else if(str == null){ System.out.print("2"); } else{ System.out.print("3"); } } }

Answer: Option D

Explanation:

10. Which of the following statements are incorrect?

Answer: Option B

Explanation:
Strings in Java are immutable that is they can not be modified.

11. String in Java is a?

Answer: Option A

Explanation:

12. What could be output of the following fragment of code? public class Test{ public static void main(String args[]){ String x = "hellow"; int y = 9; System.out.println(x += y); } }

Answer: Option B

Explanation:

13. What will be the output? 1. public class Test{ 2. public static void main(String args[]){ 3. Object myObj = new String[]{"one", "two", "three"}; 4. { 5. for(String s : (String[])myObj) 6. System.out.print(s + "."); 7. } 8. } 9. }

Answer: Option A

Explanation:

14.

What will be the output?

String str1 = "abcde";
System.out.println(str1.substring(1, 3));

Answer: Option B

Explanation:

substring() method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or upto endIndex - 1 if second argument is given.
Syntax: Here is the syntax of this method:
public String substring(int beginIndex)
or
public String substring(int beginIndex, int endIndex)
Parameters: Here is th

15.

What is the output of the following println statement?

String str1 = "Hellow";
System.out.println(str1.indexOf('t'));

Answer: Option D

Explanation: