COSC 1437 EXAM 1 NOTES

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

The preprocessor directives used with the assertion macros is/are

#include ,#define NDBUG

It is not true that a C++ predefined function

#include is all that is ever necessary to provide access.

In the expression (j > 0 && j+1 == 10), which operator executes last?

&&

Which of the following code fragments gives a random double value between 2 and 5 inclusive? You can assume that the random number seed has been set and any needed definitions and initializations have been made.

(RAND_MAX-rand())/static_cast(RAND_MAX)*5 -2

In distinguishing an expression as true or false, C++ sees which of the following as not true?

0

What is the output of the following, if it were embedded in an otherwise correct and complete program and run? int x = 10; while (x > 0) { cout << x << " "; x = x + 3;} cout << endl;

10 13 16 19 . . .

Given the function, and the main function calling it: What is the output of the following code? (You are to assume this code is embedded in a correct function that calls it.): #include <iostream> using namespace std; void func(int x, int & y) {int t = x;x = y;y = t;} int main() {int u = 3; v = 4; // ... cout << u << " " << v << endl; func ( u, v ) cout << u << " " << v << endl; // ... }

3 4 3 3

Given the function, and the main function calling it: Which, if any, of the following choices is the output of the following code? #include <iostream> using namespace std; void func ( int& x, int & y) {int t = x;x = y;y = t;} int main() {int u = 3; v = 4; // ... cout << u << " " << v << endl; func ( u, v ) cout << u << " " << v << endl; // ... }

3 4 4 3

If this code fragment were executed in an otherwise correct and complete program, what would the output be? int a = 3, b = 2, c = 5; if (a > b) a = 4; if ( b > c) a = 5; else a = 6; cout << a < endl;

6

The value of the expression 20.0 * (9.0/5) + 32.0 is

68.0

The value of the expression 20.0 * (9/5) + 32.0 is

68.0

Assume this code fragment is embedded in an otherwise correct and complete program. What should be the output from this code segment? { for( int i = 0; i < 10;i++) } . . . { cout << i << endl; }

9

Where is it legal to put a break statement?

A break is placed in a loop in nested loops, to transfer control beyond the end of the innermost loop the break is within.

Where is it legal to put a continue statement? What does the continue statement do there?

A continue statement in a nested loop causes that loop to restart, there is no effect on other loops.

Which of these remarks about formal parameters is correct?

A formal parameter is a kind of place holder that is filled with the argument at the time of call to the function

The statements int x = 1; int y; y = x++; do not...

Assign y the value 2;

The person responsible for the initial development of C++ was

Bjarne Stroustrup

Which of the following statements about the definition and declaration of functions is not correct?

Function definition is exactly the same as function prototype

Which of the following is not true of the && operator?

It can have one operand.

Which of the following is not true of the || operator?

It can have one operand.

Given the function definition, with arguments 7and 2.0 when the function was called, which of the following are correct? int func(int n, double d) {int j = n; double sum = 0; while( j >= 0) {sum += d; --j; } return sum;}

It compiles but computes none of these.

Why does this version of the swap function fail to work? void swap(int & lhs, int& rhs) { lhs = rhs; rhs = lhs; }

It fails because the first line destroys the old value of lhs without saving it. Then both variables have the old value of rhs in them, save the lhs value in a local variable before making the first assignment indicated, then instead of the second line, assign the rhs the value of the local variable

What does it mean when we say a programmer using a function should be able to treat the function like a black box?

One must be able to rely on the description of the preconditions (requirements for use of the function) and the postconditions (promise of behavior to use the function)

Which of the following determines the operator that is processed prior to another operator?

Operator precedence

Which of these "properties" of testing using stubs and drivers are correct?

Stubs are short programs that are fairly easy to write. They return only minimum data necessary for the caller to be debugged

Consider the function void doStuff(int parValue, int& parRef) { parValue = 100; cout << "parValue in call to doStuff = "<< parValue << endl; parRef =222; cout << "parRef in call to doStuff = " << parRef << endl; } and consider the call, which we assume is in a complete and correct program int n1 = 1, n2 =2; doStuff(n1, n2);

The call to doStuff results in the assignment n2 = 222

What do the calls to exit(...) do when exit(0) and exit(1) are called?

The exit( ) function stops the program. The argument is passed to the operating system which uses it for an error code

Which of the following are not true about the expression left || right.

The expression is false when left is false and right is true

The following are not true about the expression left && right.

The expression is true when left is true and right is false

Indicate which of these remarks about function parameters and arguments is correct.

The first argument fills in the first formal parameter, the second argument fills in the second formal parameter and so on

The fundamental rule for testing functions requires that every function be tested in an already fully tested and debugged program. How can this be accomplished?

The main function is tested with stub functions replacing each` function

A call to a C++ function is

The name of the function followed by exactly the number of arguments as there are parameters in the definition

What is the difference between executing the return 0; statement and its rough equivalent, a call to the exit(0); function, or the difference between return 1; and exit(1);?

These are very nearly equivalent when executed in the main function. In main, these both terminate main and send a success code to the operating system, returning control to the operating system

This question asks about nesting of if, if-else, switch, while, do-while, and for statements:

These constructs may be nested in any way that meets the needs of algorithms the programmer is coding.

What is the value of the bool valued expression, 1

This statement is incorrect and it is always true

Consider the if statement: if(condition) yes_clause; else no_clause; Under which of the following circumstances will both the yes_clause and the no_clause will be executed?

This will not happen.

Which of these remarks about overloading a function name is correct?

To decide which version of overloaded functions, C++ looks first for an exact match in the argument list types and the parameter list types

When you don't recall operator precedences you can

Use parentheses

Where can you not declare a variable in a C++ program?

Within the argument list of a function call

Which of the following are likely to be poor choices for an identifier in a program?

X

In C++, which of the following are legal identifiers?

XxXx

With regard to default arguments:

Zero or more call-by-value parameters in a function may have default arguments

A semicolon (usually) goes after which of these?

a function header to make it a function declaration

A formal parameter is ___________ .

a local variable that is initialized to the value of its corresponding argument

An r-value (is)

anything that can be placed on the right hand side of the assignment operator

A call by reference parameter is indicated by __________ .

appending the & sign to the paramater data type in the function declaration

C++ predefined functions...

are usually provided in libraries

A void function can have

as many arguments as there parameters

A l-value is

assigned a value

Which of the following does the C++ language not support?

automatic garbage collection

When your program is run the main function is __________ .

automatically called and may call other functions

The following contain several #include directives that may have problems. Which does not have one or more problems?

c) #include "MyStream.h";

The two basic types of parameters are __________ and ___________ .

call-by-value, call-by-reference

One way to write a good function declaration comment is to break it down into a precondition and a postcondition. The precondition ________, and the postcondition __________ .

clearly states what is assumed to be true when the function is called, describes the effect of the function call

Here is a small program. Which of the statements about the variables is correct? #include const double NUM = 2.9345358; double num = 3; double numTimes(int x); int main( ) {using namespace std; int value; cout << "Enter a value, I'll multiply it by " << NUM << endl; cin >> value; cout << "You entered " << value << " NUM times this is " << numTimes(value) << endl; return 0;} double numTimes(int x){double d; d = NUM * x; return d;}

d is a local variable in the numTimes function

Which of the following loop statements is guaranteed to iterate the body of the loop at least once?

do body while(control);

In the case of a void function the return statement ___________ any expression for a value returned and any return statement _________ .

doesn't need, simply ends the function call

The comma operator is a way of ________________

evaluating a list of expressions and returning the value of the last expression

In c++ there are three abs (absolute value) functions. If you want to produce a value of type long you should use labs, if you want to produce a value of type int use abs, and if you want to produce a value of type double use _______ .

fabs

fabs is the C++ abbreviation for ___________ .

floating point absolute value

Which control construct repeats a sequence of statements zero or more times?

for statement

A __________ describes how the function computes the value it returns.

function definition

Giving two or more function definitions the function name is called ________ .

function overloading

A definition of a variable outside any function is called a

global variable definition

A function definition consists of a function ______ followed by a function _____.

header, body

Suppose you have the following array declaration in a program. int yourArray[5]; Further suppose that in the implementation of C++ you are using an int that requires 4 bytes. i) When your program runs, how much memory is required for this array? ii) Suppose further that your array starts at memory location decimal 100. What will be the address of yourArray[3]? iii) If you wrote to the (illegal) index 7 position in yourArray to what address would this clobber?

i) The array takes 20 bytes, ii) yourArray[3] will be an int located at Address 112 iii) writing to yourArray[7] will clobber an int starting at location 128

It true that an assertion

if converted to a Boolean statement, may be used with the library assert macro to test whether the assertion is satisfied at that point in the code.

Default arguments should simply be listed _____________ .

in the function declaration

If your program uses a predefined function from some library, then it must contain an ________ statement that names that ________ .

include directive, library

Usually all you need to do to use a library is to place an _______ directive and a _______ directive for that library in the file with your program.

include, using

Before a variable in C++ is used, it must be

initialized

Which of the following are legal definitions with initializations? (Consider each line to be in a different scope so there is no multiple definition of identifiers.)

int limit = 19;

A global named constant _________________ .

is declared at the beginning of the program and outside the body of any function.

An enumeration type

is type whose values are defined by a list of constants of type int.

Consider the following function and code segment. void One( int first, int & second ) { first = 17; second = first + 1; } int main() {// other code ... int j = 4;int k = 3; One(j, k) ; // other code .. } After the call to One(j, k); what are the values of j and k?

j == 4, k == 18

A return statement consists of a ________ followed by an _________ .

keyword 'return', expression

The variables used in any particular function are called ________ .

local variables

The main function in your program ____________ .

needs a return for the sake of portability and should always be of type int

A switch statement must have

none of the above

The primary difference between an argument and a parameter is that an argument is __________ .

normally the value passed into the formal parameter upon the function call

In C++, a variable that has been defined but not initialized may

not be used as an r-value

It is not true that a void function

performs some action and returns a value

Given the following include directive (to get the declaration for the pow function from the math library): #include Now make these declarations: double base = 2,exponent = 3,power = 4; Which of the following is a correct invocation for the pow function?

power = pow(base, exponent);

C++ functions are typically either _______ or _________ .

predefined, programmer defined

Which of the following names might give the human reader some hint about the data that is stored in them?

principal, interest, payment

Which of the following are not names of a C++ library function:

random

Which of the following types are not built into the C++ language:

real

A void function performs some action but does not _________ and is _________ .

return a value, terminated with a semicolon

Which of the following control structures requires curly braces?

switch

Which of the following overloadings will be invoked by this call?g(1.0,2.0);

the compiler cannot decide which of these to use

The primary difference between a function declaration and a function definition is that ___________ .

the function definition must always have the formal parameter names

The sqrt function...

the return type is double.

In a switch statement, when a break statement is encountered, an immediate transfer of control is made to

the statement beyond the end of the switch statement.

An assignment of the value of a conditional expression to a variable (x =y?z:w;) can never be replaced by

these are all correct and may be used to replace the variable statement given.

Concerning return statements that functions can have:

void functions may terminate using a return; statement without an argument, or they may have no return statement at all, terminating by falling off the end of the function block.

Which of the following function declarations with default arguments are correct?

void g(int length, int width, int height = 1);

Which of the following overloadings will be invoked by this call?g(1,2)

void g(int value, int count);

Which of the following is a C++ keyword?

while

If the following code fragment is executed in an otherwise complete and correct program, which expression will be executed? x = 0; if (x = 12) yes_statement; else no_statement;

x=12 is illegal in the Boolean expression of an if statement.

If you put assert macros throughout your code,

you can define NDEBUG for the compile session by using an IDE menu option or you can use the compiler command line option -DNDEBUG


Set pelajaran terkait

QUIZ 8 MS2 Chapter 14-18 Drugs Used for Parkinsons and SEIzure Dis0rder

View Set

Brunner Chapter 68 Neurologic Trauma Questions

View Set

CITI COI, CITI RCR, CITI social and behavioral research, clinical ethics exam 1

View Set

MS3, Exam 2: Ch 34 (up to p. 750)

View Set

Chapter 3: Properties of Numbers

View Set