0% found this document useful (0 votes)
12 views8 pages

StringBuilder StringAPI

The document contains a series of direct and scenario-based questions regarding the use of StringBuilder, String API, and Java's date and time classes. It covers key methods, advantages, and functionalities of these classes, along with the correct answers to each question. This serves as a study guide for understanding string manipulation and date handling in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

StringBuilder StringAPI

The document contains a series of direct and scenario-based questions regarding the use of StringBuilder, String API, and Java's date and time classes. It covers key methods, advantages, and functionalities of these classes, along with the correct answers to each question. This serves as a study guide for understanding string manipulation and date handling in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

StringBuilder

Direct Questions

1. What is the primary advantage of using StringBuilder over String?

◦ A) It is immutable

◦ B) It consumes more memory

◦ C) It allows mutable strings

◦ D) It is slower than String

◦ Answer: C

2. Which method is used to append a string in StringBuilder?

◦ A) add()

◦ B) append()

◦ C) join()

◦ D) include()

◦ Answer: B

3. How can you reverse the contents of a StringBuilder?

◦ A) inverse()

◦ B) backward()

◦ C) reverse()

◦ D) ip()

◦ Answer: C

4. Which method returns the length of a StringBuilder?

◦ A) size()

◦ B) length()

◦ C) getLength()

◦ D) count()

◦ Answer: B

5. StringBuilder belongs to which package?


fl
◦ A) java.util

◦ B) java.io

◦ C) java.lang

◦ D) java.string

◦ Answer: C

Scenario-Based Questions

6. You are building a report by concatenating multiple strings inside a loop. Which class
provides better performance?

◦ A) String

◦ B) StringBuilder

◦ C) StringBuffer

◦ D) Arrays

◦ Answer: B

7. After appending values "Hello" and "World" using StringBuilder, what will be the
result?
StringBuilder sb = new StringBuilder();

8. sb.append("Hello");
9. sb.append("World");
10. System.out.println(sb);
◦ A) Hello World

◦ B) HelloWorld

◦ C) WorldHello

◦ D) Error

◦ Answer: B

11. How do you insert a string "Java" at index 4 in a StringBuilder object initialized with
"HelloWorld"?

◦ A) insert(4, "Java")

◦ B) add(4, "Java")

◦ C) put(4, "Java")

◦ D) append(4, "Java")

◦ Answer: A
12. Which method deletes characters from index 2 to 5 in a StringBuilder?

◦ A) delete(2, 5)

◦ B) remove(2, 5)

◦ C) erase(2, 5)

◦ D) clear(2, 5)

◦ Answer: A

13. You want to replace a portion of a string using StringBuilder. Which method should
you use?

• A) setValue()

• B) replace()

• C) change()

• D) modify()

• Answer: B

String API

Direct Questions

1. Which method is used to get the character at a speci c index?

◦ A) charAt()

◦ B) getChar()

◦ C) indexOf()

◦ D) subString()

◦ Answer: A

2. What does the equals() method do in String class?

◦ A) Compares references

◦ B) Compares content

◦ C) Checks length

◦ D) Returns ASCII values

◦ Answer: B
fi
3. Which method converts a string to lower case?

◦ A) toLowerCase()

◦ B) lower()

◦ C) downCase()

◦ D) changeCase()

◦ Answer: A

4. How do you split a string into an array?

◦ A) divide()

◦ B) separate()

◦ C) split()

◦ D) tokenize()

◦ Answer: C

5. What is the output of "Java".substring(1,3)?

◦ A) Ja

◦ B) av

◦ C) va

◦ D) Error

◦ Answer: B

Scenario-Based Questions

6. You want to nd the index of the word "world" in the string "Hello world". Which method
will you use?

◦ A) contains()

◦ B) nd()

◦ C) indexOf("world")

◦ D) substring()

◦ Answer: C

7. How would you compare two strings ignoring their case?

◦ A) equals()
fi
fi
◦ B) compareTo()

◦ C) equalsIgnoreCase()

◦ D) match()

◦ Answer: C

8. You have a string with leading and trailing spaces. Which method removes them?

◦ A) strip()

◦ B) trim()

◦ C) clear()

◦ D) removeSpaces()

◦ Answer: B

9. What is the result of "Hello".concat("World")?

◦ A) Hello World

◦ B) HelloWorld

◦ C) WorldHello

◦ D) Error

◦ Answer: B

10. You want to convert "123" into an integer. Which class/method is used?
• A) String.toInt()

• B) Integer.parseInt("123")

• C) parse("123")

• D) stringToInt("123")

• Answer: B

Date, Time, and Numeric Objects

Direct Questions

1. Which class is used to get current date and time in Java 8?

◦ A) DateTime

◦ B) LocalDateTime
◦ C) Calendar

◦ D) Clock

◦ Answer: B

2. What method returns the current system date?

◦ A) now()

◦ B) today()

◦ C) currentDate()

◦ D) getDate()

◦ Answer: A

3. Which package contains the modern date and time API?

◦ A) java.util

◦ B) java.sql

◦ C) java.time

◦ D) java.date

◦ Answer: C

4. Which class is used for formatting dates?

◦ A) DateFormatter

◦ B) SimpleDateFormat

◦ C) DateParser

◦ D) DateFormat

◦ Answer: B

5. How do you represent a speci c date in Java?

◦ A) new Date("2023-12-01")

◦ B) LocalDate.of(2023, 12, 1)

◦ C) createDate(2023, 12, 1)

◦ D) new LocalDate(2023, 12, 1)

◦ Answer: B

Scenario-Based Questions
fi
6. You want to calculate the number of days between two dates. Which class helps?

◦ A) ChronoUnit

◦ B) Duration

◦ C) Period

◦ D) Both A and C

◦ Answer: D

7. To represent time without date, which class should be used?

◦ A) Date

◦ B) LocalDate

◦ C) LocalTime

◦ D) Calendar

◦ Answer: C

8. What is the output of LocalTime.now().getHour()?

◦ A) Current hour in 24-hour format

◦ B) Hour in 12-hour format

◦ C) Minutes only

◦ D) Seconds only

◦ Answer: A

9. You want to parse a date string "2025-07-26". Which class is suitable?

◦ A) LocalDate.parse("2025-07-26")

◦ B) Date.valueOf("2025-07-26")

◦ C) DateTimeFormatter.parse("2025-07-26")

◦ D) Date.parse("2025-07-26")

◦ Answer: A

10. How do you get the current year?


• A) LocalDate.now().getYear()

• B) Date.getYear()

• C) Calendar.getYear()
• D) now().year()

• Answer: A

You might also like