Structured Programming in C++
Prof. Gregory Safko

Camden County College

Review: Exam 1


 

General Knowledge

1.   What is the difference between a compiler directive and a preprocessor directive? (Yes, it’s a trick question)

2.   What are the 2 ways to write comments?

3.   What is using namespace std; and why do you need it?

4.   Know the proper naming convention for variables

5.   What are the 6 comparison operators?

6.   What is a Boolean data type? What is a Boolean expression?

7.   Name at least 4 primitive data types.

8.   What is the && operator? What is the || operator?

9.   What is the proper way to use the if statement? The if-else statement?

10. What is the proper way to use a while loop? A do-while loop?

11. What is the difference between = and == ?

12. Know the use and difference between the pre- and post- increment and decrement operators (++a, a++, --a, a--)

13. Know what typecasting is, and how to typecast variables using the static_cast operator.

14. Know what a workspace is, and why you need to open and close them

15.Give three examples where you would not put a semicolon at the end of a statement

16. What does the const operator do?

17. What are the two ways to output a newline to the screen?

18. Know how to properly use cout and cin, and their stream operators

19. What is abstraction?

20. What does the function sqrt do? In what library is it found?

21. What does prototyping mean?

22. Know the difference between return 0; and return EXIT_SUCCESS;

23. Know the difference between int a = 5; and int a(5); (note: the second one is not a function prototype, but rather a way to declare variables and data)

24. Know the different types of errors, and what can cause them.

25. Know the results of the following:

      int x = 10;

  cout << x++ << endl;

  int x = 50;

  cout << ++x << endl;

 

 

 


 

Sample Programs

 

Many times a programmer needs to test if a number is even or odd. Although there are many techniques and algorithms for accomplishing this, one of the basic methods is to divide the number by two, and store it into a variable of type int. Then you would multiply it by two to see if it equals the original number. If it does, it is even; otherwise it is odd. This is the algorithm you must use in this code.

 

 

Write code that will do the following (Note: the following is sample input, it should also work for ALL numbers entered. You may assume that only integers are entered)

 

 

Enter a number to test (0 to end): 7

7 is odd

Enter a number to test (0 to end): 4

4 is even

Enter a number to test (0 to end): 0

0 is even, and goodbye

 

 

Some hints: 

Note that there is a while loop used in this code.

Note that if a user enters 0, still tell them it is an even number before quitting (i.e. saying “goodbye”)

Test your code with the following data:

-2

10

11

 

Debugging

Correct what is wrong with the following (there may be more than one thing wrong). Assume the all the appropriate preprocessor directives (#include’s) are defined, and using namespace std; has been declared.

 

1.  // This will test if a number equals 0

int x;

cin >> x;

if (x = 0)

  cout << "It’s zero";

 

 

2.  // This will test if a number is negative

int a;

cin >> a;

if a < 0

  cout << "It’s negative";

else

cout << "It’s positive";

 

 

3.  // This will test if a number is negative

int a;

cin >> a;

if (a < 0)

  cout << "It’s negative";

else

cout << "It’s positive";

else

cout << "It’s positive";



 

4.  // This will test if x is greater than y

if (x > y) ;

  cout << "Yes, x is greater than y";

 

5.  // This code will loop while k is not equal to zero

int b,k;

cin >> b;

k = b/2;

while k != 0

{

if k = b

  cout << "It’s even";

else

cout << "It’s odd";

}

 

6.  // This code will test if m is even

int m,p;

p = m/2;

cout << "Enter a number: ";

cin >> m;

if (m = 2 * p)

cout << m << " is even"<< endl;

else

cout << m << " is odd" << endl;

 

7.  // This code will check if a number is even, and will run until the user // enters a number less than 10

cout << "Enter a number greater than 10: ";

cin >> j;

while (j <= 10)

{

if (j % 2 == 0)

  cout << j << "is even";

cout << "Enter a number greater than 10: ";

}


 

 

8.  // This code will test if a number is unequal to 5

cout << "Enter a number: ";

int f == 5;

cin >> w;

if (w <=> 5)

cout << "This number is not 5";

 

 

9.  // This code will display a number, and it’s "half"

double m,n;

n = m/2;

cout << "Please enter a number: ";

cin >> m;

cout << "m is: " << "m" << endl;

cout << "m cut in half is: " << n << endl;

 

 

10.         // This is just a control structure. Why won’t it compile?

int x = 1;

while (x <= 10);

x++;

}

 

Short answers:

 

Why could the loop in question #5 be an infinite loop?

 

 

Under what conditions would you use a do/while loop rather than a while loop?