Java Find Output Questions and Answers - String

Here are 10 Java code snippets designed to test your understanding of Java Strings. Each question includes a code snippet and multiple-choice options for the output. After you complete the test, you can check your score, the correct answer, and an explanation.

These questions explore various string methods, including toUpperCase, substring, equals, repeat, indexOf, replace, and trim, highlighting how they manipulate and evaluate string data in Java.

1. What will be the output of the following Java code?

String text = "Java";
System.out.println(text.toUpperCase());
a) java
b) JAVA
c) Java
d) jAVA

2. What will be the output of the following Java code?

String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
a) true
b) false
c) Throws an exception
d) None of the above

3. What will be the output of the following Java code?

String text = "Hello, world!";
System.out.println(text.substring(0, 5));
a) Hello
b) Hello,
c) world
d) H

4. What will be the output of the following Java code?

String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1.equals(s2));
a) true
b) false
c) Throws an exception
d) None of the above

5. What will be the output of the following Java code?

String message = "Java";
message += " Programming";
System.out.println(message);
a) Java Programming
b) JavaProgramming
c) Java
d) Programming

6. What will be the output of the following Java code?

String base = "Java";
String text = base.repeat(3);
System.out.println(text);
a) JavaJavaJava
b) Java3
c) Java
d) JJJ

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

String word = "HelloJava";
System.out.println(word.indexOf("Java"));
a) 5
b) 6
c) -1
d) 0

8. What will be the output of the following Java code?

String text = "Java";
String modified = text.replace('a', 'o');
System.out.println(modified);
a) Jovo
b) Jova
c) Java
d) Jobo

9. What will be the output of the following Java code?

String empty = "";
System.out.println(empty.isEmpty());
a) true
b) false
c) Throws an exception
d) None of the above

10. What will be the output of the following Java code?

String text = "  Java  ";
String trimmed = text.trim();
System.out.println(trimmed.equals("Java"));
a) true
b) false
c) Throws an exception
d) None of the above

Comments