e01 : Midterm exam - I

num ready? description exam date
e01 true Midterm exam - I Thu 04/25 05:00PM

Exam logistics

Thursday, 4/25 in the regular lecture classroom at the usual lecture time

You may bring only one 8.5” x 11” piece of paper with notes on both sides.


Open Review Session and Extra Office hours

The Open Review Session information was posted on Piazza. Feel free to stop by at any time during that interval to ask for help with any of the topics or practice problems.

Just this week, Prof. K will host additional office hours on Wednesday, 4/24, 2PM-6PM in her office in HFH 2107. Feel free to stop by at any time to ask for help with any of the topics.

Have fun practicing what you’ve learned so far!

Remember: programming is not a spectator’s sport – you have to actively write code in order to get better at writing, reading, and debugging it – just looking at the code examples is not enough.

We recommend that you take the example code shown in the lectures and try to write it from scratch, without looking at the solution. Then, compare your code to the provided solution and note any differences – look into and understand whether these differences are due to style or they are important.


Main Exam Topics

Lecture Topics for the Exam Study Guide

Practice Problems


Note: these are just a few practice problems to give you additional practice and a sample of what the potential exam questions might look like.

Additional Practice Tips and Problems are included in the Lecture 7 slides and notes.

1.

Write the output for the following statements.

A.

int x = 10;
  	int y = 2;
  	 while(x > 0){
    	            cout << x + y << " ";
   		 x = x - 2;
   		 y = y + 3;
 	  }

B.

for ( int i = 0; i < 5; i++){
    for( int j = 0; j < i; j++){
        cout << "! ";
    }
    cout << endl;
}

2.

Rewrite the following function using “switch” and “case” so that it has the same functionality.

void f (int num){
    if (num == 10){
        cout << "You said 10" << endl;
    } 
    else if (num == 15){
        cout << "You said 15" << endl;
    }
    else {
        cout << "Everything else" << endl;
    }
}

3.

What is the output for the following statements? If it results in a compilation error, briefly explain why and fix the error.

	#include <iostream>
	using namespace std;
	int main(){
		f();
		return 0;
	}
	int f(){
		cout << "f()" << endl;
		return 0;
}

4.

Write UNIX commands to perform following tasks.

(a) Compile the file main.cpp into a binary executable named main.

(b) Run the executable produced in part (a).

(c) Delete the executable we just created.

5.

Write a function calc_sum that takes in a parameter integer n and returns the value 1 + 4 + 9 + … + n^2. If n is less than 1, the function returns 0.

6.

Write a complete program that takes command line arguments, a, b, c and x. The program will print the result of a*x^2+b*x+c on the terminal. For example, ./main 1 2 3 4, will effectively mean that a=1, b=2, c=3, x=4, and the program will print 27 (1*4^2+2*4+3). If the incorrect number of is provided, print a message to tell the user correct usage and exit the program.

7.

Given the following code

int x;
for (x = 10; x>0; x=x-2) {
        cout << x << endl;
}

a. What is the output for the given code?

b. Write code that produces the same output but that uses a while loop instead of a for loop.

8.

The following code prints out a block of stars (assume all variables have been initialized). What variable determines the total number of rows and which variable determines the total number of columns?

for (int a = 0; a < b; a = a + 1){
        for (int c = d; c > 0; c--){
            cout << "*";
        }
        cout << endl;
    }

9.

  1. Write a program that calculates the area of a trapezoid to 3 decimal places. The program receives the length of the 2 bases and the height as command line arguments. The program should accept exactly 3 arguments from the command line. (Topics: Command line arguments, Integer division)

10.

Write the output of the following program:

#include <iostream>
using namespace std;

int foo(int i){
    cout << i;
    return i*i;
}
int main(){
    if(foo(2) == 2)
        cout << "Fizz";
    else
        cout << foo(2);
    return 0;
}

(Topics: Return vs print, Functions)

11.

Write UNIX commands for the following tasks (a) Make a directory called myFolder in the current directory

(b) Rename the file main.cpp to hello.cpp. Assume main.cpp is in the current directory.

(c) Delete the directory myFolder.

12.

What is the output of calling foo(1)? What about foo(5)?

void foo(int i) {
	switch (i) {
		case 1:
			cout << "foo(1)" << endl;
			// no break statement!
		case 2:
			cout << "foo(2)" << endl;
        default:
            cout << "Default" << endl;
	}
}

13.

Write the Vim commands/keystrokes for the following tasks:

14.

What is wrong with the following code?

	int x;
	If (x == 0){
		cout << "hello" ;
}