A Collage Test

Essay by EssaySwap ContributorHigh School, 12th grade February 2008

download word file, 5 pages 0.0

Downloaded 461 times

1. vocabulary- fill in ty eblanks with the correct term from chapters 1-6: a. A c++ statement that declares the return value and parameters required for a function is called a function __________.

b. c++ statement that is carried out before compilation ( e.g. #include < iostream.h> is called a ___________ ___________.

c. the function that is called first in a c++ program is usually called ___________.

d.

Question 2 Evaluate the following expressions, giving the value AND the type (float, int, c har, bool...): Value Type _____ _____ 6 / 2.0 + 4 * 1 _____ _____ 12 - 4 % 8 + 4 * 2 + 3 _____ _____ (true && false) == true _____ _____ ! ( true | | false ) _____ _____ 1.0D + 1.0F + 1 _____ _____ 1 == 1 * 2 _____ _____ '2' - 1 _____ _____ '2' - '1' == '1' _____ _____ 3 / 2.0F

* 2 _____ _____ ( int i = 2.0F + 3.0 F ) Question #3 Assume we have initialized variable myChar of type char with some value extracte d from stdin.

Write a switch statement (or a series of if-else statements) that performs diffe rent actions depending on the value of myChar: - if it is a numeric character 1, 2, or 3, then two 'endl' manipulators are inserted.

- if it is a '+' then the void function add() is invoked.

- if it is a '-' then the void function subtract() is invoked.

- if it is a 'Q' or a 'q' then the program exits.

- any other character causes the following message to be inserted: "Please enter another character." 1. write an IF statement that prints out the value of variable x only if x is between 3 and 7.

2. write a switch statement thyat does thre same thing.

3 write four different c++ statements that each add 1 to variable x.

5.Provide a prototype for a function, called 'short' which returns an integer and takes two double numbers as parameters.

6. Show the output of the following program. You might want to show your work, which can help you receive partial credit.

#include int mystery(int); int main() { int result = mystery(6); cout << "Result of call to mystery = " << result << endl; return 0; } int mystery(int w) { cout << "w = " << w << endl; cout << "mystery(" << w << ") " << endl; if (w > 10) return 1; else return 3 * mystery(3 + w); } 7. #include void a( int ); void b( void ); int x = 10; int main() { int x = 20; cout << x << endl; a(x); cout << x << endl; b(); cout << x << endl; { int x = 30; cout << x << endl; } cout << x << endl; return 0; } void a( int y ) { int x = 40; cout << x << endl; x = y; cout << x++ << endl; } void b( void ) { x %= 3; cout << x << endl; } 8. Write a program that prompts the user to enter the area of a circle and then computes and prints the radius of that circle. Since the area of a circle is computed using the formula: $$mbox{area} = pi imes mbox{radius}^2,$$ you can invert the formula to compute the radius from the area like this: $$mbox{radius} = sqrt{frac{mbox{area}}{pi}}$$ You can use a constant called verb|M_PI| for the value of $pi$.

verb|M_PI| is in verb|math.h|.

9. Write a complete program that asks the user to enter an integer grade.

It echoes the input (outputs the input value) with a message: "Passed" if the grade is greater than or equal to 60, or "Failed" otherwise.

10.

ewpage item Write a complete program that computes the weight of an amount of water entered by the user. Your program should prompt the user to enter the amount of water (in American gallons) and should then compute the weight of that water in pounds or kilograms, depending on what the user wants. The formulas for computing the weight of water are: $$ pounds = gallons imes 8.327;;frac{pounds}{gallon}$$ $$ kilograms = gallons imes 3.785;;frac{kilograms}{gallon}$$ Your program should use const objects where appropriate. (25 points) Note: Your program does not have to do any error checking on its input.

underline{Sample run:} egin{verbatim} miller.cs: a.out Enter number of gallons: 4 Weight in kilograms (1) or pounds (2): 2 4 gallons of water weighs 33.308 pounds miller.cs: a.out Enter number of gallons: 4 Weight in kilograms (1) or pounds (2): 1 4 gallons of water weighs 15.14 kilograms end{verbatim} ewpage (If necessary, continue Problem 4 on this page) ewpage item What is the output of the following code segment? (10 points) ifnumlecnum=401 egin{verbatim} for (int i = 0; i < 4; i += 1) { for (int k = i; k <= 5; k++) { cout << "i = " << i << " k = " << k << endl;; k += 2; } i++; } end{verbatim} else ifnumlecnum=402 egin{verbatim} for (int i = 1; i < 7; i += 3) { for (int k = i; k <= 7; k++) { cout << "i = " << i << " k = " << k << endl; k += 2; } i++; } end{verbatim} fi fi What are the values of x and y after this code segment? egin{tabular}{lll} a) &5 &7 b) &4 &7 c) &2 &3 d) &2.5 &3.5 e) &2.5 &3 end{tabular} else ifnumlecnum=402 Which of the following five choices will be printed on the screen by this statement? egin{verbatim} cout << '2' << "*" << '2' << "!=" << (4>2 || 2*2 == 4) << endl ; end{verbatim} egin{tabular}{ll} a) & verb|2*2 ! = true| b) & verb|2*2!=1| c) & verb|'2'"*"'2'"!="0| d) & verb|2 * 2 != 1 endl| e) & verb|4!=1| end{tabular} fi fi item More Short Answer Questions (16 points) item Given these object definitions, ifnumlecnum=401 egin{verbatim} int i = 7; int j = 10; int k = -106; int m = 19; bool b = false; end{verbatim} else ifnumlecnum=402 egin{verbatim} int i = 3; int j = 10; int k = -75; int m = 21; bool b = false; end{verbatim} fi fi what is the value of each of the following objects? egin{verbatim} int v1 = j / i + m; end{verbatim} vspace{.85in} egin{verbatim} double v2 = k + i * j - m / 10.0; end{verbatim} vspace{.85in} egin{verbatim} bool v3 = i * j < -k; end{verbatim} vspace{.85in} egin{verbatim} bool v4 = (5 <= i && i <= 10) || !(b || (j > k)); end{verbatim} item This problem has two parts. First, cross out any statements in the following program that are not legal. Then, show the output of the program. (10 points) ifnumlecnum=401 egin{verbatim} #include using namespace std; int val = 7; int func1(int); int func2(int); int main() { int x = 6; cout << "func1(x) = " << func1(x) << endl; cout << "func2(x) = " << func2(x) << endl; return 0; } int func1 (int y) { int a = y + val; cout << "a = " << a << endl; { int val = 3; int b = y + val * val; a += b; } cout << "b = " << b << endl; return a; } int func2 (int val) { int c = ::val * 3; cout << "x * c = " << x * c << endl; return (val + c); } end{verbatim} else ifnumlecnum=402 egin{verbatim} #include using namespace std; int val = 4; int func1(int); int func2(int); int main() { int x = 5; cout << "func1(x) = " << func1(x) << endl; cout << "func2(x) = " << func2(x) << endl; return 0; } int func1 (int y) { int a = y + val; cout << "a = " << a << endl; { int val = 3; int b = y + val * val; a += b; } cout << "b = " << b << endl; return a; } int func2 (int val) { int c = ::val * 3; cout << "x * c = " << x * c << endl; return (val + c); } end{verbatim} fi fi