Answered step by step
Verified Expert Solution
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()
- 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 - 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)).
- 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)).
- 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.
- 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.
- What is the output produced by the following?
String verbPhrase = "is money";
System.out.println("Time" + verbPhrase); - What is the output produced by the following?
String test = "abcdefg";
System.out.println(test.length());
System.out.println(test.charAt(1)); - What is the output produced by the following?
String test = "abcdefg";
System.out.println(test.substring(3,5)); - What is the output produced by the following?
String test = "abcdefg";
System.out.println(test.substring(3)); - What is the output produced by the following?
System.out.println("abc def");
- What is the output produced by the following?
System.out.println("abc\ def");
- What is the output produced by the following?
String test = "Hello Tony";
test = test.toUpperCase();
System.out.println(test);
- 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);
- 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);
- 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?
- s1.charAt(0)
- s1.indexOf('j')
- s1.indexOf("to")
- s1.lastIndexOf('a')
- s1.lastIndexOf("o", 15)
- s1.length()
- s1.substring(5)
- s1.substring(5, 11)
- s1.toLowerCase()
- s1.toUpperCase()
- s1.concat(s2)
- "\t Wel \t".trim()
- 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());
- 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);
- 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started