What is a key difference between strings in Java and strings in C?
In Java, strings are objects and immutable, meaning they cannot be changed after creation. In C, strings are arrays of characters ending with a null character '\0', and they can be modified.
Click to reveal answer
intermediate
Why are Java strings considered immutable?
Java strings are immutable to improve security, thread safety, and performance through string pooling, which allows sharing of string instances.
Click to reveal answer
intermediate
How does Python handle strings differently compared to Java?
Python strings are also immutable like Java, but Python uses Unicode by default for all strings, making it easier to handle international text.
Click to reveal answer
advanced
What is string interning and which language commonly uses it?
String interning is a method of storing only one copy of each distinct string value, which Java uses to save memory and improve performance.
Click to reveal answer
beginner
How do mutable strings differ from immutable strings?
Mutable strings can be changed after creation (e.g., StringBuilder in Java), while immutable strings cannot be changed once created (e.g., String in Java).
Click to reveal answer
Which of the following languages treats strings as immutable objects by default?
AJava
BC
CC++ (char arrays)
DAssembly
✗ Incorrect
Java strings are immutable objects by default, unlike C or C++ char arrays which are mutable.
In C, how is the end of a string indicated?
ABy a terminating space character
BBy string length stored separately
CBy a special object property
DBy a null character '\0'
✗ Incorrect
C strings are arrays of characters ending with a null character '\0' to mark the end.
What is the main benefit of string interning in Java?
AReduced memory usage by sharing string instances
BFaster string concatenation
CAllows strings to be mutable
DEnables strings to store numbers
✗ Incorrect
String interning reduces memory usage by storing only one copy of each distinct string.
Which Java class provides mutable strings?
AString
BBoth B and D
CStringBuilder
DStringBuffer
✗ Incorrect
Both StringBuilder and StringBuffer provide mutable strings in Java.
Which language uses Unicode by default for strings?
AJava
BC
CPython
DC++
✗ Incorrect
Python uses Unicode by default for all strings, making it easier to handle international text.
Explain how strings are stored and managed differently in Java compared to C.
Think about mutability and memory management.
You got /4 concepts.
Describe the advantages of immutable strings in programming languages like Java.
Consider how immutability affects sharing and safety.
You got /4 concepts.
Practice
(1/5)
1. In Java, which of the following statements about String objects is true?
easy
A. Strings are stored as linked lists of characters.
B. Strings can be changed directly by modifying their characters.
C. Strings are immutable, meaning once created, they cannot be changed.
D. Strings are always stored in the stack memory.
Solution
Step 1: Understand Java String immutability
In Java, String objects cannot be changed after creation. Any modification creates a new String object.
Step 2: Compare other options
Strings are not linked lists, and they are stored in heap memory, not stack.
Final Answer:
Strings are immutable, meaning once created, they cannot be changed. -> Option C
Quick Check:
Java String immutability = true [OK]
Hint: Remember: Java Strings never change after creation [OK]
Common Mistakes:
Thinking Strings can be changed in place
Confusing String storage with primitive types
Assuming Strings are linked lists
2. Which of the following is the correct way to create a new String object in Java?
easy
A. String s = new String("hello");
B. String s = String("hello");
C. String s = new string("hello");
D. String s = 'hello';
Solution
Step 1: Check Java String creation syntax
The correct syntax uses the keyword new and capitalized String class: new String("hello").
Step 2: Identify incorrect options
String s = String("hello"); misses new, C uses lowercase string, and D uses single quotes which are for chars, not Strings.
Final Answer:
String s = new String("hello"); -> Option A
Quick Check:
Correct String constructor syntax = String s = new String("hello"); [OK]
Hint: Use new String("text") with capital S and double quotes [OK]
Common Mistakes:
Using single quotes for Strings
Using lowercase 'string' instead of 'String'
Omitting the new keyword
3. What will be the output of the following Java code?
String a = "hello";
String b = "hello";
System.out.println(a == b);
medium
A. Runtime exception
B. false
C. Compilation error
D. true
Solution
Step 1: Understand String literals and pooling in Java
Both a and b refer to the same String literal in the String pool, so a == b compares the same object reference.
Step 2: Evaluate the == operator on String literals
Since both point to the same object, a == b returns true.
Final Answer:
true -> Option D
Quick Check:
String literal pooling makes == true [OK]
Hint: String literals share references, so == returns true [OK]
Common Mistakes:
Confusing == with .equals() for content check
Assuming == always returns false for Strings
Ignoring String pool behavior
4. Identify the error in the following Java code snippet:
String s = "hello";
s[0] = 'H';
System.out.println(s);
medium
A. Strings are immutable; characters cannot be changed by index.
B. Syntax error: single quotes cannot be used for characters.
C. Runtime error: index out of bounds.
D. No error; output will be "Hello".
Solution
Step 1: Check String immutability in Java
Strings cannot be changed by modifying characters at an index because they are immutable.
Step 2: Analyze the code error
Attempting s[0] = 'H'; causes a compile-time error since Strings do not support indexing for assignment.
Final Answer:
Strings are immutable; characters cannot be changed by index. -> Option A
Quick Check:
Immutable Strings prevent index assignment [OK]
Hint: Strings can't change characters by index in Java [OK]
Common Mistakes:
Trying to modify String characters directly
Confusing char arrays with Strings
Expecting runtime error instead of compile error
5. Given the following Java code, what will be the output?