## C++ Problem Set

### Questions

#### Problem Set 1

Study the following program and then answer the questions below. We'd like you to answer the questions by looking only at the source code, i.e., please don't copy it into a file, compile it and answer the questions based on running it. That will take all the fun out of it!

int main() {  int counter, first, last, next;  first = 1; last = 2;  for (counter = first; counter <= last; counter++) {    cout << "\n " << counter;    next = counter * counter;    cout << " " << next;  }  counter = first;  while (counter <= last) {    cout << "\n " << counter;    next = counter * counter;    cout << " " << next;    counter++;  }  counter = first;  do {    cout << "\n " << counter;    next = counter * counter;    cout << " " << next;    counter++;  } while (counter < last);}

##### Question 1:

What is the output of this program?

 A) 1 22 41 22 41 22 4 B) 1 12 41 12 41 12 4 C) 1 12 41 12 41 1 D) 1 12 41 11 12 4 E) It does not output anything - there are errors in syntax.

##### Question 2:

What would happen if we remove the initialization of "counter" prior to the do-while loop?

 A) An Infinite loop - the do-while loop will output a series of 1's B) The program's output will not change C) The do-while loop outputs 2 and 4. D) The do-while loop will output nothing E) The do-while loop outputs 3 and 9.

##### Question 3:

Given the original program at the top of this page, assume we have removed the line that initializes the counter variable before the while loop. What would happen if we also remove the counter++ line inside the while-loop, as in the following?

 A) The while loop outputs nothing. B) The while loop outputs 1 and 1; the do-while loop outputs nothing. C) The output of the while loop is the same as when both lines are included. D) The system will output random numbers until we turn the computer off. E) The while loop is an Infinite loop

##### Question 4:

Given the original program at the top of this page, what would happen if the while loop looked like this?

counter = first;while (counter <= last) {  cout << "\n" << counter;  if (first % 2 == 0)    next = counter * counter;  cout << "  " << next;  counter++;}
 A) The output of the while loop is the same as in the original program. B) The while loop will not output anything C) The output of the while loop is 1 1 and 1 4. D) The output of the while loop is 1 2 and 2 4. E) The output of the while loop is 1 4 and 2 4. F) The output of the while loop is 2 4 and 2 4.

##### Question 5:

What would happen if the first variable is greater than the last?

 A) The while-loop will output something, but nothing else will. B) The do while-loop will output something, but nothing else will. C) There will be no output at all. D) The program will seg fault or crash E) The for-loop will output something, but nothing else will.

##### Question 6:

What will the program output if we initialize the first variable to be the same as the last variable?

 A) The do while-loop will output something, but nothing else will. B) The while-loop will output something, but nothing else will. C) Each loop will output one line. D) The do while-loop will output 2 lines, and the other loops one line. E) It will output nothing F) The for-loop will output something, but nothing else will.