## CS 1005, C Term 2000 Introduction to Programming Practice Problems for Test 1

1. a) Circle all syntactically correct C++ statements among the following.
```   int k;

bool IsEven(int n);

k-;

if (k==1) {
k += 1;
}
```

b) State two advantages of using functions in program design.

```

```

2. Using the function IsPrime whose specification is given here:
```   bool IsPrime(int n)
// Returns true if n>1 and the only divisors of n are 1 and n
// Returns false otherwise
(function body not shown)
```
fill in the main() function below so that it prints all prime numbers between 1 and 10,000 to the console output cout. A prime number is an integer p for which the function invocation IsPrime(n) returns true. Consecutive numbers should be separated by spaces in the output.
```

```

3. For each of the following program segments, state what output is produced.
```a) int i=12;
cout << i%5;

b) int j=9;
cout << ++j;

c) int k=1;
while (k/4 < 2)
k*=3;
cout << k;

d) bool flag=false;
for (int i=1; i<=1000; i++)
if (i%3 == 0)
flag = !flag;
if (flag)
cout << "true";
else if (!flag)
cout << "false";

e) int lambda = 32;
while (lambda > 0) {
int k=lambda;
while (k > 0) {
cout << "*";
k /= 2;
}
cout << endl;
lambda /= 3;
}

```

4. Circle all incorrect statements in the following C++ program. In each case, give a brief explanation of what errors are associated with the statement. You may assume that the entirety of the program source file is shown below.
```   int main() {
double x
x = 3;
y = 4.5;
cout << the value of x+y equals << x+y;
return 0.0;
}
```

5. Examine the function named UFO defined below and state in words what numbers n yield a return value of true when UFO is called with n as its input argument.
```   bool UFO(int n)
{
while (n%2 == 0)
n/=2;
return (n==1);
}

```