CISC-1600-E01 Second Midterm, October 24, 2024

  1. What is the value of each of the following C++ expressions?
    1. 	10 - 20 - 30
      
    2. 	(10 - 20) - 30
      
    3. 	10 - (20 - 30)
      
  2. Assume that you already have the following four C++ variables.
    	int i {10};
    	int j {20};
    
    	double d {10.0};
    	double e {20.0};
    
    What would be the value of each of the following C++ expressions?
    1. 	i/j
      
    2. 	d/e
      
  3. Will the following two pieces of C++ code always produce the same output as each other, no matter what values are in the variables harris and trump?
    1. 	if (harris <= trump) {
      		cout << "Harris did not win.\n";
      	}
      
      	if (harris >= trump) {
      		cout << "Trump did not win.\n";
      	}
      
    2. 	if (harris <= trump) {
      		cout << "Harris did not win.\n";
      	} else {
      		cout << "Trump did not win.\n";
      	}
      
  4. Assume you already have a variable named dollars, of data type int, containing the number of dollars you have. (You must not create this variable: it already exists.) Write C++ code that will output an English sentence such as the following, to show the value of the variable. The English sentence should end with a period. Please output one newline character after the sentence. Output the newline without using endl.
    You have 5 dollars.
    
    The code should show the value of the variable no matter what that value is. In particular, the code should show the value of the variable even if that value is not 5. In the sentence that you output, the last word must be singular (“dollar”) if the value of the variable dollars is 1, plural (“dollars”) otherwise.

    Be sure to output a space on both sides of the number: you don’t want to output something like

    You have 5dollars.
    

    
    
    
    
    
    
    
  5. What (if anything) goes wrong in the following C++ code? Assume you already have four variables of type double, named a, b, c, d. (You do not need to create these variables: they already exist.)
    	if (b != 0.0 || d != 0.0) {
    		cout << "It's okay to go ahead and perform both divisions.\n";
    		cout << "The quotients are " << a/b << " and " << c/d << ".\n";
    	} else {
    		cerr << "Sorry, you can't divide by 0.\n";
    		return EXIT_FAILURE;
    	}
    
  6. What is a computer?
  7. What is a program?
  8. Will the following two pieces of C++ code always produce the same output as each other, no matter what value is in the variable hour?
    1. 	cout << "Good ";
      
      	if (hour >= 12) {
      		if (hour >= 12 + 5) {
      			cout << "evening";
      		} else {
      			cout << "afternoon";
      		}
      	} else {
      		cout << "morning";
      	}
      
      	cout << ".\n";
      
    2. 	cout << "Good ";
      
      	if (hour < 12) {
      		cout << "morning";
      	} else if (hour < 12 + 5) {
      		cout << "afternoon";
      	} else {
      		cout << "evening";
      	}
      
      	cout << ".\n";
      
  9. How many operators are there in each of the following C++ expressions? Number the operators in the order in which they are executed. For example, in the first expression, the * is number 1, and the + is number 2.
    1. 	a + 10 * b
      	  2    1
      
    2. 	-a * 10 - b
      
      
    3. 	cout << a + 10 * b << "\n"
      
      
    4. 	cout << 1 - 2 - 3 << "\n"
      
      
    5. 	cout << a << b << c << "\n"
      
      
    6. 	z = y = x = 10
      
      
  10. How many characters does this C++ statement output?
    	cout << "\tX\n";
    
  11. What does each of the following C++ examples output?
    1. 	int i {16};
      	i += 2;
      	cout << i << "\n";
      
    2. 	int i {16};
      	i *= 2;
      	cout << i << "\n";
      
    3. 	int i {16};
      	i /= 2;
      	cout << i << "\n";
      
    4. 	int i {16};
      	++i;
      	cout << i << "\n";
      
  12. Assume you already have a variable named i, of data type int, holding a positive number. (You must not create this variable: it already exists.) In the following parentheses, write a C++ expression whose value will be true if i is a number that ends with a 0 (such as 10, 20, 120, etc.), false if i is a number that does not end with a 0 (such as 11, 12, 21, 22, etc).
    	if (         ) {
    		cout << i << " ends with a 0.\n";
    	} else {
    		cout << i << " does not end with a 0.\n";
    	}
    
  13. Assume you already have a variable named m, of data type int, containing the number of minutes that a job took. (You must not create this variable: it already exists.) Define two more variables, named hours and minutes, of type int. Initialize hours to the number of hours that this job took. Initialize minutes to the number of remaining minutes that this job took. (Hint: the initial value of minutes must be a number in the range 0 to 59 inclusive.)
    
    
    
    
    
    
  14. Draw a picture of the output produced by each of the following C++ for loops.
    1. 	int n {4};
      
      	for (int i {0}; i < n; ++i) {
      		cout << i << "\n";
      	}
      
    2. 	int n {4};
      
      	for (int i {1}; i < n; ++i) {
      		cout << i << "\n";
      	}
      
    3. 	int n {4};
      
      	for (int i {0}; i <= n; ++i) {
      		cout << i << "\n";
      	}
      
    4. 	int n {4};
      
      	for (int i {1}; i <= n; ++i) {
      		cout << i << "\n";
      	}
      
    5. 	int n {4};
      
      	for (int i {1}; i <= n; ++i) {
      		cout << i;
      	}
      
      	cout << "\n";
      
  15. Draw a picture of the output producted by the following C++ code.
    	for (int i {0}; i < 3; ++i) {
    		for (int j {0}; j < 2; ++j) {
    			cout << "X";
    		}
    		cout << "\n";
    	}
    
  16. What (if anything) goes wrong in the following C++ code?
    	for (int i {0}; i < 10; ++i) {
    		cout << i << "\n";
    	}
    
    	cout << "After the loop is over, the value of i is " << i << "\n";
    
  17. How many multiplications does the following C++ code execute?
    	double money {100.00};
    
    	//Increase the money by 6 percent each year.
    
    	for (int year {0}; year < 10; ++year) {
    		money *= 1.06;
    	}
    
    	cout << "The money has grown to " << money << "\n";
    

Table of precedence and associativity
for the C++ operators we have covered so far

Precedence Operator Description Associativity
3   -a  
  ++a  
negation (as in “negative 5”)
increment (i.e., “add 1”)
right-to-left
5   a*b     a/b     a%b   multiplication, division, and remainder left-to-right
6   a+b     a-b   addition, subtraction left-to-right
7   cout<<x     cin>>x   output, input left-to-right
9   a<b     a<=b  
  a>b     a>=b  
is less than, is less than or equal to,
is greater than, is greater than or equal to
left-to-right
10   a==b     a!=b   is equal to, is not equal to left-to-right
14   a&&b   and left-to-right
15   a||b   or left-to-right
16   a=b  
  a+=b     a-=b     a*=b     a/=b     a%=b  
assignment
(change the value of a variable)
right-to-left