Fiveable
Fiveable

String

Definition

A String is a sequence of characters in Java, enclosed in double quotes. It represents text and can be manipulated using various methods.

Analogy

Imagine a String as a necklace made up of individual beads (characters). You can add, remove, or rearrange these beads to create different words or sentences.

Related terms

length(): This method returns the number of characters in a String.

substring(): This method extracts a portion of a String based on specified indices.

equals(): This method compares two Strings for equality and returns true if they are equal.

"String" appears in:

Practice Questions (20+)

  • Consider the following method: public void printReverse(String str) { for (int i = str.length() - 1; i >= 0; i--) { System.out.print(str.charAt(i)); } } Assume that the method printReverse("Hello") appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printVowels(String str) { str = str.toLowerCase(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { System.out.print(ch + " "); } } } Assume that the method printVowels("OpenAI") appears in a method in the same class. What is printed as a result of the method call?
  • What will the following code print out? public class NamePrinter { public void printName(String name) { System.out.println("Your name is: " + name); } public void printAge(int age) { System.out.println("Your age is: " + age); } public static void main(String[] args) { NamePrinter np = new NamePrinter(); np.printName("John"); np.printAge(32); } }
  • What will the following code output? public class Book { public String title; public String author; public int pages; public Book(String bookTitle, String bookAuthor, int bookPages) { title = bookTitle; author = bookAuthor; pages = bookPages; } public void printBookInfo() { System.out.println("Title: " + title); System.out.println("Author: " + author); System.out.println("Pages: " + pages); } public static void main(String[] args) { Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald", 180); book.printBookInfo(); } }
  • What is the output of the following code? public class Employee { public String name; public int salary; public Employee(String empName, int empSalary) { name = empName; salary = empSalary; } public void printEmployeeInfo() { System.out.println("Name: " + name); System.out.println("Salary: $" + salary); } public void giveRaise(int raiseAmount) { salary += raiseAmount; } public static void main(String[] args) { Employee emp = new Employee("John", 50000); emp.printEmployeeInfo(); emp.giveRaise(2000); emp.printEmployeeInfo(); } }
  • Consider the following method. public boolean isValidEmail(String email) { /* implementation not shown */ } Which of the following lines of code, if located in a method in the same class as isValidEmail, will compile without an error?
  • Consider the following class definition. public class Animal { private String species; private int age; private double weight; public Animal(String animalSpecies, int animalAge, double animalWeight) { species = animalSpecies; age = animalAge; weight = animalWeight; } public void eat() { weight += 5; } public double getWeight() { return weight; } } Assume that the following code segment appears in a class other than Animal. java Copy code Animal dog = new Animal("Dog", 3, 15); dog.eat(); System.out.println(dog.getWeight()); What is printed as a result of executing the code segment?
  • Consider the following class definition: public class Student { private String name; private int age; private String major; public Student(String studentName, int studentAge, String studentMajor) { name = studentName; age = studentAge; major = studentMajor; } public String getName() { return name; } public int getAge() { return age; } public String getMajor() { return major; } } Assume that the following code segment appears in a class other than Student: Student student = new Student("Alice", 19, "Computer Science"); String name = student.getName(); int age = student.getAge(); String major = student.getMajor(); System.out.println(name + " is " + age + " years old and majoring in " + major + "."); What is printed as a result of executing the code segment?
  • Consider the following method: public static boolean isPalindrome(String word) { int length = word.length(); for (int i = 0; i < length / 2; i++) { if (word.charAt(i) != word.charAt(length - 1 - i)) { return false; } } return true; } Assume that the following code segment appears in a class: boolean result = isPalindrome("racecar"); System.out.println("Is 'racecar' a palindrome? " + result); What is printed as a result of executing the code segment?
  • Consider the following method: public static String capitalize(String word) { return word.substring(0, 1).toUpperCase() + word.substring(1); } Assume that the following code segment appears in a class: String result = capitalize("hello"); System.out.println("Capitalized word: " + result); What is printed as a result of executing the code segment?
  • How would you pre-initialize a string called greeting?
  • What escape sequence adds a line break in a string?
  • What is the value of the string newVal?: String newVal = "5" + 3;
  • How would you make a quotation mark part of a string?
  • What is the value of the string concatVal?: String concatVal = "5" + (2 + 3);
  • What is the result of the following code?<br>String str = "123";<br>Integer x = Integer.parseInt(str);<br>System.out.println(x + 1);
  • Consider the following code snippet: ```java String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); boolean result1 = str1.equals(str2); boolean result2 = str1.equals(str3); boolean result3 = (str1 == str2); boolean result4 = (str1 == str3); ``` What will be the values of result1, result2, result3, and result4, respectively?
  • What is the output of the following code? ```java String s1 = "hi"; String s2 = "Hi"; String s3 = new String("hi"); System.out.println(s1.equals(s2)); System.out.println(s2.equals(s3)); System.out.println(s1.equals(s3)); ```
  • Consider the following superclass and subclass declarations: ```java public class Shape { private String color; public Shape(String color) { this.color = color; } public String getColor() { return color; } } public class Circle extends Shape { private double radius; public Circle(String color, double radius) { // Circle constructor implementation goes here } } ```
  • What happens when we try to run the following? class J { public J(String x) { } } class K { public K() { } } public class L { public static void main(String[] args) { K k = new K(); } }


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.