CSCI 240 - Quiz Questions - Week 5

1. (2) The three basic flow-of-control patterns are sequence, ___________ and __________

2. (2) The three basic loop statements in C are while, _______, and _________

3. (4) Write a fragment of code that checks a string variable to see if it contains your first name or not, and then prints either "that's me" or "that's not me" accordingly.  Assume that the following variable has a valid string in it before the test is made.

string aName;

4. (4) Write a fragment of code that accepts integers from the user until a negative number is entered.  The prompt should be "Enter a number (negative to quit):"  Add up the numbers and print the sum (not including the negative number).  Assume and use the following declarations:

int sum = 0;
int num;

5. (4) Given the variables top, left, right, and bottom representing the coordinates of a rectangle's upper left and lower right corners, and variables ptX and ptY, representing the coordinates of a point, write a condition to test if the point is outside the rectangle.  Assume that x increases to the right and that y increases to the top.

6. (4) Write a for loop that does exactly the same thing as the following code:

i = 1;
while (i < 11)
  {
  cout << "the square root of " << i << " is " << sqrt(i);
  i += 2;
  }

7. (2) Create a symbolic constant to represent the value of pi in two different ways.

8. (2) Give two reasons why we might use symbolic constants in a C++ program.

9. (2) What will the following code fragment print?

for (i = 0; i < 5; i++);
    cout << i + 1;
cout << i;

10. (2) Suppose

x = 0;
y = 5;

Is the following condition true or false?

if (x > 0 && x < 10 || y = = 4)

11. (2) Which of the following is a legal way to create a C-style symbolic constant?

a. #define MAX_VAL = 30;
b. #define MAX_VAL 30
c. #define MAX_VAL = 30
d. #define MAX_VAL