A Sample Test Is Given Later in This Document. Practice It

Total Page:16

File Type:pdf, Size:1020Kb

A Sample Test Is Given Later in This Document. Practice It

COSC 199 TEST 2 STUDY GUIDE A sample test is given later in this document. Practice it.

Test will cover Chapters 2, 3, and 5. Review class notes and all assignments, quizzes, if any.

Chapter 2 & 3 Review Identifiers & their rules, Reserved word, Matters of Style, Data and Data Types, and their sizes, Data Storage, The char Data Type and string Data Type, Numerical data types, Declarations of variables, named constants, Assignment Statement , string and character output, Type coercion, Type casting, precedence rules, mixed arithmetic, C++ functions: abs(x), pow(x, n), sqrt(x), and their preprocessor directives,

Chapter 2 Quick-Check , Exam Preparation Exercises: 1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, Programming Warm-up Exercises: 2, 3, 6 (a), (b), (c), (d).

Chapter 3: Quick Check (p.133-136), Exam Preparation Exercises: 1, 2, 3, 4, 5, 8, 10, and 11

Chapter4 Review Sections 4.1, 4.2 (pp.140-152) Quick Check: 1 to 5 (p.183-184), Exam Preparation Exercises: 1 to 9 (pp.184-185) Chapter 5 Relational operators, Logical operators, precedence rules, Truth Tables One-Way if, Two-way if statements. Quick Check, Exam Preparation Exercises: 1 to 5, 7, 8, 9, 10 to 13.

My Notes A. Concept of data size: 1 character = 8 bits; 1 byte = 8 bits; 1 character = 1 byte How many bits represent a string literal “Smith”? ______

C++ data types: Review the meaning of data type and size of a data type Get familiar with at least the following data types: primitive data types: char, int, float, double, bool non-primitive data type: string and its header file (string.h or string in New Standard)

B. How to declare Variables?

C++ syntax: ; declare a character variable nameInitial: char nameInitial; declare an integer variable number: int number; declare an floating point variable number: float number; declare an floating point variable number: double number; (double the size than float) declare an string variable name: string name; (NOTE: A string data type could be declared only in New Standard of C++ and you have to include string header file.)

C. How to declare a Named Constant?

C++ Syntax: const = constant literal (or value); declare a character constant nameInitial: const char NAME_INITIAL = ‘J’; declare an integer constant number: const int NUMBER = 2345; declare an floating point constant number: const float NUMBER = 23.45; declare an floating point constant number: const double NUMBER = -2345.9 ; declare a string constant name: const string NAME = “Jack”;

D. What is a Literal? A literal means the value that is assigned to a variable or a named constant. Literals assigned to a variable char nameInitial; nameInitial = 'K' ; ==> 'K' is a character literal string name; name = "Joe"; ==> "Joe" is a string literal int number; number = -25 ==> -25 is an integer literal float number; number = 256.78 ==> 256.78 is a floating point literal

Literals assigned to a named constant const char nameInitial = 'K' ; ==> 'K' is a character constant literal const string name = "Joe"; ==> "Joe" is a string constant literal const int number = -25 ==> -25 is an integer constant literal const float number = 256.78==> 256.78 is a floating point constant literal

E. Precedence Rules:

Arithmetic Operators: (...), Unary +, Unary -, Binary: +, -, /, *, % Relational (comparison) Operators: <, >, = =, <=, >=, != Logical Operators !, &&, || AND Truth Table, OR Truth Table, NOT Truth Table.

A SAMPLE TEST QUESTION SET

1. (2pts) Write a C++ constant declaration that gives the name bonus to the value 3000.00

______

2. (2pts) Declare an int variable named count and a floating point variable named sum.

______, ______

3. You want to divide 20 by 7.

(a) (2pts) How do you write the expression in C++ if you want the result to be the floating- point value? ______

(b) (2pts) How do you write the expression in C++ if you want only the integer quotient? ______

4. (2pts) Among the C++ operators +, -, *, /, and %, which ones have the lowest precedence?

A) + and - B) * and / C) *, /, and % D) +, -, and % E) +, -, and *

5. (2pts) The value of the C++ expression 3 / 4 * 5 is:

A) 0.0 B) 0 C) 3.75 D) 3 E) 0.15

6. (2pts) Given that x is a float variable and num is an int variable containing the value 5, what will x contain after execution of the following statement:

x = num + 2;

A) 7 B) 7.0 C) 5 D) 5.0 E) nothing; a compile-time error occurs

7. (2pts) Given that x is a float variable containing the value 84.7 and num is an int variable, what will num contain after execution of the following statement: num = x + 2; A) 86.7 B) 86 C) 87 D) 87.0 E) nothing; a compile-time error occurs

8. (2pts) The value of the C++ expression 11 + 22 % 4 is:

A) 13 B) 1 C) 8 D) 16 E) none of the above

9. (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;

A) 12.5 B) 13 C) 12 D) 12.0 E) nothing; a compile-time error occurs

10. (5 pts) What is the value of the following expression? Solve step-by-step.

5 * 2 / 6 + 15 % 4

11. (5pts) If originally x = 4, y = 3, and z = 2, what is the value of the following expression? Solve step-by-step.

z - (x +z) % 4 + 6

12. (2pts) Write a valid C++ expression for the following algebraic expression: x ---- - 3 ______y

13. (2pts) Write a valid C++ expression for the following algebraic expression:

xy ------______y - 3

14. (2pts) Write a valid C++ expression for the following algebraic expression:

x / y + 3 ------______y - 3

15. A.(4pts) Write the following mathematical equation to a C++ equivalent expression. Use sqrt(x) and pow(x, y) functions where appropriate.

3xy + w4 z = ------Answer: ______5w - 7x

B. (2pts) In the above expression, identify input variables and output variables.

Input variables: ______

Output variables: ______

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?

16. The following C++ program has several syntax errors: (Note line numbers are included in the program)

1. #include 2. using namespace std; 3. int main() 4. { 5. float length, width, 6. int perimeter; 7. length = 20.0; 8. Width = length - 5.0; 9. perimeter = 2(length + width); 10. cout << "The perimeter of the rectangle is: " << Perimeter << endl; 11. return 0; 12. } A. (5pts) Identify the line numbers (all lines are essential to solve the problem) that are not correctly written: ______

B. (5pts) Write only those lines in correct syntax below:

C. (4pts) Write the OUTPUT (after the corrections) ______

17. (10pts) In the following program, two types of blanks are there: Comment types (inactive statements) and C++ codes (active statements). Fill up the blanks appropriately.

// ______#include

int main() { // ______float celsius, fahrenheit;

// ______cout << "Please enter the temperature in Celsius: "; cin >> ______;

// Process Statements fahrenheit = 9.0 / 5.0 * celsius + 32.0;

// ______cout << celsius << " degree Celsius = " << ______<< " degree Fahrenheit" << endl;

// Terminate the main function and return to Operating System return 0; }

B. (4pts) Write the OUTPUT of the program if you enter the value of the input variable, celsius = 100.0 you may use calculator). ______18. (10pts) Trace the following program:

Statements 1. #include 2. int main () 3. { 4. float carSpeed, travelTime, tavelDistance; 5. carSpeed = 50.0; // 50 miles per hour 6. traveTime = 3.2; // 3.2 hours 7. travelDistance = carSpeed * travelTime; 8. cout << "Distance traveled in " << travelTime << " hours is: " << travelDistance << endl; 9. return 0; 10 } Trace

1. ______2. ______3. ______4. ______5. ______6. ______7. ______8. ______9. ______10. ______

B. (2pts) OUTPUT of the program ______

19. Write an interactive program in C++ to calculate the area of a circle. The area of a circle is equal to (radius)2 , where  = 3.14159. While writing the program declare the  as a constant and give the name as PI instead of . First Analyze the problem and then write only active statements (code only).

(6pts) Analysis:

Inputs:

Process:

Outputs: ______(10pts) Program:

// Preprocessor Directives int main() { // Declaration Statements (if any)

// Input Statements (if any)

// Process Statements, if any

// Output Statements

// Termination of the program and return to OS

}

20. (5pts) Place a check in the appropriate column.

IDENTIFIER VALID NOT VALID

a. _2001File ______b. $first ______c. game@md ______

d. tax-rate ______

e. tax_rate ______

21. (5pts) Write the number of its data type beside the appropriate constant.

a. -2135 1. float b. -35.23 2. char c. '1' 3. string d. "35" 4. int ______e. "*" 5. bool 6. void

Following are some Sample Questions from CHAPTER 5. You may expect similar questions.

1. What is the output of the following C++ Code?

x = 10; if ( x > 10 ) { cout << “Line 1” << endl; cout << “Line 2” << endl; } else { cout << “Line 3” << endl; cout << “Line 4” << endl; }

OUTPUT:

2. What is the output of the following C++ Code?

p = -1; if ( p <= 0 ) cout << “Hi” << endl; else cout << “Bye” << endl; cout << “Thank you” << endl;

OUTPUT:

3. What is the output of the following C++ Code for the input data 24 36 30 35 cin >> a >> b >> c >> d; if (a > b | | a > c && c != d ) m = a; else if (b > d) m = b; else m = c; cout << m << endl;

OUTPUT: 4. After execution of the following code, what will be the value of angle if the input value is 5?

cin >> angle; if (angle = = 5) angle = angle + 10; else if (angle > 2) angle = angle + 5;

Value of angle is ______

5. After execution of the following code, what will be the value of angle if the input value is 0?

cin >> angle; if (angle = = 5) angle = angle + 10; else if (angle > 2) angle = angle + 5;

Value of angle is ______

6. What is the output of the following C++ code fragment?

int1 = 3; cin >> int2; // Assume user types 8 if ((int1 <= 3) | | (int2 > 8)) int3 = int1 / int2; else int3 = int1 % int2; cout << int1 << int2 << int3;

OUTPUT:

8. What is the output of the following C++ code fragment?

int1 = 3; cin >> int2; // Assume user types 5 if ((int1 <= 3) && (int2 > 8)) int3 = int1 / int2; else int3 = int1 % int2; cout << int1 << int2 << int3;

OUTPUT:

Needs Chapter 3 knowledge 9. Examine the following function and write the output: int main( ) { float a, b; a = 1.3; b= 3.154; a = 2 * a; b = a + b; cout << fixed << setprecision(2); cout << setw(5) << a << setw(7) << b << endl; }

OUTPUT : | | | | | | | | | | | | | | | | | | |

Recommended publications