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.

strings-gray

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's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

3 Comments

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