Practice high-yield Programming & Software Development MCQs on Strings — Part 2 with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
40
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
48 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
40
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/40 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 40 questions answered.
Question 1 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The String method compareTo() returns
Correct Answer: Option C — an int value
Detailed Academic Explanation:
an int value is the correct answer to “The String method compareTo() returns”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 2 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following println statement? String str1 = "Hellow"; System.out.println(str1.indexOf('t'));
Correct Answer: Option D — -1
Detailed Academic Explanation:
-1 is the correct answer to “What is the output of the following println statement? String str1 = "Hellow"; System.out.println(str1.indexOf('t'));”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 3 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The class string belongs to ................. package.
Correct Answer: Option B — java.lang
Detailed Academic Explanation:
java.lang is the correct answer to “The class string belongs to ................. package.”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 4 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output? public class Test{ public static void main (String[] args){ String test = "a1b2c3"; String[] tokens = test.split("\\d"); for(String s: tokens) System.out.print(s); } }
Correct Answer: Option A — abc
Detailed Academic Explanation:
abc is the correct answer to “What will be the output? public class Test{ public static void main (String[] args){ String test = "a1b2c3"; String[] tokens = test.split("\\d"); for(String s: tokens) System.out.print(s); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following program code? public class Test{ public static void main(String args[]){ String s = "what"; StringBuffer sb = new StringBuffer("what"); System.out.print(sb.equals(s)+","+s.equals(sb)); } }
Correct Answer: Option D — false,false
Detailed Academic Explanation:
false,false is the correct answer to “What will be the output of the following program code? public class Test{ public static void main(String args[]){ String s = "what"; StringBuffer sb = new StringBuffer("what"); System.out.print(sb.equals(s)+","+s.equals(sb)); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 6 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output? public class Test{ public static void main(String args[]){ Object myObj = new String[]{"one", "two", "three"}; { for(String s : (String[])myObj) System.out.print(s + "."); } } }
Correct Answer: Option A — one.two.three.
Detailed Academic Explanation:
one.two.three. is the correct answer to “What will be the output? public class Test{ public static void main(String args[]){ Object myObj = new String[]{"one", "two", "three"}; { for(String s : (String[])myObj) System.out.print(s + "."); } } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 7 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Determine output: public class Test{ public static void main(String args[]){ String str = null; if(str.length() == 0){ System.out.print("1"); } else if(str == null){ System.out.print("2"); } else{ System.out.print("3"); } } }
Correct Answer: Option E — An exception is thrown at runtime.
Detailed Academic Explanation:
An exception is thrown at runtime. is the correct answer to “Determine output: public class Test{ public static void main(String args[]){ String str = null; if(str.length() == 0){ System.out.print("1"); } else if(str == null){ System.out.print("2"); } else{ System.out.print("3"); } } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 8 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these methods is used to compare a specific region inside a string with another specific region in another string?
Correct Answer: Option C — regionMatches()
Detailed Academic Explanation:
regionMatches() is the correct answer to “Which of these methods is used to compare a specific region inside a string with another specific region in another string”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class output { public static void main(String args[]) { StringBuffer s1 = new StringBuffer("Hello"); s1.insert(1,"Java"); System.out.println(s1); } }
Correct Answer: Option D — HJavaello
Detailed Academic Explanation:
HJavaello is the correct answer to “What will be the output of the following Java code? class output { public static void main(String args[]) { StringBuffer s1 = new StringBuffer("Hello"); s1.insert(1,"Java"); System.out.println(s1); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class output { public static void main(String args[]) { String c = "Hello i love java"; boolean var; var = c.startsWith("hello"); System.out.println(var); } }
Correct Answer: Option B — false
Detailed Academic Explanation:
false is the correct answer to “What will be the output of the following Java code? class output { public static void main(String args[]) { String c = "Hello i love java"; boolean var; var = c.startsWith("hello"); System.out.println(var); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 11 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these data type value is returned by equals() method of String class?
Correct Answer: Option C — boolean
Detailed Academic Explanation:
boolean is the correct answer to “Which of these data type value is returned by equals() method of String class”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 12 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following are incorrect form of StringBuffer class constructor?
Correct Answer: Option D — StringBuffer(int size , String str)
Detailed Academic Explanation:
StringBuffer(int size , String str) is the correct answer to “Which of the following are incorrect form of StringBuffer class constructor”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class output { public static void main(String args[]) { String c = " Hello World "; String s = c.trim(); System.out.println("\""+s+"\""); } }
Correct Answer: Option C — "Hello World"
Detailed Academic Explanation:
"Hello World" is the correct answer to “What will be the output of the following Java program? class output { public static void main(String args[]) { String c = " Hello World "; String s = c.trim(); System.out.println("\""+s+"\""); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 14 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class output { public static void main(String args[]) { String a = "hello i love java"; System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+" "+a.lastIndexOf('v')); } }
Correct Answer: Option D — 1 14 8 15
Detailed Academic Explanation:
1 14 8 15 is the correct answer to “What will be the output of the following Java program? class output { public static void main(String args[]) { String a = "hello i love java"; System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+" "+a.lastIndexOf('v')); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 15 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these class is used to create an object whose character sequence is mutable?
Correct Answer: Option B — StringBuffer()
Detailed Academic Explanation:
StringBuffer() is the correct answer to “Which of these class is used to create an object whose character sequence is mutable”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 16 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class output { public static void main(String args[]) { String s1 = "Hello i love java"; String s2 = new String(s1); System.out.println((s1 == s2) + " " + s1.equals(s2)); } }
Correct Answer: Option D — false true
Detailed Academic Explanation:
false true is the correct answer to “What will be the output of the following Java code? class output { public static void main(String args[]) { String s1 = "Hello i love java"; String s2 = new String(s1); System.out.println((s1 == s2) + " " + s1.equals(s2)); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 17 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class output { public static void main(String args[]) { String s1 = "Hello"; String s2 = s1.replace('l','w'); System.out.println(s2); } }
Correct Answer: Option D — hewwo
Detailed Academic Explanation:
hewwo is the correct answer to “What will be the output of the following Java program? class output { public static void main(String args[]) { String s1 = "Hello"; String s2 = s1.replace('l','w'); System.out.println(s2); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 18 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these method of class String is used to remove leading and trailing whitespaces?
Correct Answer: Option B — trim()
Detailed Academic Explanation:
trim() is the correct answer to “Which of these method of class String is used to remove leading and trailing whitespaces”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 19 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class output { public static void main(String args[]) { StringBuffer sb=new StringBuffer("Hello"); sb.replace(1,3,"Java"); System.out.println(sb); } }
Correct Answer: Option C — HJavalo
Detailed Academic Explanation:
HJavalo is the correct answer to “What will be the output of the following Java code? class output { public static void main(String args[]) { StringBuffer sb=new StringBuffer("Hello"); sb.replace(1,3,"Java"); System.out.println(sb); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 20 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In the following Java code, which code fragment should be inserted at line 3 so that the output will be: "123abc 123abc"? 1. StringBuilder sb1 = new StringBuilder("123"); 2. String s1 = "123"; 3. // insert code here 4. System.out.println(sb1 + " " + s1);
Correct Answer: Option D — sb1.append("abc"); s1 = s1.concat("abc")
Detailed Academic Explanation:
sb1.append("abc"); s1 = s1.concat("abc") is the correct answer to “In the following Java code, which code fragment should be inserted at line 3 so that the output will be: "123abc 123abc"? 1. StringBuilder sb1 = new StringBuilder("123"); 2. String s1 = "123"; 3. // insert code here 4. System.out.println(sb1 + " " + s1);”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these methods of class String is used to check whether a given object starts with a particular string literal?
Correct Answer: Option A — startsWith()
Detailed Academic Explanation:
startsWith() is the correct answer to “Which of these methods of class String is used to check whether a given object starts with a particular string literal”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 22 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these method of class StringBuffer is used to find the length of current character sequence?
Correct Answer: Option A — length()
Detailed Academic Explanation:
length() is the correct answer to “Which of these method of class StringBuffer is used to find the length of current character sequence”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 23 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class output { public static void main(String args[]) { String c = "Hello i love java"; int start = 2; int end = 9; char s[]=new char[end-start]; c.getChars(start,end,s,0); System.out.println(s); } }
Correct Answer: Option D — llo i l
Detailed Academic Explanation:
llo i l is the correct answer to “What will be the output of the following Java code? class output { public static void main(String args[]) { String c = "Hello i love java"; int start = 2; int end = 9; char s[]=new char[end-start]; c.getChars(start,end,s,0); System.out.println(s); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of these class is superclass of String and StringBuffer class?
Correct Answer: Option B — java.lang
Detailed Academic Explanation:
java.lang is the correct answer to “Which of these class is superclass of String and StringBuffer class”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 25 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of this method of class StringBuffer is used to reverse sequence of characters?
Correct Answer: Option A — reverse()
Detailed Academic Explanation:
reverse() is the correct answer to “Which of this method of class StringBuffer is used to reverse sequence of characters”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 26 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will s2 contain after following lines of Java code? StringBuffer s1 = "one"; StringBuffer s2 = s1.append("two")
Correct Answer: Option C — onetwo
Detailed Academic Explanation:
onetwo is the correct answer to “What will s2 contain after following lines of Java code? StringBuffer s1 = "one"; StringBuffer s2 = s1.append("two")”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 27 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of this method of class StringBuffer is used to concatenate the string representation to the end of invoking string?
Correct Answer: Option B — append()
Detailed Academic Explanation:
append() is the correct answer to “Which of this method of class StringBuffer is used to concatenate the string representation to the end of invoking string”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 28 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the value returned by function compareTo() if the invoking string is greater than the string compared?
Correct Answer: Option C — value greater than zero
Detailed Academic Explanation:
value greater than zero is the correct answer to “What is the value returned by function compareTo() if the invoking string is greater than the string compared”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 29 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class String_demo { public static void main(String args[]) { char chars[] = {'a', 'b', 'c'}; String s = new String(chars); System.out.println(s); } }
Correct Answer: Option D — abc
Detailed Academic Explanation:
abc is the correct answer to “What will be the output of the following Java program? class String_demo { public static void main(String args[]) { char chars[] = {'a', 'b', 'c'}; String s = new String(chars); System.out.println(s); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 30 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of this method of class StringBuffer is used to get the length of the sequence of characters?
Correct Answer: Option A — length()
Detailed Academic Explanation:
length() is the correct answer to “Which of this method of class StringBuffer is used to get the length of the sequence of characters”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 31 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class output { public static void main(String args[]) { String s1 = "one"; String s2 = s1 + " two"; System.out.println(s2); } }
Correct Answer: Option C — one two
Detailed Academic Explanation:
one two is the correct answer to “What will be the output of the following Java program? class output { public static void main(String args[]) { String s1 = "one"; String s2 = s1 + " two"; System.out.println(s2); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 32 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of this method of class String is used to extract a substring from a String object?
Correct Answer: Option A — substring()
Detailed Academic Explanation:
substring() is the correct answer to “Which of this method of class String is used to extract a substring from a String object”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 33 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class output { public static void main(String args[]) { String s1 = "Hello World"; String s2 = s1.substring(0 , 4); System.out.println(s2); } }
Correct Answer: Option A — Hell
Detailed Academic Explanation:
Hell is the correct answer to “What will be the output of the following Java program? class output { public static void main(String args[]) { String s1 = "Hello World"; String s2 = s1.substring(0 , 4); System.out.println(s2); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 34 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class output { public static void main(String args[]) { char ch; ch = "hello".charAt(1); System.out.println(ch); } }
Correct Answer: Option B — e
Detailed Academic Explanation:
e is the correct answer to “What will be the output of the following Java code? class output { public static void main(String args[]) { char ch; ch = "hello".charAt(1); System.out.println(ch); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 35 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class String_demo { public static void main(String args[]) { int ascii[] = { 65, 66, 67, 68}; String s = new String(ascii, 1, 3); System.out.println(s); } }
Correct Answer: Option B — BCD
Detailed Academic Explanation:
BCD is the correct answer to “What will be the output of the following Java program? class String_demo { public static void main(String args[]) { int ascii[] = { 65, 66, 67, 68}; String s = new String(ascii, 1, 3); System.out.println(s); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 36 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class output { public static void main(String args[]) { String s = "Hello World"; int i = s.indexOf('o'); int j = s.lastIndexOf('l'); System.out.print(i + " " + j); } }
Correct Answer: Option C — 4 9
Detailed Academic Explanation:
4 9 is the correct answer to “What will be the output of the following Java program? class output { public static void main(String args[]) { String s = "Hello World"; int i = s.indexOf('o'); int j = s.lastIndexOf('l'); System.out.print(i + " " + j); } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 37 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the string contained in s after following lines of Java code? StringBuffer s new StringBuffer("Hello"); s.deleteCharAt(0);
Correct Answer: Option B — ello
Detailed Academic Explanation:
ello is the correct answer to “What is the string contained in s after following lines of Java code? StringBuffer s new StringBuffer("Hello"); s.deleteCharAt(0);”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 38 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java program? class output { class output { public static void main(String args[]) { char c[]={'A', '1', 'b' ,' ' ,'a' , '0'}; for (int i = 0; i < 5; ++i) { i++; if(Character.isDigit(c[i])) System.out.println(c[i]+" is a digit"); if(Character.isWhitespace(c[i])) System.out.println(c[i]+" is a Whitespace character"); if(Character.isUpperCase(c[i])) System.out.println(c[i]+" is an Upper case Letter"); if(Character.isLowerCase(c[i])) System.out.println(c[i]+" is a lower case Letter"); i++; } } }
Correct Answer: Option C — 1 is a digit a is a lower case Letter
Detailed Academic Explanation:
1 is a digit a is a lower case Letter is the correct answer to “What will be the output of the following Java program? class output { class output { public static void main(String args[]) { char c[]={'A', '1', 'b' ,' ' ,'a' , '0'}; for (int i = 0; i < 5; ++i) { i++; if(Character.isDigit(c[i])) System.out.println(c[i]+" is a digit"); if(Character.isWhitespace(c[i])) System.out.println(c[i]+" is a Whitespace character"); if(Character.isUpperCase(c[i])) System.out.println(c[i]+" is an Upper case Letter"); if(Character.isLowerCase(c[i])) System.out.println(c[i]+" is a lower case Letter"); i++; } } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 39 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of this method of class String is used to obtain a length of String object?
Correct Answer: Option D — length()
Detailed Academic Explanation:
length() is the correct answer to “Which of this method of class String is used to obtain a length of String object”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
Question 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Java code? class output { public static void main(String args[]) { char c[]={'a', '1', 'b' ,' ' ,'A' , '0'}; for (int i = 0; i < 5; ++i) { if(Character.isDigit(c[i])) System.out.println(c[i]+" is a digit"); if(Character.isWhitespace(c[i])) System.out.println(c[i]+" is a Whitespace character"); if(Character.isUpperCase(c[i])) System.out.println(c[i]+" is an Upper case Letter"); if(Character.isLowerCase(c[i])) System.out.println(c[i]+" is a lower case Letter"); i=i+3; } } }
Correct Answer: Option C — a is a lower case Letter A is an upper case Letter
Detailed Academic Explanation:
a is a lower case Letter A is an upper case Letter is the correct answer to “What will be the output of the following Java code? class output { public static void main(String args[]) { char c[]={'a', '1', 'b' ,' ' ,'A' , '0'}; for (int i = 0; i < 5; ++i) { if(Character.isDigit(c[i])) System.out.println(c[i]+" is a digit"); if(Character.isWhitespace(c[i])) System.out.println(c[i]+" is a Whitespace character"); if(Character.isUpperCase(c[i])) System.out.println(c[i]+" is an Upper case Letter"); if(Character.isLowerCase(c[i])) System.out.println(c[i]+" is a lower case Letter"); i=i+3; } } }”. This question is standard in competitive examination syllabi for Strings.
💡Key Conceptual Anchor:Strings
✓Link copied to clipboard
Continue Your Practice
Advance Html — Part 1
Master the next curriculum module with 40 exam-standard MCQs.
💡Examiner's Advisory for Programming & Software Development
In competitive commission exams (FPSC, PPSC, KPPSC, SPSC, CSS, PMS), questions are frequently phrased with distractor options designed to catch rote learners. Whenever you answer an MCQ, read the Detailed Academic Explanation and Conceptual Anchor to understand why the remaining options are incorrect.