CMSC 140 FINAL REVIEW PART 2 UPDATED QUESTIONS
AND CORRECT ANSWERS
Question:
1. Global Variables
Answer:
Variables defined outside of any function, and can be ac-cessed and changed in any function
Question:
2. Local Variables
Answer:
Variables defined within a function, and can only be changed and accessed with-in that function
(unless passed as a parameter)
Question:
3. What is the difference between global and local vari-ables?
Answer:
Global variables can be ac-cessed across the whole pro-gram and local variables can only be accessed
within the function that created it
Question:
4. Write the function header for a function called over-timePay, that returns a double and has a
parameter list that takes a string called name, an int named hours, and a double named payrate. Then,
write the function prototype for this function in the space above the main function.
Answer:
double overtimePay(string name, int hours, double payrate)
Question:
5. What is a function signature?
Answer:
The name of the function and the data types of the func-tion's parameters in proper order.
Question:
6. What does it mean to overload a function?
Answer:
when another function is created with the same name as the first function but with a different
parameter list (thusly, a different function signature).
Question:
7. Given the function definition below (also found after the main function), call the function and pass
the variables 'firstVal' and 'secondVal' into the func-tion, respectively. What will be displayed on the
screen after the execu-tion of the code? What will the variables 'firstVal' and 'secondVal' contain?
void doSomething(int a, double &b) { for (int i = 0; i < a; i++) { cout << ".";
b += 0.01;
} cout << endl;
a += 5;
cout << "A variable a is: " << a << endl;
AND CORRECT ANSWERS
Question:
1. Global Variables
Answer:
Variables defined outside of any function, and can be ac-cessed and changed in any function
Question:
2. Local Variables
Answer:
Variables defined within a function, and can only be changed and accessed with-in that function
(unless passed as a parameter)
Question:
3. What is the difference between global and local vari-ables?
Answer:
Global variables can be ac-cessed across the whole pro-gram and local variables can only be accessed
within the function that created it
Question:
4. Write the function header for a function called over-timePay, that returns a double and has a
parameter list that takes a string called name, an int named hours, and a double named payrate. Then,
write the function prototype for this function in the space above the main function.
Answer:
double overtimePay(string name, int hours, double payrate)
Question:
5. What is a function signature?
Answer:
The name of the function and the data types of the func-tion's parameters in proper order.
Question:
6. What does it mean to overload a function?
Answer:
when another function is created with the same name as the first function but with a different
parameter list (thusly, a different function signature).
Question:
7. Given the function definition below (also found after the main function), call the function and pass
the variables 'firstVal' and 'secondVal' into the func-tion, respectively. What will be displayed on the
screen after the execu-tion of the code? What will the variables 'firstVal' and 'secondVal' contain?
void doSomething(int a, double &b) { for (int i = 0; i < a; i++) { cout << ".";
b += 0.01;
} cout << endl;
a += 5;
cout << "A variable a is: " << a << endl;