C++ Midterm

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

What will be the value of result after the following code has been executed? int a = 60; int b = 15; int result = 10; if (a = b) result *= 2;

20

How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl;

21

In the following C++ statement, what will be executed first according to the order of precedence? result = 6 - 3 * 2 + 7 - 10 / 2 ;

3 * 2

Assuming x is 5, y is 6, and z is 8, which of the following is false? 1. x == 5; 2. 7 <= (x + 2); 3. z < = 4; 4. (1 + x) != y; 5. z >= 8; 6. x >= 0; 7. x <= (y * 2)

3 and 4 are False

What will the value of x be after the following statements execute? int x; x = 18 / 4;

4

What is the value of average after the following code executes? double average; average = 1.0 + 2.0 + 3.0 / 3.0;

4.0

Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 const int MY_VAL; 7 MY_VAL = 77; 8 cout << MY_VAL << endl; 9 return 0; 10 }

6

==

Equal to

T OR F: In C++, it is impossible to display the number 34.789 in a field of 9 spaces with 2 decimal places of precision.

False

T OR F: The C++ language requires that you give variables names that indicate what the variables are used for.

False

T OR F: The default section is required in a switch statement.

False

T OR F: The preprocessor executes after the compiler.

False

T OR F: The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.

False

T OR F: When a program uses the setw manipulator, the iosetwidth header file must be included in a preprocessor directive.

False

T OR F: You may not use the break and continue statements within the same set of nested loops.

False

What will the following code display? cout << "Four\n" << "score\n"; cout << "and" << "\nseven"; cout << "\nyears" << " ago" << endl;

Four score and seven years ago

>=

Greater than or equal to

Besides decimal, two other number systems you might encounter in C++ programs are:

Hexadecimal and Octal

<=

Less than or equal to

These are data items whose values do not change while the program is running:

Literals

Mistakes that cause a running program to produce incorrect results are called:

Logic errors

Unix and Windows 2000 are examples of _______________ operating systems.

Multi-tasking

!=

Not equal to

In memory, C++ automatically places a ___________ at the end of string literals.

Null terminator

Operator

Operators perform operations on one or more operands. An operand is usually a piece of data, like a number.

When converting some algebraic expressions to C++, you may need to insert ___________ that do not appear in the algebraic expression.

Parentheses

A(n) ____________ is a set of instructions that the computer follows to solve a problem.

Program

This is used in a program to mark the beginning or ending of a statement, or separate items in a list:

Punctuation

The computer's main memory is commonly known as:

RAM

Syntax

Rules that must be followed when constructing a program. Syntax dictates how key words and operators may be used, and where punctuation symbols must appear.

In a C++ program, two slash marks ( // ) indicate:

The beginning of a comment

What will the following code display? int number = 7; cout << "The number is " << "number" << endl;

The number is number

What does the term hardware refer to?

The physical components that a computer is made of

Source Code and Source File

The state-ments written by the programmer are called source code, and the file they are saved in is called the source file.

A variable declaration announces the name of a variable that will be used in a program, as well as:

The type of data it will be used to hold

You want the user to enter the length, width, and height from the keyboard. Which cin statement is correctly written?

cin >> length >> width >> height;

The _________ causes a program to wait until information is typed at the keyboard and the Enter key is pressed.

cin object

This function tells the cin object to skip one or more characters in the keyboard buffer:

cin.ignore

When using the sqrt function you must include this header file:

cmath

This statement may be used to stop a loop's current iteration and begin the next one:

continue

To use the rand() function, you must #include this header file in your program:

cstdlib

What defines a double-precision floating point variable named payCheck?

double payCheck;

What will be the output of the following code segment after the user enters 0 at the keyboard? int x = -1; cout << "Enter a 0 or a 1 from the keyboard: "; cin >> x; if (x) cout << "true" << endl; else cout << "false" << endl;

false

During which stage does the central processing unit retrieve from main memory the next instruction in the sequence of program instructions?

fetch

This is a variable, usually a boolean or an integer, that signals when a condition exists:

flag

If you want a user to enter exactly 20 values, which loop would be the best to use?

for

Every complete C++ program must have a _________

function named main

Which statement will read an entire line of input into the following string object? string address;

getline(cin, address);

This is a special value that marks the end of a list of values:

sentinel

The first step in using the string class is to #include the ___________ header file.

string

This statement lets the value of a variable or expression determine where the program will branch to:

switch

What is the value of the following expression? true || true

true

This operator performs a logical NOT operation.

!

The _____ causes the contents of another file to be inserted into a program

#include directive

This operator represents the logical AND

&&

Character constants in C++ are always enclosed in ______

'single quotation marks'

When this operator is used with string operands it concatenates them, or joins them together.

+

These are operators that add and subtract one from their operands:

++ and --

/ /

- Double slash. - Marks the beginning of a comment.

#

- Pound sign. - Marks the beginning of a preprocessor directive.

Preprocessor

- Reads the source code. - Searches for special lines that begin with the # symbol. These lines contain commands, or directives, that cause the preprocessor to amend or process the source code in some way

{ }

-Opening and closing braces -Encloses a group of statements, such as the contents of a function.

< >

-Opening and closing brackets -Encloses a filename when used with the #include directive.

( )

-Opening and closing parentheses -Used in naming a function, as in int main().

" "

-Opening and closing quotation marks -Encloses a string of characters, such as a message that is to be printed on the screen.

What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; }

0 1 2 3 4

What will the following program display? #include <iostream> using namespace std; int main() { int a = 0, b = 2, x = 4, y = 0; cout << (a == b) << " "; cout << (a != b) << " "; cout << (b <=x) << " "; cout << (y > a) << endl; return 0; }

0 1 1 0

Given that x = 2, y = 1, and z = 0, what will the following cout statement display? cout << "answer = " << (x || !y && z) << endl;

1

What are the five major components of a computer system?

1. The CPU (central processing unit) 2. Main Memory 3. Secondary Memory / Storage 4. Input Devices 5. Output Devices

What is the output of the following statement? cout << 4 * (15 / (1 + 3)) << endl;

12

What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2;

12

In the C++ instruction, cookies = number % children; given the following declaration statement: int number = 38, children = 4, cookies; what is the value of cookies after the execution of the statement?

2

What will the value of result be after the following statement executes? result = 6 - 3 * 2 + 7 - 10 / 2 ;

2

How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;

20

An example of a secondary storage device is:

A hard disk

The programmer usually enters source code into a computer using:

A text editor

What will the following segment of code output if 11 is entered at the keyboard? int number; cin >> number; if (number > 0) cout << "C++"; else cout << "Soccer"; cout << " is "; cout << "fun" << endl;

C++ is fun

\r

Causes the cursor to go to the beginning of the current line, not the next line.

For every opening brace in a C++ program, there must be a:

Closing brace

This step will uncover any syntax errors in your program:

Compiling

Even when there is no power to the computer, data can be held in:

Secondary storage

This is used to mark the end of a complete C++ programming statement.

Semicolon

The float data type is considered _____ precision, and the double data type is considered _______ precision.

Single, Double

The purpose of a memory address is:

To identify the location of a byte in memory

T OR F: A CPU really only understands instructions that are written in machine language.

True

T OR F: An output file is a file that data is written to.

True

T OR F: As a rule of style, when writing an if statement you should indent the conditionally-executed statements.

True

T OR F: C++ does not have a built in data type for storing strings of characters

True

T OR F: Escape sequences are always stored internally as a single character.

True

T OR F: If the sub-expression on the left side of an && operator is false, the expression on the right side will not be checked.

True

T OR F: Software engineering is a field that encompasses designing, writing, testing, debugging, documenting, modifying, and maintaining computer programs.

True

T OR F: The cin >> statement will stop reading input when it encounters a newline character.

True

T OR F: The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.

True

T OR F: When the fixed manipulator is used, the value specified by the setprecision manipulator will be the number of digits to appear after the decimal point.

True

T OR F: You should be careful when using the equality operator to compare floating point values because of potential round-off errors.

True

Programmer-defined names of memory locations that may hold data are:

Variables

Programmer-Defined Identifiers

Words or names defined by the programmer. They are symbolic names that refer to variables or programming routines.

Key Words

Words that have a special meaning. Key words may only be used for their intended purpose. Key words are also known as reserved words.

In C++ the = operator indicates:

assignment

The statements in the body of a while loop may never be executed, whereas the statements in the body of a do-while loop will be executed:

at least once

A file _________ is a small holding section of memory that file-bound information is first written to.

buffer

Which statement allows you to properly check the char variable code to determine whether it is equal to a "C" and then output "This is a check" and then advance to a new line?

if (code == 'C') cout << "This is a check\n";

Something within a while loop must eventually cause the condition to become false, or a(n) __________ results.

infinite loop

In a for statement, this expression is executed only once:

initialization

When a program lets the user know that an invalid choice has been made, this is known as:

input validation

In any program that uses the cin object, you must include the ___________.

iostream header file

This manipulator causes the field to be left-justified with padding spaces printed to the right.

left

This is a control structure that causes a statement or group of statements to repeat:

loop

To write data to a file, you define an object of this data type:

ofstream

A file must be ________ before data can be written to or read from it.

opened

Assuming outFile is a file stream object and number is a variable, which statement writes the contents of number to the file associated with outFile?

outFile << number;

When a variable is assigned a number that is too large for its data type, it:

overflows

The do-while loop is considered a(n) _________ loop.

post-test

This operator increments the value of its operand, then uses the value in context:

prefix increment

This is a pre-test loop that is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning:

while

Which statement is equivalent to the following? x = x * 2;

x *= 2;


Kaugnay na mga set ng pag-aaral

Complex Care: Syndrome of Inappropriate Antidiuretic Hormone (SIADH)

View Set

Data Collection, Behavior, & Decisions

View Set