Strings & Text Processing MCQs for NSCT
Strings and text processing covers how C++, Java and Python store text, which string methods you use to search, split and change it, why strings are immutable in Java and Python, and how to format output. It is topic 7 of the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 47.8% in Programming, per the HEC NSCT report and APP.
Most string questions are output questions, so they build on data types and operators: a single character in the wrong place changes the answer.
How each language stores text
| C | C++ | Java | Python 3 | |
|---|---|---|---|---|
| Type | char array |
std::string |
String |
str |
| Mutable? | Yes | Yes | No | No |
| End of text | '\0' terminator |
Stored length | Stored length | Stored length |
| Content equality | strcmp(a, b) == 0 |
a == b |
a.equals(b) |
a == b |
A C string "hello" takes 6 bytes because of the terminating null character. Exam angle: in Java, == on two String objects compares references (whether they are the same object), not characters, so always use equals().
String methods
A string method is a built-in operation on a string object. The ones that appear most often:
| Task | Python | Java |
|---|---|---|
| Length | len(s) |
s.length() |
| Character at i | s[i] |
s.charAt(i) |
| Part of the string | s[2:5] |
s.substring(2, 5) |
| Find | s.find("x") (−1 if missing) |
s.indexOf("x") (−1 if missing) |
| Split and join | s.split(","), ",".join(parts) |
s.split(","), String.join(",", parts) |
| Change case | s.upper(), s.lower() |
s.toUpperCase(), s.toLowerCase() |
Slices and substring are half-open: the end index is excluded. So "network"[0:3] is "net", and s[::-1] reverses a Python string. Exam angle: split(",") keeps empty strings between adjacent commas, while Python's split() with no argument drops them.
Immutability
An immutable object cannot change after it is created. Java String and Python str are immutable; every "change" returns a new string.
name = "ali"
name.upper() # result thrown away
print(name) # ali
name = name.upper()
print(name) # ALI
Immutability makes strings safe to share and to use as dictionary keys, and it lets Java keep a string pool of literals. The cost is that building text with += in a loop copies the whole string each time, which is O(n²) overall. Use StringBuilder in Java or "".join(parts) in Python. C++ std::string is mutable, so s[0] = 'b' is legal there. Exam angle: "the method ran but the string did not change" nearly always means the return value was not assigned.
Formatting
Formatting turns values into text with a set width, precision or alignment.
pi, n = 3.14159, 42
print(f"{pi:.3f}|{n:5d}|{n:<5d}|{n:05d}") # 3.142| 42|42 |00042
Java's String.format("%.3f", pi) and C's printf use the same % codes: d for integers, f for floating point, s for strings, with width before the dot and precision after it. Exam angle: count the padding spaces exactly; width is the total field size, not the number of spaces added.
Text processing basics
Text processing means cleaning and analysing text: trimming whitespace, normalising case, counting words or extracting numbers. A regular expression (regex) is a pattern language for this; re.findall(r"[A-Z]+", "NSCT 2026 HEC") returns ['NSCT', 'HEC']. Characters are numbers underneath: ord('a') is 97, so comparisons are by code point and every capital letter sorts before every lowercase one.
Common traps
- Calling
upper(),replace()ortoUpperCase()without assigning the result. - Using
==for Java string content instead ofequals(). - Forgetting that the end index of a slice or
substringis excluded. - Forgetting the
'\0'byte when sizing a C char array. - Assuming
count()counts overlapping matches; it does not. - Treating
'a' + 1in Java as text: it is the int 98.
Worked examples
Understanding. Which Java class should you use to reverse a string in place and append to it many times?
Answer: StringBuilder. It is mutable and has append() and reverse(); String would create a new object for every change.
Application. What does print("data,science,,ai".split(",")[2] == "") print in Python?
Answer: True. The two adjacent commas create an empty string, so the list is ['data', 'science', '', 'ai'] and index 2 is ''.
Analysis. A Python function returns word.strip().lower() == "yes". A user types " Yes\n", and it still returns True. A colleague says strip() is unnecessary because lower() handles case. Who is right?
Answer: the function is right as written. lower() changes case only; strip() removes the leading space and the trailing newline. Without strip(), " yes\n" == "yes" is False.
Practise
The quiz below has 100 Strings & Text Processing questions, which you can filter by tier and difficulty.
Practise: 100 questions
FAQs
Are strings mutable in C++?
Yes. std::string in C++ is mutable: you can assign to s[i], call append() or +=, and erase characters in place. Only Java's String and Python's str are immutable among the three languages. A C-style char array is also mutable, but a string literal such as "hello" must not be modified.
What is the difference between == and equals() in Java?
== checks whether two references point to the same object. equals() checks whether two strings contain the same characters. Literals with the same text share one pooled object, so == can appear to work in small tests, then fail for strings read from input or built at run time.
Why is StringBuilder faster than + in a loop?
Each + on a Java String creates a new object and copies all existing characters, so n appends copy about n²/2 characters. StringBuilder keeps a resizable buffer and adds characters to the end, so the same loop does linear work. Python's equivalent is collecting pieces in a list and calling "".join() once.
How do I format a number to two decimal places?
In Python write f"{x:.2f}" or "{:.2f}".format(x). In Java use String.format("%.2f", x) or System.out.printf("%.2f", x). In C++ use std::fixed << std::setprecision(2) from <iomanip>. All three round to two digits after the decimal point.
Next step
Continue with Arrays & Collections MCQs, go back to the Programming hub, or try a timed NSCT mock test.