CS 2073 Section 2 Exam 1

Write all of your answers in the space provided. You may not use the computer or refer to any other materials while you are taking this test. Read each question carefully. Make sure to write your name on the test. Don't write ≤, &ge, or &ne when you mean <=, >=, or != ; we will take points off for that.
  1. True or False. Write 'T' for True and 'F' for False for each of the following statements:
    1. _F_ The int data type in C does not allow negative numbers.
    2. _T_ The double data type in C can be read with the "%lf" specifier using scanf.
    3. _T_ The for loop is not necessary in C.
    4. _F_ The computer uses a three digit number system called minary for representing values.
    5. _T_ (1 < 2) || (2 < 1)
    6. _F_ 'Z' <= 'Y'
    7. _T_ In a + b * c, the multiplication is done before the addition.
    8. _F_ In a * b / c, the division is done before the multiplication.
    9. _F_ The printf function is the only way to produce output on the screen.
    10. _T_ Your program is in main memory while it is running.
    11. _F_ The pico command is used to compile your program.
    12. _T_ You must compile with the -lm option to use the cos function.
    13. _F_ The three elements of structured programming are integers, doubles, and functions.
    14. _F_ Q is a hexadecimal digit whose value is 26.
    15. _T_ When adding binary numbers, it is still sometimes necessary to "carry the one."

  2. List three input devices.
    Some answers:
    Keyboard.  
    Mouse.  
    Microphone.  
    Touchscreen display.  
    Trackball.  
    Joystick.
    Scanner.
    
  3. List three output devices.
    Some answers:
    Monitor.
    Printer / plotter.
    Speakers.
    

  4. What is the output of the following program?
    #include <stdio.h>
    
    int main () {
            int     i, j, k;
            double  a, b, c;
            char    x, y, z;
            i = 10;
            j = 100;
            k = 5;
            a = i;
            b = j;
            c = k;
            x = 'a';
            y = 'b';
            z = 'c';
            printf ("%d\n", j / i);
            printf ("%d\n", i / j);
            printf ("%d\n", k % 2);
            printf ("%f\n", c / a);
            printf ("%f\n", b / c / a);
            printf ("%f\n", b / (c / a));
            if (x >= y && z >= y)
                    printf ("yes\n");
            else
                    printf ("no\n");
            printf ("%d\n", x < y || y > z);
    	return 0;
    }
    
    Solution:
    
    10
    0
    1
    0.500000
    2.000000
    200.000000
    no
    1
    

  5. Write a program that prompts the user with "Enter your dog's age:" then reads an integer. The program should then print the age in dog years, i.e., the number of actual years multiplied by 7. For example, if the user types in 10, the program should print "Your dog is 70 in dog years."
    Solution:
    
    #include <stdio.h>
    
    int main () {
    	int years;
    
    	printf ("Enter your dog's age: ");
    	scanf ("%d", &years);
    	printf ("Your dog is %d in dog years\n", years * 7);
    	return 0;
    }
    

  6. Consider the following code:
    #include <stdio.h>
    
    int main () {
    	int	i;
    	i = 1;
    	while (i <= 10) {
    		printf ("%d\n", i);
    		i++;
    	}
    	return 0;
    }
    
    Rewrite this code to do the same thing using for instead of while.
    Solution:
    
    #include <stdio.h>
    
    int main () {
            int     i;
    	for (i=1; i<=10; i++) {
                    printf ("%d\n", i);
            }
            return 0;
    }
    
    

  7. Consider the following code:
    #include <stdio.h>
    
    int main () {
    	int	n, i, found_a_factor;
    	printf ("Enter an integer: ");
    	scanf ("%d", &n);
    	found_a_factor = 0;
    	for (i=2; i <= n-1; i++)
    		if (n % i == 0) found_a_factor = 1;
    	if (found_a_factor == 0) 
    		printf ("prime\n"); 
    	else
    		printf ("not prime\n");
    	return 0;
    }
    
    This code prompts the user for an integer and prints "prime" if the integer is prime, "not prime" otherwise. Using this code as a starting point, write a program that prints the prime numbers between 2 and 1000. Hint: get rid of the scanf. Hint 2: get rid of the part that says "prime" or "not prime" and replace it something that only prints n if it is prime. Hint 3: you'll need another loop.

    Solution:
    
    #include <stdio.h>
    
    int main () {
            int     n, i, found_a_factor;
            for (n=2; n<=1000; n++) {
                    found_a_factor = 0;
                    for (i=2; i <= n-1; i++)
                            if (n % i == 0) found_a_factor = 1;
                    if (found_a_factor == 0)
                            printf ("%d\n", n);
            }
            return 0;
    }