Skip to content Skip to navigation

Connexions

You are here: Home » Content » Jb0280r Review

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Jb0280r Review

Module by: Richard Baldwin. E-mail the author

Summary: This module contains review questions and answers keyed to the module titled Jb0280: Java OOP: String and StringBuffer.

Preface

This module contains review questions and answers keyed to the module titled Jb0280: Java OOP: String and StringBuffer .

The questions and the answers are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back again.

Questions

Question 1 .

Java provides two different string classes from which string objects can be instantiated. What are they?

Answer 1

Question 2

True or false? The StringBuffer class is used for strings that are not allowed to change. The String class is used for strings that are modified by the program. If false, explain why.

Answer 2

Question 3

True or false? While the contents of a String object cannot be modified, a reference to a String object can be caused to point to a different String object. If false, explain why.

Answer 3

Question 4

True or false? The use of the new operator is required for instantiation of objects of type String . If false, explain your answer.

Answer 4

Question 5

True or false? The use of the new operator is required for instantiation of objects of type StringBuffer . If false, explain your answer

Answer 5

Question 6

Provide a code fragment that illustrates how to instantiate an empty StringBuffer object of a default length and then use a version of the append method to put some data into the object.

Answer 6

Question 7

Without specifying any explicit numeric values, provide a code fragment that will instantiate an empty StringBuffer object of the correct initial length to contain the string "StringBuffer named str6" and then store that string in the object.

Answer 7

Question 8

Provide a code fragment consisting of a single statement showing how to use the Integer wrapper class to convert a string containing digits to an integer and store it in a variable of type int .

Answer 8

Question 9

Explain the difference between the capacity method and the length method of the StringBuffer class.

Answer 9

Question 10

True or false? The following is a valid code fragment. If false, explain why.

Note:


StringBuffer str6 = 
  new StringBuffer("StringBuffer named str6".length());

Answer 10

Question 11

Which of the following code fragments is the most efficient, first or second?

Note:


String str1 = "THIS STRING IS NAMED str1";

String str1 = new String("THIS STRING IS NAMED str1");

Answer 11

Question 12

Write a Java application that illustrates the fact that while a String object cannot be modified, the reference variable can be modified to point to a new String object, which can have the appearance of modifying the original String object.

Answer 12

Question 13

Write a Java application that illustrates different ways to create String objects and StringBuffer objects.

Answer 13

Question 14

Write a Java application that illustrates conversion from string to numeric.

Answer 14

Listings

What is the meaning of the following two images?

This image was inserted here simply to insert some space between the questions and the answers to keep them from being visible on the screen at the same time.

The image is also an example of the kinds of things that we do in my course titled ITSE 2321, Object-Oriented Programming.

Missing image

This image was also inserted for the purpose of inserting space between the questions and the answers.

Missing image

Answers

Answer 14

1
Listing 1: File SampProg26.java.

/*File SampProg26.java from module 50
Copyright 1997, R.G.Baldwin
Without viewing the solution that follows, write a Java
application that illustrates conversion from string to 
numeric, similar to the atoi() function in C.

The output from the program should be:
The value of the int variable num is 3625
===========================================================
*/

class SampProg26{
  public static void main(String[] args){
    int num = new Integer("3625").intValue();
    System.out.println(
      "The value of the int variable num is " + num);
  }//end main()
}//end class SampProg26

Back to Question 14

Answer 13

2
Listing 2: File SampProg25.java.

/*File SampProg25.java from module 50
Copyright 1997, R.G.Baldwin
Write a Java application that illustrates different ways to
create String objects and StringBuffer objects.

The output from this program should be (line breaks 
manually inserted to make it fit the format):

Create a String using new and display it
String named str2

Create a String without using new and display it
String named str1

Create, initialize, and display a StringBuffer using new
StringBuffer named str3

Try to create/initialize StringBuffer without using new 

Create an empty StringBuffer of default length
Now put some data in it and display it
StringBuffer named str5

Create an empty StringBuffer and specify length when 
created
Now put some data in it and display it
StringBuffer named str6

Try to create and append to StringBuffer without using new
**********************************************************/

class SampProg25{
  void d(String displayString){//method to display strings
    System.out.println(displayString);
  }//end method d()

  public static void main(String[] args){
    //instantiate an object to display methods
    SampProg25 o = new SampProg25();
    
    o.d("Create a String using new and display it");
    String str1 = new String("String named str2");
    o.d(str1 + "\n");
    
    o.d(
    "Create a String without using new and display it");
    String str2 = "String named str1";
    o.d(str2 + "\n");
  
    o.d("Create, initialize, and display a StringBuffer " 
      + "using new");
    StringBuffer str3 = new StringBuffer(
      "StringBuffer named str3");
    o.d(str3.toString()+"\n");

    o.d("Try to create/initialize StringBuffer without " 
      + "using new \n");    
    //StringBuffer str4 = //not allowed by compiler
    //  "StringBuffer named str4";
    
    o.d(
    "Create an empty StringBuffer of default length");  
    //accept default initial length
    StringBuffer str5 = new StringBuffer();
    o.d("Now put some data in it and display it");
    //modify length as needed
    str5.append("StringBuffer named str5");
    o.d(str5.toString() + "\n");
    
    o.d("Create an empty StringBuffer and specify length "
      + "when created");
    StringBuffer str6 = new StringBuffer(
      "StringBuffer named str6".length());
    o.d("Now put some data in it and display it");    
    str6.append("StringBuffer named str6");
    o.d(str6.toString() + "\n");
    
    o.d(
    "Try to create and append to StringBuffer without "
      + "using new");
    //StringBuffer str7;
    //str7.append("StringBuffer named str7");
  }//end main()  
}//end class SampProg25

Back to Question 13

Answer 12

3
Listing 3: File SampProg24.java.

/*File SampProg24.java from module 50
Copyright 1997, R.G.Baldwin
Without viewing the solution that follows, Write a Java
application that illustrates the fact that while a String 
object cannot be modified, the reference variable can be 
modified to point to a new String object which can have the
appearance of modifying the original String object.

The output from this program should be

Display original string values
THIS STRING IS NAMED str1
This string is named str2
Replace str1 with another string
Display new string named str1
THIS STRING IS NAMED str1 This string is named str2
Terminating program
**********************************************************/

class SampProg24{
  String str1 = "THIS STRING IS NAMED str1";
  String str2 = "This string is named str2";
  
  public static void main(String[] args){
    SampProg24 thisObj = new SampProg24();
    System.out.println("Display original string values");
    System.out.println(thisObj.str1);
    System.out.println(thisObj.str2);
    System.out.println(
      "Replace str1 with another string");
    thisObj.str1 = thisObj.str1 + " " + thisObj.str2;
    System.out.println("Display new string named str1");
    System.out.println(thisObj.str1);
    System.out.println("Terminating program");
  }//end main()
}//end class SampProg24

Back to Question 12

Answer 11

The first code fragment is the most efficient.

Back to Question 11

Answer 10

Answer 9

The capacity method returns the amount of space currently allocated for the StringBuffer object. The length method returns the amount of space used.

Back to Question 9

Answer 8

Note:


    int num = new Integer("3625").intValue();

Back to Question 8

Answer 7

Note:


StringBuffer str6 = 
  new StringBuffer("StringBuffer named str6".length());
str6.append("StringBuffer named str6");

Back to Question 7

Answer 6

Note:


StringBuffer str5 = 
  new StringBuffer();//accept default initial length
str5.append(
      "StringBuffer named str5");//modify length as needed

Back to Question 6

Answer 5

Answer 4

False. A String object can be instantiated using either of the following statements:

Note:


    String str1 = new String("String named str2");
   
    String str2 = "String named str1";

Back to Question 4

Answer 3

Answer 2

False. This statement is backwards. The String class is used for strings that are not allowed to change. The StringBuffer class is used for strings that are modified by the program.

Back to Question 2

Answer 1

The two classes are:

  • String
  • StringBuffer

Back to Question 1

Miscellaneous

This section contains a variety of miscellaneous information.

Note:

Housekeeping material
  • Module name: Jb0280r Review: String and StringBuffer
  • File: Jb0280r.htm
  • Originally published: 1997
  • Published at cnx.org: 11/29/12
  • Revised: 01/02/13

Note:

Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks