Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What are the values of the following expressions? In each line, assume that String s = Hello; String t = World; a. s.length() + t.length()


  1. What are the values of the following expressions? In each line, assume that
    String s = "Hello";
    String t = "World";
    a. s.length() + t.length()

    b. s.substring(1, 2)

    c. s.substring(s.length() / 2, s.length())

    d. s + t

    e. t + s
  2. The following pseudocode describes how to obtain the name of a day, given the day number (0 = Sunday, 1 = Monday, and so on.)
    Declare a string called names containing "SunMonTueWedThuFriSat".

    Compute the starting position as 3 x the day number.

    Extract the substring of names at the starting position with length 3.

    Check this pseudocode, using the day number 4. Draw a diagram of the string that is being computed, similar to Figure 5 (See BJLO: Chapter 2, Section 2.5.6 (Substrings)).
  3. Suppose you are given a string str and two positions i and j, where i comes before j. The following pseudocode describes how to swap two letters in a word.
    We are given a string str and two positions i and j. (i comes before j)

    Set first to the substring from the start of the string to the last position before i.

    Set middle to the substring from positions i + 1 to j - 1.

    Set last to the substring from position j + 1 to the end of the string.

    Concatenate the following five strings: first, the string containing just the character at position j, middle, the string containing just the character at position i, and last.

    Check this pseudocode, using the string "Gateway" and positions 2 and 4. Draw a diagram of the string that is being computed, similar to Figure 5 (See BJLO: Chapter 2, Section 2.5.6 (Substrings)).
  4. Write Java code that transforms numbers 1, 2, 3, , 12 into the corresponding month names January, February, March, , December.
    Hint: Make a very long string "January February March ...", in which you add spaces such that each month name has the same length. Then use substring to extract the month you want.
  5. Writing large letters. A large letter H can be produced like this:
    * *
    * *
    *****
    * *
    * *

    It can be declared as a string literal like this:
    final string LETTER_H = "* *%n* *%n*****%n* *%n* *%n";

    Print the string with System.out.printf. The %n format specifiers cause line breaks in the output. Do the same for the letters E, L, and O. Then write the message
    H
    E
    L
    L
    O

    in large letters.
  6. What is the output produced by the following?
    String verbPhrase = "is money";

    System.out.println("Time" + verbPhrase);
  7. What is the output produced by the following?
    String test = "abcdefg";

    System.out.println(test.length());

    System.out.println(test.charAt(1));
  8. What is the output produced by the following?
    String test = "abcdefg";

    System.out.println(test.substring(3,5));
  9. What is the output produced by the following?
    String test = "abcdefg";

    System.out.println(test.substring(3));
  10. What is the output produced by the following?
    System.out.println("abc def");
  11. What is the output produced by the following?
    System.out.println("abc\ def");
  12. What is the output produced by the following?
    String test = "Hello Tony";

    test = test.toUpperCase();

    System.out.println(test);
  13. What is the output of the following two lines of Java code?
    System.out.println("2 + 2 = " + (2 + 2));

    System.out.println("2 + 2 = " + 2 + 2);
  14. The following code is supposed to output the string in lowercase letters but it has an error. What is wrong? How can the code be corrected?
    String test = "WHY ARE YOU SHOUTING?";

    test.toLowerCase();

    System.out.println(test);
  15. Suppose that s1 , s2 , and s3 are three strings, given as follows:
    String s1 = "Welcome to Java";
    String s2 = "Programming is fun";
    String s3 = "Welcome to Java";

    What are the results of the following expressions?
    1. s1.charAt(0)
    2. s1.indexOf('j')
    3. s1.indexOf("to")
    4. s1.lastIndexOf('a')
    5. s1.lastIndexOf("o", 15)
    6. s1.length()
    7. s1.substring(5)
    8. s1.substring(5, 11)
    9. s1.toLowerCase()
    10. s1.toUpperCase()
    11. s1.concat(s2)
    12. "\t Wel \t".trim()
  16. Suppose that s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
    String s = "Welcome to Java";
    String s3 = s1 + s2;
    String s3 = s1 - s2;

    s1 == s2;
    s1 >= s2;
    s1.compareTo(s2);
    int i = s1.length();
    char c = s1(0);
    char c = s1.charAt(s1.length());
  17. Show the output of the following statements (write a program to verify your results):
    System.out.println("1" + 1);
    System.out.println('1' + 1);
    System.out.println("1" + 1 + 1);
    System.out.println("1" + (1 + 1));
    System.out.println('1' + 1 + 1);
  18. Evaluate the following expressions (write a program to verify your results):
    1 + "Welcome " + 1 + 1
    1 + "Welcome " + (1 + 1)
    1 + "Welcome " + ('\u0001' + 1)
    1 + "Welcome " + 'a' + 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

=+ (a) Show that the definition is consistent.

Answered: 1 week ago

Question

Create a workflow analysis.

Answered: 1 week ago