CS 1428 Final Exam (old)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Convert the following pseudocode to C++ code. Be sure to declare the appropriate variables: -Store 172.5 in the force variable. -Store 27.5 in the area variable. -Divide area by force and store the result in the pressure variable. -Display the contents of the pressure variable.

#include <iostream> using namespace std; int main() { double force = 172.5; double area = 27.5; double pressure = area / force; cout << pressure << endl; return 0; }

Which expression best determines if double variable x is equal to double variable y? (a) x == y (b) fabs(x - y) < 0.0001 (c) x - y < 0.0001 (d) all of the above will work equally well

(b) fabs(x - y) < 0.0001

Assume the variables have been declared and initialized: int j = 4; int k = 8; What is 19 / k ? (a) 2 r 3 (b) 2.375 (c) 2 (d) 3

(c) 2

What are the two steps that can be used for effective debugging? (a) Google the problem, follow the instructions (b) Change an existing statement, see if it worked (c) Predict the cause, modify the code to test the prediction (d) Read the compiler error message, change the code accordingly

(c) Predict the cause, modify the code to test the prediction

What will be output when the code below is executed? // int size = 15; int x = size * 10; cout << x << endl; (a) 15 (b) 150 (c) 10 (d) the code will not compile due to a syntax error

(d) the code will not compile due to a syntax error

What is output by the following program? int fun(int &x, int y) { x = 3; y = 4; return 5; x++; } int main() { int a = 1, b = 2, c = 3; c = fun(a, b); cout << a << " " << b << " " << c; } (a) 1 2 3 (b) 3 4 3 (c) 3 4 5 (d) 3 4 6 term-6 (e) 3 2 5

(e) 3 2 5

int i, j = 6, k = 2; //given this 1. 28 / 4 − k 2. j + 12 * k − 8 3. j + 17 % 3 − k 4. k + 22 * (9 − 7) 5. 12 / (10 − j) 6. (19 − 3) * (k + k) / 4 7. i = 38.9; //what is stored in i? 8. k > 0 && false (a) true (b) false (c) unknown (d) error 9. k > 0 || k < 10 (a) true (b) false (c) unknown (d) error 10. k < 0 || k > 10 (a) true (b) false (c) unknown (d) error

1. 5 2. 22 3. 6 4. 46 5. 3 6. 16 7. 8. false 9. true 10. false

If a variable named x is defined in function main: 1. You cannot have a variable named x in another function. (a) true (b) false 2. You cannot declare another variable named x inside main (unless it is inside a nested block). (a) true (b) false 3. You cannot declare a parameter named x in another function. (a) true (b) false 4. You cannot declare a variable named x that is global to all functions. (a) true (b) false

1. false 2. true 3. false 4. true

Assume the variables have been declared and initialized: int j = 4; int k = 8; What is 22 % j ? (a) 2 (b) 20 (c) 5.5 (d) 5

(a) 2

What is the output of code? int a, b; a = 18; b = 3; a = b; cout << a << " " << b; cout << endl; (a) 3 3 (b) 18 3 (c) 18 18 (d) nothing, there is an error

(a) 3 3

What would output to the console if the user enters 85? int grade; cin >> grade; if (grade > 70) { if (grade < 90) cout << "A message! \n"; else cout << "A different message! \n"; } else cout << "Yet another message! \n"; (a) A message! (b) A different message! (c) Yet another message! (d) None of the above

(a) A message!

What is the output by the following code? void func1(int x) { x = 57; } int main { int m = 25; func1(m); cout << "m is " <<< m << endl; } (a) m is 25 (b) m is 57 (c) m is 58 (d) nothing is output

(a) m is 25

What is this? void power(int,int); (a) a function definition (b) a function prototype (c) function call (d) all of the above

(b) a function prototype

Given the following variable declarations, which of the assignment statements are valid? char ch; (a) ch = *; (b) ch = 'F'; (c) ch = "Y"; (d) all of the above (e) both (a) and (b)

(b) ch = 'F';

What is the error in the following program? #include <iostream> using namespace std; void showEmployees(Employee emps[]); //prototype, definition int main() { not shown struct Employee { string name; // Employee name int empNum; // Employee number double payRate; // Hourly pay rate }; Employee employees[3]; //not shown: code to input data from user showEmployees(employees); } (a) the size of the array is missing from the prototype (b) showEmployees(employees) should be showEmployees(employees[3]) (c) struct Employee{...}; must come before the prototype (d) struct Employee{...}; AND Employee employees[3]; must come before the prototype (e) nothing is wrong

(c) struct Employee{...}; must come before the prototype

Which of the following relational expressions in C++ will be true when an integer variable x contains 1 or 2 or 3 or 4 or 5 and false otherwise? (a) x == 1 II 2 II 3 II 4 II 5 (b) x < 1 II x > 5 (c) x >= 1 && x <= 5 (d) both (a) and (b) are correct

(c) x >= 1 && x <= 5

What is the difference between x++ and x + 1 ? (a) There is no difference, they do exactly the same thing (b) x + 1 changes the value stored in x (c) x++ changes the value stored in x

(c) x++ changes the value stored in x

What is the output by the following? for (int outer = 1; outer <= 5; outer++) { for (int inner = 1; inner <= 2; inner++) { cout << "*"; } cout << endl; } (a) ********** (b) ***** ***** (c) * * * * * * * * * * (d) ** ** ** ** ** (e) something else

(d) ** ** ** ** **

What is output by the following program? const int SIZE = 5; void sky(int a[ ]) { a[1] = 25; a[SIZE-1] = 66; } int main() { int nums[SIZE] = {1,2,3,4,5}; sky(nums); for (int i=0; i<SIZE; i++) { cout << nums[i] << " "; cout << endl; } } (a) 1 2 3 4 5 (b) 25 2 3 66 5 (c) 1 25 3 66 5 (d) 1 25 3 4 66 (e) 25 2 3 4 66

(d) 1 25 3 4 66

What is output by the following program? const int SIZE = 5; void sky(int a[ ]) { a[1] = 8; a[SIZE-1] = 9; } int main() { int nums[SIZE] = {1,2,3,4,5}; sky(nums); for (int i=0; i<SIZE; i++) cout << nums[i] << " "; cout << endl; } (a) 1 2 3 4 5 (b) 8 2 3 9 5 (c) 1 8 3 9 5 (d) 1 8 3 4 9 (e) 8 2 3 4 9

(d) 1 8 3 4 9

Using the following chart, write a nested if/else statement that assigns .10, .15, or .20 to commission, depending on the value in sales. Try not to use any redundant boolean expressions in your if/else statement. Sales Commission Rate Under $10,000 10% $10,000 to $15,000 15% Over $15,000 20%

if(sales < 10000) { commission = 0.10; } else if(sales >= 10000 && sales <= 15000) { commission = 0.15; } else { commission = 0.20; }

This function that should calculate and return the average of three integers. Fix the errors in the function definition. double average(int value1, int value2) { average = value1 + value2 + value3 / 3; }

should be: double average(int value1, int value2, int value3) { average = (value1 + value2 + value3) / (double)(3); }

In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n.

#include <iostream> using namespace std; int Nums(int arr[], const int SIZE, int number) { arr[SIZE]; cout << "Please enter a series of numbers: " << endl; for (int i = 0; i < SIZE; i++) { cin >> arr[i]; } cout << "The numbers greater than " << number << " are: "; for (int i = 0; i < SIZE; i++) { if(arr[i] > number) { cout << arr[i] << " "; } } } int main() { const int SIZE=3; int Array[SIZE]; int number; cout << "Please enter a number: " << endl; cin >> number; Nums(Array, SIZE, number); }

Write a a program that asks the user to enter a golfer's score for three games of golf, and then displays the average of the three scores.

#include <iostream> using namespace std; int main() { int score1; int score2; int score3; cout << "Enter the 3 scores: " << endl; cin >> score1; cin >> score2; cin >> score3; double average = (score1 + score2 + score3) / (double)(3); cout << "Average: " << average << endl; return 0; }

The arrays numberArray1 and numberArray2 have 100 elements. Write code that copies the values in numberArray1 to numberArray2.

#include <iostream> using namespace std; int main() { const int SIZE = 100; int Array1[SIZE]; int Array2[SIZE]; cout << "Please enter 10 numbers: " << endl; for (int i = 0; i < SIZE; i++) { cin >> Array1[i]; Array2[i] = Array1[i]; } cout << endl; cout << "The numbers for the second array are: " << endl; for (int i = 0; i < SIZE; i++) { cout << Array2[i] << " "; } }

Write a program that lets the user enter ten values into an array. The program should then display the largest and smallest values stored in the array.

#include <iostream> using namespace std; int main() { const int SIZE = 10; int Array[SIZE]; int max = -9999999999; int min = 9999999999; cout << "Please enter 10 numbers: " << endl; for (int i = 0; i < SIZE; i++) { cin >> Array[i]; if (Array[i] > max) { max = Array[i]; } if (Array[i] < min) { min = Array[i]; } } cout << "The maximum value is: " << max << endl; cout << "The minimum value is: " << min << endl; }

Write a code segment that asks the user to "enter a series of positive numbers, then enter a -1 when finished." Your code should compute and output the sum of the numbers (do not include -1 in the sum).

#include <iostream> using namespace std; int main() { int nums; int count = 0; cout << "Please enter a series of positive numbers and"; "then -1 when you are done: " << endl; while(nums > -1) { cin >> nums; count += nums; } cout << "The sum of these numbers are: " << count + 1 << endl; }

Write a function named getNumber that uses a reference parameter variable to accept an integer argument. The function should prompt the user to enter a number in the range of 1 through 100. The input should be validated and stored in the parameter variable.

#include <iostream> using namespace std; void getNumber(int parameter) { int number; bool statement = false; while (statement == false) { cout << "Please enter an integer from 1 to 100: " << endl; cin >> number; { if (number > 1 && number < 100) { parameter = number; cout << "the parameter is " << parameter <<endl; statement = true; } else { cout << "Sorry, the number you entered is invalid "; cout << "Please try again."<< endl; cout << endl; } } } } int main() { int parameter; getNumber(parameter); }

The structure Town is declared as follows: struct Town { string townName; string countyName; int population; Int elevation; }; (a) Define an array of 10 of the Town structure variables. Initialize the first three elements with the following data: Town County Population Elevation San Marcos Hays 64,776 617 Austin Travis 978,908 1,330 Fort Stockton Pecos 8,421 2,972 (b) Write a function that will take the array you defined in Question A as a parameter, and display the contents of each element.

#include <iostream> using namespace std; struct Town { string townName; string countyName; int population; int elevation; }; void display(Town arr[]) { const int SIZE = 10; for(int i = 0; i < SIZE; i++) { cout << arr[i].townName << " "; cout << arr[i].countyName << " "; cout << arr[i].population << " "; cout << arr[i].elevation << endl; } } int main() { const int SIZE = 10; Town TownStructs[SIZE] = { {"San Marcos", "Hays", 64, 776617}, {"Austin", "Travis", 978908, 1330}, {"Fort Stock", "tonPecos", 8421, 2972} }; display(TownStructs); }

What would the value of x be if the user enters A? int x = 0; char ch; cin >> ch; switch(ch) { case 'A': x = 10; break; case 'B': x = 20; default: x = 30; } (a) 10 (b) 20 (c) 30 (d) 60

(a) 10

Which of the following best describes the kind of values that can be stored in the string datatype? (a) A sequence of characters (b) A sequence of numbers (c) A single character only (d) Numbers in scientific notation

(a) A sequence of characters

If your program executes and outputs an incorrect result (say when converting Fahrenheit temperature to Celsius), what kind of error is it? (a) Logic error (b) Syntax error (c) Mathematical error (d) Compile-time error

(a) Logic error

Assume a string variable has been declared a following: string fullName; Which statement should be used to read in someone's full name with spaces? (a) get line(cin, fullName); (b) cin >> fullName; (c) cin.get(fullName); (d) both (a) and (b) will work

(a) get line(cin, fullName);

Given the following function prototype: void myFunction(double, string); which of the following statements are valid function calls to this function? (a) myFunction(3.14, "Hello); (b) myFunction("Hello", 3.14); (c) myFunction(); (d) all of the above (e) A and B

(a) myFunction(3.14, "Hello);

Indicate if the boolean expression evaluates to true or false. Assume the variable have been declared and initialized. int x = 6; int y = 4; int z = 2; x == z - 4 II y >= 4 (a) true (b) false

(a) true

What is the 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; } (a) 2 5 (b) 2 12 (c) 6 5 (d) 6 12

(b) 2 12

Which of the following is NOT a valid variable name in C++? (a) xyz3 (b) 42skidoo (c) my_var (d) SkyForce (e) all of the above are valid

(b) 42skidoo

______________ runs the computer's program, reading and executing the instructions. (a) Main memory (RAM) (b) The processor (CPU) (c) Secondary storage (disk) (d) An input device

(b) The processor (CPU)

Which of the following statements outputs the value of the gpa member of the second structure of the student array? (a) cout << student.gpa[1]; (b) cout << student[1].gpa; (c) cout << student.gpa; (d) cout << gpa.student[1];

(b) cout << student[1].gpa;

Indicate if the boolean expression evaluates to true or false. Assume the variable have been declared and initialized. int x = 6; int y = 4; int z = 2; z != 2 && y > 6 (a) true (b) false

(b) false

Which while loop below is equivalent to the for loop below? for (int j = 1; j < 500; j++) cout << j << endl; (a) int j = 1; while (j < 500) cout << j << endl; (b) int j = 1; while (j < 500) { cout << j << endl; j++; } (c) while (int j = 1; j < 500; j++) cout << j << endl; (d) int j = 1; while (j < 500) { j++; cout << j << endl; }

(b) int j = 1; while (j < 500) { cout << j << endl; j++; }

What is wrong with the following 5 line code segment? if (i < 100) { int x = 25; i++; } cout << x << endl; (a) line 2, you cannot declare x inside an if statement (b) line 5, you cannot access x outside the if statement (c) nothing is wrong, it will output 25 or garbage

(b) line 5, you cannot access x outside the if statement

Which of the following is an example of a variable declaration statement in C++? (a) y = 3400; (b) long long x; (c) cout << z; (d) all of the above are variable declarations (e) both (a) and (b) are variable declarations

(b) long long x;

What is the output by the following code? void func1(int &x) { //Note & x = 57; } int main { int m = 25; func1(m); cout << "m is " << m << endl; } (a) m is 25 (b) m is 57 (c) m is 58 (d) nothing is output

(b) m is 57

In the following expression, which operation will the computer perform first? x = m + n / p * q; (a) m + n (b) n / p (c) p * q (d) x = n

(b) n / p

Given the following program: int cube(int x){ return x * x * x; } int main() { int result; //here cout << "result: " << result << endl; } Which statement below can replace the comment //here in main, to call the cube function, passing the value 4 to it, and assign the value that is produced by the function by function to result? (a) cube(result); (b) result = cube(4); (c) cube(4); (d) cube(4) = result; (e) result = 4 * 4 * 4;

(b) result = cube(4);

I want the computer to store the result of dividing a by b in variable m, and I want to preserve the fractional part (if a is 5 and b is 2, m should get 2.5, not 2 or 2.0). What can go in the blank/ int a, b; cout << "Enter two whole numbers: "; cin >> a >> b; double m = ________________________; (a) static_cast<double> (a / b) (b) static_cast<double> (a) / b (c) a/ b (d) any of the above

(b) static_cast<double> (a) / b

Which part of the following statement should replaced by a named constant (also known as a constant variable)? Assume total and subtotal are variables of type double. total = subtotal + (subtotal * 0.875); (a) subtotal (b) subtotal * 0.875 (c) 0.875 (d) total

(c) 0.875

What value would be stored in y after it is executed? int x = 2; float f = 10.8; int y = f * 2; (a) 10.8 (b) 21.6 (c) 21 (d) none, an error would occur

(c) 21

What is the value of the variable x after executing the following statement? float x = 13/4; (a) 3.25 (b) 3.3 (c) 3.0 (d) 1.75

(c) 3.0

What would the value of x be if the user enters B? int x = 0; char ch; cin >> ch; switch(ch) { case 'A': x = 10; break; case 'B': x = 20; default: x = 30; } (a) 10 (b) 20 (c) 30 (d) 50

(c) 30

Given the following declarations, which code below is valid and will copy the name and age from b into a? struct Person { string name; int age; }; Person a; Person b {"George", 10}; (a) a=b; (b) a.age = b.age; a.name = b.name; (c) both of the above (d) none of the above

(c) both of the above

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() { ... } (a) a and b (b) g and h (c) c and e (d) all of the above (e) none of the above

(c) c and e

Is there an error in the following function? If so, how can it be fixed? void cycle(int b){ if(b == 99) return 0; else return b + 1; (a) there is no error (b) remove "int b" and add "int b;" as the first line inside the {...} (c) change "void" to "int" (d) change "b + 1" to "b" (e) add "return 0;" as the last line inside the {...}

(c) change "void" to "int"

the following statement defines a new ______________ called "Point" struct Point { double x; double y; }; (a) variable (b) function (c) data type (d) constant

(c) data type

Which of the following loop's body will always be executed at least once, even if the test expression is false to start with? (a) while loop (b) for loop (c) do-while loop (d) all of them

(c) do-while loop

There are approximately 7x10^27 atoms in the average human body(that's a number with 27 digits). Which datatype could represent that number? (a) int (b) long long (c) float (d) string

(c) float

After execution of the code, what will be the value of input_value if the value 10 is entered at the keyboard at run time? cin >> input_value; if (input_value < 2) input_value = input_value + 1; else if (input_value < 5) input_value = input_value + 2; else input_value = input_value + 3; (a) 10 (b) 11 (c) 12 (d) 13 (e) 16

(d) 13

How many total elements are contained in the following array? int table[5][6]; (a) 5 (b) 6 (c) 11 (d) 30 (e) error: arrays can only have on size declarator

(d) 30

What is the output of the code? int x = 5; while (x > x - 1) { cout << x << " "; x--; } (a) 5 4 3 2 1 (b) 5 4 3 2 (c) 5 5 5 5 5 ... (d) 5 4 3 2 1 0 -1 ... (e) nothing

(d) 5 4 3 2 1 0 -1 ...

After execution of the code, what will be the value of input_value if the value 4 is entered at the keyboard at run time? cin >> input_value; if (input_value < 2) input_value = input_value + 1; else if (input_value < 5) input_value = input_value + 2; else input_value = input_value + 3; (a) 10 (b) 4 (c) 5 (d) 6 (e) 7

(d) 6

A(n) ____________________ is used to translate each high level language instruction into the appropriate machine language instruction. (a) Clock (b) Assembler (c) Processor (d) Compiler

(d) Compiler

What is the purpose of having style guidelines? (a) To produce code that does not contain syntax errors (b) To produce code that does not contain logic errors (c) To produce code that is more efficient (d) To produce code that is easy for humans to read and maintain

(d) To produce code that is easy for humans to read and maintain

A sequence of instructions that solves a problem is: (a) computational thinking (b) a process (c) an application (d) an algorithm

(d) an algorithm

fill in the blank to make this program compile: #include <iostream> using namespace std; struct Sample { int a; double b; }; _______ myFunc() { Sample s; cin >> s.a; cin >> s.b; return s; } int main() { Sample sam; sam = myFunc(); cout << sam.a << endl; } (a) int (b) void (c) double (d) sample

(d) sample

When a loop processes data until it reaches a special value that marks the end of a list of values it is called a (a) count-controlled loop (b) interactive loop (c) nested loop (d) sentinel-controlled loop

(d) sentinel-controlled loop

What is output by the following program? int fun(int &x, int y) { x = 3; y = 4; return 5; x++; } int main() { int a = 1, b = 2, c = 3; c = fun(a, b); cout << a << " " << b << " " << c; } (a) 1 2 3 (b) 3 4 3 (c) 3 4 5 (d) 3 4 6 (e) 3 2 5

(e) 3 2 5

Given 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; (a) cout << half(x); (b) double w = half(x + 100); (c) double t = (37 * half(x)) / 3000; (d) double u = half(half(x)); (e) all of the above are valid

(e) all of the above are valid

You have the following function prototype in your program: void factorial(int); given: int x;int factor; in main, indicate if the following function calls in main are valid or not. 1. factorial(17); (a) valid (b) not valid 2. factorial(x); (a) valid (b) not valid 3. factorial(factor-17); (a) valid (b) not valid 4. x = factorial(100); (a) valid (b) not valid

1. valid 2. not valid 3. not valid 4. valid

The following statement calls a function named half. The half function returns a value that is half that of the argument. Write the function. result = half(number);

double half(int num) { double result = num / 2; return result; }

Write a for loop that displays the following set of numbers: 0, 10, 20, 30, 40, 50 . . . 1000

for(int i = 0; i <= 1000; i = i + 10) { cout << i << ", "; }


Ensembles d'études connexes

Anatomy L54: The Last Four Cranial Nerves

View Set

Chapter 11 Real Estate and Other Investments

View Set

Pharm Final Exam/ Chapters 63-64

View Set

QDC CHAPTER 9: MANAGEMENT OF QUALITY

View Set