Programming Fundamentals II (C++) Unit 1 test study
Which of the following statments will read in a one word description?
cin >> value;
Relational operators allow you to ________ numbers.
compare
The -- operator ... (check all that apply)
is a unary operator. must have an lvalue, such as a variable, as its operand. subtracts one from the value of its operand. can be used in either prefix or postfix mode.
if letter and w are defined as char data type. Which of the following statement(s) are valid?
letter = w; letter = 'w';
The _________ of a variable determines where it can be used withing the program.
scope
Match the value of score with the resulting value of pass. pass = (score >= 7) ? true : false;
score = 6 B. pass = false score = 7 A. pass = true score = 10 A. pass = true score = 0 B. pass = false
What will the following expression evaluate to? !(6 > 7 || 3 == 4)
true
When a function just needs to use a copy of an argument passed to it, the argument should normally be passed by value.
true
C++ will attempt to convert operands to the same data type when performing arithmetic operations. This is known as:
type coercion
Which of the following function prototype(s) are correct.
void setValue(int &value); void setValue(int &); void setValue(int value, int max=100);
In the following logical expression, under what circumstance will checkFlag() not be evaluated? if x >= 99 || checkFlag() cout << y + x;
when x is any value greater then or equal to 99.
A function other than the main function is executed
whenever it is called
Which of the following statements will result in an infinite loop.
x = 10; while (x >= 0) { cout << x;++x; } x = 10; while (x >= 0) { cout << x; } x = 10; while (x >= 0); { cout << x;x--;} x = 0; while (x <= 10) { cout << x;x = 10; }
Which of the following statements show the proper way to use the increment (++) operator.
y = ++x; x = 23++;
A string literal with 5 characters requires 5 bytes of storage.
false
Every complete C++ statement ends with a
Semicolon
What is printed when the following program is run? int main(){string name;string city;cout << "Enter your name:";cin >> name; cout << "Enter your city:"cin >> city;cout << "Hello " << name << ", welcome to " << city; return 0;}Enter your name:Spider ManEnter you city:Hello [spider], welcome to [man].
Spider Man
Select all the tasks that Software Engineering encompasses.
Testing Programs Designing Programs Documenting Programs Debugging Programs Coding
You are asked to write a program that helps bank customers calculate their balance. Select all the items that might be required as input to your program.
The total amount of withdrawals. Total amount of deposits The starting balance. Interest rate on the account.
Calling an overloaded function can result in multiple values being returned to the caller.
false
In C++ global and local numeric variables are initialized to zero by default.
false
In C++, a file must exists before it can be written to.
false
When writing a C++ program you must be careful to arrange all the functions in the order in which they are called.
false
While loops can only be used when a counter is decremented to zero. If the counter is incremented you must use the do-whileloop.
false
cin can read multiple values but only if they all have the same data type.
false
A(n) ________ is a variable which is usually of type bool, that signals when a condition exists.
flag
Which of the following statements will read in multiple words separated by blanks?
getline(cin, words);
When a function is overloaded it means that it... (select one or more answers)
has the same name as another function
The ________ statement causes other program statements to execute only under certain conditions.
if
If nothing within a while loop causes the condition to become false, a(n) ________ may occur.
infinite loop
In a for statement, the ________ expression is executed only once.
initialization
A for statement contains three expressions:
initialization, test, update
complete the following statement that initializes an integer variable called x to the value of one hundred. ____ x = 100;
int
Complete the following program statements that initialize two variables, x and y of type int, Multiplies them together and stores the result in the variable, z. [int] x = 12, y = 26, [z1]; [z2] = [x] * [y] ;
int z z x y
To use the cout statment (or object) you must first include what header file?
iostream
Which C++ operator reverses the truth or falsehood of a statement. (turns true to false and false to true).
!
In C++ , the logical AND operator is :
&&
What is the difference between the literals 'A' and "A"?
'A' requires just one byte whereas "A" requires two bytes.
Select the C++ expression that calculates the equation: g=(3x+2)/x^3
(3 * x + 2) / pow(x,3) Response Feedback: C++ does not provide an exponent operator such as found in many other languages. Therefore you must use the pow() library function.
How many times will the following program print Hello? counter = 3; while (counter > 0) { loop = 5; while (loop > 0) { cout << "Hello" << endl; loop--; } counter--;}
15
In C++ when a relational expression is false, it has the value
0
Put the following statements in the correct order for a valid C++ program.
1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. int value = 100; 6. cout << value; 7. return 0; 8. }
Put the following processes in order in which they occur to generate an executable program.
1. Preprocessor, Compiler, Linker
Set the size of each of the following character arrays so they are valid. So as not to waste memory, make the character arrays as small as possible . char name[[name11]] = "Spider Man"; char store[[store23] = "Downtown Auto Supplies";char fav_fool[[food18]] = "Billy Bob's Pizza";
11, 23, 18
Match the desired output to the statement. double x = 123.0;
123 C. cout << x; 123.000 B. cout << showpoint << x; 123.0 A. cout << fixed << showpoint << setprecision(1) << x;
If x = 2 and y = 6, what is the value of x + y / 2?
5
What is the ouput of the following program: main() { string x = "5"; string y = "3"; string z = x + y; cout << z; return 0;}
53
What is the value of x after the following statements are executed? int x; x = 7.2;
7
What will the following code print? num = 8; cout << --num << " "; cout << num++ << " "; cout << num;
7 7 8
Which statement best characterizes a low-level programming language?
A low-level language is closer to machine language.
A [void] function has [zero] return value(s), but can have [many] parameter(s). Otherwise a function can return [one] value(s).
A void function has zero return value(s), but can have many parameter(s). Otherwise a function can return one value(s).
Match the variable type to it use. QuestionCorrect Match A ________ variable is defined inside the body of a function and is not accessible outside that function. The value in ________ local variable is retained between function calls. When used as a parameter, a ________ variable allows a function to access and modify the original argument passed to it. A ________ variable is declared outside all functions.
A. local B. a static C. reference D. global
Match the arithmetic operation with the correct C++ operator.
Addition A. + Multiplication B. * Division C. / Modulus D. %
The following C++ test checks if the variable child is in the range 3 to 12. if (child >= 3 || child <= 12)
False
To check if a variable has a particular value, use the = operator, as in the statement: if (s = 3) cout << "S has the value 3";
False
Which of the following is NOT an input device?
Bluetooth Headphones
The following C++ test checks if the variable child is in the range 3 - 12. if (child >= 3 && <= 12)
False
logical operators AND and OR have a higher precedence than the NOT operator.
False
When a C++ program is compiled, it can run on any computer.
False, C++ is portable - but only at the code level. The code must be re-compiled for separate platforms such as Windows and MacOS.
What is printed by the following statement? string month = "April"; int day = 26; cout << "Her birthday" << " is " << endl << month << '\n' << day + 1;
Her birthday is April 27
72, 'A' and "Hello" are examples of ________ ?
Literals
When an argument is passed by reference it means that ... (check all that apply)
Only the memory location of the variable is passed to the function. The function can change the value of the variable
Computers can do many different jobs because they are [answer].
Programmable
All C++ relational operators are binary.
True
Main memory is where a program is stored while it is running.
True
Most C++ compilers will allow you to use int arguments for for functions that accept a double.
True
Consider the following code: if ( condition1 ) { block1 } else if (condition2) { block2 } else { block3 } Under what condition will block3 be executed?
When both condition1 and condition2 are false.
One advantage to using the string object instead of a character array is:
With the string object you don't have to worry about there not being enough memory to store the string.
What is the role of key words in a programming language?
Words that has a special meaning specific to the language.
In a function prototype, in addition to the name of the function, you are required to furnish
a data type for each parameter. the data type of the return value.
The statements in the body of a do-while loop are executed
at least once
The ________ statement causes a loop to terminate early.
break
Which of the following statements prevents a case in a switch statement from falling through to the next case below it?
break
Which of the following statments are valid C++ statements?
cout << "Hello " << "World"; cout << "Hello" << "World";
elect the header files which must be included in the following program. int main() {double amount = 89.7;cout << fixed << showpoint << setprecision(1);cout << setw(8) << amount << endl;return 0;}
iostream, iomanip
To write to an output file, a C++ program you must first...
do B and C, but not A.
Which of the following correctly declares an enumerated data type named student?
enum student { Bill, Tom, Mary };
A sentinel is a special value that ...
marks the end of a list of values
Breaking a program up into a set of manageable sized functions is called ________ programming.
modular
Which statement should NOT be used to set the value of name. char name[20]; cout << name << end;
name = "Captain America";
When an if statement is placed within the conditionally-executed code of another if statement, this is known as a(n).
nested if statement
Two or more functions may have the same name provided that
parameter lists are different
Since the while loop evaluates the condition before executing the statement(s), it is known as a(n) [pretest] loop, whereas the do-whileloop evaluates the condition after the statements have been executed, it is known as a(n) [posttest] loop.
pretest, posttest
Consider the following code segment: float x;double y,z; ... z = x * y; Before performing the multiplication of x and y, the compiler will first:
promote x to a double
Read the input from user; Check for valid entries If input is invalid: print an error message; else: calculate the result and print to the console; exit; The code snippet above may be best described as:
pseudocode
What does the following statement do? char line[100]; cin.getline(line, 20);
reads up to 19 characters and stores them in the variable line
The while loop has two important parts: a condition that is tested and a statement or block of statements that is
repeated as long as the condition is true
The ________ statement causes a function to end and the flow of control to move back to the point where the function call was made.
return
A void function is one that
returns no value
elect all the C++ math library functions you will need to solve the following equation: (Remember you cannot take the square root of a negative number) c=sqrt(An+Bn)
sqrt() abs() pow()
To use files in a C++ program you must include the ________ header file.
stream
In a function header, in addition to the name of the function, you are required to furnish
the data type of the return value. a data type for each parameter. an identifier name for each parameter.
A sample run of the following program is below. What went wrong? #include <iostream> using namespace std; int main() { char first[8]; char last[8]; cout<<"Enter first and last name\n"; cin >> first >> last; cout << "First:" << first << endl; cout << "Last:" << last << endl; return 0; }
there was a buffer overrun
A function can have zero to many parameters and either zero or one return value(s).
true
A pair of characters or a pair of string objects can be compared with any of the relational operators.
true
Assuming goodData is a Boolean variable, the following two tests are logically equivalent. if (goodData == false) if (!goodData)
true
In C++, a file must be opened before the contents can be read.
true
In C++, a variable must always be defined before it is used.
true
Only variables can be passed as reference.
true
The block of code in the body of a while statement can contain an unlimited number of statements, provided they are enclosed in a set of braces.
true
The char data type is used to store a single character.
true
The initialization statement in a for loop is optional.
true