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";
System.out.println(str.charAt(6));
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?

System.out.println("1".concat("2").repeat(5).charAt(7));
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.equals(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
d) NONE

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

String s1 = "ONE";
s1.concat("TWO");
s1.concat("THREE");
System.out.println(s1);
a) ONETWOTHREE
b) ONE
c) ONETWO
d) Error

16. What is the output of the following code?

System.out.println(1000+2000+3000+"Java");
a) 6000Java
b) Java6000
c) 100020003000Java
d) Error

17. What will the following code output?

System.out.print("0".indent(0));
System.out.print("1".indent(1));
System.out.print("2".indent(2));
System.out.print("3".indent(3));
System.out.print("4".indent(4));
System.out.print("5".indent(5));
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);
System.out.println(languages);
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

Comments