🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Welcome to the Java String Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge on the Java String topic. Each question has a correct and brief explanation.
1. What is the output of the following Java code snippet?
String str = "Hello World";
System.out.println(str.length());
Answer:
Explanation:
The length() method returns the number of characters in the string, including spaces. "Hello World" has 11 characters.
2. What does this Java code snippet output?
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);
Answer:
Explanation:
'==' compares references, not values. s1 and s2 refer to different objects, so the output is false.
3. Identify the output of the following code:
String text = "JavaProgramming";
System.out.println(text.substring(4, 7));
Answer:
Explanation:
substring(int startIndex, int endIndex) returns the string from startIndex (inclusive) to endIndex (exclusive). Here, it's "Pro".
4. What will be printed by this Java code?
String str1 = "Java";
String str2 = "Java";
System.out.println(str1.equals(str2));
Answer:
Explanation:
The equals() method compares the values of strings. str1 and str2 have the same value, so the result is true.
5. What does this code snippet output?
String word = "Hello";
word.concat(" World");
System.out.println(word);
Answer:
Explanation:
Strings in Java are immutable. concat() creates a new string, which is not assigned to a word. So, the word remains unchanged.
6. What is the result of executing this code?
String a = "Java";
String b = " Programming";
String c = a + b;
System.out.println(c);
Answer:
Explanation:
The + operator concatenates two strings, creating a new string "Java Programming".
7. What will the following Java code snippet output?
String str = "Java";
System.out.println(str.toUpperCase());
Answer:
Explanation:
The toUpperCase() method converts all characters in the string to upper case.
8. What does the following code snippet print?
String text = "Java";
System.out.println(text.replace('a', 'x'));
Answer:
Explanation:
The replace(char oldChar, char newChar) method replaces all occurrences of the specified old character with the new character.
9. Determine the output of this Java code:
String s = "programming";
System.out.println(s.charAt(3));
Answer:
Explanation:
charAt(int index) returns the char value at the specified index. Indexing starts from 0, so s.charAt(3) is 'g'.
10. What is the result of the following code snippet?
String first = "Java";
String second = "Python";
System.out.println(first.compareTo(second));
Answer:
Explanation:
compareTo() compares two strings lexicographically. Since "Java" is lexicographically less than "Python", it returns a negative number.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment