Java Online Test - String

This quiz tests various aspects of Java string manipulation, including methods for comparison, concatenation, formatting, and more. It offers a practical way to assess and deepen your understanding of string operations in Java.

1. What is the output of the following Java code snippet?

String str = "Hello World";
a) W
b) H
c) e
d) o

2. Which method would you use to compare two strings for equality, ignoring case sensitivity?

a) equals()
b) equalsIgnoreCase()
c) compareTo()
d) compareToIgnoreCase()

3. How can you convert a primitive int to a String in Java?

a) String.valueOf(int)
b) Integer.toString(int)
c) int.toString()
d) Both a and b

4. What does the substring(int beginIndex, int endIndex) method return?

a) A new string that starts from beginIndex and ends at endIndex
b) A new string that starts from beginIndex to endIndex - 1
c) A new string from beginIndex inclusive to endIndex exclusive
d) The entire string from beginIndex

5. Which of the following is not a valid way to instantiate a string in Java?

a) String s1 = new String("Hello");
b) String s2 = "Hello";
c) char[] c = {'H', 'e', 'l', 'l', 'o'}; String s3 = new String(c);
d) String s4 = new char[]{"H", "e", "l", "l", "o"};

6. What is the result of concatenating an empty string "" with any non-empty string "Hello" using the + operator?

a) The original non-empty string "Hello"
b) A new string that is "Hello "
c) A new string that is " Hello"
d) A new string that is empty ""

7. What will be the output of the following code?

String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);
a) true
b) false
c) Java
d) An error occurs

8. Which method checks if a string starts with a specified prefix?

a) startsWith()
b) endsWith()
c) beginsWith()
d) hasPrefix()

9. What does the trim() method do to a string?

a) Removes all whitespace characters from the beginning and end of the string
b) Deletes all spaces in the string
c) Replaces spaces with empty characters
d) None of the above

10. How can you find the index of the first occurrence of a specified character in a string?

a) indexOf(char)
b) find(char)
c) search(char)
d) locate(char)

11. What is the output of the following code?

a) 1
b) 2
c) Error
d) None of the above

12. Which statement about the following code snippet is true?

String str1 = "Java";
String str2 = new String("Java");
System.out.println(str1 == str2);
System.out.println(str1.hashCode() == str2.hashCode());
a) str1 and str2 refer to the same object
b) str1.equals(str2) returns true
c) str1 and str2 have different hashCodes
d) None of the above

13. What does the following code output?

String str1 = "Java";
String str2 = str1.intern();
String str3 = new String("Java");
System.out.println(str1 == str2);
System.out.println(str2 == str3);
System.out.println(str3 == str1);
a) true, false, false
b) true, true, true
c) false, false, true
d) false, true, false

14. The hashCode() and equals() methods are overridden in which class?

a) java.lang.String
b) java.lang.StringBuffer
c) java.lang.StringBuilder

15. After executing the following code, what will be the output?

String s1 = "ONE";
b) ONE
d) Error

16. What is the output of the following code?

a) 6000Java
b) Java6000
c) 100020003000Java
d) Error

17. What will the following code output?

a) 012345
b) Each number indented increasing from left to right
c) Each number on a new line
d) 543210

18. What does the following code output?

String[] strings = {"Java", "Python", "JavaScript", "HTML", "CSS"};
String languages = String.join("_", strings);
a) Java_Python_JavaScript_HTML_CSS
b) JavaPythonJavaScriptHTMLCSS
c) Java+Python+JavaScript+HTML+CSS
d) None of the above

19. Which method is correctly used to determine if a string starts with a specific prefix?

a) startsWith()
b) beginsWith()
c) hasPrefix()
d) starts()

20. How can the length of a string be obtained in Java?

a) using length()
b) using size()
c) using getSize()
d) using length