Computer Programming 1500 Final

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which expression fails to compute the area of a triangle having a base b and height h (area is one-half base time height)? (1.0 / 2.0) * b * h (1 / 2)* b * h (b * h) / 2.0 0.5 * b * h

(1 / 2)* b * h

Which expression evaluates to 3.5? int x = 7; int y = 1; x * (1 / 2) (x / 2.0) x.0 / 2 (x / 2) * 1.0

(x / 2.0)

What possible values can x % 10 evaluate to? (x is an integer) 0..9 1..10 0..10 1..11

0..9

What is the ending value of y? int x; int y; x = 2; y = ((x + 1) / 5) + (x / 2) ;

1

Which is a correct scientific notation for the floating-point literal: 0.00001 0.1e-6 0.1e-5 1.0e-5 1.0e-6

1.0e-5

How many elements does the vector declaration create? vector<int> scores(10); // Vector declaration scores.at(0) = 25; scores.at(1) = 22; scores.at(2) = 18; scores.at(3) = 28; a. 0 b. 4 c. 9 d. 10

10

What is y after executing the statements? x = 4; y = x + 1; x = 3; y = y * 2;

10

Which is not a valid identifier? firstName first_Name 1stName name1

1stName

What is the ending value of x? x = 160 / 20 / 4;

2

What is the ending value of cost? int numApples = 3; double cost; cost = 1.2 * numApples;

3.6

What is the ending value of y? sqrt(u) returns the square root of u. pow(u, v) returns u raised to the power of v. x = 9.0; y = 4.0; y = pow(sqrt(x), sqrt(y));

9.0

For which quantity is a double the best type?

Height of a building

What is read into string myString for input "One-hit wonder". One One- One-hit One-hit wonder

One-hit

Which is best declared as a constant?

The number of feet in a yard

A __________ is a computer component that stores files and other data. a. disk b. monitor c. keyboard d. processor

a

A loop should sum all inputs, stopping when the input is 0. If the input is 2 4 6 0, sum should end with 12. What should XXX, YYY, and ZZZ be? Choices are in the form XXX / YYY / ZZZ. int sum; int currVal; XXX; cin >> currVal; while (YYY) { ZZZ; cin >> currVal; } a. sum = 0 / currVal != 0 / sum = sum + currVal b. sum = currVal / currVal == 0 / sum = currVal c. sum = 1 / currVal != 0 / sum = sum + 1 d. cin >> sum / currVal == 0 / cin >> sum

a

Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items in parentheses are evaluated first. (x == 5) || (y == 2) && (z == 5) a. false OR (true AND false) --> false OR false --> false b. false OR (true AND false) --> false OR true --> true c. (false OR true) AND false --> true AND false --> false d. (false OR true) AND false --> true AND false --> true

a

If a program compiles without errors, the program is free from__________. a. syntax errors b. logic errors c. runtime errors d. semantic errors

a

If the input is -5, what is the output? If x less than 0 Put "Low " to output Else if x greater than 0 Put "OK " to output Else if x less than -3 Put "Very low " to output a. Low b. Low Very low c. (Runs but no output) d. error

a

What are the ending values in itemCount? vector<int> itemCount; itemCount.push_back(6); itemCount.push_back(7); itemCount.push_back(8); itemCount.pop_back(); a. 6, 7 b. 6, 7, 8 c. 7, 8 d. 6, 7, 0

a

What does this code output? cout << "I "; cout << "want pie." << endl; a. I want pie. b. "I " "want pie." c. I want pie d. I want pie e. I wantpie

a

What value of outputs "Junior"? if (x < 56) { // Output "Sophomore" } else if (x > 56) { // Output "Senior" } else { // Output "Junior" } a. Value 56 b. Values 57 or larger c. Values 55 or 57 d. No such value

a

Which XXX / YYY declare a vector having MAX_SIZE elements and initializes all elements with -1? const int MAX_SIZE = 4; vecotr<int> myNumbers(XXX); int i; for (i = 0; i < YYY; ++i) { myNumbers.at(i) = -1; } a. MAX_SIZE / MAX_SIZE b. MAX_SIZE / MAX_SIZE - 1 c. MAX_SIZE - 1 / MAX_SIZE d. MAX_SIZE - 1 / MAX_SIZE - 1

a

Which XXX and YYY will loop as long as the input is an integer less than 100? Choices are in the form XXX / YYY. cin >> w; while (XXX) { // Do something YYY; } a. w < 100 / cin >> w b. w >= 100 / (nothing) c. w < 100 / (nothing) d. w >= 100 / cin >> w

a

Which input value causes "Goodbye" to be output next? int x; cin >> x; while (x >= 0) { // Do something cin >> x; } cout << "Goodbye"; a. -1 b. 0 c. 1 d. no such value

a

Which item converts a high-level language program to low-level machine instructions? a. Compiler b. Machine instruction c. Assembly language d. Memory

a

Given only int variables a and b, which is a valid expression? 4a + 3 a + ab a + -2 2 x 3

a + -2

What does the compiler do upon reaching this variable declaration? int x;

allocates a memory location for x

A loop should output 1 to n. If n is 5, the output is 12345. What should XXX and YYY be? Choices are in the form XXX / YYY. cin >> n; for (XXX; i++) { cout << YYY; } a. i = 0; i < n / i b. i = 0; i < n / i + 1 c. i = 1; i < n / i d. i = 1; i < n / i + 1

b

A restaurant gives a discount for children under 10. They also give the discount for adults over 55. Which expression evaluates to true if a discount should be given? a. (age < 10) && (age > 55) b. (age < 10) || (age > 55) c. (age >= 10) && (age <= 55) d. (age >= 10) || (age <= 55)

b

C++ is _________. a. derived from Fortran b. derived from C c. derived from Python d. derived independently of any other language

b

Choose the statement that generates this output: I wish you were here a. cout << "I wish" + "you were here"; b. cout << "I wish" << endl; cout << "you were here"; c. cout << "I wish"; cout << "you were here" << endl; d. cout << "I wish" << ln << "you were here";

b

Given a list of syntax errors from a compiler, a programmer should focus attention on which error(s) before recompiling? a. All the errors b. The first error only c. The middle error only d. The last error only

b

Given myVector initially has 5 elements with values 3, 4, 6, 8, and 9. What are the values in myVector after the following? myVector.resize(2); a. 3 b. 3, 4 c. 3, 4, 6 d. 3, 4, 6, 8, 9, 0, 0

b

How many function calls exist in the following code? int Calc1 (int a, int b) { return a + b / 2; } int Calc2 (int a, int b) { return a * b / 100; } int main () { int x; int y; x = Calc1 (5,3); cout << x; y = Calc2 (5,3); cout << y; cout << Calc2 (5,3); } a. 2 b. 3 c. 4 d. 5

b

How many times does the while loop execute for the given input values of -1 4 0 9? userNum = 3; while (userNum > 0) { // Do something // Get userNum from input } a. 0 b. 1 c. 2 d. 3

b

How many times will the loop iterate, if the input is 105 107 99 103? cin >> x; while (x > 100) { // Do something cin >> x; } a. 1 b. 2 c. 3 d. 4

b

If the input is 12, what is the final value for numItems? int x; int numItems = 0; cin >> x; if (x <= 12) { numItems = 100; } else { numItems = 200; } numItems = numItems + 1; a. 100 b. 101 c. 200 d. 201

b

The numNegatives variable counts the number of negative values in the vector userVals. What should numNegatives be initialized to? vector<int> userVals(20); unsigned int i; numNegatives = XXX; for (i = 0; i < userValues.size(); ++i) { if (userValues.at(i) < 0) { numNegatives = numNegatives + 1; } } a. no initialization needed b. 0 c. -1 d. userVals.at(0)

b

The program should output even values between -10 and 10 (inclusive), so -10 -8 ... 8 10. What should XXX be? for (XXX) { cout << i << " "; } a. i = -10; i < 10; i = i + 2 b. i = -10; i <= 10; i = i + 2 c. i = -10; (i * 2) < 10; ++i d. i = -10; (i * 2) <= 10, ++i

b

Three sock types A, B, C exist in a drawer. Pulled socks are kept and not returned. Which is true? a. If the first two pulls are different socks, only one more pull is needed for a match b. If the first three pulls are different socks, only one more pull is needed for a match c. If the first two pulls are different socks, at least two more pulls are needed for a match d. If the first three pulls are different socks, at least two more pulls are needed for a match

b

What is output? double MyFct(double a, double b) { return (a + b) / 2.0; } int main() { double x = 3.0; double y = 5.0; double z = 8.0; double t; t = MyFct(x, y); t = MyFct(t, z); cout << t << endl; return 0; } a. 4.0 b. 6.0 c. 8.0 d. 16.0

b

What is the output, if the input is 3 2 4 5? All variables are ints. cin >> num; for (i = 0; i < num; ++i) { cin >> curr; cout << curr; } a. 24 b. 245 c. 324 d. 3245

b

What is the output? int Calc(int num1, int num2) { return 1 + num1 + num2; } int main() { int x; x = Calc(4, 5); cout << Calc(x, 5); return 0; } a. 5 b. 10 c. 16 d. error

b

Which XXX causes every character in string inputWord to be output? for (XXX) { cout << inputWord.at(i) << endl; } a. i = 0; i < (inputWord.size() - 1); ++i b. i = 0; i < inputWord.size(); ++i c. i = 0; i < (inputWord.size() + 1); ++i d. i = 0; i <= inputWord.size(); ++i

b

Which XXX causes the program to output the message "Hello!"? void PrintMessage() { cout << "Hello!"; } int main() { XXX; return 0; } a. cout << PrintMessage() b. PrintMessage() c. void PrintMessage() d. PrintMessage("Hello!")

b

Which XXX is valid for the following code? int CalcSum(int a, int b) { return a + b; } int main() { int y; XXX return 0; } a. y = CalcSum(); b. y = CalcSum(4, 5); c. y = CalcSum(4 + 5); d. CalcSum(y, 4, 5);

b

Which assigns the vector's last element with 99? vector<int> myVector(15); a. myVector.at(0) = 99; b. myVector.at(14) = 99; c. myVector.at(15) = 99; d. myVector.at(16) = 99;.

b

Which expression evaluates to false if x is 0 and y is 10? a. (x == 0) && (y == 10) b. (x == 0) && (y == 20) c. (x == 0) || (y == 10) d. (x == 0) || (y == 20)

b

Which expressions for XXX, YYY, and ZZZ output "Great job" for scores above 90, and "Nice try" otherwise? Choices are in the form XXX / YYY / ZZZ. int score; cin >> score; if (XXX) { cout << YYY; } else { cout << ZZZ; } a. score < 91 / "Great job" / "Nice try" b. score < 91 / "Nice try" / "Great job" c. score > 90 / "Nice try" / "Great job" d. (Not possible for given code)

b

Which expressions for YYY and ZZZ will output "Young" for ages less than 20 and "Young Adolescent" for ages between 10 and 20? int u; cin >> u; if (YYY) { cout << "Young"; if (ZZZ) { cout << " Adolescent"; } } a. YYY: u < 20 ZZZ: u < 10 b. YYY: u < 20 ZZZ: u > 10 c. YYY: u > 20 ZZZ: u < 10 d. YYY: u > 20 ZZZ: u > 10

b

Which outputs "Negative" for values less than 0, otherwise outputs "Non-negative"? a. if (val > 0) { cout << "Non-negative"; } else { cout << "Negative"; } b. if (val >= 0) { cout << "Non-negative"; } else { cout << "Negative"; } c. if (val < 0) { cout << "Non-negative"; } else { cout << "Negative"; } d. if (val <= 0) { cout << "Negative"; } else { cout << "Non-negative"; }

b

A program should compute two times x. Which statement has a logic error? a. y = x + x; b. y = 2 * x; c. y = x * x; d. y = x * 2;

c

A sequence of instructions that solves a problem is called a(n) _________. a. instruction b. process c. algorithm d. variable

c

For the following function, which is a valid function call? Assume maxValue is an integer. int Max(int x, int y) { if (x > y){ return x; } else { return y; } } a. maxValue = Max(15 25); b. maxValue = Max(); c. maxValue = Max(15, Max(35, 25)); d. Max = maxValue(15, 25);

c

For what values of x does the default case execute in the code below? x is declared as an integer. switch (x) { case 2: ... break; case 3: ... break; case 4: ... break; default: ... // When does this execute? } a. only for value 5 b. only for all values greater than 4 c. only for values that are not 2, 3, or 4 d. for any value

c

Given integers i, j , k, which XXX correctly passes three integer arguments for the following function call? addInts XXX; a. (j, 6 + 7) b. (i + j + k) c. (10, j, k +5) d. (10 15 20)

c

Given two integer vectors origList = {2, 3, 4, 5} and offsetList = {6, 7, 8, 9}, which statement prints out the sum of the second element in the origList with the corresponding element in the offsetList? a. cout << origList + offsetList << endl; b. cout << origList.at(2) + offsetList.at(2) << endl; c. cout << origList.at(1) + offsetList.at(1) << endl; d. cout << origList.at(2) + offsetList.at(2) << endl;

c

Given two vectors, studentNames that maintains a list of students, and studentScores that has a list of the scores for each student, which XXX and YYY prints out only the student names whose score is above 80? Choices are in the form XXX/ YYY. vector<string> studentNames(NUM_STUDENTS); vector<int> studentScores(NUM_STUDENTS); unsigned int i; for (i = 0; i < studentScores.size(); ++i) { if (XXX > 80) { cout << YYY << " "; } } a. studentNames.at(i) / studentNames.at(i) b. studentNames.at(i) / studentScores.at(i) c. studentScores.at(i) / studentNames.at(i) d. studentScores.at(i) / studentScores.at(i)

c

If the input is 5, what is the output? int x; cin >> x; if (x < 10) { cout << "Live "; } else if (x < 20) { cout << "long "; } else if (x < 30) { cout << "and "; } cout << "prosper!"; a. Live b. Live long c. Live prosper! d. Error: The compiler will complain about a missing else statement

c

In an instruction like: z = x + y, the symbols x, y, and z are examples of ___________. a. output b. visibles c. variables d. instructions

c

In what component does a processor store the processor's required instructions and data? a. Mailbox b. Switch c. Memory d. Bit

c

In what year was the first book of C published? a. 1819 b. 1920 c. 1978 d. 2010

c

RAM is an abbreviation that stands for: a. Real Analog Memory b. Ranged Access Memory c. Random Access Memory d. Random Abbreviated Memory

c

What is Moore's law? a. A processor used in cold weather is more likely to fail b. Every action has an equal and opposite reaction c. The capacity of ICs doubles roughly every 18 months d. Any given program will become obsolete

c

What is the ending value of sum, if the input is 2 5 7 3? All variables are ints. cin >> x; sum = 0; for (i = 0; i < x; ++i) { cin >> currValue; sum += currValue; } a. 5 b. 10 c. 12 d. 15

c

What is the final value of y? int x = 77; int y = 4; if (x == 77) { y = y + 1; } if (x < 100) { y = y + 1; } if (x > 77) { y = y + 1; } y = y + 1; a. 5 b. 6 c. 7 d. 8

c

What is the output if count is 4? for (i = count; i > 0; --i) { // Output count } a. 4 b. 321 c. 4321 d. 43210

c

What is the output? int n; for (n = 0; n < 10; n = n + 3) { cout << n << " "; } a. 0 1 2 3 4 5 6 7 8 9 b. 1 4 7 10 c. 0 3 6 9 d. 0 3 6 9 12

c

What is the output? int x = 18; while (x > 0) { // Output x and a space x = x / 3; } a. 6 2 b. 6 2 0 c. 18 6 2 d. 18 6 2 0

c

Which XXX outputs all the elements of vector myVals? for (i = 0; XXX; ++i) { cout << myVals.at(i) << " "; } a. i < myVals.size; b. i <= myVals.size(); c. i < myVals.size(); d. No such expression as the number of elements is unknown.

c

Which YYY outputs the string in reverse? Ex: If the input is "Oh my", the output is "ym hO". int i; string str; getline(cin, str); for (YYY) { cout << str.at(i); } a. i = str.length(); i < 0; --i b. i = str.length(); i > 0; --i) c. i = str.length() - 1; i >= 0; --i) d. i = str.length() + 1; i > 0; --i)

c

Which assigns the vector's first element with 99? vector<int> myVector(4); a. myVector.at() = 99; b. myVector.at(-1) = 99; c. myVector.at(0) = 99; d. myVector.at(1) = 99;

c

Which best describes what is output? Assume v is a large vector of ints. int i; int s; s = v.at(0); for (i = 0; i < v.size(); ++i) { if (s > v.at(i)) { s = v.at(i); } } cout << s; a. first value in v b. max value in v c. min value in v d. last value in v

c

Which expressions for YYY and ZZZ correctly output the indicated ranges? Assume int x's value will be 0 or greater. Choices are in the form YYY / ZZZ. if (YYY) { // Output "0-29" } else if (ZZZ) { // Output "30-39" } else { // Output "40+" } a. x < 29 / x >= 29 b. x < 30 / x >= 30 c. x < 30 / x < 40 d. x > 29 / x > 40

c

Which for loop will iterate 100 times? a. for (i = 0; i < 99; i++) b. for (i = 1; i < 99; i++) c. for (i = 0; i < 100; i++) d. for (i = 1; i < 100; i++)

c

Which is an invalid access for the vector? vector<int> numsList(5); int x = 3; a. numsList.at(x-3) b. numsList.at(0) c. numsList.at(x+2) d. numsList.at((2*x) - x)

c

Which is true? a. C came after C++ b. C++ came after C+ c. C++ came after C d. C++ came after C+ but before C

c

Which text most closely resembles the form in which one instruction is stored in a single location of a typical computer's memory? a. w = (x + y) * (y - z) b. w = x + 000111 - (y * 1110101) c. Add 34, #9, 25 d. (Mul 45, 2) + (Mul 33, 5) + (Mul 4, 8)

c

Which declaration assigns a variable with 5 such that the value can not be later changed? const int = 5; int numItems = 5; int NUM_ITEMS = 5; const int NUM_ITEMS = 5;

const int NUM_ITEMS = 5;

Given an integer vector of size NUM_ELEMENTS, which XXX, YYY, and ZZZ will count the number of times the value 4 is in the vector? Choices are in the form XXX / YYY / ZZZ. vector<int> myVect(NUM_ELEMENTS); int cntFours; XXX for (i = 0; YYY; ++i) { if (myVals.at(i) == 4) { ZZZ; } } a. cntFours = myVect.at(0); / i > myVect.size(); / cntFours = myVect.at(i); b. cntFours = myVect.at(1); / i < myVect.size(); / cntFours = myVect.at(i) + 1; c. cntFours = 1; / i > NUM_ELEMENTS; / cntFours = cntFours + 1; d. cntFours = 0; / i < NUM_ELEMENTS; / ++cntFours;

d

Given integer vector itemNumbers has three elements with values 33, 34, 35. What are the values in itemNumbers after the following? itemNumbers.push_back(32); a. 32, 34, 35 b. 33, 34, 32 c. 32, 33, 34, 35 d. 33, 34, 35, 32

d

The ____________ is a process that manages programs and interfaces with peripherals. a. clock b. BIOS c. integrated circuit d. operating system

d

The following program generates an error. Why? void PrintSum(int num1, int num2) { cout << num1 + num2; } int main() { int y; y = PrintSum(4, 5); return 0; } a. The void function is missing a "return;" statement. b. The values 4 and 5 cannot be passed directly to PrintSum() c. main() has a return statement that returns the value 0 d. PrintSum() has void return type, so cannot be assigned to a variable

d

What does a clock do? a. Stores the computer's programs b. Counts the number of instructions executed by a processor c. Interfaces with peripherals d. Determines the rate of execution for a processor's instructions

d

What is the output for x = 15? switch (x) { case 10: // Output: "First " break; case 20: // Output: "Second " break; default: // Output: "No match " break; } a. First b. Second c. First Second d. No match

d

What is the output? int columns; int rows; for (rows = 0; rows < 2; ++rows) { for (columns = 0; columns < 3; ++columns) { cout << "x"; } cout << endl; } a. xxx xxx b. xx xx xx c. xx xx xx d. xxx xxx

d

What will a compiler do for the following code? /* numItems = 2; /* Total items to buy */ rate = 0.5; */ a. Ignore numItems = 2, but generate code for rate = 0.5. b. Generate code for numItems = 2, but ignore rate = 0.5. c. Ignore both statements. d. Generate an error.

d

What will a compiler do for the following three lines of code? // x = 2; // width // y = 0.5 // z = x * y; Total area a. Yield an error for line 1 (x = ...) b. Yield an error for line 2 (y = ...) c. Yield an error for line 3 (z = ...) d. Ignore all three lines

d

Which XXX and YYY correctly output the smallest value? Vector userVals contains integers (which may be positive or negative). Choices are in the form XXX / YYY. // Determine smallest (min) value int minVal; XXX for (i = 0; i < userVals.size(); ++i) { if (YYY) { minVal = userVals.at(i); } } cout << "Min: " << minVal << endl; a. minVal = 0; / userVal > minVal b. minVal = 0; / userVal < minVal c. minVal = userVals.at(0); / userVals.at(i) > minVal d. minVal = userVals.at(0); / userVals.at(i) < minVal

d

Which assigns the last vector element with 20? vector<int> userNum(N_SIZE); a. userNum.at(20); b. userNum.at() = 20; c. userNum.at(N_SIZE) = 20; d. userNum.at(N_SIZE - 1) = 20;

d

Which declares two related integer vectors named personName and personAge each with 50 elements? a. vector<int> personName, personAge; b. vector<int> personName, personAge = 50; c. vector<int> personName = 50; vector<int> personAge = 50; d. vector<int> personName(50); vector<int> personAge(50);

d

Which expression for YYY correctly outputs that x is between 50-100? if (YYY) { // Output "50, 51, ..., 99, 100" } a. (x >= 50) || (x <= 100) b. 50 >= x <= 100 c. 50 <= x <= 100 d. (x >= 50) && (x <= 100)

d

Which is a valid definition for a function that passes two integers (a, b) as arguments, and returns an integer? a. myFunction(int a, int b) b. int myFunction(a, b) c. void myFunction (a, b) d. int myFunction(int a, int b)

d

Which is an example of a process? a. int b. y, 10 c. 15 d. x + y

d

Which is not a component of a computer? a. Clock b. Memory c. Processor d. Programmer

d

Which is true for comments? a. The compiler converts comments into a series of machine instructions. b. The compiler translates comments into ASCII values. c. The compiler allocates memory for comments along with for variables. d. The compiler does not generate machine code for comments.

d

The world population is over 7 billion. Which declaration uses the fewest bits while guaranteeing that worldPopulation can be assigned the value 7 billion without error? int worldPopulation; long worldPopulation; long long worldPopulation; long long long worldPopulation;

long long worldPopulation;

An identifier can _______.

start with an underscore

Which expression doubles x? x += 2; x *= 2; x *= x; x *= x * 2;

x *=2


Kaugnay na mga set ng pag-aaral

EAPS 10500 | Units 10-12 (Moons, Tides, and Rings; Exoplanets; Hazards of Space Travel) | Study Guide (Exam 4)

View Set

Mortgage Loan Originator Prelicense Exam Prep

View Set

Chart conversions. Inches to feet

View Set

Vocabulary Workshop Level E Unit 6 - Synonyms

View Set

Psychology202 Stress, lifestyle health

View Set

Marketing - Final Exam - Valerie 2

View Set