Download test 2 - study guide

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Law of large numbers wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
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
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: <data type> <variable name>;
declare a character variable nameInitial:
declare an integer variable number:
declare an floating point variable number:
declare an floating point variable number:
float)
declare an string variable name:
char nameInitial;
int number;
float number;
double number; (double the size than
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 <data type> <constant name> = constant literal (or value);
declare a character constant nameInitial:
declare an integer constant number:
declare an floating point constant number:
declare an floating point constant number:
declare a string constant name:
const char NAME_INITIAL = ‘J’;
const int NUMBER = 2345;
const float NUMBER = 23.45;
const double NUMBER = -2345.9 ;
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' ;
const string name = "Joe"; ==>
const int number = -25
const float number = 256.78 ==>
==> 'K' is a character constant literal
"Joe" is a string constant literal
==> -25 is an integer constant literal
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)
D)
+ and +, -, and %
B)
* and /
E) +, -, and *
C)
*, /, 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
E) nothing; a compile-time error occurs
D)
5.0
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)
E) nothing; a compile-time error occurs
87
D)
87.0
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)
compile-time error occurs
13
C)
12
D)
12.0
E) nothing; a
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 = ---------5w - 7x
Answer: ________________________________________
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.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
#include<iostream.h>
using namespace std;
int main()
{
float length, width,
int perimeter;
length = 20.0;
Width = length - 5.0;
perimeter = 2(length + width);
cout << "The perimeter of the rectangle is: " << Perimeter << endl;
return 0;
}
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<iostream.h>
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<iostream.h>
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.
21.
(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
______
______
______
______
______
________
________
________
________
________
(5pts) Write the number of its data type beside the appropriate constant.
a. -2135
b. -35.23
c. '1'
d. "35"
______ e. "*"
1.
2.
3.
4.
5.
6.
float
char
string
int
bool
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
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;
36
30 35
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;
if ((int1 <= 3) |
int3 = int1 /
else
int3 = int1 %
cout << int1
// Assume user types 8
| (int2 > 8))
int2;
int2;
<< 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: