Hello! Friends, we have prepared a 40-question core Java quiz online test for you. It intends to quiz you on the concept of Java Strings and how you use them in your programs.
Java String is one of the commonly asked topics in all Java interviews, so this quiz is here to test your preparation level. With 40 questions on Strings, it’ll prove to be a great help for those interested in learning the concept of Java String.
This test covers questions on several Java classes, such as String, StringBuffer, and StringBuilder, that implement string-handling functionality. It will help Java developers, and the QA engineers can also attempt it and check their readiness to start developing any automation project using Java.
However, beginners should also check out this entry-level test for Java developers. Another Java concept that you should be familiar with is the Collection framework. Since it’s an important topic, read these essential questions on the Collection framework and fill in the gaps you may have.
Core Java Quiz Online Test – Make Sure You Know Strings
Answer Key with Reasoning
Here’s a reasoning table explaining the correct answers for each question:
Q# | Question | Correct Answer | Reasoning |
---|---|---|---|
1 | Which is invalid for Java String? | String represents an array. | Java Strings are not arrays, they are objects. |
2 | Why is String immutable? | All of these. | String immutability helps with caching, security, and performance. |
3 | Difference between String & StringBuffer? | String is immutable. | String objects cannot be changed, whereas StringBuffer allows modifications. |
4 | Difference between C and Java String? | C Strings are null-terminated. | C uses \0 to terminate strings, Java does not. |
5 | What does replaceAll() do? | Replaces all matches. | replaceAll() replaces all occurrences of a substring. |
6 | Which method compares Strings? | compareTo(String str) | compareTo() compares lexicographically, while equals() checks exact matches. |
7 | Convert String to byte array? | getBytes() and new String(byte[]) | getBytes() converts String to bytes, new String(byte[]) converts it back. |
8 | What is the Java String pool? | Stores String literals in heap. | JVM stores String literals in a special memory area to save space. |
9 | Purpose of intern()? | Moves String to pool. | intern() ensures a single instance of the String exists in the pool. |
10 | Why use char[] for passwords? | String stays in memory. | String values cannot be erased from memory, but char[] can. |
11 | What is output of equals()? | false, true | equals() compares values, while == checks references. |
12 | Difference between ‘==’ and equals()? | References vs values. | == checks if objects are same reference, equals() checks actual content. |
13 | Are immutable objects thread-safe? | true | Immutability ensures no shared modification issues in threads. |
14 | Does modifying a String create a new object? | true | Java String objects are immutable, so changes result in new objects. |
15 | Use StringBuffer for modifications? | true | StringBuffer is mutable and optimized for modifications. |
16 | Can Strings be used in switch? | Yes, from JDK 7. | Java 7 introduced String-based switch statements. |
17 | What does split(‘\s+’) do? | Splits whitespace. | \\s+ is a regex pattern that splits by one or more spaces. |
18 | Output of parseInt(“10”)? | 10 | parseInt("10") converts the String “10” to integer 10. |
19 | Default StringBuilder capacity? | 16 | StringBuilder starts with a 16-character buffer. |
20 | Output of substring(9, 12)? | “bee”, 3 | Extracts characters from index 9 to 11. |
21 | What is “Java”.length()? | 4 | "Java" has 4 characters. |
22 | What is “Java”.charAt(2)? | “v” | The third character (index 2) is ‘v’. |
23 | Which returns ‘?’? | JavaStr.charAt(21) | The 22nd character (index 21) is ‘?’. |
24 | Correct way to concatenate Strings? | Str1.concat(Str2) | concat() efficiently joins Strings. |
25 | What does toString() return? | String representation. | toString() returns a String representation of an object. |
26 | Is String a keyword? | false | String is a class, not a keyword. |
27 | Which class is final? | String | String is final, so it cannot be subclassed. |
28 | StringBuffer vs StringBuilder? | StringBuffer is thread-safe. | StringBuffer has synchronized methods, making it thread-safe. |
29 | Where are String objects stored? | Heap | All String objects are stored in Heap Memory. |
30 | What is the output? | true | Based on the question context. |
31 | Output of the loop? | Ac | The correct loop behavior produces "Ac" . |
32 | Convert char[] to String? | abc | Converting a char[] { ‘a’, ‘b’, ‘c’ } results in "abc" . |
33 | Convert ASCII to String? | BCD | The ASCII values convert to "BCD" . |
34 | What does toString() return? | String representation. | toString() converts objects to Strings. |
35 | Output of string comparison? | true | The comparison is true based on the logic. |
36 | What happens when modifying String? | New object is created. | Strings are immutable, so modifications create new objects. |
37 | Default value of an uninitialized String? | null. | Uninitialized String variables default to null . |
38 | Can String be used in for-each loop? | Yes, for characters. | Strings are iterable by character in for-each loops. |
39 | Which class is mutable? | Both 2 and 3. | StringBuffer and StringBuilder are mutable. |
40 | substring(2, 5) for “JavaString”? | “vaS” | Extracts characters from index 2 to 4. |
Key Takeaways from Core Java Quiz
The demand for Java is growing but also the competition among the developers. IT companies are using new avenues to hunt for the best talent like online evaluation platforms. Hence, you should also be on your toes and keep working on your skills. We’ll be helping you out with more challenging stuff like this core Java online test.
Hopefully, this quiz helped us improve your Java programming skills. Rest assured that we’ll continue to create new quizzes on trending subjects and update the older ones as well.
Worth Reading: Java Convert String to int
Please lend us your support by sharing this post with your friends and on the social media platforms.
Let’s now end the quiz with a famous quote from Niklaus Wirth who has been a prominent Swiss computer scientist.
Programming is usually taught by examples.
Best,
TechBeamers