Questions and Answers
Which operator is used to perform string concatenation in Java?
What does the following expression do: greeting.toUpperCase()
if a String variable called greeting contains the characters 'hello'?
Returns a new string containing the characters 'HELLO'
What is the result of the following expression: pioneer.length()
if a string variable called pioneer contains the characters 'Grace Murray Hopper'?
19
Given the following declaration, which expression would extract the substring 'ledge'? String words = 'Knowledge is power.'
Signup and view all the answers
What output would be produced when the following is executed:
String state = 'Mississippi';
System.out.println(state.replace('is', 'was'));
Signup and view all the answers
If a String variable called address contains the characters '123 Main Street', what is the result of the following expression: address.indexOf('3')
?
Signup and view all the answers
Which statement would produce the following output: 'Oom Pah Pah Oom Pah Pah,' that's how it goes?
Signup and view all the answers
Which of the following literals is NOT valid?
Signup and view all the answers
What is the output of the following statement: System.out.println('He said "Avada Kedavra"');
?
Signup and view all the answers
What is '\t'?
Signup and view all the answers
What is '\n'?
Signup and view all the answers
What do the characters in a Unicode escape sequence represent?
Signup and view all the answers
What is a small character set containing primarily English characters and basic symbols?
Signup and view all the answers
What is a way to represent non-ASCII Unicode characters in a program?
Signup and view all the answers
What is the technique of ordering characters based on a character set?
Signup and view all the answers
What is the way characters are stored in memory?
Signup and view all the answers
What are characters that don't map to the fixed-width, 16-bit Unicode encoding?
Signup and view all the answers
What are characters (like newline) that have no visual symbol?
Signup and view all the answers
What is the character set Java uses to represent characters?
Signup and view all the answers
What is a list of characters used by a programming language?
Signup and view all the answers
The String class is part of the java.__ package.
Signup and view all the answers
Two classes can have the same name if they are in different ____.
Signup and view all the answers
In a program, a class must always be referred to using its fully qualified name.
Signup and view all the answers
An ___ ___ tells the compiler which class you're referring to in a program.
Signup and view all the answers
An import statement should be written ____ a class.
Signup and view all the answers
The classes of the . package are automatically imported into every Java program.
Signup and view all the answers
An entire package can be imported using ___ import statement(s).
Signup and view all the answers
A __ __ lets you refer to a static method without using its class name.
Signup and view all the answers
The Java keyword new is ____ __.
Signup and view all the answers
A constructor of a class has the ____ ____ as the class, and is called when ____ ____ ____ ___.
Signup and view all the answers
An object does not have to be created when its object reference variable is declared.
Signup and view all the answers
Following a ____ reference will cause an exception to be thrown.
Signup and view all the answers
A String object can be created with a new operator.
Signup and view all the answers
A String method can be called through a ____ __.
Signup and view all the answers
What will happen if the variable total holds 5 when the following code is executed?
if (total > 8)
System.out.println("collywobbles");
Signup and view all the answers
Study Notes
String Manipulation in Java
- String concatenation in Java uses the
+
operator. - Method
toUpperCase()
converts all characters in a String to uppercase, e.g., "hello" becomes "HELLO". - The
length()
method returns the number of characters in a String, e.g., "Grace Murray Hopper" has a length of 19. - The method
substring(startIndex, endIndex)
extracts a portion of a String; for "Knowledge is power.",words.substring(4, 9)
returns "ledge". - Using
replace(oldChar, newChar)
modifies a String, as in the case of "Mississippi" to "Mwasswassippi" by replacing "is" with "was". - The
indexOf(char)
method returns the index of the first occurrence of a character in a String, e.g.,'3'
in "123 Main Street" is at index 2.
Output Control
- To print special characters or formatted strings, escape sequences like
\"
are used. For example,System.out.println("\"Oom Pah Pah Oom Pah Pah,\" that's how it goes.");
prints with quotation marks. - Valid and invalid string literals include options with escape characters; option c
""\n""
is invalid.
Character Handling
- The escape sequence
\t
represents a horizontal tab, while\n
denotes a newline character. - Unicode escape sequences specify characters in hexadecimal format for non-ASCII characters. ASCII refers to a small character set primarily for English and symbols.
- Lexicographic ordering is the technique used to order characters based on a character set, which in Java is represented by Unicode.
Memory and Encoding
- Character encoding determines how characters are stored in memory, with supplementary characters representing those that don't fit into fixed-width encodings.
- Non-printable characters, such as newline, do not have visual symbols.
Java Package System
- The String class belongs to the
java.lang
package, which is auto-imported into every Java program. - Classes can share names if they are in different packages; however, their fully qualified names do not always need to be used in the code.
- An import statement specifies which class to reference in a program and should be placed above the class definition.
- A static import allows direct access to static methods without needing to reference the class name.
Objects and Constructors
- The keyword
new
is an operator used to create new objects, and a class constructor has the same name as the class. - Objects can be declared without being instantiated immediately. A null reference will raise an exception if accessed.
- A String object can indeed be created with the
new
operator, and String methods can also be called on String literals.
Control Flow
- Conditional statements evaluate expressions, e.g., if a total of 5 is compared in an
if
statement with a value of 8, it does not execute the inner statement, resulting in no output and continued processing.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge of string operations in Java with these flashcards covering key concepts from weeks 3 to 6. Each card presents a question that helps reinforce your understanding of string concatenation and manipulation methods. Perfect for quick review and improving your Java skills!