Computer Science 1428 Test 2 Squarecap

Ace your homework & exams now with Quizwiz!

Squarecap Unit 6

A function is: Answer:a named collection of statements. used to break programs into manageable units, or modules. A function __________ contains the statements that make up the function. Answer: definition A function is executed when it is: called What is output when the following program is executed? void function1 () { cout << "Function 1 "; } int main () { cout << "Main "; function1 (); } Answer: Main Function 1 A(n) ______ produces a value that is passed to a function, and a(n) ______ is a variable that receives the value passed to the function. Answer: argument, parameter Given the following function definition void f(int x) { cout << x*2 << endl; } which of the following statements are valid function calls to this function? Answer: f(1); and f(x); //where x is a variable of type int Given the following function prototype void myFunction(double,string); which of the following statements are valid function calls to this function? Answer: myFunction(3.14,"Hello"); What is this? void power(int,int); Answer: a function prototype What is the output of the following program? void sequence(int count, char ch) { for (int i=0; i < count; i++) cout << ch; } int main() { int x = 2; char y = '!' sequence(4,'+'); sequence(x,y); } Answer: ++++!! What is the output of the following program? void f(char ch) { cout << ch; } void pl(int count) { for (int i = 0; i < count; i++) f('+'); } int main() { int x = 2; pl(3); f('*'); pl(x); } Answer: +++*++ What is the output of the following program? void doSomething(int num) { num = num + 1; cout << num << " "; } int main () { int x = 3; cout << x << " "; doSomething(x); cout << x << endl; } Answer: 3 4 3 What is output by the following function when called with this function call: func1(10); ? void func1(int m) { cout << m << " "; if (m < 25) return; cout << m+1 << " "; } Answer: 10 Given the following program: int cube(int x) { return x * x * x; } int main() { int result; //here } Which statement below can replace the comment //here in main, to call the cube function, passing the value 4 to it, and assigning the function's return value to result? Answer: result=cube(4); iven the following function definition: double half (double a) { return a/2; } which of the following is not a valid statement in main? Assume main contains: double x; Answer: cout << half(x); double w = half(x+100); double t = (37 * half(x)) / 3000; double u = half(half(x)); Based on the code in main, what should go in the blank to make the prototype correct? _____ func (int); int main() { int x = 10; if (func(x)) cout << "You passed." << endl; } Answer: bool During the execution of a function, a reference parameter, such as int &t, Answer: is an alias to the argument What is output by the following code? void func1 (int x) { x=57; } int main { int m = 25; func1(m); cout << "m is " << m <<endl; } Answer: m is 25 What is output by the following code? void func1 (int &x) { // Note & x=57; } int main { int m = 25; func1(m); cout << "m is " << m <<endl; } Answer: m is 57 What is output by the following code? void calc (int a, int& b) { int c; c = b + 1; a = a * 3; b = c + a; } int main() { int x = 2, y = 5; calc(x, y); cout << x << " " << y << endl; } Answer: 2 12 A _______ is declared outside of all functions. Answer: global variable This type of variable is defined inside a function and is not accessible outside the function Answer: local variable Given the program below, which variables could have the same name (without causing a compiler error)? void f (int a, int& b) { int c; c = a + b; } int g (int d) { int e; e = d*2; return e; } int h (int i) { return 2*i; } int main() { ... } Answer: c and e

Squarecap Unit 5

What does the following C++ statement do, when executed? float x[10]; Answer: Creates a variable named x that can store 10 different float values. What will the following code display when executed? int numbers[5] = {99, 87, 66, 55, 101}; cout << numbers[2] << endl; Answer: 66 What is the subscript of the last element in the following array? int values[10]; Answer: 9 What will be output by the following code? int numbers[ ] = {99, 87, 66, 55, 101}; for (int i=1; i < 4; i++) cout << numbers[i] << " "; Answer: 87 66 55 What will fix the error in the following code, which copies the elements from arrayB to arrayA? int arrayA[3]; int arrayB[3] = {3,6,9}; arrayA = arrayB; Answer: arrayA=arrayB should be arrayA[0]=arrayB[0]; arrayA[1]=arrayB[1]; arrayA[2]=arrayB[2]; What is output by the following statements? int z[ ] = {11, 12, 13, 15, 14, 10}; int m = z[0]; for (int i=1; i<5; i++) { if (z[i] < m) m = z[i]; } cout << "It's " << m << endl; Answer: It's 11

Squarecap Unit 4

What is output when the following code is executed? int x = 0; while (x <= 5) { cout << x << "!"; x = x + 2; } Answer: 0!2!4! What is output when the following code is executed? int x = 7; while (x <= 5) { cout << "Repeat " ; x = x + 1; } cout << "Done!" << endl; Answer: Done! What is output when the following code is executed? int x = 7; do { cout << "Repeat " ; x = x + 1; } while (x <= 5); cout << "Done!" << endl; Answer: Repeat Done! What will be output when the following code is executed? for (int x=1; x <= 5; x++) { cout << "One "; } cout << "Done!" << endl; Answer: One One One One One Done! How many times will the following loop display "Hello"? for (int i=0; i<=20; i++) cout << "Hello!" << endl; Answer: 21 What goes in the blanks to make this for loop sum the values 1..n? int n, x = 0; cout << "Enter a positive number: "; cin >> n; for (int count = 1; __________; count++) _______________ cout << x << endl; Answer: count <= n and x = x+count; A special value that marks the end of a list of values is a---sentinel What two elements are needed to calculate the total of a series of numbers? Answer: A loop and a variable that accumulates the total The valid range of values for integer variable x is described by this relational expression: -10<=x && x<=10. Which of the following integer values could be used as a sentinel value when inputting a value for x in a loop? Answer: 50 What is wrong with the following loop that attempts to sum the numbers from 1 to n? int total; for (int i=1; i<=n; i++) total = total + i; cout << "The sum is " << total << e Answer: int total; should be int total = 0; What does the following code do when executed? for (int i=1; i<=10; i++) for (int j=1; j<=4; j++) cout << "*"; Answer: Outputs 40 stars all on one line What code should go in the blank in order to store a count of the numbers in the file in the count variable? int number, count=0; ifstream fin; fin.open("numbers.txt"); while (__________________) { count++; } Answer: fin>>numbered


Related study sets

Chapter 12: Skin, Hair, and Nails Jarvis: Physical Examination & Health Assessment, 7th Edition

View Set

Chapter 8: Business Organization

View Set

ATI: Maternal Newborn Practice Assessment

View Set

Foreign Corrupt Policies Act (FCPA)

View Set

Intro to Criminal Justice: Chapter 4

View Set

Psychiatric Nursing Midterm Practice Questions

View Set