OCAJP 8 – StringBuilder Mock Questions

OCAJP and OCPJP Certifications
In my previous post, I have reviewed an OCAJP 8 mock exam simulator which I have used for the exam preparation.
One of the exam objectives in OCAJP 8 exam is “Manipulate data using the StringBuilder class and its methods”. This exam objective tests your knowledge on how to write StringBuilder class and use its methods. I have prepared a set of good mock questions for the StringBuilder class that might be useful for the readers who are preparing for the OCAJP 8 certification exam.

1. What is the output ?

public class Test {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder(“Java”);
String s2 = s1.toString();
String s3 = s1.toString();
StringBuilder s4 = s1;
System.out.println((s2 == s3)+ ”  “+ (s1 == s4) );
} }
A. true true
B. false true
C. true false
D. false false
E. Compiler error

2. Which of the following is not a method of StringBuilder ?

A. trim ( ) 
B. ensureCapacity( )
C. append( int a)
D. substring(int i)
E. setLength(int c)

3.How can you initialize a StringBuilder to have a capacity of at least 100 characters?

Select 2 options 
A. StringBuilder sb = new StringBuilder(100); 
B. StringBuilder sb = StringBuilder.getInstance(100);
C. StringBuilder sb = new StringBuilder(); sb.setCapacity(100); 
D. StringBuilder sb = new StringBuilder(); sb.ensureCapacity(100);

4.What will be the result of attempting to compile and run the following program?

public class TestClass{ 
public static void main(String args[ ] ){ 
StringBuilder sb = new StringBuilder(“OCAJP8”); 
sb.setLength(4); 
sb.setLength(15); 
System.out.println(sb.length()); 
} } 
Select 1 option 
A. It will print 4. 
B. It will print 15. 
C. It will print 11. 
D. Compilation error. 
E. None of the above

5.What is the output ?

public class Test {
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder(“OCAJP8”);
StringBuilder sb2 = sb1 ;
sb1.append(“J”);
System.out.println(sb1 + ” “+ sb2+ ” ” + (sb1==sb2)); 
}
}
A. OCAJP8J OCAJP8J true
B. OCAJP8J OCAJP8 false
C. OCAJP8 OCAJP8  true
D. OCAJP8J OCAJP8J false
E. Compiler error

6. What is the output ?

public class Test {

public static void main(String[] args) {

StringBuilder sb = new StringBuilder(5 + 7 + “Java” + 4 + 5);
sb.append(sb.delete(1, 4));
System.out.println(sb); 
}

}

A. 12Java451va45
B.1va451va45
C.57Java455va45
D.5va455va45
E.Compiler Error 
F. Exception at Run time

7. What is the output ?

public class Test {
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder(“OCAJP8”);
sb1.subSequence(2, 4);
sb1.deleteCharAt(3);
sb1.reverse( );
System.out.println(sb1);

}
}
A. PCO
B. OCAJP8
C. 8PACO
D. 8PJACO
E. Compiler error
F. Exception at Run time

8. What is the output ?

public class Test{

public static void main(String[] args) {
int s = 0;
StringBuilder sb = new StringBuilder(“1234567”);
s += sb.substring(1, 2).length( );
s += sb.substring(6, 6).length( );
s += sb.substring(6, 5).length( );
System.out.println(s);
}

}
A. 1
B. 2
C. 3
D. 7
E. An exception is thrown.
F. The code does not compile.

9.What is the result of the following code?

StringBuilder sb= new StringBuilder(“0123456789”);
sb.delete(2, 8);
sb.append(“-“).insert(2, “+”);
System.out.println(sb);
A. 01+89–
B. 012+9–
C. 012+–9
D. 0123456789
E. An exception is thrown.
F. The code does not compile.

10.What is the result of the following code?

StringBuilder b = “rumble”;
b.append(4).deleteCharAt(3).delete(3, b.length() – 1);
System.out.println(b);
A. rum
B. rum4
C. rumb4
D. rumble4
E. An exception is thrown.
F. The code does not compile.

11.Which of the following can replace line 4 to print “avaJ”? (Choose all that apply)

StringBuilder sb= new StringBuilder(“Java”);
// INSERT CODE HERE
System.out.println(sb);
A. sb.reverse();
B. sb.append(“vaJ$”).substring(0, 4);
C. sb.append(“vaJ$”).delete(0, 3).deleteCharAt(sb.length() – 1);
D. sb.append(“vaJ$”).delete(0, 3).deleteCharAt(sb.length());
E. None of the above.

12) Which are true about StringBuilder class ( choose all that apply ) 

A. StringBuilder is not a final class
B. StringBuilder is a final class
C. StringBuilder extends String
D. StringBuilder object is Immutable 
E. StringBuilder object is Mutable
F. StringBuilder implements CharSequence 
G. StringBuilder is thread safe.
H. StringBuilder is not a thread safe.

Answers

1) Correct Option :  B
First String Builder object is created assigned to reference s1 . toString method is called on s1. Remember, toString is always creats a new object every time. s2, s3 are referring to two different objects, so s2==s3 gives false because == operator compares only references but not object contents . Next, s1 is assigned to s4.  s1, s4 are referencing to the same object. So s1==s4 gives true.
Find details about toString method in Java Doc here 
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#toString–

2) Correct Option :  A 
To answer this type of questions you need to remember StringBuilder class methods which comes with practice and revising multiple times. To know full list of methods in StringBuilder class . Follow this link
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

3) Correct Options :  A, D
To answer this type of questions you need to remember StringBuilder class constructors and methods which are used to increase StringBuilder object capacity and also make sure you understand the difference between StringBuilder object capacity and size. Follow this link
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

4) Correct Option :  B
StringBuilder object is created with content “OCAJP8” and its size is 6 now.
If stringBuilder object size is more than the setLength method argument then 
setLength method will remove extra characters in the object starting from last index position. If stringBuilder object size is less than the setLength method argument then setLength method will add extra spaces in the object starting from last index+1 position.
Here setLength(4) will remove P8 characters and makes object size is 4 . After that setLength(15) will makes object size is 15 by adding 11 extra spaces in the object starting from last index+1 position. Follow this link
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#setLength-int-

5) Correct Option : A
StringBuilder object is created with content “OCAJP8”and reference is sb1.
sb1 is assigned to sb2 , so sb1, sb2 are referencing to the same object. The changes made through one reference will reflect in another one. sb1.append(“J”); will append J at the end. If you print sb1 or sb2, the result is same. sb1==sb2 gives true because both references pointing to the same object. Follow this link https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#append-java.lang.String-

6) Correct Option : B
To answer this question you need to aware about + operator working properly.
1) If both operands numeric values, It performs normal addition.
2) If either operand is String , It performs String concatenation.
StringBuilder object is created with content 12Java45. sb.delete(1, 4) will get remove characters from 1 index to 3 index . Now original object content is 1va45. sb.delete(1, 4) will return StringBuilder Object with content 1va45 which is passes as argument to append method which adds it to the end of object content. So the output is 1va451va45. Follow this link https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

7)  Correct Option : C
StringBuilder object is created with content OCAJP8. sb1. subSequence(2, 4) will return StringBuilder object with two characters and it doesn’t has no any reference , it is garbage collected. sb1.deleteCharAt(3) will remove character at 3rd index i.e J . sb1.reverse( ) will reverse the character sequence in the object. So the output is 8PACO . Follow this link :
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#append-java.lang.String-

8)  Correct Option : E
sb.substring(6, 5) will generate StringIndexOutOfBoundsException at runtime, because the arguments to substring method are invalid.
Java doc says substring throws IndexOutOfBoundsException. But practically it throws StringIndexOutOfBoundsException. Follow this link :
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#substring-int-int-

9) Correct Option : A
StringBuilder object is created with content 0123456789.  sb.delete(2, 8) will remove characters from index 2 to 7. Now object contains 0189. sb.append(“-“) will add ‘-’ at the end. Now object contains 0189- .  The insert(2, “+”) will add ‘+’ at the 2nd index. Now object contains 01+89-  
Follow this link :Follow this link :
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html-

10) Correct Option :  F 

StringBuilder b = “rumble” ; will generate compile time error. Because “rumble” is String object and is assigned StringBuilder  type reference. String and StringBuilder  are incompatible data types. 

11) Correct Options : A, C
The question is asking you about to reverse the character sequence.
sb.reverse(); can reverse the character sequence.
sb.append(“vaJ$”).delete(0, 3).deleteCharAt(sb.length() – 1) can also reverse the character sequence. Here append(“vaJ$”) will append the string at the end of object content. Now object content is JavavaJ$ .  delete(0, 3) will remove characters from index 0 to 2. Now object content is avaJ$ . deleteCharAt(sb.length() – 1) removes last index character. So the output is avaJ .

12) Correct Options : B,E,F,H
Follow this link :
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html-I hope this post will help you to prepare for OCAJP 8 . I will be sharing more such posts in future.

Related Posts

  • Top 10 Oracle certifications
    22 March

    10 Best Oracle Certification Courses, Training, Classes & Tutorials Online

    Table of Contents6 Categories Of Oracle CertificationsOracle Certified Junior Associate (OJA)Oracle Certified Associate (OCA)Oracle Certified Professional (OCP)Oracle Certified Master (OCM)Oracle Certified Expert (OCE)Oracle Certified Specialist (OCS)Top 10 Oracle CertificationsOracle Java CertificationOracle Database 12c SQL Certified AssociateA Complete Oracle SQL CertificationOracle Autonomous Database Cloud Certified SpecialistOracle Database: Administration WorkshopOracle PL/SQL Is My GameOracle Cloud Infrastructure Foundations […]

  • 14 October

    Top 3 OCPJP 8 books for 1Z0-808 and 1Z0-809 Exam

    Table of ContentsIntroductionJava Programmer II Study Guide: Exam 1Z0-809Author(s): Esteban HerreraOCP: Oracle Certified Professional Java SE 8 Programmer II Study GuideAuthor(s): Jeanne Boyarsky and Scott SelikoffOracle Certified Professional Java SE 8 Programmer Exam 1Z0-809: A Comprehensive OCPJP 8 Certification GuideAuthor(s): SG Ganesh, Hari Kiran Kumar, Tushar Sharma In this article, we will see top 3 […]

  • 15 September

    How to become oracle certified Java programmer

    Table of ContentsIntroductionWhat is OCPJP?Levels and Pre-RequisitesJava SE 8 Programmer Level 1Java SE 8 Programmer Level 2Syllabus for OCPJP 8Java Class DesignAdvanced Java Class DesignGenerics and CollectionsLambda Interfaces ( built in )Stream APIAssertions and ExceptionsDate/Time APII/O FundamentalsFile I/OConcurrencyJDBCLocalizationPreparing for the ExaminationLearning about the examination paths and processesFamiliarizing with the latest concepts added into Java, with […]

  • 18 August

    How to prepare for OCMJEA Java architect certification exam

    Table of ContentsWhat is the format of OCMJEA exam?What is the prerequisite for preparing OCMJEA exam?What is the syllabus of OCMJEA?How to prepare for multiple choice question level for OCMJEA exam correctly?OCM Java EE 6 Enter Architect Guide by Paul R.Allen and Joseph J.BambaraOracle Certified Master, Java EE Enterprise Architect Practical Guide by Amritendu DeHow […]

  • 14 August

    Top 4 OCPJP 7 books for 1Z0-804 and 1Z0-805 Exam

    Table of ContentsCertificationsOCPJP 7 BooksOCJP Java SE Programmer by Kathy Sierra and Bert BatesA Comprehensive OCPJP 7 Certification Guide by S G Ganesh and Tushar SharmaJava Practice Questions: Oracle Certified Professional by Esteban HerraraOCP Java SE 7 Programmer II Certification Guide by Mala GuptaOnline sourcesOther BooksHead First Java by Kathy Sierra and Bert BatesEffective Java […]

  • 11 December

    How to pass OCPJP java certification with the help of Whizlabs

    Table of ContentsWhat is OCPJP 8?Why Whizlabs OCPJP 8?Some Additional Information for OCPJP Preparation I have already shared about Whizlabs OCAJP 8 exam simulator in my previous post. I have looked further to and found it useful too, hence sharing it. OCPJP is tougher exam than OCAJP hence you will need more practice to pass it than […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.