Docsity

24 Questions on Programming and Problem Solving I - Midterm | CECS 174, Exams of Computer Science

Toggle sidebar
bg1
Chapter 7 Self Test Exercises
1. Determine the value, true of false, of each of the following Boolean expressions, assuming
that the value of the variable count is 0, and the value of the variable limit is 10.
a.(count == 0) && (limit < 20)
b.count == 0 && limit < 20
c.(limit > 20) || (count < 5)
d.!(count == 12)
e.(count == 1) && (x < y)
f.(count < 10) || (x < y)
g.!( ((count < 10) || (x < y)) && (count >= 0) )
h.((limit/count) > 7) || (limit < 20)
i.(limit < 20) || (limit/count) > 7)
j.((limit/count) > 7) && (limit < 0)
k.(limit < 0) && ((limit/count) > 7)
l.(5 && 7) + (!6)
2. Name two kinds of statements in C++ that alter the order in which actions are performed.
Give some examples.
3. In college algebra we see numeric intervals given as 2 < x < 3. In C++ this interval does
not have the meaning you may expect. Explain and give the correct C++ Boolean
expression that specifies that x lies between 2 and 3.
4. Does the following sequence produce division by zero?
j = -1;
if ((j > 0) && (1/(j+1) > 10))
cout << i << endl;
5. Write a function definition for a function called in_order that takes three arguments of
type int. The function returns true if the three arguments are in ascending order;
otherwise, it returns false. For example, in_order(1, 2, 3) and in_order(1,
2, 2) both return true, while in_order(1, 3, 2) returns false.
6. Write a function definition for a function called even that takes one argument of type int
and returns a bool value. The function returns true if its one argument is an even
number; otherwise, it returns false.
7. Write a function definition for a function is_digit that takes one argument of type char
and return a bool value. The function returns true if the argument is a decimal digit;
otherwise, it returns false.
pf3
pf4
pf5

Partial preview of the text

Download 24 Questions on Programming and Problem Solving I - Midterm | CECS 174 and more Exams Computer Science in PDF only on Docsity!

Chapter 7 Self Test Exercises

  1. Determine the value, true of false, of each of the following Boolean expressions, assuming that the value of the variable count is 0, and the value of the variable limit is 10. a.(count == 0) && (limit < 20) b.count == 0 && limit < 20 c.(limit > 20) || (count < 5) d.!(count == 12) e.(count == 1) && (x < y) f.(count < 10) || (x < y) g.!( ((count < 10) || (x < y)) && (count >= 0) ) h.((limit/count) > 7) || (limit < 20) i.(limit < 20) || (limit/count) > 7) j.((limit/count) > 7) && (limit < 0) k.(limit < 0) && ((limit/count) > 7) l.(5 && 7) + (!6)
  2. Name two kinds of statements in C++ that alter the order in which actions are performed. Give some examples.
  3. In college algebra we see numeric intervals given as 2 < x < 3. In C++ this interval does not have the meaning you may expect. Explain and give the correct C++ Boolean expression that specifies that x lies between 2 and 3.
  4. Does the following sequence produce division by zero? j = -1; if ((j > 0) && (1/(j+1) > 10)) cout << i << endl;
  5. Write a function definition for a function called in_order that takes three arguments of type int. The function returns true if the three arguments are in ascending order; otherwise, it returns false. For example, in_order(1, 2, 3) and in_order(1, 2, 2) both return true, while in_order(1, 3, 2) returns false.
  6. Write a function definition for a function called even that takes one argument of type int and returns a bool value. The function returns true if its one argument is an even number; otherwise, it returns false.
  7. Write a function definition for a function is_digit that takes one argument of type char and return a bool value. The function returns true if the argument is a decimal digit; otherwise, it returns false.
  1. Write a function definition for a function is_root_of that takes two arguments of type int and returns a bool value. The function returns true if the first argument is the square root of the second; otherwise, it returns false.
  2. What output will be produced by the following code, when embedded in a complete program? int x = 2; cout << “Start\n” if (x <= 3) if (x != 0) cout << “Hello from the second if.\n”; else cout << “Hello from the else.\n”; cout << “End\n”; cout << “Start again\n”; if (x > 3) if (x != 0) cout << “Hello from the second if.\n”; else cout << “Hello from the else.\n”; cout << “End again\n”; 10.What output will be produced by the following code, when embedded in a complete program? int extra = 2; if (extra < 0) cout << “small” else if (extra == 0) cout << “medium”; else cout << “large”; 11.What would be the output in problem 10 above if the assignment were changed to the following? int extra = -37; 12.What would be the output in problem 10 above if the assignment were changed to the following? int extra = 0; 13.What output willl be produced by the following code, when embedded in a complete program? int x = 200; cout << “Start\n”

32.For each of the following situations, tell which type of loop(while, do-while, or for) would work best: a) Summing a series, such as 1/2 + 1/3 + 1/4 + 1/5 + … + 1/10. b) Reading in the list of exam scores for one student. c) Reading in the number of days of sick leave taken by employees in a department. d) Testing a function to see how it performs for different values of its arguments. 33.Rewrite the following loops as for loops: a. int i = 1; while ( i <= 10) { if (i < 5 && i != 2) cout << ‘X’; i++; } b. int i = 1; while (i <= 10) { cout << ‘X’; i = i + 3; } c. long m = 100; do { cout << ‘X’; m = m + 100; } while (m < 1000); 34.What is the output of this loop? Identify the connection between the value of n and the value of the variable log. int n = 1024; int log = 0; for (int i = 1; i < n; i = i * 2) log++; cout << n << “ “ << log << endl; 35.What is the output of this loop? Comment on the code. int n = 1024; int log = 0; for (int i = 1; i < n; i = i * 2) log++; cout << n << “ “ << log << endl; 36.What is the output of this loop? Comment on the code. int n = 1024; int log = 0; for (int i = 0; i < n; i = i * 2) log++; cout << n << “ “ << log << endl;

Chapter 7 Self Test Exercises (on sections that were skipped in Midterm 2) 17.Given the following declaration and output statement, assume that this has been embedded in a correct program and is run. What is the output? enum Direction { N, S, E, W }; //... cout << W << “ “ << E << “ “ << S “ “ << N << endl; 18.Given the following declaration and output statement, assume that this has been embedded in a correct program and is run. What is the output? enum Direction { N = 5, S = 7, E = 1, W }; //... cout << W << “ “ << E << “ “ << S “ “ << N << endl; 19.What output will be produced by the following code, when embedded in a complete program? int first_choice = 1; switch (first_choice + 1) { case 1: cout << “Roast beef\n”; break; case 2: cout <, “Roast worms\n”; break; case 3: cout << “Chocolate ice cream\n”; case 4: cout << “Onion ice cream\n”; break; default: cout << “Bon appetite!\n”; } 20.What would be the output in problem 19 above if the first line were changed to the following? int first_choice = 3; 21.What would be the output in problem 19 above if the first line were changed to the following? int first_choice = 2; 22.What would be the output in problem 19 above if the first line were changed to the following? int first_choice = 4;