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

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. Which of these methods is used to compare a specific region inside a string with another specific region in another string?

Answer: Option D

Explanation:

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

5. The output of the following fraction of code is public class Test{ public static void main(String args[]){ String s1 = new String("Hello"); String s2 = new String("Hellow"); System.out.println(s1 = s2); } }

Answer: Option B

Explanation:

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

7.

What will be output?

String S1 = "S1 ="+ "123"+"456";
String S2 = "S2 ="+(123+456);

Answer: Option A

Explanation:

If a number is quoted in "" then it becomes a string, not a number any more.

8.

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 E

Explanation:

9.

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

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

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

12.

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:

13.

What is the output of the following println statement?

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

Answer: Option D

Explanation:

14. How many Constructor String class have?

Answer: Option

Explanation:

15.

What will be the output of the following program code?

class LogicalCompare{
        public static void main(String args[]){
                String str1 = new String("OKAY");
                String str2 = new String(str1);
                System.out.println(str1 == str2);
        }
}

Answer: Option B

Explanation: