❮ String Methods Show ExampleCompare strings to find out if they are equal:
Try it Yourself » Definition and UsageThe Tip: Use the compareTo() method to compare two strings lexicographically. Syntax
Parameter Values
Technical Details
❮ String Methods In the below code, which call to sum() method is appropriate? class Output { public static int sum(int ...x) Asked by ChancellorDog960 Please give answer for all the questions, Thanks in advance😊 1. Which of these method of class String is used to compare two String objects for their equality? a) equals() b) Equals() c) isequal() d) Isequal() 2. Which of these methods is used to compare a specific region inside a string with another specific region in another string? a) regionMatch() b) match() c) RegionMatches() d) regionMatches() 3. Which of these methods of class String is used to check whether a given object starts with a particular string literal? a) startsWith() b) endsWith() c) Starts() d) ends() 4. What is the value returned by function compareTo() if the invoking string is less than the string compared? a) zero b) value less than zero c) value greater than zero d) none of the mentioned 5. Which of these data type value is returned by equals() method of String class? a) char b) int c) boolean d) all of the mentioned 6. What will be the output of the following Java code? class output { public static void main(String args[]) { String c = "Hello i love java"; boolean var; var = c.startsWith("hello"); System.out.println(var); } } a) true b) false c) 0 d) 1 7. What will be the output of the following Java code? class output { public static void main(String args[]) { String s1 = "Hello i love java"; String s2 = new String(s1); System.out.println((s1 == s2) + " " + s1.equals(s2)); } } a) true true b) false false c) true false d) false true 8. What will be the output of the following Java code? class output { public static void main(String args[]) { String s1 = "Hello"; String s2 = new String(s1); String s3 = "HELLO"; System.out.println(s1.equals(s2) + " " + s2.equals(s3)); } } a) true true b) false false c) true false d) false true 9. In the following Java code, which code fragment should be inserted at line 3 so that the output will be: "123abc 123abc"? 1. StringBuilder sb1 = new StringBuilder("123"); 2. String s1 = "123"; 3. // insert code here 4. System.out.println(sb1 + " " + s1); a) sb1.append("abc"); s1.append("abc"); b) sb1.append("abc"); s1.concat("abc"); c) sb1.concat("abc"); s1.append("abc"); d) sb1.append("abc"); s1 = s1.concat("abc"); 10. What will be the output of the following Java code? class output { public static void main(String args[]) { String chars[] = {"a", "b", "c", "a", "c"}; for (int i = 0; i < chars.length; ++i) for (int j = i + 1; j < chars.length; ++j) if(chars[i].compareTo(chars[j]) == 0) System.out.print(chars[j]); } } a) ab b) bc c) ca d) ac Answer & Explanation Solved by verified expert Answered by srinadhusaipreetham ia pulvinar tortor nec facilisis. Pellentesque dapibus efficit lestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus Unlock full access to Course Hero Explore over 16 million step-by-step answers from our library Subscribe to view answer Step-by-step explanation
What type of values does the equals () method return?The equals() method compares two strings, and returns true if the strings are equal, and false if not.
What is the return type of equals method in Java?Java String equals()
The Java String class equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.
Why does string defines equals () method instead of using ==?We can use == operators for reference comparison (address comparison) and . equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects.
Which value will be returned if invoking string is equal to Str?The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).
|