![Coppin State College](https://data.docslib.org/img/3a60ab92a6e30910dab9bd827208bcff-1.webp)
<p> COPPIN STATE COLLEGE COSC 199 - Fall 2005 TEST 2</p><p>NAME ______</p><p>1. (1pt) Write a C++ constant declaration that gives the name PI to the value 3.142. const float PI = 3.142 ;</p><p>2. (1pt) Write a C++ constant declaration that gives the name A_CHARACTER to the value 'z'. cons char A_CHARACTER = ‘z’ ;</p><p>3. (1pt) Write a C++ constant declaration that gives the name LAST_NAME to the value"Smith". Const string LAST_NAME = “Smith”; </p><p>4. (2pts) What is the difference between the literal string “area” and the identifier area? “area” – string constant value area – a variable</p><p>5. (2pts) Write a declaration statement and an assignment (input) statement to assign 30 meter to a variable named length. int length; length = 30;</p><p>OR int length = 30;</p><p>6. (2pts) Declare two integer variables in one declaration statement. The two integer variables should be named testNumber and testScore. </p><p> int testNumber, testScore;</p><p>7. (5pts) Place a check in the appropriate column.</p><p>IDENTIFIER VALID NOT VALID</p><p> a. _filesOf2004 ___X______b. $first ______X____ c. 25Days ______X____ d. my state ______X___ e. my_state __X______</p><p>8. (5pts) Place a check identifying reserve words and programmer-defined identifiers.</p><p>Reserved Programmer-Defined a. double __X______b. Char ______X____ c. integer ______X_____ d. sqrt ______X____ e. int ____X______9. You want to divide 21 by 4. </p><p>(a) (1pt) How do you write the expression in C++ if you want the result to be the floating- point value 5.25? _____21 / 4.0 or 21.0 / 4.0, or 21.0 / 4______</p><p>(b) (1pt) How do you write the expression in C++ if you want only the integer quotient? ____21 / 4______</p><p>(c) (1pt) How do you write the expression in C++ if you want only the remainder? _____21 % 4 ____</p><p>10. (1pt) Among the C++ operators +, *, /, %, <, &&, ||, ! which one(s) has or have the lowest precedence? </p><p>A) && B) || C) *, /, and % D) < E) ! F. +</p><p>11. (1pt) Among the C++ operators unary -, +, -, *, /, %, and =, which one(s) has or have the highest precedence? </p><p>A) +, - B) = C) *, /, and % D) % E) / F) +</p><p>12. (2pts) The value of the C++ expression 9 / 5 * 2 is: </p><p>A) 3.6 B) 0.9 C) 0 D) 2 E) none</p><p>13. (2pts) The value of the C++ expression 5 / 9 * 2 is: </p><p>A) 0.27… B) 1.11… C) 0 D) 2 E) none</p><p>14. (2pts) Given that x is a int variable and num is an float variable containing the value 10.5, what will x contain after execution of the following statement: x = num + 2; </p><p>A) 12.5 B) 10.5 C) 12 D) nothing; a compile-time error occurs</p><p>15. (2pts) Given that x is a float variable containing the value 80.9 and num is an int variable, what will num contain after execution of the following statement: num = x + 2.9; </p><p>A) 83.8 B) 84.0 C) 83 D) 83.0 E) nothing; a compile-time error occurs</p><p>16. (2pts) The value of the C++ expression 13 + 21 % 4 - 2 is: </p><p>A) 0 B) 12 C) 16 D) 14 E) none of the above</p><p>17. (2pts) Given that x is a float variable and num is an int variable containing the value 38, what will x contain after execution of the following statement: x = num / 4 + 3.0; </p><p>A) 12.5 B) 13 C) 12 D) 12.0 E) nothing; a compile-time error occurs</p><p>18. (5pts) If originally x = 5, y = 4, and z = 2, what is the value of the following expression? Solve step-by- step. y * (x + y) % 3 / z + 10 = 10</p><p>19. (2pts) Write a valid C++ expression for the following algebraic expression:</p><p> x ---- - 32 _x / (y * z) - 32 yz</p><p>20. (2pts) Write a valid C++ expression for the following algebraic expression:</p><p> xy ------______x * y / (y – 32)______y - 32</p><p>21. (2pts) Write a valid C++ expression for the following algebraic expression:</p><p> x + 32 ------(x – 2y) ______(x + 32) / (y – 32) – (x – 2 * y) y - 32</p><p>22. A.(5pts) Write the following mathematical equation to a C++ equivalent expression. Use sqrt(x) and pow(x, y) functions where appropriate.</p><p>3ijk + k9 result = ------Answer: 7ik - 5j + k result = (3 * I * j * k + pow (k, 9)) / (7 * I * k – 5 * sqrt(j = k))</p><p>B. (2pts) In the above expression, identify input variables and output variables.</p><p>Input variable(s): _____i , j, k______</p><p>Output variable(s): _____result ______</p><p>C. (2pts) If you have to write a C++ program to solve the above equation, what is or are the header file(s) to be included as Preprocessor Directive Statements? <iostream> <cmath> using namespace std;</p><p>23. (2pts) Write an output statement that prints your first name.</p><p> cout << “Joe”;</p><p>24. (2pts) Given the constant declaration: const int CONSTANT = 95; Which of the following is not a valid use of CONSTANT? A. cout << CONSTANT * 3; B. CONSTANT = 24; C. cin >> CONSTANT; D. A and C E. B and C above</p><p>25. (10pts) the following program is to convert temperatures in Celsius from Fahrenheit temperatures. In the following program, two types of blanks are there: Comment types (inactive statements) and C++ codes (active statements). Fill up the blanks appropriately. </p><p>// ___Preprocessor Directives______#include<iostream> using namespace std;</p><p> int main() { // Declaration Statements float celsius; // __output______variable. float fahrenheit; // ___input______variable</p><p>// _____Input______Statements cout << "Please enter the temperature: ; cin >> ___fahrenheit______;</p><p>// ______?Process statement______celsius = 5.0 / 9.0 * (fahrenheit - 32.0) ;</p><p>// Output Statements cout<< "Given Temperature in degree Fah = " << __ fahrenheit __ << endl << "Equivalent Temperature in degree ____Celsius______is " << ____ celsius _____ << endl ;</p><p>// Terminate the main function and return to Operating System return 0; } B. (2pts) Write the OUTPUT of the program if you enter the value of the input variable, fahrenheit = 32.0 Given Temperature in degree Fah = 32.0 Equivalent Temperature in degree Celsius is 0.0</p><p>26. (10pts) Trace the following program:</p><p>Statements 1. #include<iostream.h> ______syntax______2. const int TOT_COST = 120; ______TOT_COST = 120 ______3. const int POUNDS = 5; ______POUNDS = 5______4. const int OUNCE = 10; ______OUNCE = 10______5. int main () ______....______6. { ______....______7. int totOz; ______empty memory location for “totOz” ______8. float uCost; ______empty memory location for “totOz” ______9. totOz = 16 * POUNDS; ______totOz = 80______10. totOz = totOz + OUNCES; ______totOz = 90 ______11. uCost = TOT_COST / totOz; ______uCost = 1.3333 _____ 12. cout << "Cost per unit: ” << uCost << endl; ___Cost per Unit: 1.3333 ___ 13. return 0; ______....______14. } ______....______</p><p>27. The following C++ program has several syntax errors: (Note line numbers are not included in the program)</p><p>1. #include<iostream>; 2. using namespace std; 3. const float PI; 4. PI = 3.142; 5. int main () 6. [ 7. double radius; areaOfCircle; 8. cout >> “please enter the value of radius: ” ; 9. cin >> Radius; 10. areaOfCircle = PI (radius)2 ; 11. cout << "The Area of the Circle is" << “areaOfCircle” ; 12. << “for the radius = ” << Radius << endl; 13 Return 0; 14. ]</p><p>A. (5pts) Identify the line numbers (all lines are essential to solve the problem) that are not correctly written: </p><p>__1, 5, 7, 8, 9, 10, 11, 12, 13______</p><p>B. (5pts) Write only those lines in correct syntax below:</p><p>#include<iostream> using namespace std; const float PI; PI = 3.142; int main () { double radius, areaOfCircle; cout << “please enter the value of radius: ” ; cin >> radius; areaOfCircle = PI * radius * radius ; cout << "The Area of the Circle is" << areaOfCircle ; << “for the radius = ” << radius << endl; return 0; }</p><p>NOTE: Line 11 is a logical error and not a syntax error. Fin why? C. (5pts) After the syntax corrections, suppose you entered the value of radius as 3.0 in Line #8. Write the correct output of the program.</p><p>The Area of the Circle is201.09for the radius = 8</p><p>28. (2pts) What is the output of the following C++ Code?</p><p> x = 5; if ( x > 10 ) { cout << “Line 1” << endl; cout << “Line 2” << endl; } else cout << “Line 3” << endl; cout << “Line 4” << endl;</p><p>OUTPUT: Line 3 Line 4</p><p>29. (2pts) What is the output of the following C++ Code? </p><p> p = 1; if ( p >= 0 ) cout << “Hi” << endl; else cout << “Bye” << endl; cout << “Thank you” << endl;</p><p>OUTPUT: HI Thank you</p><p>30. (2pts) What is the output of the following C++ Code for the input data 35 30 36 24 cin >> a >> b >> c >> d; if (a > b | | a > c && c != d ) 35> 30 || 35 > 36 && 36 != 24 T || F && T T || F = T m = a; else if (b > d) m = b; else m = c; cout << m << endl;</p><p>OUTPUT: 35</p><p>31. (2pts) After execution of the following code, what will be the value of angle if the input value is 4?</p><p> cin >> angle; if (angle == 5) angle = angle + 10; else if (angle > 2) angle = angle + 5;</p><p>Value of angle is _____9 ______</p><p>32. (2pts) After execution of the following code, what will be the value of angle if the input value is 0?</p><p> cin >> angle; if (angle == 5) angle = angle + 10; else if (angle > 2) angle = angle + 5;</p><p>Value of angle is ____0 ______</p><p>32. (2pts) What is the output of the following C++ code fragment?</p><p> int1 = 5; cin >> int2; // Assume user types 6 if ((int1 <= 5) || (int2 > 6)) int3 = int1 / int2; else int3 = int1 % int2; cout << int1 << int2 << int3;</p><p>OUTPUT: 560</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-