C++ Exam 1

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the output of the following C++ code? int x = 1; do { cout << x << " "; x--; } while (x > 0); cout << endl;

1 or 10 (not sure)

Assigning a beginning value to a memory location is referred to as

initializing

What is the output of the following C++ code? num = 10; while (num > 10) num = num - 2; cout << num << endl;

10

What is the output of the following C++ code? int j; for (j = 10; j <= 10; j++) cout << j << " "; cout << j << endl;

10 11

Find the value of the expression: 5.2 * 2.0

10.4

#include <iostream> using namespace std; int main() { int A = 1, B = 3, C = 5; cout << (1 - 6 * B) << endl; (Line 1) cout << ((-A < -B) && (C >= A)) << endl; (Line 2) cout << ((C < 1) || (B == 1)) << endl; (Line 3) cout << (A / B + 2) << endl; (Line 4) cout << (1 % 5) << endl; (Line 5) return 0; } What value is displayed by the statement labeled "Line 4"

2

#include <iostream> using namespace std; int main() { int R = 0, S = 0, T = 0; for (R = -1; R < 6; R = R + 3) { if (R > T) S++; else T++; }cout << R << endl; // Line 1 cout << S << endl; // Line 2 cout << T << endl; // Line 3 return 0; } What output is produced by the statement labeled "Line 2" when the source code in SET 2 is compiled and executed?

2

What is the output of the following for loop? for (int Y = 2; Y<=10; Y++) cout<<Y<<" ";

2 3 4 5 6 7 8 9 10

If you use the floating-point number 3.7 to initialize a memory location that can store only integers, the computer converts it to

3

What is the next Fibonacci number in the following sequence? 1, 1, 2, 3, 5, 8, 13, 21, ...

34

What is the output of the following C++ code? int x = 35; int y = 45; int z; if (x > y) z = x + y; else z = y - x; cout << x << " " << y << " " << z << endl;

35 45 10

Suppose x and y are int variables. Consider the following statements. if (x > 5) y = 1; else if (x < 5) { if (x < 3) y = 2; else y = 3; } else y = 4; What is the value of y if x = 5?

4

Find the value of the expression: 12 * 4 + 7

55

Suppose sum and num are int variables, and the input is 11 35 28 -1. What is the output of the following code? cin >> num; sum = num; while (num != -1) { cin >> num; sum = sum + num; } cout << sum << endl;

73

#include <iostream> using namespace std; int main() { int R = 0, S = 0, T = 0; for (R = -1; R < 6; R = R + 3) { if (R > T) S++; else T++; }cout << R << endl; // Line 1 cout << S << endl; // Line 2 cout << T << endl; // Line 3 return 0; } What output is produced by the statement labeled "Line 1" when the source code in SET 2 is compiled and executed?

8

What value of X is printed by the following program segment? X = 5;while(X <=60) X = X * 2;cout<< X<< endl;

80

In a for loop, which of the following is executed first? A) initial statement B) loop condition C) update statement D) for loop statement

A) initial statement

Given the declaration "int A, X;" which expression will correctly assign the absolute value of A to X? A) if(A>0) X = A; elseX = -1*A; B) if((A>=0)&&(A!=0)) X = A; elseX = -1*A; C) if(A<0)X = -1*A; elseX = A; D) All of the above. E) None of the above.

All of the above

Given the declaration "int A,B,C;" which expression will correctly calculate the average of the three numbers? A) (A + B + C) / 3 B) (A + B + C) / 3.0 C) (A + B + C) / (3) D) All of the above. E) None of the above.

B) (A + B + C) / 3.0

Which is not a high-level language? A) Java B) Assembly C) COBOL D) Pascal

B) Assembly

If E1 is true, and E2 is false, then which of the following Boolean expressions is true? A) E1 && E2 B) E1 || E2 C) ! (E1 || E2) D) ! (E1) &&E2

B) E1 || E2

Which of the following statements regarding identifiers in a C++ program is false? A) The identifier must begin with a letter or an underscore B) The identifier must contain only letters and underscores C) The identifier cannot be a keyword D) Identifiers in C++ are case sensitive

B) The identifier must contain only letters and underscores

Suppose x is 5 and y is 7. Choose the value of the following expression: (x != 7) && (x <= y) A) false B) true C) 0 D) null

B) true

All computer programs, no matter how simple or how complex, are written using one or more of three basic control structures. Which of the following is not one of them? A) Sequence B) Selection C) Random D) Repetition

C) Random

Which of the following is not a C++ keyword? A) char B) false C) after D) bool

C) after

Which loop will output the sequence "1 2 4 7 "? A) for( int x=1; x<=5 ; x++ ) cout << x << " "; B) for(intx=0;x<2;x++) cout << 2*x-1 << " "; C) for( int x=0; x<=2 ; x++ ) cout << 2*x+1 << " "; D) for(intx=1;x<5;x++) cout << x << " "; E) None of the above.

E) None of the above

What is the output of the following code? int x = 0; int i; for (i = 0; i < 4; i++); x++; if (x == 3) cout << "*"; cout << endl;

There is no output

A set of instructions that tells a computer exactly what to do is called _____.

a program

An algorithm is

a set of step by step instructions that transforms the problem's input into it's output

A(n) _______ is simply the set of step-by-step instructions that accomplish a task.

algorithm

A correct statement to get values for the variables A, B, and C would be:

cin >>A >> B >> C;

The _____ structure directs the computer to process the program instructions, one after another, in the order listed in the program.

sequence

______ refers to the process of locating and removing any errors in a program.

debugging

______loops are called post-test loops.

do...while

______ is a C++ manipulator that advances the cursor to the next line on the screen.

endl

A(n) _____ uses symbols to show the steps the computer needs to take to accomplish the program's goal.

flowchart

Describe the exact output that is produced by the following program segments. int k, max = 10; for(k=1; k <= max; k++) { if( k == 6 ) break; cout << "k = " << k << endl; } cout << "All done!" << endl;

k=1 k=2 k=3 k=4 k=5 All done!

Which one of the following operators do you use to test for inequality in C++? A) /= B) /= C) != D) =!

C) !=

Which of the following loops is guaranteed to execute at least once? A) while loop B) for loop C) do...while loop

C) do...while loop

Assume you need to write a posttest loop that asks the user for an integer. If the number is non- negative, add it to a sum. If it is negative, stop the loop. Output the sum.Here is the code, minus one statement: int sum = 0; int number = 0; do { cout << "Input a number (negative to stop): "; cin >> number; // Missing statement goes here } while (number>=0); cout << "The sum = " << sum << endl; What is the missing statement? A) if (number < 0) sum = sum + number; B) if (number > 0) sum = sum + number; C) if (number >= 0) sum = sum + number; D) if (number == 0) sum = sum + number;

C) if (number >= 0) sum = sum + number;

Which of the following statement(s) is/are true? A) The CPU is the Central Processing Unit. B) The ALU is the Arithmetic-Logic Unit. C) RAM is Random Access Memory. D) All of the above are true. E) None of the above is true.

D) All of the above are true

Given the statement: if (A > 70) || (A < 110) .... which value of A would make this statement true? A) 60 B) 75 C) 130 D) All of these

D) All of these

Which of the following is an example of hardware? A) a computer game B) a compiler C) a word processor D) a printer

D) a printer

Which of these is not a reserved word in C++? A) default B) protected C) void D) foreach

D) foreach

// Include What? using namespace std; int main() { double X=3.0 ; cout << fixed << setprecision( 2 ) << sqrt(X); return 0; }

Only #include<cmath>, #include<iostream>, and #include<iomanip>

Within the switch statement are the individual ____ clauses, each representing a different path that the selection structure can follow.

case

The ____ combines the object file with other machine code necessary for your C++ code to run correctly.

linker

Programmers use the repetition structure, referred to as a _____ , when they need the computer to repeatedly process one or more program instructions until some condition is met, at which time the repetition structure ends.

loop

Assume all variables are properly declared. What is the output of the following C++ code? num = 100; while (num <= 150) num = num + 5; cout << num << endl;

155

Consider the following for loop. int i; for (i = 1; i < 20; i++) cout << "Hello World"; cout << endl; How many times will Hello World be printed by this for loop?

19

A do while statement ends with a

semicolon

The three arguments in a for clause are separated by two

semicolons

The comparison operator "<" has ____ the comparison operator "<="..

the same precedence number as

A postest loop begins with

the word do

What is the value of x after the following statements execute? int x = 5; int y = 30; do x = x * 2; while (x < y);

40

In algebra we see numeric intervals given as 2<x<3 This is not how we express this relationship in C++, however. Give the correct C++ Boolean expression that specifies that x lies between 2 and 3.

(2 < x) && (x < 3)

#include <iostream> using namespace std; int main() { int A = 1, B = 3, C = 5; cout << (1 - 6 * B) << endl; (Line 1) cout << ((-A < -B) && (C >= A)) << endl; (Line 2) cout << ((C < 1) || (B == 1)) << endl; (Line 3) cout << (A / B + 2) << endl; (Line 4) cout << (1 % 5) << endl; (Line 5) return 0; } What value is displayed by the statement labeled "Line 1"

-17

Find the value of the expression: 12 % 3 * 6 - 7

-7

#include <iostream> using namespace std; int main() { int A = 1, B = 3, C = 5; cout << (1 - 6 * B) << endl; (Line 1) cout << ((-A < -B) && (C >= A)) << endl; (Line 2) cout << ((C < 1) || (B == 1)) << endl; (Line 3) cout << (A / B + 2) << endl; (Line 4) cout << (1 % 5) << endl; (Line 5) return 0; } What value is displayed by the statement labeled "Line 2"

0

#include <iostream> using namespace std; int main() { int A = 1, B = 3, C = 5; cout << (1 - 6 * B) << endl; (Line 1) cout << ((-A < -B) && (C >= A)) << endl; (Line 2) cout << ((C < 1) || (B == 1)) << endl; (Line 3) cout << (A / B + 2) << endl; (Line 4) cout << (1 % 5) << endl; (Line 5) return 0; } What value is displayed by the statement labeled "Line 3"

0

How many times will this loop execute? for (int x = 2; x <= 2; x++) for (int y = 5; y <= 1; y--) cout<<x<<" "<<y<<endl;

0

What is the output of the following C++ code? int x = 1; int j; for (j = 0; j <= 2; j++) x = x * j; cout << x << endl;

0

What is the output of the following C++ code? num = 0; while (num < 5) { cout << num << " "; num = num + 1; }

0 1 2 3 4

#include <iostream> using namespace std; int main() { int A = 1, B = 3, C = 5; cout << (1 - 6 * B) << endl; (Line 1) cout << ((-A < -B) && (C >= A)) << endl; (Line 2) cout << ((C < 1) || (B == 1)) << endl; (Line 3) cout << (A / B + 2) << endl; (Line 4) cout << (1 % 5) << endl; (Line 5) return 0; } What value is displayed by the statement labeled "Line 5"

1

#include <iostream> using namespace std; int main() { int R = 0, S = 0, T = 0; for (R = -1; R < 6; R = R + 3) { if (R > T) S++; else T++; }cout << R << endl; // Line 1 cout << S << endl; // Line 2 cout << T << endl; // Line 3 return 0; } What output is produced by the statement labeled "Line 3" when the source code in SET 2 is compiled and executed?

1

Consider the following for loop. int i; for (i = 1; i < 20; i++) cout << "Hello World"; cout << "!"; cout << endl; How many times will ! be printed by this for loop?

1

Consider the following statements. int x = 5; int y; do x = x * 2; while (x < y); If y = 0, how many times would the do...while loop execute?

1

Using scientific notation, the number 1,225,000.00, would be displayed as

1.225e+006

Suppose that the input is 4 7 -8 5 2. What is the output of the following C++ code? int sum = 0; int num; int j; for (j = 1; j <= 5; j++) { cin >> num; if (num < 0) break; sum = sum + num; } cout << sum << endl;

11

Suppose sum and num are int variables, and the input is 18 25 61 6 -1.What is the output of the following code? sum = 0; cin >> num; while (num != -1) { sum = sum + num; cin >> num; } cout << sum << endl;

110

Suppose j, sum, and num are int variables, and the input is 26 34 61 4 -1. What is the output of the code? sum = 0; cin >> num; for (int j = 1; j <= 4; j++) { sum = sum + num; cin >> num; } cout << sum << endl;

125

Find the value of the expression: 14 + 75 * (18 - 5 % 2)

1289

How many times will this loop execute? for (int a = 2; a <= 4; a++) for (int b = 1; b <= 5; b++) cout<<a<<" "<<b<<endl;

15

Suppose that the input is 5 3 4 -6 8. What is the output of the following C++ code? int sum = 0; int num; int j; for (j = 1; j <= 5; j++) { cin >> num; if (num < 0) continue; sum = sum + num; } cout << sum << endl;

20

Assume all variables are properly declared. What is the output of the following C++ code? num = 100; while (num <= 200) num = num + 3; cout << num << endl;

202

Suppose sum, num, and j are int variables, and the input is4 7 12 9 -1. What is the output of the following code? cin >> sum; cin >> num; for (j = 1; j <= 3; j++) { cin >> num; sum = sum + num; } cout << sum << endl;

24

What is the output of the following C++ code? count = 1; num = 25; while (count < 25) { num = num - 1; count++; } cout << count << " " << num << endl;

25 1

Suppose x and y are int variables. Consider the following statements. if (x > 5) y = 1; else if (x < 5) { if (x < 3) y = 2; else y = 3; } else y = 4; What is the value of y if x = 3?

3

int x = 5; int y = 30; do x = x * 2; while (x < y); How many times does the statement x = x * 2; execute?

3

What is the value of counter after the following statements execute? counter = 0; while (counter <= 50) counter = counter + 3;

51

Find the value of the expression: 7 + 4 * 12

55

Suppose that x = 55.68, y = 476.859, and z = 23.8216. What is the output of the following statements? cout << fixed << showpoint; cout << setprecision(3); cout << x << ' ' << y << ' ' << setprecision(2) << z << endl;

55.680 476.859 23.82

What is value of x after the following code executes? int x = 0; int i; for (i = 0; i < 5; i++) x = 3 * x + i;

58

Find the value of the expression: 8 - 7 / 3

6

What is the output of the following program segment? int x = 14; int y = 60; while (((y - x) % 3) != 0) { cout << y << " "; y = y - 5; }

60 55

What is the output of the following C++ code? int x = 7;bool found = false; do { cout << x << " "; if (x <= 2) found = true; else x = x - 5; } while (x > 0 && !found); cout << endl;

7 2

Find the value of the expression: 3.5 + 8.7 / 2.1

7.6428571

Which statement only outputs three prime numbers? A) for (int X=2; X<8; X++) { if (X%2==1) cout << X ; B) for (int i=0; i<4; i++) cout << 2*i+1; C) for (int a=2; a<10; a++) { if (a/2==1)cout << a + a + 1; } D) All of the above. E) None of the above.

A) for (int X=2; X<8; X++) { if (X%2==1) cout << X ;

Which code fragment correctly expresses the following statement? Assume that variables age, clothesAllowance, and foodAllowance have been correctly initialized and are accessible by the fragment you are writing. All children under the age of 12 receive a $5 increase in their clothes allowance, and a 6% increase in their food allowance. A) if (age < 12) { clothesAllowance = clothesAllowance + 5; foodAllowance = foodAllowance * 1.06; } B) if (age <= 12) { clothesAllowance = clothesAllowance + 5; foodAllowance = foodAllowance * 1.06; } C) if (age < 12) { clothesAllowance = clothesAllowance * 5; foodAllowance = foodAllowance + 1.06; } D) if (age < 12) { clothesAllowance = clothesAllowance = 5; foodAllowance = foodAllowance = 1.06; }

A) if (age < 12) { clothesAllowance = clothesAllowance + 5; foodAllowance = foodAllowance * 1.06; }

Which one of the following operators do you use to test for equality in C++? A) = B) == C) => D) <=>

B) ==

Given the declaration "bool X = true, Y = false;" which statement will NOT output "This One" A) if (X || Y) cout "This one"; B) if (X && Y) cout "This one"; C) if (X) cout "This one"; D) if (!Y) cout "This one"; E) None of the above

B) if (X && Y) cout "This one";

Assume you need to write a code segment to output a product table showing all products between two variables, low and high. Here is the code, minus one line: int low, high; cout << "Enter the low value\n"; cin >> low; cout << "Enter the high value\n"; cin >> high; int product = 0; for (int i = low; i<=high; i++) { for (int j = low; j<= high; j++) { // Missing line goes here cout << product; } cout << endl; } What is the missing line? A) product = i+j B) product = i*j; C) product = low*high D) product = low+high

B) product = i*j;

Consider the following: condition 1 = true condition 2 = false What is the value of condition1 && condition2?

False

Which of the following is the main function of the CU?

Fetch and decode the instructions

What is the output of the following code fragment? x = 10; if (x > 15) x = 0; cout << x << endl; else cout << x + 5;

None of these (syntax error, else is dangling)

Machine code equivalent of a high level language code is usually called

Object code

What is the output of the following loop? count = 5; cout << 'St'; do { cout << 'o'; count--; } while (count <= 5);

This is an infinate loop

The input/output symbol in a flowchart is represented by a _____ .

Triangle

What is the output of the following C++ code? x = 6; if (x > 10) cout << "One "; cout << "Two ";

Two

What is the output of the following C++ code? x = 0; if (x < 0) cout << "One "; cout << "Two "; cout << "Three";

Two Three

Which executes immediately after a continue statement in a for loop?

Update Statement

Given the declaration "Int X;" which expression will output the rightmost two digits of X?

X%100

Arrange the following steps in the correct chronological order when the computer encounters a pretest for statement in a program: a. The computer processes the code in the for clause's initialization argument b. The computer processes the loop condition c. The computer processes the code in the for clause's update argument d. The computer processes the loop body if the loop condition is met

a, b, d, c

Assuming the value of variable count is 0 and the value of variable limit is 10, state whether each the following expressions are true or false. a. (count == 0) && (limit > 12) b. (limit > 20) || (count < 5) c. !(count == 12)

a. False b. True c. True

The first step in solving a familiar problem is to _____ the problem.

analyze

When a(n) _____ statement is encountered in a program, the computer gives the value of the expression appearing on the right side to the variable whose variablename appears on the left side.

assignment

You create a statement block by enclosing the statements in a set of

braces ({})

You can create a comment by typing two ____ before the text you want treated as a comment

forward slashes (//)

If you do not supply an initial value for a variable in a C++ program, the variable may contain a meaningless value, referred to by programmers as

garbage

You should assign a descriptive name to each variable and named constant used in a program. The name, also called the ______ , should help you remember the purpose of the memory location.

identifier

Which of the following is equivalent to if (expr1) if (expr2) statement;

if (expr1 && expr2) Statement;

When the loop condition evaluates to false, the loop instructions are skipped over and processing continues with the instructions

immediately following the end of the loop

When a program instructs the computer to initialize a memory location, the computer first compares the data type of the value assigned to the memory location with the data type of the memory location itself to verify that the value is appropriate for the memory location. If the value's data type does not match the memory location's data type, the computer uses a process called ____ type conversion to convert the value to fit the memory location.

implicit

The ____ arithmetic operator is used to divide two integers, and results in the remainder of the division

modulus (%)

The statement num is less than 0 or num is greater than 50 in C++ is written as

num < 10 || num > 50

The instructions contained in a posttest loop always are processed at least

once

Programmers refer to the goal as the ______ , and the items needed to achieve the goal as the _____ .

output, input

In a ____ loop, the word while ___ the loop

pretest, appears at the beginning

A coded algorithm is called a

program

What is the output of the following code? char lastInitial = 'S'; switch (lastInitial) { case 'A': cout << "section 1 << endl; break; case 'B': cout << "section 2 << endl; break; case 'C': cout << "section 3 << endl; break; case 'D': cout << "section 4 << endl; break; default: cout << "section 5 << endl; }

section 5


Conjuntos de estudio relacionados

Chapter 5.1-5.2 Review (Operations on Functions and Inverses)

View Set

Common Diseases of Livestock Animals: Cause & Control

View Set