CSE 701 Practice Test 1: Chapters 1-3
McMaster University, Fall 2024

Table of contents

Instructions ^

You have 1 hour to solve the exam. There are 8 questions in total, and the points sum up to 100.

The only allowed material is the course lecture notes. You are not allowed to write anything on the printed notes or modify them in any way before or after printing, but you can use a highlighter. Computers, phones, tablets, and other digital devices cannot be used during the exam.

Question 1 (10 points) ^

What does it mean for a language to be "higher-level" than another language?

Question 2 (12 points) ^

The following C program has 4 errors. Find the errors and explain why each of them is an error. Rewrite the program without these errors, so that it works correctly and prints out the expected output.

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

int main(void)
{
    int64_t x;
    int64_t y = 6;
    printf("5 * 6 = %s", x * y)
    x = 5;
}

Question 3 (12 points) ^

The following C program has 4 errors. Find the errors and explain why each of them is an error. Rewrite the program without these errors, so that it works correctly and prints out the expected output.

#include <inttypes>
#include <stdint>
#include <stdio>

int main(void)
{
    const uint64_t x = -100;
    x++;
    printf(PRIu64, x);
}

Question 4 (12 points) ^

The following program is not portable, meaning it may produce different outputs on different operating systems. Explain why that is. Fix it so that it is portable and produces the same output on all operating systems.

#include <stdio.h>

int main(void)
{
    const long x = 3000000000;
    printf("%ld", x + 1);
}

Question 5 (12 points) ^

What will be the output of the following program?

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

int main(void)
{
    int64_t a = 1;
    int64_t b = (a == 2) ? 3 : 4;
    int64_t c;
    if (b = 3)
        c = 5;
    else
        c = 6;
    int64_t d = 7;
    switch (c)
    {
    case 5:
        d = 8;
    case 6:
        d = 9;
    }
    printf("%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, a, b, c, d);
}

Question 6 (12 points) ^

What will be the output of the following program?

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

int64_t x = 256;
int64_t y = 512;

void swap()
{
    int64_t temp = x;
    x = y;
    y = temp;
}

int main(void)
{
    int64_t x = 1024;
    int64_t y = 2048;
    swap();
    printf("x = %" PRId64 ", y = %" PRId64, x, y);
}

Question 7 (15 points) ^

Write a function int_power() that takes two positive integers a and b and outputs the first integer to the power of the second integer. For example, int_power(4, 3) will output 64. Your function must NOT use the built-in pow() function or any other math function from math.h or tgmath.h.

Note that you do not need to write a full program, just one function. However, the function must be complete and correct, such that if you enter it as-is into a program in VS Code, it will compile and run successfully without any errors or warnings (with all the warning flags recommended in this course enabled) and produce the expected output for any reasonable input. Make sure that the function uses portable integer types!

Question 8 (15 points) ^

Write a program that finds the largest and smallest elements of the array {6.4, 3.0, -23.3, 5349.2, -311.1, 10.5}.

This must be a complete and correct program, with all the header files, main() function, and so on, such that if you enter it as-is into VS Code, it will compile and run successfully without any errors or warnings (with all the warning flags recommended in this course enabled) and produce the expected output. Make sure that the program uses portable integer types!

© 2024 Barak Shoshany