Course Materials

Mockup Final Exam 2

Question 1

For each of the following code snippets, determine the output. If the code causes an error or undefined behavior, state so and explain why.

1.a

void addOne(int x) { x++; }

int main() {
    int a = 5;
    addOne(a);
    printf("%d", a);
    return 0;
}
void addOne(int x) { x++; }

int main() {
    int a = 5;
    addOne(a);
    cout << a;
    return 0;
}

1.b

int i = 0;
while (i++ < 3) {
    printf("%d", i);
}
int i = 0;
while (i++ < 3) {
    cout << i;
}

1.c

int a[3] = {1, 2, 3};
a[a[1]] = 5;
printf("%d", a[2]);
int a[3] = {1, 2, 3};
a[a[1]] = 5;
cout << a[2];

1.d

int arr[] = {100, 200};
int *p = arr;
*p = *p + 1;
printf("%d %d", arr[0], arr[1]);
int arr[] = {100, 200};
int *p = arr;
*p = *p + 1;
cout << arr[0] << " " << arr[1];

1.e

int x = 10;
printf("%d %d", x);
int x = 10;
printf("%d %d", x);

1.f

int x = 2;
switch(x) {
    default: printf("D");
    case 1: printf("1"); break;
    case 3: printf("3");
}
int x = 2;
switch(x) {
    default: cout << "D";
    case 1: cout << "1"; break;
    case 3: cout << "3";
}

1.g

int a = 5, b = 2;
float f = a / b;
printf("%.1f", f);
int a = 5, b = 2;
float f = a / b;
cout << fixed << setprecision(1) << f;

Question 2

Given two strings (pointers to char), write a function that finds their longest common prefix and writes it into result, a character array that you can assume is large enough to hold it. The result must end with the null character '\0'. If there is no common prefix, result should be an empty string.

void longestPrefix(char *str1, char *str2, char *result) {
    // Write your code here
}
void longestPrefix(char *str1, char *str2, char *result) {
    // Write your code here
}

Example:

First StringSecond Stringresult after the call
"flower""flow""flow"
"interview""internet""inter"
"abc""abc""abc"
"dog""racecar"""

Question 3

Given a string (pointer to char), you want to modify it so that it does not contain any 3 consecutive identical characters. Write a function that returns the minimum number of character changes required to achieve this. You do not need to actually modify the string, just return the integer count of changes needed.

You can assume the string consists only of lowercase English letters, and that a changed character can become any lowercase letter.

int minChanges(char *str) {
    // Write your code here
}
int minChanges(char *str) {
    // Write your code here
}

Example:

StringReturns
"aabbcc"0
"aaab"1
"abcccd"1

Question 4

Given an array of integers, you are guaranteed that it contains exactly one 0. Write a function to rearrange the array such that all negative elements are to the left of the 0, and all positive elements are to its right. The order of the elements among the negatives, and among the positives, does not matter.

You must do this in-place; declaring a new array is not allowed.

void partitionZero(int arr[], int size) {
    // Write your code here
}
void partitionZero(int arr[], int size) {
    // Write your code here
}

Example:

For arr = {3, -1, 0, 2, -4, -6, 5, 7}, one valid result is:

{-6, -1, -4, 0, 2, 5, 7, 3}

Question 5

You are given two arrays a and b of the same size, where a[i] determines the start of interval i and b[i] determines the end of interval i. You can assume a[i] <= b[i] for every i. The size of an interval is defined as end - start.

Write a function to find the maximum possible interval size that can be formed by merging exactly two overlapping intervals. If no two intervals overlap, return -1.

Two intervals overlap if they share at least one point (for example [1, 4] and [4, 9] overlap). Merging two overlapping intervals gives one interval that covers both of them.

int maxMergedInterval(int a[], int b[], int size) {
    // Write your code here
}
int maxMergedInterval(int a[], int b[], int size) {
    // Write your code here
}

Example:

abReturns
{1, 3, 6, 10}{4, 8, 7, 12}7
{1, 4}{4, 9}8
{1, 5}{3, 8}-1