JMU
Sample Questions for Exam 1


  1. Indicate whether each of the following statements is true or false:
    (1) _____ All functions/methods in C++ must return a value.
    (2) _____ >> is the operator in the statement cin >> x;
    (3) _____ #define is a function.
    (4) _____ C++ only uses "call by value".
    (5) _____ A class can have more than one constructor.
  2. Evaluate each expression (written in C++) below or indicate that it will not compile:
    ((5 % 2) == 0)


    ((9 > 2) && (9 < 2))


    ((5 / 2) * 2)


    (false && (2 == 2) && (3 == 3))


  3. Evaluate each expression below (where the numbers are in binary, & denotes the bitwise AND operator, and | denotes the bitwise OR operator):
    10010110 & 10010110


    10010110 & 11111111


    10010110 | 111111111


    00001111 | 11110000


    00110011 & 00111100


  4. Carefully define each of the following:
    Pass by value


    Garbage


    Copy constructor


    Pointer variable


    cin


    Executable file


    Memory leak


    Local variable


    Side effect


    type casting


    void


  5. Indicate whether each of the following statements is true or false:
    (1) _____ Dynamic memory allocation is rarely used in practice.
    (2) _____ Uninitialized pointers should never be set to NULL.
    (3) _____ The "memory heap" uses a queue to allocate memory.
    (4) _____ new is a binary operator.
    (5) _____ In C++, exceptions are handled in do-handle statements.
  6. Suppose you have three object files, CDplayer.o, Equalizer.o and JMUtunes.o. What "command" (in DOS/Windows or UNIX) would you use to link these three files into an executable named JMUtunes (assuming that JMUtunes.o does have a main method)?
  7. Explain each line in the following makefile:
        test.exe: test.o module1.o
          g++ test.o module1.o -o test.exe
    
        test.o: test.cpp module1.h
          g++ -c test.cpp
    
        module1.o: module1.cpp module1.h
          g++ -c module1.cpp
    
  8. Assuming the following program is compiled and linked properly:
    #define QUIET
    #include <iostream.h>
    
    void main()
    {
      std::cout << "Execution begins...\n";
    
      #ifndef QUIET
      for (int i=0; i<5; i++) {
    
        std::cout << "Loop: " << i << "\n";
      }
      #endif
    
    
      std::cout << "Execution ends.\n";
    }
        

    what will be printed when it is executed?

  9. Given the following contents of the file Window.h:
    class Window
    {
      public:
    
        int CLOSED;
        int OPEN;
    
        int status;
    };
    
    int Window::CLOSED = 0;
    int Window::OPEN   = 1;
    

    will the following program compile, link and execute? If so, what will be printed? If not, why won't it compile and/or execute and how would you fix those problems?

    #include <iostream>
    #include "Window.h"
    
    int main(void)
    {
      Window     w;
    
      w.status = 0;
    
      if (w.status == Window::CLOSED) std::cout << "Closed\n";
      else                            std::cout << "Open\n";
    }
    
    
  10. What will be printed if the following is compiled, linked and executed properly? (Hint: Be careful!)
    #include <iostream>
    using namespace std;
    
    
    int max(int a, int b)
    {
       if (a > b) return a;
       else       return b;   
    }
    
    
    int min(int &a, int &b)
    {
       ++a;
       b = b + 2;
       
       if (a < b) return a;
       else       return b;   
    }
    
    
    int main(void)
    {
       int        temp, x, y;
       
       x = 3;
       y = 7;
       
       temp = max(x, y);
       
       cout << "max(" << x << "," << y << ") = " << temp << "\n";
    
       temp = min(x, y);   
    
       cout << "min(" << x << "," << y << ") = " << temp << "\n";
    }
      
  11. Given the following Rectangle.h:
    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    class Rectangle
    {
      protected:
       double  height, width;
       
      public:
       Rectangle(void);
       Rectangle(double w, double h);
       double area(void);
       double flange(void);
       virtual double perimeter(void);   
    };
    #endif
      

    Rectangle.cpp:

    #include "Rectangle.h"
    
    Rectangle::Rectangle(void)
    {
       width  = 0.0;
       height = 0.0;   
    }
    
    Rectangle::Rectangle(double w, double h)
    {
       width  = w;
       height = h;   
    }
    
    double Rectangle::area(void)
    {
       return width*height;   
    }
    
    double Rectangle::flange(void)
    {
       return 3.0;   
    }
    
    
    double Rectangle::perimeter(void)
    {
       return 2.0*width + 2.0*height;   
    }
      

    Square.h:

    #ifndef SQUARE_H
    #define SQUARE_H
    
    #include "Rectangle.h"
    
    class Square: public Rectangle
    {
      public:
       Square(double w);
       double flange(void);
       double perimeter(void);
    };
    #endif
      

    and Square.cpp:

    #include "Square.h"
    
    Square::Square(double w)
    {
       width  = w;
       height = w;   
    }
    
    
    double Square::flange(void)
    {
       return 17.0;   
    }
    
    
    double Square::perimeter(void)
    {
       return 10.0;   
    }
      

    whate will be printed by the following driver?

    #include <iostream>
    #include "Rectangle.h"
    #include "Square.h"
    
    using namespace std;
    
    
    int main(void)
    {
       Square       *s;
       Rectangle    *q;   
       Rectangle    *r;
       
    
       r = new Rectangle(2.0, 6.0);   
       s = new Square(4.0);
    
       q = new Square(5.0);
    
       cout << "r->area():      " << r->area() << "\n";
       cout << "r->perimeter(): " << r->perimeter() << "\n";
       cout << "r->flange():    " << r->flange() << "\n";
       cout << "\n";
       
       cout << "s->area():      " << s->area() << "\n";
       cout << "s->perimeter(): " << s->perimeter() << "\n";
       cout << "s->flange():    " << s->flange() << "\n";
       cout << "\n";
       
       cout << "q->area():      " << q->area() << "\n";
       cout << "q->perimeter(): " << q->perimeter() << "\n";
       cout << "q->flange():    " << q->flange() << "\n";
       cout << "\n";
       
    }
      
  12. Suppose that the output of the first four cout statements in the following program:
    #include <iostream>
    using namespace std;
    
    
    int main(void)
    {
       int        i;   
       int        j;
       int        *p;
       int        *q;
       
       i = 5;   
    
       cout << "i:  " <<  i << "\n";  //  1
       cout << "&i: " << &i << "\n";  //  2
       
       j = 9;   
    
       cout << "j:  " <<  j << "\n";  //  3
       cout << "&j: " << &j << "\n";  //  4
    
       p = &i;
       cout << "p:  " <<  p << "\n";  //  5
       cout << "*p: " << *p << "\n";  //  6
       
       j  =  i;
       q  = &j;
       *q =  7;
       cout << "i:  " <<  i << "\n";  //  7
       cout << "j:  " <<  j << "\n";  //  8
       cout << "q:  " <<  q << "\n";  //  9
       cout << "*q: " << *q << "\n";  // 10
       
       *p = *q;
        j =  3;
       cout << "i:  " <<  i << "\n";  // 11
       cout << "j:  " <<  j << "\n";  // 12
       cout << "*p: " << *p << "\n";  // 13
    }
      

    is as follows:

    i:  5
    &i: 0x22ff74
    j:  9
    &j: 0x22ff70
      

    what will be printed by the remaining cout statements?

  13. Suppose that the function change is implemented as follows:
      void change(int *a, int length)
      {
        int i;
    
        for (i=1; i<length; i++) {
    
           a[i] += *(a+i-1);
        }
      }
      

    What will be printed by the following code snippet (assuming that everything is compiled and linked properly)?

      int  i, n;
      int *value;
    
    
      n = 4;
      value = new int[n];
    
      for (i=0; i<n; i++) value[i] = 100 * i;
    
      change(value, n);
    
      for (i=0; i<n; i++) cout << value[i] << "\n";
    
      
  14. Given the following add() function:
    int add(int &a, int &b)
    {
       a = a + b;
       
       return a;
    }
    

    what will be printed by each of the following (assuming each is executed independently)? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location). (Hint: Be careful!)

    1.    int     sum, x, y;
         
         x   = 5;
         y   = 7;
         sum = add(x, y);
         
         cout << sum << " is the sum of " << x << " and " << y << "\n";
              
    2.    int     sum, x, y;
         
         x   = 5;
         y   = 7;
         sum = add(x, y);
      
         cout << x << " + " << y << " = " << sum << "\n";
              
    3.    int     x, y;
         
         x   = 5;
         y   = 7;
         
         cout << add(x, y) << " is the sum of " << x << " and " << y << "\n";
              
    4.    int     x, y;
         
         x   = 5;
         y   = 7;
      
         cout << x << " + " << y << " = " << add(x, y) << "\n";
              
  15. Identify all of the compile-time errors that will be generated by the following function:
    double total(int[] a, int n)
    {
       int sum;
       
       sum = 0;   
       for (int i=0; i<n; i++) sum += a[i];   
    
       return sum;   
    }
    
  16. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       sum = 0;   
       for (int i=0; i<n; i++) sum += a[i];   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       cout << total(x,4) << "\n";
    
  17. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       for (int i=0; i<n; i++) sum += a[i];   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       cout << total(x,4) << "\n";
    
  18. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       sum = 0;   
       for (int i=0; i<n; i++) sum += *(a++);   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       cout << total(x,4) << "\n";
    
  19. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       sum = 0;   
       for (int i=0; i<n; i++) sum += *(a+i);   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       cout << total(x,4) << "\n";
    
  20. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       sum = 0;   
       for (int i=0; i<n; i++) sum += *(++a);   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       cout << total(x,4) << "\n";
    
  21. Complete the following function. You may assume that the "string" is terminated with a '\0'. You may not use any library functions (e.g., functions in String.h).
      /**
       * Returns a substring of the given "string".
       * The correct amount of memory required 
       * by the substring is allocated from the heap.
       *
       * @param s      A pointer to the string
       * @param start  Index of the first char in the substring
       * @param length Length of the substring
       */
      char *substring(const char *s, int start, int length)
      {
    
    
      }
      
  22. Suppose you have a Car class that has a default constructor and a void method named start().
    1. Write a statement (or statements) that declares a Car object named chevy and constructs junk using memory from the stack.
    2. Write a statement (or statements) that declares a Car object named ford and constructs junk using memory from the heap (also known as the free store).
    3. Write a statement that calls the chevy object's start() method.
    4. Write a statement that calls the ford object's start() method.
  23. Write a Fraction class in C++ that includes the ability to multiply two Fraction objects and the ability to multiply a Fraction by an int. You may use either methods or operator overloading.
  24. Given the following specification in a file named Pong.h:
    #ifndef PONG_H
    #define PONG_H
    class Pong
    {
      private:
       int spaces;   
       
      protected:
       int current;
       int direction;
       
      public:
      /**
       * Explicit Value Constructor
       *
       * @param width   The width of the field
       */
       Pong(int width);
    
      /**
       * Get the char that should be used to represent the ball.
       *
       * In Pong, the ball is always represented by a 'o'.
       */
       virtual char getBall(void);   
    
    
      /**
       * Move the ball to the next position (based on the direction)
       *
       * This method ensures that the ball does not leave the field
       * (i.e., it ensures that current is never greater than spaces
       * and never less than 0).
       */
       void next(void);
    
    
      /**
       * Print the field (including the walls and the ball)
       */
       void print(void);   
    };
    #endif
    

    and the following partial implementation in a file named Pong.cpp, complete the next(). Your answer must be consistent with the comments describing the methods.

    #include "Pong.h"
    #include <iostream>
    using namespace std;
    
    Pong::Pong(int width)
    {
       spaces    =  width-1; // Total number of spaces
       direction =  1;       // Current direction (Right:1, Left: -1)
       current   = -1;       // Current position of the ball
    }
    
    
    char Pong::getBall()
    {
       return 'o';   
    }
    
    
    void Pong::next(void)
    {
    
    
    
    
    
    
    
    
    
    }
    
    
    void Pong::print()
    {
       char      ball;
       
       ball = getBall();   
    
       cout << "|";                                       // Left wall
       for (int i=0; i<current; i++) cout << " ";         // Spaces to the left
       cout << ball;                                      // The ball
       for (int i=current+1; i<=spaces; i++) cout << " "; // Spaces to the right
       cout << "|";                                       // Right wall
       cout << "\n";
    }
    

    When used with the following driver:

    #include "Pong.h"
    
    
    int main(void)
    {
       Pong       game = Pong(5);
    
       for (int i=0; i<21; i++)
       {
          game.next();
          game.print();
       }
    }
    

    the following output should be generated:

    |o    |
    | o   |
    |  o  |
    |   o |
    |    o|
    |   o |
    |  o  |
    | o   |
    |o    |
    | o   |
    |  o  |
    |   o |
    |    o|
    |   o |
    |  o  |
    | o   |
    |o    |
    | o   |
    |  o  |
    |   o |
    |    o|
    
  25. Given the following specification in a file named Tron.h:
    #ifndef TRON_H
    #define TRON_H
    
    #include "Pong.h"
    
    class Tron: public Pong
    {
      private:
       int sDirection;
       
      public:
      /**
       * Explicit Value Constructor
       *
       * @param width          The width of the field
       * @param starDirection  The direction in which the ball should be a '*'
       */
       Tron(int width, int starDirection);
    
    
      /**
       * Get the char that should be used to represent the ball.
       *
       * In Tron, the ball is represented by a '*' if the current
       * direction is equal to the "star direction".  Otherwise,
       * the ball from Pong is used.
       */
       char getBall(void);   
    };
    #endif
    

    fully implement the Tron class (in other words, create a complete Tron.cpp "file"). You may not change the visibility/accessibility of any attributes or methods in the Tron class, nor may you add attributes or methods to that class. However, you may make methods in that class virtual if necessary.

    When used with the following driver:

    #include "Tron.h"
    
    
    int main(void)
    {
       Tron       *game = new Tron(5,-1);
    
       for (int i=0; i<21; i++)
       {
          game->next();
          game->print();
       }
    }
    

    the following output should be generated:

    |o    |
    | o   |
    |  o  |
    |   o |
    |    *|
    |   * |
    |  *  |
    | *   |
    |o    |
    | o   |
    |  o  |
    |   o |
    |    *|
    |   * |
    |  *  |
    | *   |
    |o    |
    | o   |
    |  o  |
    |   o |
    |    *|
    

Copyright 2014