Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:28:38 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 7: Strings

Section 7.2 The String Class
1   Suppose s is a string with the value "java". What will be assigned to x if you execute the following code?

char x = s.charAt(4);

A. 'a'
B. 'v'
C. Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

2   What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java!");
     String s2 = s1;

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 and s2 reference to different String objects");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

3   What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java!");
     String s2 = new String("Welcome to Java!");

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 and s2 reference to different String objects");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

4   What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java!");
     String s2 = new String("Welcome to Java!");

     if (s1.equals(s2))
       System.out.println("s1 and s2 have the same contents");
     else
       System.out.println("s1 and s2 have different contents");
   }
}

A. s1 and s2 have the same contents
B. s1 and s2 have different contents

5   What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java!");
     String s2 = s1.toUpperCase();

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else if (s1.equals(s2))
       System.out.println("s1 and s2 have the same contents");
     else
       System.out.println("s1 and s2 have different contents");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 have the same contents
C. s1 and s2 have different contents

6   What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java");
     String s2 = s1;

     s1 += "and Welcome to HTML";

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 and s2 reference to different String objects");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

7   What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = "Welcome to Java!";
     String s2 = "Welcome to Java!";

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 is not equal to s2");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

8   Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?

A. String s = new String("new string");
B. String s3 = s1 + s2
C. s1 >= s2
D. int i = s1.length
E. s1.charAt(0) = '5'

9   Suppose s1 and s2 are two strings. Which of the following statements or expressions is correct?

A. String s3 = s1 - s2;
B. boolean b = s1.compareTo(s2);
C. char c = s1[0];
D. char c = s1.charAt(s1.length());
E. None

10   What is the return value of "SELECT".substring(0, 5)?

A. "SELECT"
B. "SELEC"
C. "SELE"
D. None.

11   Analyze the following code.

class Test {
   public static void main(String[] args) {
     String s;
     System.out.println("s is " + s);
   }
}

A. The program has a compilation error because s is not initialized, but it is referenced in the println statement.
B. The program has a runtime error because because s is not initialized, but it is referenced in the println statement.
C. The program has a runtime error because because s is null in the println statement.
D. None of the above.

12   To check if a string s contains the prefix "Java", you write


A. if (s.startsWith("Java")) ...
B. if (s.indexOf("Java") == 0) ...
C. Both a and b are fine
D. None of the above.

13   To check if a string s contains the suffix "Java", you write


A. if (s.endsWith("Java")) ...
B. if (s.lastIndexOf("Java") >= 0) ...
C. Both a and b are fine
D. None of the above.

14   Which of the following is the correct statement to return JAVA?


A. toUpperCase("Java")
B. "Java".toUpperCase("Java")
C. "Java".toUpperCase()
D. None of the above

15   Which of the following is the correct statement to return a string from an array a of characters?


A. toString(a)
B. new String(a)
C. convertToString(a)
D. None of the above

Section 7.3 The Character Class
16   Which of following is not a correct method in Character?

A. isLetterOrDigit(char)
B. isLetter(char)
C. isDigit()
D. toLowerCase(char)
E. toUpperCase()

Section 7.4 The StringBuffer Class
17   Analyze the following code.

class Test {
   public static void main(String[] args) {
     StringBuffer strBuf = new StringBuffer(4);
     strBuf.append("ABCDE");
     System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(5));
   }
}

A. The program has a compilation error because you cannot specify initial capacity in the StringBuffer constructor.
B. The program has a runtime error because because the buffer's capacity is 4, but five characters "ABCDE" are appended into the buffer.
C. The program has a runtime error because because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range.
D. None of the above.

18   Which of the following is true?

A. You can add characters into a string buffer.
B. You can delete characters into a string buffer.
C. You can rverse the characters in a string buffer.
D. The capacity of a string buffer can be automatically adjusted.

Section 7.5 The StringTokenizer Class
19   What is the output of the following program?

import java.util.StringTokenizer;
 
public class TestStringTokenizer {
    public static void main(String[] args) {
      // Create a string and string tokenizer
      String s = "I$am*learning Java.";
      StringTokenizer st = new StringTokenizer(s, "$*.");
 
      // Retrieve and display tokens
      while (st.hasMoreTokens())
        System.out.print(st.nextToken() + " ");
    }
}

A. I am learning Java.
B. I$am*learning Java.
C. I am learning Java
D. None of the above.

20   What is the output of the following code?
import java.util.StringTokenizer;

public class Test {
    public static void main(String[] args) {
      // Create a string and string tokenizer
      String s = "Key:c The explanation follows\n\r";
      StringTokenizer st = new StringTokenizer(s, " ");
      st.nextToken();
      System.out.print(st.nextToken("\n\r"));
    }
}

A. The
B. explanation
C. follows
D. The explanation follows

Section 7.6 Command-Line Arguments
21   How can you get the word "abc" in the main method from the following call?

java Test "+" 3 "abc" 2

A. args[0]
B. args[1]
C. args[2]
D. args[3]

22   Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked?

A. int count = args.length;
B. int count = args.length - 1;
C. int count = 0; while (args[count] != null) count ++;
D. int count=0; while (!(args[count].equals(""))) count ++;

23   Which correctly creates an array of five empty Strings?

A. String[] a = new String [5];
B. String[] a = {"", "", "", "", ""};
C. String[5] a;
D. String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);
E. String[5] a;

24   Identify the problems in the following code.
                
public class Test {
   public static void main(String argv[]) {
     System.out.println("argv.length is " + argv.length);
   }
}
A. The program has a syntax error because String argv[] is wrong and it should be replaced by String[] args.
B. The program has a syntax error because String args[] is wrong and it should be replaced by String args[].
C. If you run this program without passing any arguments, the program would have a runtim error because argv is null.
D. If you run this program without passing any arguments, the program would display argv.length is 0.