{"a": 2, "b": 2, "c": 1} matches the counts exactly. Others have incorrect counts.
Final Answer:
{"a": 2, "b": 2, "c": 1} -> Option C
Quick Check:
Count 'a'=2, 'b'=2, 'c'=1 [OK]
Hint: Count each character carefully, then match counts [OK]
Common Mistakes:
Mixing counts of 'b' and 'c'
Counting characters incorrectly
Ignoring character case
4. Identify the error in this C code snippet for counting character frequency:
char str[] = "hello";
int freq[256];
for(int i = 0; i < 256; i++) freq[i] = 0;
for(int i = 0; i <= strlen(str); i++) freq[str[i]]++;
medium
A. Frequency array size should be 128, not 256
B. Loop should run till i < strlen(str), not <= strlen(str)
C. Frequency array should be of type char, not int
D. Initialization loop should start from i = 1
Solution
Step 1: Analyze loop condition for frequency counting
The loop runs from i = 0 to i <= strlen(str), which includes the null character '\0' at the end.
Step 2: Understand why this causes error
Accessing freq[str[i]] when str[i] is '\0' is valid but incrementing frequency for null char is usually unintended. Loop should run till i < strlen(str) to avoid counting null.
Final Answer:
Loop should run till i < strlen(str), not <= strlen(str) -> Option B
Quick Check:
Loop boundary off by one error [OK]
Hint: Use i < strlen(str) to avoid counting null char [OK]
Common Mistakes:
Using <= instead of <
Wrong array size assumption
Wrong data type for freq array
5. Given a string with mixed uppercase and lowercase letters, how can you count character frequency ignoring case in C?
hard
A. Ignore characters that are uppercase
B. Count uppercase and lowercase separately
C. Use a frequency array of size 52 for letters
D. Convert each character to lowercase before counting frequency
Solution
Step 1: Understand ignoring case means treating 'A' and 'a' same
To do this, convert each character to lowercase (or uppercase) before counting frequency.
Step 2: Check options for correct approach
Convert each character to lowercase before counting frequency converts characters to lowercase before counting, which correctly ignores case. Others either separate counts or ignore characters incorrectly.
Final Answer:
Convert each character to lowercase before counting frequency -> Option D
Quick Check:
Case normalization before counting = Convert each character to lowercase before counting frequency [OK]
Hint: Normalize case before counting frequency [OK]
Common Mistakes:
Counting uppercase and lowercase separately
Ignoring uppercase letters
Using wrong array size
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.