CS 271 Exam 1 review

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

Which of the following statements about the inclusion of <stdio.h> is false?a) It is required.b) This header file contains information and declarations used by thecompiler when compiling standard input/output library functionssuch as printf.c) This header file contains information that helps the compiler de-termine if calls to library functions have been made correctly.d) This header helps locate bugs in your program at compile time, ra-ther than at execution time (when errors are usually more costly tocorrect).

a) It is required.

Every variable has all the attributes below, except a) name b)value c)alias d)type

alias

A valid reason for building programs out of functions is: (a) that the divide-and-conquer approach facilitates program con- struction (b) that pre-existing functions can be used to create new programs (c) the avoidance of code iteration within a program (d) all of the above

all of the above

Using standard library functions can be more efficient because __________.

all of the above

An example of a unary operator is

an increment operator

What is produced by a for statement with a correct body and with the following header for (i = 20; i >= 2; i += 2)

an infinite loop

How many times will the following program print hello? i = 1; while (i <= 10) { puts("hello"); }

an infinite number of times

Lines beginning with a # are processed

at preprocessor time.

Which statement is true? a) Operator || has a higher precedence than operator &&. b) In expressions involving operator ||, making the condition that is most likely to be false the leftmost condition can often reduce execu- tion time. c) The logical negation operator is a binary operator. d) In expressions using operator &&, making the condition that is most likely to be false the leftmost condition can often reduce execution time.

b) In expressions involving operator ||, making the condition that is most likely to be false the leftmost condition can often reduce execu- tion time

Which statement about precedence is false? a) Parentheses may be used to force the order of evaluation to occur in any sequence desired by the programmer. b) Nested, or embedded parentheses are evaluated last. c) Multiplication has a higher precedence than addition. d) Subtraction has a lower precedence than division.

b) Nested, or embedded parentheses are evaluated last.

The smallest data item in a computer, called a ________, can assume the value 0 or the value 1.

bit

Which of the following is not specified by the following code segment: for (c = 1; c <= 10; c++) a) Names the control variable.b) Defines the control variable to be an integer.c) Specifies the sentinel value.d) Sets the initial value of the control variable to 10

body statement of the loop

If the loop-continuation condition in a for statement is initially false, ________.

both a and b

C's if statement executes the statement inside its body if a specified __________ is __________.

condition, true

A ________ is an electronic collection of data that's organized for easy access and manipulation.

database

The function prototype double mySqrt(int x);

defines a function called mySqrt which takes an integer as an argument and returns a double

Which of the following is an iteration statement?

do...while

Which of the following code segments does not contain any errors? (a) void printnum (int x) { print("%i", x); return x; } (b) int cube(int s) { int s; return (s * s * s); } (c) double triple(float n) { return (3 * n); } (d) double circumference (int r) return (3.14 * 2 * r);

double triple(float n) { return (3 * n); }

The most concise notation to use to define function parameters x and y as double is __________.

double x, double y

A ________ is a group of characters or bytes that conveys meaning.

fields

In the line int main(), the parentheses indicate that main is a program building block called

function

As used in int square(int);, int is not a(n) __________

function prototype

When programming in C you'll typically use all of the following building blocks except __________.

functions provided by ANSI / ISO

Each standard library has a corresponding __________.

header file

Which of the following statements is false? a) C is case sensitive. b) Uppercase and lowercase letters are different in C. c) identifier and IdEnTiFiEr are identical identifiers in C. d) Identifiers can be of any length

identifier and IdEnTiFiEr are identical identifiers in C.

The __________ selection statement performs an action if a condition is true and performs a different action if the condition is false.

if...else

Functions are __________ by a function call.

invoked

A function prototype can always be omitted when a function ________.

is defines before it is first invoked

Recursion is memory-intensive because ________.

it requires large data values

Which of the following is not an indication that a function may be too complex?

its name is a clear reflection of its function

Which of the following must every C program have?

main

Using an incorrect relational operator or using an incorrect final value of a loop counter in the condition of a while or for statement is a frequent cause of __________ errors.

off-by-one

If grade has the value of 60 what will the following code print if (grade >= 60) {puts("Passed");}

passed

The program segment int counter = 1; do { printf("%i ", counter); } while (++counter <= 10); will ________.

print the numbers 1 through 10

int square(int); is an example of a function __________.

prototype

Which statement prints "hi" on the screen? a) puts("hi");(b) put "hi";(c) puts "hi";(d) none of the above

puts("hi");

When arguments are passed by __________, the caller allows the called function to modify the original variable's value.

reference

When a called function completes its task, it normally:

returns to the calling function

A recursive function is a function that ________.

scope

Every statement in C must end with a

semicolon (;)

Normally, statements in a program are executed one after the other in the order in which they are written. This is called __________ execution.

sequential

The following line is most properly an example of a __________. puts( "Welcome to C!" );

statement

The OR (||) operator

stops evaluation upon finding one condition to be true

Placing a semicolon after the right parenthesis enclosing the parameter list of a function definition is a __________ error.

syntax

If a do...while statement is used:

the body of the loop will execute at least once.

Which statement regarding the switch statement is false? a) It's appropriate for algorithms that contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may assume. b) The default case is required. c) The default case must be at the bottom of the switch after all the non-default cases. d) Many cases may all invoke the same code.

the default case is required.

When a number gets assigned to a variable that already has a value __________.

the new number overwrites the previous value at that memory location

An uninitialized variable contains ________.

the value last stored in the memory location reserved for that var- iable

Arrays are data structures consisting of related data items of the same __________.

type

The for statement header for (i = 1; i < 100; ++i) performs the body of the loop for:

values of the control variable from 1 to 100 in increments of 1.

A(n) __________ is a location in the computer's memory where a value can be stored for use by a program.

variable

If x = 3, which of the following sets x to 7? (a) x *= 4;(b) x += 4;(c) x =+ 4;(d) x + 4 = x;

x += 4;

The condition num != 65 cannot be replaced by:

!(num-65)

The address operator is

&

Which of the following statements is true in secure C programming? (a) You should avoid using printf to display a single string argu- ment. (b) You should always use printf to display a single string argu- ment. (c) You should always use puts to display a single string argument. (d) None of the above.

(b) You should always use printf to display a single string argu- ment..

If the value of counter is 2 or more, then the statement while (--counter >= 1) { printf("%s\n", counter % 2 ? "even" : "odd"); } can not be rewritten as: (a) while (--counter >= 1) { if (counter % 2) { puts("even"); } else { puts("odd"); } } (b) while (counter >= 1) { if (counter % 2) { puts("even"); } else { puts("odd"); } } --counter; (c) while (counter >= 2) { counter--; if (counter % 2) { puts("even"); } else { puts("odd"); } } (d) Chapter 4 Multiple Choice questions do { printf("%s\n", counter % 2 ? "odd" : "even"); --counter; } while (counter >= 2);

(b) while (counter >= 1) { if (counter % 2) { puts("even"); } else { puts("odd"); } } --counter;

A function prototype does not have to ________.

(c) agree with the function definition

a * ( b + c ) may also be written in C as

(c) d) a b + a c

Which of the following statements correctly prints "Passed" if the student's grade is greater than or equal to 60 and "Failed" if the student's grade is less than 60? a) printf("%s\n", grade >= 60 : "Passed" : "Failed");b) grade >= 60 : puts("Passed ") ? puts("Failed ");c) printf("%s\n", grade >= 60 ? "Passed" : "Failed");d) grade >= 60 ? puts("Passed ") ? puts("Failed ");

(c) printf("%s\n", grade >= 60 ? "Passed" : "Failed");

Which of the following is not a valid integer value? (a) -3(b) 0(c) 2134859(d) 1.1

1.1

Which operation will find the remainder when 15 is divided by 6?

15 % 6

Evaluate the expression 3 * 4 % 6 + 4 * 5

20

What is the value of p after this while loop completes its execution?p = 2; while (p < 2000) { p = 2 * p; }

2048

What value does function mystery return when called with a value of 4? int mystery (int number){if (number <= 1) {return 1;}else {return number * mystery(number - 1);}}

24

Which statement about C arithmetic is false? a) 6 / 3 yields 2 b) 5 / 2 yields 2.5 c) 7 % 3 yields 1 d) 6 % 3 yields 0

5 / 2 yields 2.5

Consider the following code, assuming that x is an integer variable with an initial value of 12: if (x = 6) { printf("%i", x); } What is the output?

6

If a = 7.0, b = 7.0 and c = 6.0, then what is printed by printf("%.2f", sqrt(a + b * c));

7.00

What is the highest value assumed by the loop counter in a correct for statement with the following header? for (i = 7; i <= 72; i += 7)

77

What is the final value of x after performing the following operations? int x = 3; double y = 6; double z = 2; y = x / z; x = 5.5 * y;

8

A programmer writes a for statement to count from 1 to 10 and explicitly mentions the 1 and the 10 in the for "header." Which relational operator would probably be used in the loop-continuation test?

<=

The ________ sign is also known as the ________ operator.

=, assignment

Which of the following is an equality operator?

==

Which statement is false? (a) Classes are reusable software components. (b) A class is to an object as a blueprint is to a house. (c) Performing a task in a program requires a method. (d) A class is an instance of its object

A class is an instance of its object

Which of the following statements is true? (a) A counter variable that stores only non-negative numbers should be declared as an unsigned integral type. (b) A counter variables that stores only non-negative numbers should be declared as a signed integral type. (c) The type of a counter does not matter (d) None of the above.

A counter variable that stores only non-negative numbers should be declared as an unsigned integral type.

Which of the following statements is true? (a) If the return value of function scanf matches the number of items that should have been input, then all the inputs are valid. (b) Even if a scanf operates successfully, the values read might still be invalid. (c) When a program expects to receive input values in a specific range, you should peform range checking on the inputs to ensure that the values received are indeed in that range (e.g., in a program that ex- pects grades in the range 0-100, you should check that every grade is in that range). (d) Both (b) and (c).

Both (b) and (c).

In C, the condition 4 > y > 1

Does not evaluate correctly and should be replaced by ( 4 > y && y > 1 ).

Which statement is false? a) Each function should be limited to performing a single, well-defined task. b) If you cannot choose a concise name that expresses what a function does, it's possible that the function is attempting to perform too many diverse tasks. c) Every function should be broken into smaller functions. d) A function's parameters are local variables.

Every function should be broken into smaller functions

Which is not a motivation for "functionalizing" a program?

Execution performance—functionalized programs run faster.

Which statement is false? a) In algebra, we write ab to multiply a times b. b) In C, we write ab to multiply a times b. c) In C, the remainder operator is %. d) In C, integer division yields an integer result.

In C, we write ab to multiply a times b.

Which of the following is not an advantage of object-oriented programming? a) Software is more reusable.(b) Software is more understandable, correct and modify.(c) Using a modular, object-oriented design-and-implementation approach canmake software-development groups much more productive.(d) None of the above—these are all advantages of object-oriented programming

None of the above—these are all advantages of object-oriented programming.

________ models software in terms similar to those that people use to describe real-world objects.

Object-oriented design

Which statement is false? a) in the statement sum = integer1 + integer2; both = and + are binary operators. b) The statement in part a) is an example of an assignment statement. c) The spaces around each of the binary operators in the statement of part a) are required. d) In part a), the = operator's two operands are sum and the value of the expression integer1 + integer2.

The spaces around each of the binary operators in the statement of part a) are required.

Which statement is false? a) Whitespace characters such as tabs, newlines and spaces are gen- erally ignored by the C compiler. b) The statements in an if statement must be indented. c) Placing a blank line before and after every control structure can improve program readability. d) There can be (but should not be) more than one statement per line.

The statements in an if statement must be indented.

What is wrong with the following loop? While (sum <= 1000) {sum = sum + 30;}

While should be while

Which of the following statements is true? (a) Function scanf does not return a value. (b) You should never check the return value of function scanf. (c) You should check the return value of function scanf to ensure that the value it returns matches the number of items that should have been input. (d) None of the above.

You should check the return value of function scanf to ensure that the value it returns matches the number of items that should have been input.

Which of the following is false? a) the first element of an array is the zeroth (b) the last element of an array has index array size - 1 (c) the position number contained within square brackets is called a subscript (d) a subscript cannot be an expression.

a subscript cannot be an expression.

A programmer intentionally creates a for-loop with the following for header: for (; ;) The programmer's intent was most likely to create:

a syntax error

srand:

can use time as an automatically input seed value

Which of the following will not produce a syntax error? (a) Omitting a return type from a function definition if the functionprototype specifies a return type other than int(b) Returning a value from a function defined as void(c) Defining a function parameter again inside a function(d) Using the same names for arguments passed to a function and thecorresponding parameters in the function definition

Returning a value from a function defined as void

In the context of counter-controlled iteration, which of the following is not accomplished by the control-variable initialization statement?

Specifies the sentinel value.

Which statement is generally false? a) Statements preceding a for and statements in the body of a for should typically be merged into the for header. b) Limit the size of control statement headers to a single line, if possi- ble. c) Initialization of a for loop control variable can occur before the for loop executes and not in the loop itself. d) The increment portion of a for header can be a decrement.

Statements preceding a for and statements in the body of a for should typically be merged into the for header.

Which statement is true? a) The conversion specifier %7.2f prints a floating-point value with a field width of 10 positions. b) The conversion specifier %7.2f prints a floating-point value with 7 positions to the left of the decimal point. c) The conversion specifier %7.2f prints a floating-point value with 5 positions to the left of the decimal point. d) The conversion specifier %7.2f prints a floating-point value with 4 positions to the left of the decimal point.

The conversion specifier %7.2f prints a floating-point value with 7 positions to the left of the decimal point.

Which statement about a correct for statement with an initialization expression, a loop-continuation test, an increment expression and a loop body is false?

The initialization is performed each time through the loop

Which statement is true? a) The do ... while iteration statement is an alternate notation for the while iteration statement; these statements function identically. b) The do ... while iteration statement tests the loop-continuation condition before the loop body is performed. c) The loop body of a correct do ... while iteration statement is al- ways executed at least once. d) The braces delineating the body of a do ... while statement are al- ways required.

The loop body of a correct do ... while iteration statement is always executed at least once.

Which of the following is an incorrect expression to increment c by 1 in the increment expression of a for "header?" a) c += 1 b) ++c c) c++ d) c + 1 = c

c + 1 = c

Which of the following will not increment variable c by one? (a) c + 1; (b) c++; (c) ++c; (d) c += 1;

c + 1;

Which assignment expression is equivalent to c = c / 2 ?

c /= 2

Which statement is false? a) Any expression in C that produces a value can be used in the deci- sion portion of any control statement. b) When tested for truth or falsity, an expression that produces a nonzero value is treated as true. c) Assignments in C produce a value, namely the value that the left-hand side of the assignment had prior to the assignment. d) Operator == is for comparisons; operator = is for assignment.

c) Assignments in C produce a value, namely the value that the left-hand side of the assignment had prior to the assignment.

Which of the following is false? a) Each variable being input in a scanf statement is generally pre- ceded by an &. b) Each variable being output in a printf statement is generally not preceded by an &. c) In a printf statement, the comma that separates the format con- trol string from the expressions to be printed is placed inside the for- mat control string. d) Calculations can be performed inside printf statements.

c) In a printf statement, the comma that separates the format con- trol string from the expressions to be printed is placed inside the for- mat control string

Which is not an ANSI standard library function?

c) scan


Set pelajaran terkait

Davis Advantage Chp 10 Older Adult

View Set

Chapter 10: Documentation and Communication

View Set

Exam 1 Reading questions RelA250

View Set

Med Surg 1 Chapter 31 (Respiratory Disorders)

View Set

Pharmacology - Prep U - Chapter 41

View Set