0
0
Javaprogramming~15 mins

String creation in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
String Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
What is the output of this Java code?

Consider the following Java code snippet:

String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1 == s2);

What will be printed?

Java
String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1 == s2);
Afalse
Btrue
CCompilation error
DRuntime exception
Attempts:
2 left
💻 code output
intermediate
2:00remaining
What is the output of this Java code using StringBuilder?

Look at this code:

StringBuilder sb = new StringBuilder("abc");
sb.append("def");
System.out.println(sb.toString());

What will be printed?

Java
StringBuilder sb = new StringBuilder("abc");
sb.append("def");
System.out.println(sb.toString());
Aabc def
Babcdef
Cabc
DCompilation error
Attempts:
2 left
🔧 debug
advanced
2:00remaining
What error does this code raise?

Examine this Java code snippet:

String s = null;
int length = s.length();
System.out.println(length);

What error will this code cause when run?

Java
String s = null;
int length = s.length();
System.out.println(length);
ANullPointerException
BCompilation error
CArrayIndexOutOfBoundsException
DNo error, prints 0
Attempts:
2 left
💻 code output
advanced
2:00remaining
What is the output of this code using String intern()?

Consider this Java code:

String s1 = new String("test");
String s2 = s1.intern();
String s3 = "test";
System.out.println(s2 == s3);

What will be printed?

Java
String s1 = new String("test");
String s2 = s1.intern();
String s3 = "test";
System.out.println(s2 == s3);
ACompilation error
Bfalse
CRuntime exception
Dtrue
Attempts:
2 left
🧠 conceptual
expert
2:00remaining
How many String objects are created here?

Analyze this Java code snippet:

String s = "a" + "b" + new String("c");

How many distinct String objects are created at runtime?

A4
B3
C2
D1
Attempts:
2 left