Mockup Final Solutions 1
Question 1
1.a
World
The if statement has no braces, so only printf("Hello ") is controlled by it. Since a == 2 is false, "Hello " is skipped. The second printf("World") is not inside the if β it always executes regardless of the condition.
1.b
One Two
There are no break statements, so fall-through occurs. n matches case 1, which prints "One ", then execution falls through to case 2, which prints "Two".
1.c
5 6
The post-increment operator i++ returns the current value of i (5) and then increments it. So a gets 5, and i becomes 6.
1.d
Undefined behavior (typically a segmentation fault / runtime crash).
str points to a string literal, which is stored in read-only memory. Attempting to modify it with str[0] = 'H' causes undefined behavior. To make this work, you would need to use a character array: char str[] = "hello";
Compile-time error.
str is a pointer to const char, because a string literal must not be modified. The line str[0] = 'H'; tries to write through it, so the compiler rejects the program (for example βassignment of read-only locationβ). To make this work, you would need to use a character array: char str[] = "hello";
1.e
Undefined behavior (garbage value).
The array arr has 3 elements with valid indices 0, 1, and 2. Accessing arr[3] is an out-of-bounds access, which reads whatever happens to be in memory at that location.
1.f
(No output β nothing is printed)
This is the classic dangling else problem. Despite the indentation suggesting else belongs to the outer if, the else actually binds to the nearest unmatched if, which is if (b > 0). The code is equivalent to:
if (a > 20) {
if (b > 0)
printf("A");
else
printf("B");
}
Since a > 20 is false (a is 10), the entire block is skipped and nothing is printed.
1.g
10
Short-circuit evaluation: since x == 1 is false, the right side of && (y++) is never evaluated. Therefore y remains 10. The if body is skipped, and printf("%d", y) prints 10.
Question 2
int suspectStatus(int age, int isDrunk, int arrestTime, int hasWeapon) {
if (age < 15) {
if (hasWeapon) {
return 0; // temporarily held
}
return 1; // freed
}
// age >= 15
if (isDrunk) {
return -1; // prison
}
if (arrestTime >= 1 && arrestTime <= 4 && hasWeapon) {
return -1; // prison
}
return 0; // temporarily held
}
int suspectStatus(int age, int isDrunk, int arrestTime, int hasWeapon) {
if (age < 15) {
if (hasWeapon) {
return 0; // temporarily held
}
return 1; // freed
}
// age >= 15
if (isDrunk) {
return -1; // prison
}
if (arrestTime >= 1 && arrestTime <= 4 && hasWeapon) {
return -1; // prison
}
return 0; // temporarily held
}
Question 3
#include <string.h>
int canReorderToContain(char *str, char *target) {
int freq[26] = {0};
int lenStr = strlen(str);
int lenTarget = strlen(target);
// Count frequencies of characters in original string
for (int i = 0; i < lenStr; i++) {
freq[str[i] - 'a']++;
}
// Check if we have enough characters for the target
for (int i = 0; i < lenTarget; i++) {
freq[target[i] - 'a']--;
if (freq[target[i] - 'a'] < 0) {
return 0; // Not enough characters
}
}
return 1; // Can be formed
}
#include <cstring>
bool canReorderToContain(char *str, char *target) {
int freq[26] = {0};
int lenStr = strlen(str);
int lenTarget = strlen(target);
for (int i = 0; i < lenStr; i++) {
freq[str[i] - 'a']++;
}
for (int i = 0; i < lenTarget; i++) {
freq[target[i] - 'a']--;
if (freq[target[i] - 'a'] < 0) {
return false;
}
}
return true;
}
Question 4
int countWords(char *str) {
int count = 0;
int inWord = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] != ' ') {
if (!inWord) {
count++;
inWord = 1;
}
} else {
inWord = 0;
}
}
return count;
}
int countWords(char *str) {
int count = 0;
int inWord = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] != ' ') {
if (!inWord) {
count++;
inWord = 1;
}
} else {
inWord = 0;
}
}
return count;
}
Question 5
int largestDiffPeak(int arr[], int size) {
int bestIndex = -1;
int bestDiff = -1;
for (int i = 1; i < size - 1; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
int diff = 2 * arr[i] - arr[i - 1] - arr[i + 1];
if (diff > bestDiff) {
bestDiff = diff;
bestIndex = i;
}
}
}
return bestIndex;
}
int largestDiffPeak(int arr[], int size) {
int bestIndex = -1;
int bestDiff = -1;
for (int i = 1; i < size - 1; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
int diff = 2 * arr[i] - arr[i - 1] - arr[i + 1];
if (diff > bestDiff) {
bestDiff = diff;
bestIndex = i;
}
}
}
return bestIndex;
}