CSCI 103 - Spring 2024 Introduction to Programming

Midterm Practice Questions

Note that these practice questions may not entirely match the exact content mix or difficulty of the actual midterm. However, they attempt to exercise the most fundamental skills that would appear in any midterm. See the topic list on the midterm information page to see a more comprehensive list of what you should study.

You can print this page if you like.

Q1

What is the type and value of each of the following expressions? If it would not compile, write an X instead.

Q2

Here is a short program with several bugs. It is supposed to compute your homework score. It takes an integer n as input, then n space-separated characters, each of which is C (complete, worth one point), L (late, worth half a point), or I (incomplete, worth no points). It should print the total score. For example if the input is

5
C I L C C

then the output should be 3.5.

#include <iostream>
using namespace std;

int main() {
   int n;
   cin >> n;
   double total = 0;
   for (i=0; i<n; i++) {
      char ch;
      cin >> ch;
      if (ch == "C")
         total += 1;
      else if (ch == 'L')
         total += 1/2;
   }
   cout << total << endl;
}

Q3

Consider the following program fragment.

int inputs[7] = {8, 1, 4, 7, 3, 2, 7};
int n=7;
for (int i=0; i<7; i++) {
   if (i < n-i-1) {                           // !!
      inputs[i] += inputs[n-i-1];
      inputs[n-i-1] = inputs[i] - inputs[n-i-1];
      inputs[i] -= inputs[n-i-1];
   }                                          // !!
}
  1. In the first iteration,
    • what is the effect of the first assignment statement?
    • what is the effect of the second assignment statement?
    • what is the effect of the third assignment statement?
  2. At the end of the program, what are the values in the array?
  3. Imagine we remove the "if" by deleting the lines containing `// !!`. At the end of the program, what are the values in the array?

Q4

What is the output of the following program?

#include <iostream>
using namespace std;
void x(double f) {
   cout << f << endl;
}

double f(int x, int y) {
   if (y == 0)
      y = 1;
   return (double) x / (double) y;
}

int main() {
   int y = 0;
   x(f(103, 0));
   cout << y << endl;
}

Q5

Assume that “elephant.bmp” is a file in the current directory that contains a picture of an elephant. We run this program:

#include "bmplib.h"
int main() {
   unsigned char src[SIZE][SIZE];
   unsigned char dest[SIZE][SIZE];
   readGSBMP("elephant.bmp", src);
   for (int i=0; i<SIZE; i++) {
      for (int j=0; j<SIZE; j++) {
         if (i<SIZE/2) dest[i][j] = src[i*2][j];
         else dest[i][j] = 0;
      }
   }
   writeGSBMP("output.bmp");
}

What image is created at the of this program? (On an exam, we would probably give you a list to select from, since this is not an art class.)

Q6

True or False?

  1. If `a` is a variable of an array type, then `a[1]` is the same as `*(a+1)`.
  2. A pointer to an `int` takes up the same amount of memory as a pointer to a `char`.
  3. Some functions don't contain a `return` statement.
  4. Any newly-defined `int` variable always starts at 0.
  5. A region of memory allocated by `new` can be resized later on.

Q7

We create a program q7.cpp containing

#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[]) {
   cout << argc            << endl; // output line 1
   cout <<  argv[1]         << endl; // output line 2
   cout <<  argv[2][0]      << endl; // output line 3
   cout <<  *argv           << endl; // output line 4
   cout <<  **(argv+2)      << endl; // output line 5
   cout <<  atof(argv[2])/2 << endl; // output line 6
}

Suppose that we compile this program successfully, and at the command line, we run

./q7 fish 37 banana 28

What is its output?

Q8

What is printed by the following code?

char* buffer = new char[80];
strcpy(buffer, "Question");
strcpy(buffer+5, "s");
cout << buffer;

Q9

If we buy N copies of an item, the total price is N times the unit price.

Write a function to compute this in a way that changes the caller’s variable rather than using return.

Fill out the blanks below — the parameter declarations, function body, and values to call on — to accomplish this.

Make sure that the update to total_cost is visible back in main().

void set_total_cost(/* FILL THIS IN */)
{
/* FILL THIS IN */
}

// assume appropriate prototype of set_total_cost()
int main() {
   int quantity; double unit_cost, total_cost;
   cin >> quantity >> unit_cost;
   set_total_cost(/* FILL THIS IN */);
   cout << &quot;Total cost is: &quot; << total_cost << endl;
}

Q10

Part 1

Write a function convert(...) that takes a lab score from 0 to 10, and converts it to an appropriate letter grade: 9-10 is ‘A’, 8 is ‘B’, and 6-7 is ‘C’. (Assume no grades less than 6 exist.) It should work with the main() below.

void convert( /* FILL THIS IN */ ) {
   // YOUR CODE HERE (use as many lines as you want)
}

int main() {
   int scores[8] = {9, 10, 7, 8, 6, 9, 8, 7};
   char grades[8];
   convert(scores, grades, 8);
   for (int i=0; i < 8; i++) {
      cout << "Student " << i << "'s grade is " << grades[i] << endl;
   }
}
Part 2

Suppose C did not have an if statement (nor switch nor the ?: operator). Re-write the code to work to do the same thing as above, but in a different way, using the look-up table LUT below.

void convert( /* SAME AS BEFORE */ ) {
   char lut[5] = {'C', 'C', 'B', 'A', 'A'};
   // YOUR CODE HERE (use as many lines as you want)
}