Quizzes – Java strings

The following Java exercise(s) are designed to help you understand the article to which it belongs. If the following quizzes are too hard, then I recommend to read the article first, which offers a theoretical explanation including examples and exercises.


Quiz 1: Introducing some important methods of Java Strings

What is written to the standard output as the result of executing the following code?

public class MyString {

	String str = "All that we are is the result of what we have thought. Buddha";

	public static void main(String[] args){

		MyString ms = new MyString();
		char c = ms.str.charAt(5);
		boolean b = ms.str.contains("w");
		boolean b2 = ms.str.contains("z");
		int i = ms.str.indexOf("e");

		System.out.print(c + "-" + b + "-" + b2 + "-" + i);

Select the correct answer.


Quiz 2: What can you do with the replace and replaceAll methods?

The “str” statement is not correct.
It should be “Be the change that you wish to see in the world.”
Which one of the following claims is true?

public class MyString {

	public static void main(String[] args){
		String str = "Be zhe change zhat you wish zo see in zhe world. Gandhi";

		String str2 = str.replaceAll("z","t");
		String str3 = str.replaceFirst("z","t");
		String str4 = str.replace('z','t');
		System.out.println("str2: " + str2);
		System.out.println("str3: " + str3);
		System.out.println("str4: " + str4);

Select the correct answer.

Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.


Add a Comment
  1. Too many coemmiplnts too little space, thanks!

  2. Thank.I hope the material will help.

    1. What I find so insnrettieg is you could never find this anywhere else.

Leave a Reply