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.

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.

Sar Maroof is graduated from HBO Amsterdam “higher professional education” when he already had a bachelor of science degree in Physics.
He is a SUN certified JSP as well as EJB. He has experience with Java since 2001 and worked for several big as well as small companies and later as a freelancer.
He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof



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

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017