Course Materials

Mockup Final Exam 1

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

int a = 1;
if (a == 2)
    printf("Hello ");
    printf("World");
int a = 1;
if (a == 2)
    cout << "Hello ";
    cout << "World";

1.b

int n = 1;
switch(n) {
    case 1: printf("One ");
    case 2: printf("Two");
}
int n = 1;
switch(n) {
    case 1: cout << "One ";
    case 2: cout << "Two";
}

1.c

int i = 5;
int a = i++;
printf("%d %d", a, i);
int i = 5;
int a = i++;
cout << a << " " << i;

1.d

char *str = "hello";
str[0] = 'H';
printf("%s", str);
const char *str = "hello";
str[0] = 'H';
cout << str;

1.e

int arr[3] = {10, 20, 30};
printf("%d", arr[3]);
int arr[3] = {10, 20, 30};
cout << arr[3];

1.f

int a = 10, b = 5;
if (a > 20)
    if (b > 0) 
        printf("A");
else 
    printf("B");
int a = 10, b = 5;
if (a > 20)
    if (b > 0) 
        cout << "A";
else 
    cout << "B";

1.g

int x = 0, y = 10;
if (x == 1 && y++) {
    printf("Inside ");
}
printf("%d", y);
int x = 0, y = 10;
if (x == 1 && y++) {
    cout << "Inside ";
}
cout << y;

Question 2

A police officer must decide whether a suspect brought to the station is freed, temporarily held, or sent to prison. The decision depends on the following inputs: age, isDrunk (1 if drunk), arrestTime (hour in 24-hour format), and hasWeapon (1 if armed).

Minors (under 15) are generally released immediately, but any minor found carrying a weapon must be temporarily held at the station. Adults and teens (15 and older) who are drunk are sent straight to prison. Armed suspects aged 15 or older who were arrested during late-night hours (between 1:00 and 4:00 inclusive) are also sent to prison. All remaining suspects are temporarily held.

Write a function with the following signature that returns 1 if the suspect is freed, 0 if they are temporarily held, and -1 if they are sent to prison.

int suspectStatus(int age, int isDrunk, int arrestTime, int hasWeapon) {
    // Write your code here
}
int suspectStatus(int age, int isDrunk, int arrestTime, int hasWeapon) {
    // Write your code here
}

Question 3

Given a string (pointer to char) and a target string, write a function that returns 1 (or true in C++) if the characters of the original string can be reordered such that the target string appears as a substring within it, and 0 (or false) otherwise. You can assume both strings consist only of lowercase English letters.

int canReorderToContain(char *str, char *target) {
    // Write your code here
}
bool canReorderToContain(char *str, char *target) {
    // Write your code here
}

Example:

Original StringTargetReturns
"aecbdf""abc"1
"programing""gram"1
"hello""world"0
"aabb""abcb"0

Question 4

Given a string (pointer to char), write a function that counts and returns the number of words in the string. A word is any sequence of non-space characters separated by one or more spaces. Leading and trailing spaces should not count as extra words.

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

Example:

Input StringOutput
"Hello World"2
" leading spaces"2
"trailing spaces "2
" multiple spaces here "3
""0

Question 5

Given an array of integers and its size, find the peak element with the largest โ€œdiffโ€ value. A peak is an element that is strictly greater than both its left and right neighbors. The first and last elements of the array cannot be peaks.

The diff for a peak at index i is calculated as:

diff = arr[i] - arr[i-1] + arr[i] - arr[i+1]

The function should return the index of the peak with the largest diff. If there are no peaks, return -1. If there are multiple peaks with the same largest diff, return the one with the smallest index.

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

Example:

arrReturns
{1, 5, 2, 8, 3, 7, 4}3
{3, 9, 2, 6, 1}1
{1, 2, 3, 4}-1