CSCE 2004 - Practice Final

This assignment is intended to be a practice session for the final exam.

Work each of the following questions on separate paper. Attempt all questions on your own. Once you are done, email your TA a copy of your solutions to receive an attendance/participation credit for one lab grade (due by the start time of the final exam, at the latest). Your TA will then respond with the URL link to the solutions so you can check your answers. For any questions whose answers you don't understand, see your TA for an explanation. This practice final is slightly longer than the one you can expect to see Saturday to provide extra questions for your practice.

  1. C++ Logical Syntax

    Consider the following declarations:

    int catHeight = 6; // the cat's height
    int dogHeight = 7; // the dog's weight
    string dogName = "Rover"; // the dog's name
    string catName = "Sylvester"; // the cat's name
    float catWeight = 15.0; // the cat's weight
    float dogWeight = 20.0; // the dog's weight
    bool dogRabies = true; // flag which indicates whether animal has rabies
    bool catRabies = false; // flag which indicates whether animal has rabies
    

    Answer the following questions by selecting the logical expressions that are equivalent to the given English descriptions:

    a) The cat has rabies but does not weigh 20 lbs or less.
    
    1. (catRabies != true) && (catWeight >= 20)
    2. (catWeight < 20.0) || (catRabies == true)
    3. !(catRabies == true) && (catWeight <= 20)
    4. !(catRabies == false) && (catWeight > 20.0)
    5. None of the above
    b) The dog does not weigh less than the cat but neither the cat nor the dog has rabies.
    
    1. !(dogWeight < catWeight) && !((catRabies == true) &&(dogRabies == true))
    2. !(dogWeight >= catWeight) && ((catRabies == true) || (dogRabies == true))
    3. (dogWeight >= catWeight) && ((catRabies == false) && (dogRabies == false))
    4. (dogWeight != catWeight) && ((catRabies == true) && (dogRabies == true))
    5. None of the above
    c) The cat height and the dog height are not 10.
    
    1. !(catHeight == 10) || !(dogHeight ==10)
    2. (catHeight != 10) && (dogHeight >= 10)
    3. !((catHeight != 10) && (dogHeight != 10))
    4. ((catHeight != 10) && (dogHeight != 10))
    5. None of the above

  2. Evaluating/Tracing Loops

    Consider the following C++ code:

    int main()                           
    {
    	int Control = 2;
    	int Sum = 0;
            while (Control < 5)
            {
                    for (int Inner = 1; Inner <= 2; Inner++)
    		{
                    	Sum = Sum + Control;
    		}
                    
    		Control = Control + Control;
    
                    cout << "Control=" << Control << ", "
                         << "Sum=" << Sum << endl;
            }
    
    	cout << "Control=" << Control << ", "
                 << "Sum=" << Sum << endl;
    	return 0;
    }       
    

    a) What will the output of this program be?

    1. Control=4, Sum=4
      Control=8, Sum=12
      Control=8, Sum=12
      
    2. Control=2, Sum=4
      Control=4, Sum=12
      Control=8, Sum=12
      
    3. Control=2, Sum=2
      Control=4, Sum=12
      Control=4, Sum=12
      
    4. Control=4, Sum=4
      Control=8, Sum=8
      Control=12, Sum=12
      
    5. None of the above
      

    b) Which of the following C++ statements have the same effect as the line:

    "for(int Inner = 1; Inner <= 2; Inner++)"?
    
    1. int Inner = 1; while (Inner < 3)
      
    2. for(int Inner = 2; Inner > 0; Inner++)
      
    3. while(int Count = 2; Count-1 > 0)
      
    4. for(int Count = 0; Count < 2; Count++)
      
    5. None of the above
      

    Consider the following C++ code:

    int main()
    {
    	int Sum = 0;
    	int Num = 1;
            while (Sum < 10)
            {
                    Sum = Sum + Num;
                    Num = Num + 1;
            }
            cout << "Sum= " << Sum << endl
                 << "Num= " << Num << endl;
    	return 0;
    }
    

    c) How many times will the program execute the code inside the loop?

    1. 0
    2. 4
    3. 5
    4. 10
    5. None of the above

  3. Array Usage

    a) Which of the following statements properly declares a static integer array of 10 rows and 5 columns?

    1. int[10][5] array;
    2. int array[5][10];
    3. int[5][10] array;
    4. int array[10][5];
    5. None of the above

    b) Which of the following statements properly declares a dynamic integer array of 10 rows and 5 columns?

    1. int * array = &int[10][5];
    2. int & array = &int[5][10];
    3. int & array = new int[10][5];
    4. int * array = new int[5][10];
    5. None of the above

    c) Write a C++ function that takes a 2-dimensional integer array as input and returns both the maximum value in the array and its location in the array.

    //Your code goes here
    

  4. Tracing Functions

    Consider the following C++ code:

    double modifyValue(double & value)
    {
    	int num;
    	value = value * 2.0;
            num = value + 10;
    	return num;
    }
    
    double modifyResult(double result)
    {
    	result = result / 3.0;
    	if (result < 10)
    	   return result;
    	else
    	   return modifyValue(result);
    }
    
    int main()
    {
    	double value, result;
    
    	value = 42;
    	result = modifyValue(value);
    	cout << value << " " << result << endl; // Position A
    
    	result = 36;
    	value = modifyResult(result);
    	cout << value << " " << result << endl; // Position B
    
    	return 0;
    }
    

    a) What values will be printed at Position A?

    1. 42.0 42.0
    2. 42.0 94.0
    3. 84.0 94.0
    4. 94.0 42.0
    5. None of the above

    b) What values will be printed at Position B?

    1. 34.0 36.0
    2. 36.0 42.0
    3. 12.0 84.0
    4. 36.0 24.0
    5. None of the above

  5. File I/O

    Consider the following plain text file, data.txt:

    1 student1 3.5
    2 student2 0.6
    3 student3 4.0
    4 student4 2.2
    5 student5 2.3
    6 student6 3.8
    7 student7 3.5
    8 student8 3.1
    9 student9 1.5
    10 student10 3.0
    11 student11 2.0
    12 student12 1.6
    13 student13 3.1
    14 student14 3.9
    15 student15 2.6
    16 student16 4.0
    17 student17 3.3
    18 student18 2.6
    

    Fill in the blanks in the following code so that the resulting program will read the file and display the information of a student with a specific ID# on the screen.

    #include ________________
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	string idToLookFor;
    	string id;
    	string name;
    	string gpa;
    	string filename;
    
    	cout << "What is the ID you are looking for?" << endl;
    	cin >> idToLookFor;
    
    	cout << "What is the filename to read from?" << endl;
            cin >> filename
    
    	ifstream din;
    	______________________
    
            if ___________________
               cout << "Error: Unable to open file.\n";
            else
            {
    	    while((din >> id) && ______________________)
                   _______________________
    
                if (id == idToLookFor)
                {
    	       ________________________
    	       cout << "The student you are looking for is " << name << endl;
                   cout << "His/her GPA is " << gpa << endl; 		
                }
                else
                   cout << "The student was not found." << endl;
    	    
    	    ___________________
            }
    
    	return 0;
    }
    

  6. String Operations

    A palindrome is a phrase that can be read the same way in either direction. For example, "abccba" is a palindrome.

    Write a C++ function that takes any C++ string as a pass-by-reference argument and modifies the string to be a palindrome. For example, given "abc", the function would modify the string to be "abccba".

    //Your code goes here
    

  7. Using Pointers

    Consider the following C++ code:

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a = 6;
    	int b = 5;
    	int c = 10;
    	
    	int *aptr = &a;
    	int *bptr = &b;
    	int *cptr = &c;
    
    	cout << "The value of a is: " << a << endl;
    	cout << "The value of b is: " << *bptr << endl;
    	*aptr = *aptr + *cptr;
    	cout << "The value of a is: " << a << endl
                 << "The value of c is: " << c << endl;  
    
    	return 0;
    }
    

    a) What is the output of this code?

    Consider the following C++ code:

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int A = 1;
    	int * X = &A;
    	int * Y = &A;
    	
    	cout << "X: " << *X << endl;
    	*X = 5;
    	cout << "X: " << *X << endl;
    	*Y = 10;
    	cout << "X: " << *X << endl;
    	cout << "A: " << A << endl;
    
    	return 0;
    }
    

    b) What is the output of this code?

  8. Calling Object-Oriented Methods

    Consider the following C++ code:

    const int MAX_STUDENTS = 100;
    
    class Group
    {
    public:
            Group();
            bool fillInfo(const char filename[]);
            int getNumStudents() const;
            void getNumStudents(int & Num) const;
            void print() const;
            bool changeGPA(char *& name, const float GPA);
    
    private:
            void setNumStudents(const int num);
            string names[MAX_STUDENTS];
            float GPAs[MAX_STUDENTS];
            int UAIDs[MAX_STUDENTS];
            int numStudents;
    };
    
    Given the following definitions (in main()):
    Group ultimateFrisbeeClub;
    int myUAID;
    int numStudents;
    

    Which of the following calls use correct C++ syntax?

    Correct/Incorrect : numStudents = ultimateFrisbeeClub.getNumStudents();

    Correct/Incorrect : ultimateFrisbeeClub.fillInfo("data.txt");

    Correct/Incorrect : print(ultimateFrisbeeClub);

    Correct/Incorrect : myUAID = ultimateFrisbeeClub.UAIDs[7];

    Correct/Incorrect : if(changeGPA("Tom", 4.0)) cout << "Perfect!\n";

    Correct/Incorrect : ultimateFrisbeeClub.getNumStudents(numStudents);

    Correct/Incorrect : ultimateFrisbeeClub.setNumStudents(15);

    Correct/Incorrect : ultimateFrisbeeClub.getNumStudents(15);

  9. Tracing Object-Oriented Method Calls

    Consider the following C++ code:

    class Account
    {
    public:
            Account();
            int getBalance();
            int updateBalance(int Amount);
    
    private:
            int balance;
    };
    
    Account::Account()
    {
    	balance = 100;
    }
    
    int Account::getBalance()
    {
            return balance;
    }
    
    int Account::updateBalance(int amount)
    {
            balance = amount + 1;
            return amount;
    }
    
    int main()
    {
    	Account mySavings;
    
            cout << "My balance is: " << mySavings.getBalance() << endl; // Position A
    
            int amount = mySavings.updateBalance(10);
    
            cout << "My updated balance is: "<< mySavings.getBalance() << endl; // Position B
    
    	return 0;
    }
    

    a) What value will be printed at Position A?

    1. 10
    2. 100
    3. 101
    4. 110
    5. None of the above

    b) What value will be printed at Position B?

    1. 10
    2. 100
    3. 101
    4. 110
    5. None of the above

  10. Designing Classes

    Consider the following C++ code:

    class Line
    {
    public:
    	Line();
    	Line(const Line & otherLine);
    	bool fillLine(ifstream & din);
    	double getSlope();
    	void print();
    private:
    	double start_x;
    	double start_y;
    	double end_x;
    	double end_y;
    };
    
    Line::Line()
    {
    	start_x = 0.0;
    	start_y = 0.0;
    	end_x = 1.0;
    	end_y = 1.0;
    }
    
    Line::Line(const Line & otherLine)
    {
    	start_x = otherLine.start_x;
    	start_y = otherLine.start_y;
    	end_x = otherLine.end_x;
    	end_y = otherLine.end_y;
    }
    
    bool Line::fillLine(ifstream & din)
    {
    	if(din >> start_x >> start_y >> end_x >> end_y)
    		return true;
    	else
    		return false;
    }
    
    double Line::getSlope()
    {
    	return (start_y - end_y) / (start_x - end_x);
    }
    
    void Line::print()
    {
    	cout << "(" << start_x << "," << start_y << ") to ("
    	     << end_x << "," << end_y << ")" << endl;
    }
    
    

    Write a (minimal) Polygon class that is based on a statically-allocated array of Line objects.

    You should include code for the class declaration (the contents of polygon.h), where you should have headers for a default constructor as well as the Polygon::fillArray() and Polygon::print() methods.

    Additionally, write the implementations for these three methods (the contents of polygon.cpp). You are not required to include any other methods or write a main() function, but your code should both be capable of compiling (if a main() were added) and operate well with the above Line class.