Study questions for Lecture Test 2 CS 115

These will not be collected. They are not guaranteed to be 100% of the test material. You can do these and discuss them with your TA or Dr. Keen.

  1. Describe the difference between a control structure and a data structure. Name an example of each.
  2. What's the job of a call stack (in the debugger)?
  3. Trace this code with speed = 55:
    	if (speed < 40)
    		cout << "slow";
    	else if (speed < 55)
    		cout << "legal";
    	else 
    		cout << "ticket";
    	
    
    Write a test plan for this code. Rewrite this code so that it does not use nested if's. (Hint: boolean/logical operators)
  4. Knowing the difference between logical and relational operators.
  5. How are strings compared? How is the ASCII code involved?
  6. Put parentheses in the expression below so that they show what order the operations will be done in:
        a < 0 && b >= 5 + y || z > 3 && q == p
  7. What is the difference in operation between a "=" operator and a "==" operator?
  8. Describe precisely what happens when the code below is executed. Describe how type coercion is involved.
    	x = 10;
    	if (x = 0)
    		cout << "no";
    	else
    		cout << "yes";
    
  9. Trace this code with the values str = "OK" and num = 17:
    	if (str == "OK")
    		if (num > 5)
    			cout << " white";
    		else
    			cout << "blue";
    
    Write a test plan for this code. What are the possible outputs of this code? what are values that would cause them?
  10. Knowing the difference between prototypes, headers and calls.
  11. Know the right ways to call a value-returning function. to call a void function.
  12. Trace a program with functions.
  13. Know the difference between global and local scope.
  14. Understand the difference between parameters and arguments. How do they work together?
  15. Trace this code:
    
    float f1 (int a, int b);
    
    int main()
    {
    	float v1;
    	int v2, v3, v4;
    	v2 = 5;
    	v3 = v2 * 2;
    	v4 = v2 + v3;
    	v1 = f1( v3, v4);
    	cout << v1 << f1 (v2, v4) << endl;
    	return 0;
    }
    float f1 (int a, int b)
    {
    	return (a + b) / 2.0;
    }
    
  16. Trace this code:
    
    int fun1 (int p1)
    {
    	cout << "red ";
    	return p1 + 3;
    }
    int fun2 (int p2)
    {
    	cout << "green ";
    	return fun1(p2) * 3;
    }
    
    int main()
    {
    	
    	cout << fun2 (12) << endl;
    	cout << "yellow";
    	return 0;
    }
    
  17. Write a loop that will read in numbers from the keyboard until a 0 is entered, then report the largest and smallest numbers entered. Be careful NOT to process the zero.
  18. Write an expression that would output a random number between 15 and 100 inclusive.
  19. Why are each of these an infinite loop?
    a.      char Answer = 'y';
            while (Answer = 'y')
            {
              n++;
              cout << "another loop? y/n";
              cin >> Answer;
            }
    
    b.      i = 0;
            while (i < n)
            {
               n ++;
               cout << i;
            }
    
    
  20. Correct the logic errors in the code below.
       // Add numbers entered until sentinel -1 is encountered.
              cin >> n;
              while (n = -1)
                 sum = sum + n;
                 cin >> n;
    
    
  21. What's the difference between a counter and an accumulator? When do you use each?
  22. Write a while loop that would generate the output below:
            0.0 - 0.1
            0.1 - 0.2
            0.2 - 0.3
            0.3 - 0.4
              etc.
            0.9 - 1.0
    
  23. Write some code where you use a loop controlled by a counter to input 10 numbers and report the average of only the POSITIVE ones. Include the initializations needed.
  24. Trace this code - show what happens in RAM at each step and what is output.
    	int c = 5;
    	int x = 2;
    	cout << "x " << x << " c " << c << endl;
    	while (c > 0)
    	{
    		x = x * 2;
    		cout << "x " << x << " c " << c << endl;
    		c = c - 1;
    	}
    
  25. Given this code:
    int fun3 (int x, char y)
    {
    	int z;
    
    }
    int main ()
    {
    	char z;
    	
    }
    

    if z is referred to in the main function, what type is it?
    if z is referred to in the fun3 function, what type is it?
    if x is referred to in the main function, what happens?
    if y is referred to in fun3, what type is it?

  26. In this code, give three sets of values for state and age that would make drinking true.
    	int age;
    	bool drinking;
    	char state;
    
    	if (age >= 18)
    		if (state == 'K' || state == 'R')
    			drinking = true;
    		else if (state == 'C' && age > 25)
    			drinking = true;
    		else drinking = false;
    	else if (age > 15)
    		if (state == 'T' || state == 'M')
    			drinking = true;
    		else drinking = false;
    	else
    		drinking = false;
    
  27. Write a function that would accept two float parameters and return true if the two parameter values are within 0.01 of each other, and return false otherwise.
  28. Write a while loop that counts from 0 to 200 by 20's.
  29. What's the output?
    	int x = 5, y = 7, z = 9;
    	if (x + 2 > y - 5 && z > 3 || z == 10)
    		cout << "yes";
    	else cout << "no";
    
    
  30. What's wrong with this code?
            void fun1 (int a);
            int fun2 (int b);
    
            int main ()
            {
                    int x;
                    if (fun1 (x) > 3)
                            cout << "ok";
                    fun2 (x);
                    return 0;
            }
    
                                                    
    
  31. Write the truth table for the && operator.
  32. Describe two ways to control a loop.
  33. __________________ are the blanks that are filled in by copies of _______________________________ when the function is called.
  34. Why doesn't this code compile?
            if (b == c)
                    cout << "ok";
                    b = c + 1;
            else
                    cout << "no";
                    b = c - 1;
    
  35. What is the job of the srand function?
  36. What distinguishes a counter from just another assignment statement?
  37. What distinguishes a counter from an accumulator?
  38. What are the three steps to writing a counter-controlled loop?