C++ Final
Assume that the user enters: Steve 3.5 68 What value is stored in gpa? string name; int age; double gpa; cout << "Enter your name, age and gpa: "; cin >> name >> age >> gpa;
.5
What prints? int main() { cout << fixed << 2.0 / 3.0 << endl; }
.666667
This code is legal, compiles and is well defined. Which line(s) contain initialization? int a = 5; // 1 a == 5; // 2 int b = 6; // 3 a ={b}; // 4 auto c = a == b; // 5
1 3 5
Match each item with the correct statement below. unsigned long signed int unsigned long long unsigned int signed long signed long long
15UL 12 15ULL 15U 3L 15LL
Which line or lines are illegal? /*1*/ int a, b; /*2*/ a = 3; int main() { /*3*/ b = 4; /*4*/ cout << a << ", " << b << endl ; }
2
This code is legal, compiles and is well defined. Which line(s) contain comparison? int a = 5; // 1 a == 5; // 2 int b = 6; // 3 a ={b}; // 4 auto c = a == b; // 5
2 5
What is printed when this runs? int sum = 22; sum +=2; cout << sum++; // sum = sum + 4 cout << sum << endl;
2425
Assume that the user enters: Steve 3.5 68 What value is stored in age? string name; int age; double gpa; cout << "Enter your name, age and gpa: "; cin >> name >> age >> gpa;
3
Assume that the user enters: Steve 60 3.5 What value is stored in gpa? string name; int age; double gpa; cout << "Enter your name, age and gpa: "; cin >> name >> age >> gpa;
3.5
This code is legal, compiles and is well defined. Which line(s) contain an assignment? int a = 5; // 1 a == 5; // 2 int b = 6; // 3 a ={b}; // 4 auto c = a == b; // 5
4
What is the output of the following program? int value = 3; value++; cout << value << endl;
4
Which line prints 5? int n = 12; cout << n/3 << endl; // 1 cout << n/7 << endl; // 2 cout << n % 3 << endl; // 3 cout << n % 7 << endl; // 4
4
What prints? int main() { cout << 2000 / 3.0 << endl; }
6.66667e-02
What prints? int main() { cout << setprecision(2) << 2000 / 3.0 << endl; }
6.67e02
What prints? int main() { cout << fixed << 2000 / 3.0 << endl; }
666.666667
What prints? int main() { cout << fixed << setprecision(2) << 2000 / 3.0 << endl; }
666.67
Which operator is the insertion operator?
<<
What header is needed to use the sqrt() function?
<cmath>
What header is needed for output formatting?
<iomanip>
What header is needed to use cin and cout?
<iostream>
What header is needed to use the string type?
<string>
Which operator is the extraction operator?
>>
On line 2, b is: int main() { a = 3; // 1 const int b = 7; // 2 a = b; // 3 }
A non-modifiable lvalue
What kind of error is this? error: expected ';' after expression
A syntax error
Used by compiler to produce object code.
Assembler
Which of these five concepts are illustrated here? int main() { extern int a; a = 3; }
Assignment Declaration
Literals like 3 and 7 are always: On line 3, b is: int a = 3; // 1 int a = 7; // 2 a = b; // 3
B O T H : rvalue
Converts processed source code to object code.
Compiler
The makefile for h04 is missing int main() { }
Compiles, runs and returns 0 to the O/S
Allows you to run your program in a controlled environment.
Debugger
Which of these five concepts are illustrated here? int main() { extern int a; }
Declaration Definition
Which of these five concepts are illustrated here? int main() { int a; }
Declaration Definition
>> cout << cin \n endl
Extraction or input operator Analogous to Java's System.out Insertion or output operator Similar to Java's Scanner objects Escape character Stream manipulator
Assuming str is a string object, this syntax is legal in both Java and C++. Does this code work correctly in both languages? if (str == "quit") . . .
False
Explain this output. Why is nothing printed? #include <iostream> using namespace std; int main() { cout << "Hello, World"; } make example ./example
File not saved
What is wrong with this IPO code fragment? cout << "Name: "; string name; cout << "Hello, " << name << endl; cin >> name;
Input occurs after output
Unix and C Fortran Simula Berkeley Systems Distribution Unix C++ GNU, GCC and Free Software
Ken Thomson and Dennis Ritchie John Backus O. Dahl & K. Nygaard Bill Joy Bjarne Stroustrop Richard Stallman
Below is the main function from the f2c program in Chapter 1. Which line(s) contain a output statement? int main() { 15 cout << "Enter a temperature in fahrenheit: "; 16 double fahr; 17 cin >> fahr; 18 double celsius = convert(fahr); 19 cout << "Converted: " << fahr << "F -> " << celsius << "C" << endl; return 0; }
Line 15 Line 19
Below is the main function from the f2c program in Chapter 1. Which line(s) contain a variable defintion? int main() { 15 cout << "Enter a temperature in fahrenheit: "; 16 double fahr; 17 cin >> fahr; 18 double celsius = convert(fahr); 19 cout << "Converted: " << fahr << "F -> " << celsius << "C" << endl; return 0; }
Line 16 line 18
Below is the main function from the f2c program in Chapter 1. Which line(s) uses the character input stream? int main() { 15 cout << "Enter a temperature in fahrenheit: "; 16 double fahr; 17 cin >> fahr; 18 double celsius = convert(fahr); 19 cout << "Converted: " << fahr << "F -> " << celsius << "C" << endl; return 0; }
Line 17
Below is the main function from the f2c program in Chapter 1. Which line(s) contain a function call? int main() { 15 cout << "Enter a temperature in fahrenheit: "; 16 double fahr; 17 cin >> fahr; 18 double celsius = convert(fahr); 19 cout << "Converted: " << fahr << "F -> " << celsius << "C" << endl; return 0; }
Line 18
Below is the main function from the f2c program in Chapter 1. Which line(s) use the insertion operator? int main() { 15 cout << "Enter a temperature in fahrenheit: "; 16 double fahr; 17 cin >> fahr; 18 double celsius = convert(fahr); 19 cout << "Converted: " << fahr << "F -> " << celsius << "C" << endl; return 0; }
Line 19 Line 15
Combines object modules to produce an executable.
Linker
Reads an executable image on disk and starts it running.
Loader
Provides instructions for building your program.
Make
Which of these statements apply to C++?
More efficient than Java or Python Produces native code that runs on the CPU Compiles to native code
Below is the main function from the f2c program in Chapter 1. Which line(s) contain a function declaration? int main() { 15 cout << "Enter a temperature in fahrenheit: "; 16 double fahr; 17 cin >> fahr; 18 double celsius = convert(fahr); 19 cout << "Converted: " << fahr << "F -> " << celsius << "C" << endl; return 0; }
None of these
This code is legal, compiles and is well defined. Which line(s) contain an input statement? int a = 5; // 1 a == 5; // 2 int b = 6; // 3 a ={b}; // 4 auto c = a == b; // 5
None of these
Which line or lines are illegal? int a; int b = 3; int main() { a = 4; cout << a << ", " << b << endl; }
None of these
Which of the following variables have the value null? int global; int main { string localStr; double localDouble; }
None of these
Performs text substitution on your source code.
Preprocessor
1 cout << 10 + 1 << endl; 2 cout << (10 + 1) << endl; 3 (cout << 11) << endl;
Rule 1 precedence Rule 2 associativity Rule 3 side effect
What is the problem here? make: *** No targets specified and no makefile found. Stop.
The programmer is in the wrong directory.
What is true about identifiers in C++?
They may contain an underscore
Assuming str is a string object does this correctly test if str consists of the characters "quit" in C++? if (str == "quit") . . .
True
Assuming str is a string object, is this syntax legal in both Java and C++? if (str == "quit") . . .
True
In C++ you can concatenate string objects using the + or += operators.
True
In C++ you can compare strings using all of the relational operators. In C++ you cannot use the relational or equality operators with strings.
True False
Strings in C++ are mutable. String in C++ are immutable
True False
A set of bits interpreted according to its type
Value
What is the problem here? You have submitted another student's completion code
You filled out the STUDENT variable incorrectly
As an application programmer, which of the following names for local variables are both legal and recommended for stylistic reasons.
_ (single underscore) CamelCase
The __________________ of an operator determines the number of items it operates on?
arity
The __________________ of an operator determines the order of operations when operators share an operand?
associativity
The + arithmetic operator is a(n) __________ operator
binary
What command makes hw the current folder?
cd ~/workspace/cs150/hw
The standard input object; analogous to a Scanner in Java Modifies and manipulates data to produce information The header used to include the standard streams A single entity that bundles data and instructions Displays the results of calculations cout stands for ___________________ The standard output object; analogous to System.out in Java The output or insertion operator endl is a _____________________ Retrieves data and stores it in variables C++ uses an ____________ library for input and output. Text enclosed in double quotes The header used for formatting real numbers Asking an object to perform certain operations
cin processing iostream object output character output cout << stream manipulator input object-oriented string iomanip sending a message
As an application programmer, which of the following names for local variables are legal (even if they are unwise from a stylistic perspective).
cout 2cool CamelCase u2 integer
Associates a name with a type Read a value and store it in a varaible Copy a new value into an existing variable Allocates space for a variable Provides a starting value when a variable is created A named storage area that holds a value
declare input assign define initialize variable
The variable ASSIGNMENT from your homework has been ______________________.
declared
Which manipulator is used to ensure that large numbers appear using regular decimal notation?
fixed
Which manipulator(s) is/are used to make sure the value 2.0/3 prints like this: 0.677?
fixed setprecision()
Which of the following variables have the value 0? int global; int main { string localStr; double localDouble; }
global
The variable STUDENT from your homework has been ______________________.
initialized defined declared
Legacy or assignment initialization
int a = 0;
Direct initialization
int b(3);
Uniform or list initialization
int c{5};
narrowing conversion
int e(3.5);
Symbols which directly represent a value (literal) Determines the direction of operations for operators at the same level (associativity) Describes how many operands an operator requires(arity) Operators that require a single data value (unary) Symbol which indicates a value (operand) Determines how tightly operators bind to operands (precedence) Any combination of operators and operands which yields a value (expression) A symbol that can be used to produce a value at runtime (function call) Symbol which indicates an operation (operator) A storage location containing a value (variable) Operators that require two data values(binary)
literal associativity arity unary operand precedence expression function call operator variable binary
Which of the following variables have an undefined value? int global; int main { string localStr; double localDouble; }
localDouble
x in the expression x = 3;
lvalue
What command only builds hw04?
make
What command hands in hw04 for course credit?
make submit
What command checks hw04 for correctness?
make test
Code is written in machine (and assembly) language for a specific processor; thus it is non-portable or machine dependent.
native code machine language
const double PI = 3.14159;
non-modifiable value
The __________________ of an operator determines which operands the operator binds with?
precedence
x in the expression y = x;
rvalue
Which manipulator is used to change the padding character used in a column like: 0045?
setfill()
Which manipulator(s) is/are used to make sure the number 45 prints like this: 0045?
setfill() setw()
The ++ arithmetic operator is a(n) __________ operator
side effect unary
The - operator is a(n) __________ operator
unary binary
Assume that the user enters: Steve Gilbert 68 3.5 What value is stored in age? string name; int age; double gpa; cout << "Enter your name, age and gpa: "; cin >> name >> age >> gpa;
undefined
Types such as classes, structures and enumerations Types such as pointers, arrays and references Built-in types, such as int and double The "kind" of a variable Read a value and store it in a variable Types such as string and vector
user-defined types derived types primitive types data type input library types
Which of these are unavoidable conditions? auto floor ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v1 v5
Which of these are impossible conditions? auto floor ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v2
[] and () denote whether a range includes or excludes an endpoint: [ includes the endpoint ( excludes the endpoint [] = 'Closed', includes both endpoints () = 'Open', excludes both endpoints [) and (] are both 'half-open', and include only one endpoint Which variable correctly indicates that the variable floor is in the range (0...20)? auto floor ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v3
[] and () denote whether a range includes or excludes an endpoint: [ includes the endpoint ( excludes the endpoint [] = 'Closed', includes both endpoints () = 'Open', excludes both endpoints [) and (] are both 'half-open', and include only one endpoint Which variable correctly indicates that the variable floor is in the range [0...20]? auto floor ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v4
[] and () denote whether a range includes or excludes an endpoint: [ includes the endpoint ( excludes the endpoint [] = 'Closed', includes both endpoints () = 'Open', excludes both endpoints [) and (] are both 'half-open', and include only one endpoint Which variable correctly indicates that the variable floor is in the range [0...20)? auto floor ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v8
Assume int x, y, z; Shorthand assignment Post increment Undefined behavior Widening conversion Pre decrement Chained assignment Narrowing conversion Mixed-type expression
y += z; x++; x = z++ - ++z; double a = y; --z; x = y = z = 10; z = 3.15; auto v = x * 2.3;