Java Strings

Ace your homework & exams now with Quizwiz!

"foxtrot".substring( 0, 0 )What is returned by substring?

"" (the empty string)

"foxtrot".substring( 0, 1 )What is returned by substring?

"f"

"foxtrot".substring( 0 )What is returned by substring?

"foxtrot"

"foxtrot".substring( 3, 5 )What is returned by substring?

"tr"

"foxtrot".substring( 3 )What is returned by substring?

"trot"

"foxtrot".substring( 2, 6 )What is returned by substring?

"xtrot"

"tictactoe".indexOf( 'T' )What is returned by indexOf?

-1

"foxtrot".substring( 4, 4 ).length()What is returned by length?

0

"tictactoe".indexOf( 't' )What is returned by indexOf?

0

int test = "fox".compareTo( "fox" );What is the approximate value of test?

0

"foxtrot".substring( 5, 6 ).length()What is returned by length?

1

What is the value of len after the following executes? String s1 = "Hey, buddy!"; int len = s1.length(); A. 8B. 10C. 11

11

"foxtrot".substring( 3, 5 ).length()What is returned by length?

2

"tictactoe".indexOf( 'c' )What is returned by indexOf?

2

4-7-3: What is the value of pos after the following code executes?String s1 = "ac ded ca"; int pos = s1.indexOf("d"); A. 3B. 4C. 5D.

3

"tictactoe".indexOf( 'a' )What is returned by indexOf?

4

"tictactoe".indexOf( 'e' )What is returned by indexOf?

8

: What is the value of s1 after the following code executes? String s1 = "Hey"; String s2 = s1.substring(0,1); String s3 = s2.toLowerCase(); A. Hey B. he C. H D. h

A ( ✔️ Strings are immutable, meaning they don't change. Any method that that changes a string returns a new string. So s1 never changes unless you set it to a different string.)

Examine the following code. What will the value of the variable str1 be after it processes? String str1 = new String("Join us at 6!"); String.touppercase(); A)JOIN US AT 6! B)join us at 6! C)Join Us At 6! D)jOiN uS aT 6!

Guess your answer

String employee1 = "Jane Austen"; String employee2 = new String ("Jane Austen");

True both will retrun the same value

boolean test( String str ) { return str == "fox" ); } If str contains "fox" what will test return?

Unpredictable

public class Test { public static void main(String[] args) { String s1 = new String("HELLO"); String s2 = new String("HELLO"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); } }

false true

int test = "fox".compareTo( "Fox" );What is the approximate value of test?

greater than 0

int test = "fox".compareTo( "Hen" );What is the approximate value of test?

greater than 0

int test = "hen".compareTo( "fox" );What is the approximate value of test?

greater than 0

What method is used to find the length of a string?

length

int test = "Hen".compareTo( "fox" );What is the approximate value of test?

less than 0

int test = "fox".compareTo( "hen" );What is the approximate value of test?

less than 0

What are the two overloads of the substring method?

substring( int, int ) substring( int )

import java.util.*; public class GFG { public static void main(String args[]) { String string1 = new String("Geeksforgeeks"); String string2 = new String("Geeks"); String string3 = new String("Geeks"); String string4 = null; String string5 = null; // Comparing for String 1 != String 2 System.out.println("Comparing " + string1 + " and " + string2 + " : " + Objects.equals(string1, string2)); // Comparing for String 2 = String 3 System.out.println("Comparing " + string2 + " and " + string3 + " : " + Objects.equals(string2, string3)); // Comparing for String 1 != String 4 System.out.println("Comparing " + string1 + " and " + string4 + " : " + Objects.equals(string1, string4)); // Comparing for String 4 = String 5 System.out.println("Comparing " + string4 + " and " + string5 + " : " + Objects.equals(string4, string5)); } }

Comparing Geeksforgeeks and Geeks : false Comparing Geeks and Geeks : true Comparing Geeksforgeeks and null : false Comparing null and null : true

import java.util.*; public class GFG { public static void main(String args[]) { String string1 = new String("Geeksforgeeks"); String string2 = new String("Practice"); String string3 = new String("Geeks"); String string4 = new String("Geeks"); // Comparing for String 1 < String 2 System.out.println("Comparing " + string1 + " and " + string2 + " : " + string1.compareTo(string2)); // Comparing for String 3 = String 4 System.out.println("Comparing " + string3 + " and " + string4 + " : " + string3.compareTo(string4)); // Comparing for String 1 > String 4 System.out.println("Comparing " + string1 + " and " + string4 + " : " + string1.compareTo(string4)); } }

Comparing Geeksforgeeks and Practice : -9 Comparing Geeks and Geeks : 0 Comparing Geeksforgeeks and Geeks : 8

public class GFG { public static void main(String args[]) { String string1 = new String("Geeksforgeeks"); String string2 = new String("Practice"); String string3 = new String("Geeks"); String string4 = new String("Geeks"); String string5 = new String("geeks"); // Comparing for String 1 != String 2 System.out.println("Comparing " + string1 + " and " + string2 + " : " + string1.equals(string2)); // Comparing for String 3 = String 4 System.out.println("Comparing " + string3 + " and " + string4 + " : " + string3.equals(string4)); // Comparing for String 4 != String 5 System.out.println("Comparing " + string4 + " and " + string5 + " : " + string4.equals(string5)); // Comparing for String 1 != String 4 System.out.println("Comparing " + string1 + " and " + string4 + " : " + string1.equals(string4)); } }

Comparing Geeksforgeeks and Practice : false Comparing Geeks and Geeks : true Comparing Geeks and geeks : false Comparing Geeksforgeeks and Geeks : false

public class GFG { public static void main(String args[]) { String string1 = new String("Geeksforgeeks"); String string2 = new String("Practice"); String string3 = new String("Geeks"); String string4 = new String("Geeks"); String string5 = new String("geeks"); // Comparing for String 1 != String 2 System.out.println("Comparing " + string1 + " and " + string2 + " : " + string1.equalsIgnoreCase(string2)); // Comparing for String 3 = String 4 System.out.println("Comparing " + string3 + " and " + string4 + " : " + string3.equalsIgnoreCase(string4)); // Comparing for String 4 = String 5 System.out.println("Comparing " + string4 + " and " + string5 + " : " + string4.equalsIgnoreCase(string5)); // Comparing for String 1 != String 4 System.out.println("Comparing " + string1 + " and " + string4 + " : " + string1.equalsIgnoreCase(string4)); } }

Comparing Geeksforgeeks and Practice : false Comparing Geeks and Geeks : true Comparing Geeks and geeks : true Comparing Geeksforgeeks and Geeks : false

public class Test { public static void main(String[] args) { String s1 = "Ram"; String s2 = "Ram"; String s3 = new String("Ram"); String s4 = new String("Ram"); String s5 = "Shyam"; String nulls1 = null; String nulls2 = null; System.out.println(" Comparing strings with equals:"); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s1.equals(s5)); // System.out.println(nulls1.equals(nulls2)); // NullPointerException System.out.println(" Comparing strings with ==:"); System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s3 == s4); System.out.println(nulls1 == nulls2); System.out.println(" Comparing strings with compareto:"); System.out.println(s1.compareTo(s3)); System.out.println(s1.compareTo(s5)); // System.out.println(nulls1.compareTo(nulls2)); // NullPointerException } }

Comparing strings with equals: true true false Comparing strings with ==: true false false true Comparing strings with compareto: 0 -1

Examine the following code. What will the result of be? String m1 = new String("Goodbye"); String m2 = new String("Goodbye"); if (m1==m2) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } Will java disply? Java will display 'equal' Java will display "Not Equal'

Java will display 'equal'

. Examine the following code. What will the value of the variable str3 be? String str1 = new String("Join us at 6"); String str2 = new String("For Happy hour"); String str3 = Str1 +Str2; A)Nothing, the compiler will error B)Join us at 6 for Happy Hour! C)Join us at 6for Happy Hour! D)Join us at 6

Please guess your answer ( my guess is C)

String str1 = "Cccp"; String str2 = "Rabbit"; str1 = str1.toUpperCase(); str2 = str2.toUpperCase(); println(str1 + ":" + str2); what is the output?

Prints "CCCP:RABBIT"

What type of string is 'Welcome!' in the code below? String greeting = 'Welcome' string constant pool string object string literal string heap

String literal

Which of the following correctly declares a new String object called hello? String new = String "Hello"; String Hello = String();

String new = String "Hello"; String Hello = String(); - This is correct

Given the following code segment, what is in the string referenced by s1. String s1 = "xy"; String s2 = s1; s1 = s1 + s2 + "z"; A. xyz B. xyxyz C. xy xy z D. xy z E. z

xyxyz


Related study sets

Home Inspector National Exam Prep 2015-Copy

View Set

fluid, electrolytes, acid-base balance

View Set

"Haven't I Made a Difference!" Vocabulary

View Set

Élelmiszeripar - hús fogalma bevezető

View Set

Statistics Unit 3: Hypothesis Testing, One Sample t-Tests, Paired- Samples t-Tests, Independent-Samples t-Tests

View Set

CLASSIFICATION and SIX KINGDOMS TEST

View Set

Business Finance Reading Assignment - Chapter 1 Connect

View Set

Guide To Computer Forensics And Investigations - All Chapter Reviews

View Set