Java String Operations Flashcards (Weeks 3-6)

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which operator is used to perform string concatenation in Java?

  • *
  • + (correct)
  • -
  • //

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.'

<p>words.substring(4, 9)</p> 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'));

<p>Mwasswassippi</p> 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')?

<p>2</p> Signup and view all the answers

Which statement would produce the following output: 'Oom Pah Pah Oom Pah Pah,' that's how it goes?

<p>System.out.println('&quot;Oom Pah Pah Oom Pah Pah,&quot; that's how it goes.');</p> Signup and view all the answers

Which of the following literals is NOT valid?

<p>&quot;&quot;\n&quot;&quot; (B)</p> Signup and view all the answers

What is the output of the following statement: System.out.println('He said "Avada Kedavra"');?

<p>He said 'Avada Kedavra'</p> Signup and view all the answers

What is '\t'?

<p>A horizontal tab</p> Signup and view all the answers

What is '\n'?

<p>A newline character which sends the output to the next line</p> Signup and view all the answers

What do the characters in a Unicode escape sequence represent?

<p>The hexadecimal code for a particular Unicode character</p> Signup and view all the answers

What is a small character set containing primarily English characters and basic symbols?

<p>ASCII</p> Signup and view all the answers

What is a way to represent non-ASCII Unicode characters in a program?

<p>Unicode escape sequence</p> Signup and view all the answers

What is the technique of ordering characters based on a character set?

<p>Lexicographic ordering</p> Signup and view all the answers

What is the way characters are stored in memory?

<p>Character encoding</p> Signup and view all the answers

What are characters that don't map to the fixed-width, 16-bit Unicode encoding?

<p>Supplementary characters</p> Signup and view all the answers

What are characters (like newline) that have no visual symbol?

<p>Non-printable characters</p> Signup and view all the answers

What is the character set Java uses to represent characters?

<p>Unicode</p> Signup and view all the answers

What is a list of characters used by a programming language?

<p>Character set</p> Signup and view all the answers

The String class is part of the java.__ package.

<p>lang</p> Signup and view all the answers

Two classes can have the same name if they are in different ____.

<p>Packages</p> Signup and view all the answers

In a program, a class must always be referred to using its fully qualified name.

<p>False (B)</p> Signup and view all the answers

An ___ ___ tells the compiler which class you're referring to in a program.

<p>Import statement</p> Signup and view all the answers

An import statement should be written ____ a class.

<p>Above</p> Signup and view all the answers

The classes of the . package are automatically imported into every Java program.

<p>java.lang</p> Signup and view all the answers

An entire package can be imported using ___ import statement(s).

<p>One</p> Signup and view all the answers

A __ __ lets you refer to a static method without using its class name.

<p>Static import</p> Signup and view all the answers

The Java keyword new is ____ __.

<p>An operator</p> Signup and view all the answers

A constructor of a class has the ____ ____ as the class, and is called when ____ ____ ____ ___.

<p>Same name; the object is created</p> Signup and view all the answers

An object does not have to be created when its object reference variable is declared.

<p>True (A)</p> Signup and view all the answers

Following a ____ reference will cause an exception to be thrown.

<p>Null</p> Signup and view all the answers

A String object can be created with a new operator.

<p>True (A)</p> Signup and view all the answers

A String method can be called through a ____ __.

<p>String literal</p> 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");

<p>The word collywobbles is not printed and processing continues</p> Signup and view all the answers

Flashcards are hidden until you start studying

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.

Quiz Team

More Like This

String Operations in Java
34 questions

String Operations in Java

SuaveTungsten1435 avatar
SuaveTungsten1435
Java Strings - Basics and Operations
37 questions
Use Quizgecko on...
Browser
Browser