CSCE 2004 - Practice Midterm

This lab is intented to be a practice session for the midterm exam.

Work each of the following questions on separate paper. Attempt all questions on your own. Once you are done, ask your TA for the URL link to the solutions and check your answers. When you are finished, you DO NOT need to turn anything in; this lab is an attendance/participation grade. For any questions whose answers you don't understand, see your TA for an explanation. This practice midterm is slightly longer than the one you can expect to see Friday to provide extra questions for your practice.

Alternatively, if you worked this practice midterm before your lab session, you may email your solutions to the TA to receive lab credit. However, if you do so and have questions about any problem, you should come to your lab session or office hours before emailing your TA.

The questions in this practice midterm are tagged with the book chapters where the relevant conceptual material is discussed.

  1. Evaluating Expressions (Chapters 2 and 3)

    Examine the following C++ assignment statements (of the form: variable = expression;). For each, consider the order in which the sub-pieces are evaluated, and the values along the way. Then, indicate the data type (e.g., float, int, char) and value (e.g. 3.7, 2, 'A') of the final expression as well as the value and type stored in the variable.

    a) float Taxes;
    
       Taxes = (20 * 4 + 30 * 3)/50;
    
    Expression:  Data Type:_____________  Value:_____________
    
    Variable:    Data Type:_____________  Value:_____________
                				        
    	
    b) char Grade;
    
       Grade = 'F' - 5 % 2;
    
    Expression:  Data Type:_____________  Value:_____________
    
    Variable:    Data Type:_____________  Value:_____________
    
    
    c)  int Distance;
         
        Distance = 3.5 + 3 / (2 + 3);
    
    Expression:  Data Type:_____________  Value:_____________
    
    Variable:    Data Type:_____________  Value:_____________
    
    
    d) int Number;
    
       Number = 3 * 2 / 10.0;
    
    Expression:  Data Type:_____________  Value:_____________
    
    Variable:    Data Type:_____________  Value:_____________
    
    
  2. Basic File I/O (Chapters 3 and 4)

    Write a short C++ program that reads rectangle dimensions for 3 rectangles from a file and outputs the area of each rectangle to the screen. You should perform appropriate File I/O error checking to ensure that the file opens successfully.

    Assume you have access to the following file named "rect.dat":

    rect.dat
    7 8
    3 2
    3 6
    
  3. If-Else statements (Chapter 4)

    Write a C++ program which asks the user several questions and selects a car for them. The questions are 1) do you have children; 2) do you have lots of money; 3) do you like trucks.

    Porsche:   have no children, have lots of money, don't like trucks
    Yukon:     have no children, have lots of money, like trucks
    Civic:     have no children, have little money
    Villager:  have children, have lots of money, don't like trucks
    Explorer:  have children, have lots of money, like trucks
    Sentra:    have children, have little money
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
       // declare variables
    
       // get user input (no error checking necessary)
    
       // output the correct car
    
    }
    
  4. Designing Loops (Chapter 5)

    (a) Rewrite the following while loop as a for loop.  
    
        int Max = 1900;
        int Sum = 0;
        while ((Max < 1950))
        {
           Sum = Sum + (Max - 1900);
           cout << "Sum: " << Sum << "\n";
           Max = Max + 5;
        }
    
    (b) Rewrite the following do-while loop as a while loop:
    
        int Hours, Total;
        do
        {
           cout << "Enter a number between 0 and 20: ";
           cin >> Hours;
    
           if ((Hours >= 0) && (Hours <= 20))
              Total = Total + Hours;
        }while (Total < 200);
    
    
    (c) Rewrite the following nested for loops as a single for loop that 
    produces the same output in all cases.  Hint:  trace the loops to see what
    they do.
    
       int i, j;
       for (j = 1; j < 4; j++)
       {
          for (i = j; i < j+1; i++)
          {
             cout << "The value is: " << i << "\n";
          }
       }
    
    
  5. Evaluating Loops (Chapter 5)

    What does the following program output to the screen? Show your work where necessary.

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int X, Y;
    
    	// a loop
    	for (Y = 1; Y < 10; Y = Y + X)
    	{
    		X = 1;
    		while(X < 3)
    		{
    			cout << "X: " << X << endl;
    			X = X + 1;
    		}
    		cout << "Y: " << Y << "\n";
    	}
    	//code after loop
    	cout << "Final value of X: " << X << "\n";
    	cout << "Final value of Y: " << Y << "\n";
    
    	return 0;
    }
    
    
  6. Calling Functions (Chapter 6)

    For each of the following function prototypes (shown in bold), which of the following calls are syntactically correct? Note, function calls that would compile with warnings, cause implicit type conversions, or ignore return values should be considered incorrect.

    Assume the following declarations are given:

    int A, B, C;
    float X, Y;
    char Ch;
    

    int maximum(int num1, int num2);
    
    maximum(A, B);					Correct(Y or N):__________
    
    A = maximum(7, 3);				Correct(Y or N):__________
    
    A = maximum(num1, num2);			Correct(Y or N):__________
    
    void total(float A, float B, float & C);
    
    total(A, B, C);					Correct(Y or N):__________
    
    total(3.0, X, Y);				Correct(Y or N):__________
    
    total(3.0, 5.0, 8.0);				Correct(Y or N):__________
    
    total(Y, X, Y);					Correct(Y or N):__________
    
    char getChar();
    
    getChar(Ch);					Correct(Y or N):__________
    
    Ch = getChar();					Correct(Y or N):__________
    
    
  7. Designing Functions (Chapter 6)

    Fill out the parameter lists for the following functions. You do NOT have to fill in the bodies.

    // This function takes in two numbers and returns their Average.
    
    ________ average (_________________________________________________)
    
    // This function asks the user how many sisters they have and returns
    their answer.
    
    ________ getSisters (______________________________________________)
    
    // This function is given a person's last name and it modifies it to be
    // all lowercase.
    
    ________ downcaseName (____________________________________________)
    
    // This function displays a menu of choices to the user and returns what
    // their GPA is (e.g., 4.0) and also returns the letter equivalent of
    // their GPA (e.g., 'A').
    
    ________ getGPA (__________________________________________________)
    
    
  8. Tracing Functions (Chapter 6)

    What does the following program output to the screen? Show your work where necessary.

    
    #include <iostream>
    using namespace std;
    
    const int X = 3;
    
    void foo(int &Y)
    {
    	Y = 6;
    	cout << "Inside foo(), X: " << X << ", Y: " << Y << endl;
    }
    
    int bar(int &Y, const int Z)
    {
    	int X = 7;
    
    	foo(X);
    	foo(Y);
    	Y = 3;
    	cout << "Inside bar(), X: " << X << ", Y: " << Y << ", Z: " << Z << endl;
    
    	return (Y + Z);
    }
    
    int main()
    {
    	int A = 1;
    	int B = 3;
    	int C = 5;
    
    	C = bar(A, B);
    	cout << "Finally, A: " << A << ", B: " << B << ", C: " << C << endl;
    
    	return 0;
    }
    
    
  9. Declaring Arrays (Chapter 7)

    Indicate whether the following array declarations are correct or not.

    array[3] int(3);				Correct(Y or N):_________
    
    double float[2][3];				Correct(Y or N):_________
    
    int nums[20];					Correct(Y or N):_________
    
    int[20] nums;					Correct(Y or N):_________
    
    bool counter[5][2];				Correct(Y or N):_________
    
    double population(10)(2);			Correct(Y or N):_________
    
  10. Accessing Arrays (Chapter 7)

    Write a C++ code segment (not a full program) that uses a for loop to sum all of the elements of the following array and prints the total to the screen.

    const int NUM_ITEMS = 5;
    int items[NUM_ITEMS] = {1, 3, 5, 7, 9};