Computer Science and Programming

Rowan University
Prof. Gregory Safko

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.

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. Know how to use the string class

22. Know the difference between a variable declared as string and one declared as char

23. What does prototyping mean?

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

25. Know how to prototype overloaded functions, and know how to overload functions (proper parameters, proper return types, etc.

26. Know how and why to use the void type

27. Know the difference between ‘A’ and “A” (or any other character represented in single quotes  vs. double quotes)

28. What is a formal parameter name?

29. 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)

30. In the following statement: cout.setf(ios::showpoint);, know the purpose of setf, ios::, and showpoint

31. Know the difference between pass-by-value and pass-by-reference (our book refers to it as call-by-value and call-by-reference)

32. What is a stub? What is a driver?

33. What is a test suite?

34. What is a bit? What is a byte? What is the binary counting system? What is the hexadecimal counting system?

35. Know how to declare and use an external file for reading (input) data.

36. Know how to declare and use an external file for writing (output) data.

37. Know how to read and interpret an ASCII table (page 958)

 


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 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 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?

 

 

To test if the first character of the string flag is the letter S, use:

a.  flag.at(0) = 'S';

b.  flag.at(0) == 'S';

c.  flag.at(0) = "S"; 

d.  flag.at(0) == "S"; 

 

 

 


 

Back to the main course page