created: 6/18/08; revised 10/30/2012


Quiz on Substring

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.


1. What will the following write?

String str = "Hello World!" ;
str.substring(6);
System.out.println( str );

A.  World!

B.  Hello

C.  Hello World!

D.  The fragment will not compile.

correct answer


2. Examine this code:

String str = "Hello World!" ;
System.out.println( str.substring(6) );

What will it write?

A.  World!

B.  Hello

C.  Hello World!

D.  The fragment will not compile.

correct answer


3. Examine this code:

String str = "Hello World!" ;
System.out.println( str.length() );

What will it write?

A.  0

B.  10

C.  11

D.  12

correct answer


4. Examine this code:

String myString = "";
System.out.println( myString.length() );

What what will it write?

A.  0

B.  1

C.  2

D.  the code will not compile

correct answer


5. Examine this code:

String str = "Hello\tWorld!" ;
System.out.println( str.length() );

What will it write?

A.  0

B.  10

C.  11

D.  12

correct answer


6. Examine this code:

String str = "Hello World!" ;

What is the index of the character 'W' ?

A.  0

B.  5

C.  6

D.  7

correct answer


7. Examine this code:

String str = "One Fine Day" ;
String val = str.substring(4) ; 
System.out.println( val );

What does the fragment print?

A.  One Fine Day

B.  Fine Day

C.  Day

D.  One Fine

correct answer


8. Examine this code:

String str = "One Fine Day" ;
String val = str.substring(0, 3) ; 
System.out.println( val );

What does the fragment print?

A.  One Fine Day

B.  Fine Day

C.  One

D.  One Fine

correct answer


9. Examine this code:

String str = "One Fine Day" ;
String val = str.substring(0, str.length() ) ; 
System.out.println( val );

What does the fragment print?

A.  One Fine Day

B.  Fine Day

C.  One

D.  The code with throw an IndexOutOfBoundsException

correct answer


10. Examine this code:

String str = "One Fine Day" ;
String val = str.substring(4, 7 ) ; 
System.out.println( val );

What does the fragment print?

A.  nothing is printed since val contains an empty string.

B.  Fine

C.  Fin

D.  The code with throw an IndexOutOfBoundsException

correct answer


11. Examine this code:

String str = "One Fine Day" ;
String val = str.substring( 7, 4 ) ; 
System.out.println( val );

What does the fragment print?

A.  nothing is printed since val contains an empty string.

B.  Fine

C.  Fin

D.  The code with throw an IndexOutOfBoundsException

correct answer


12. Examine this code:

String str = "One Fine Day" ;
String A =  str.substring(0,3);
String B =  str.substring(3);

System.out.println( A+B );

What does the fragment print?

A.  One Day

B.  One Fine Day

C.  OneFine Day

D.  On Fine Day

correct answer


13. Examine this code:

String str = "One Fine Day" ;
String A =  str.substring(4);
String B =  A.substring(0,4);

System.out.println( B );

What does the fragment print?

A.  One

B.  Fine

C.  Day

D.  Fine Day

correct answer


14. Examine this code:

String str = "One Fine Day" ;

System.out.println( str.indexOf( "Fine" );

What does the fragment print?

A.  0

B.  3

C.  4

D.  5

correct answer


15. Examine this code:

String str = "One Fine Day" ;

System.out.println( str.indexOf( "" ) );

What does the fragment print?

A.  0

B.  3

C.  4

D.  The code with throw an IndexOutOfBoundsException

correct answer


16. Examine this code:

String str = "One Fine Day" ;

System.out.println( str.substring( str.indexOf("Fine") ) );

What does the fragment print?

A.  Fine Day

B.  Day

C.  One Fine Day

D.  The code with throw an IndexOutOfBoundsException

correct answer


The number you got right: Percent Correct: Letter Grade:


Click here If you have returned here from another page, or have re-loaded this page, you will need to click again on each of your choices for the grading program to work correctly. You may want to press the SHIFT KEY while clicking to clear the old answers.