CSCI Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Given the following definition: int values [5] = {1,2,3}; What will the following statement display? std::cout << values[3] << std::endl;

0

Read the following program. #include <iostream> void ClaculateSum (int sum) { sum = 0; for (int i = 0; i < 5; i++) sum +=i; } int main(){ int sum = 0; CalculateSum(sum); std::cout << sum << std::end; } What is the printed value at end of execution?

0

The first accessible index in the array is ____?

0

Given the following array definition: int scores[10]; How many elements can the array contain?

10

Given the following array definition: double my_array[20]; How much memory in bytes will this array use?

160 bytes

Read the function definition: void myFunction(int value) { if (value !=0) { cout << "Hello" << endl; } else { return; } } The return statement causes an error when trying to compile. (T or F)

False

The following statement will correctly output the entire contents of the integer array called numbers: std::cout << numbers << std::endl; (T o F)

False

The functions defined in the program are value returning functions. (T or F)

False

The size of an array can be declared with any numberic value including floating point numbers. For example, both of the following are acceptable: (T o F) int my_array[25]; float an_array[10.5];

False

Assume that in_file has been defines as a file object you can read from. int value; in_file >> value; The statements above would create a variable called value and repeatedly read from the file until no values were left in it.

False.

Assume the out_file has been defined as a file object you can write to. outFile << "Hello" << "World" << 10; The statement above would output each literal to the file on separate lines.

False.

The following array declaration would create an array of size 10 and initialize all values in the array to 0. (T o F) int numbers[10];

False.

When you call an ifstream object's open function, the file will be erased if it already exists.

False.

In the following function definition (line numbers provided for reference: 1 void B(int a, int b){ 2 b + a+3; 3 cout << a << " " << b << endl; 4 } Line 1 in the definition above is referred to as the ____?

Function Header

No bounds checking

The lack of validation in C++ to ensure that array subscripts are within the bounds of the array

A function can be re-used multiple times throughout your program. (T or F)

True

A function must be declared before it is used. (T or F)

True

A local variable for one function cannot be accessed in another function by name and must be passed as an argument while calling the other function. (T or F)

True

An array always passes by reference into a function. This means the function can alter the original array unless we define our function to treat the array as constant.

True

Arrays can be passed to functions (T o F)

True

C++ allows you to have arrays with more than one dimension (T or F)

True

C++ allows you to have multiple functions with the same name provided their function signatures are different. (T or F)

True

C++ allows you to partially initialize an array.

True

In C++ the name (identifier) of an array is really the address of the beginning/first element. (T o F)

True

In memory, all elements of an array are guaranteed to be stored contiguously that is to say each element of an array in memory is sequential in the address space and not randomly placed all over memory.

True

Many functions may have local variables with the same name as other function's local variables. (T or F)

True

Parenthesis are Required to enclose the logical expression in an if statement. (T o F)

True

The following statement would result in [2,3,4,5] being placed in an array of size 4 because the compiler can determine how large it should be based on the initialization syntax. int my array[]= {2,3,4,5,6};

True

You cannot use the assignment operator (=) to copy the contents of an entire array into another array. For example, the following would not work assuming arr1 and arr2 have both been properly declared, are the same size, and arr1 had values in it. (T o F) arr2 = arr1;

True

You should always close a file when you are done with it.

True

When you call an ofstream object's function, the file will be erased if it already exists.

True.

You can use the same tools with files that you have used so far with std::can(>>,get line(),get() ), if the file has been opened correctly such that you can read from it.

True.

Parallel arrays

Two or more arrays that contain related data, with elements at the same subscript being related

File stream

Type of stream used for input from or output to a file

Subscript

Unique index assigned to each element in an array

Given the following array definition: int values[]= {2,6,10,14}; What would the following output statement display? std::court << values[-1] << std::endl;

Unknown, the program will compile but -1 is not within the bounds so we can't gurantee anything.

Removing vector elements

Using the pop_back member function to remove the last element from a vector, or the clear member function to remove all elements

Adding elements to a vector

Using the push_back member function to add elements to a vector

Range-based for loop with a vector

Using the range-based for loop to iterate through each element in a vector

Sentinel

Value in a list of values that indicates the end of data

Consider the following function definition: ___ getGrade (double score) { if (score >= 90.0) return 'A'; else if (score >= 80.0) return 'B': else if (score >= 60) return 'D'; else return 'F'; What should the return type of the function be?

char

How many bytes of memory does the following array declaration use? char alphabet[26];

26

The following statement would result in what values being in the array? int myarry[5] = {3};

3 0 0 0 0

Use the program below to answer the following questions. (Line numbers included for reference): 1. #include <iostream> 2- 3. void One (int&); 4. void Two (int); 5. int main () 6. { 7. int b = 3; 8. int a = 6; 9. std::cout << a << " " << b << std::endl; 10- One(b); 11- std::cout << a << " " << b << std::endl: 12- Two (b); 13. std:: cout << a << " " << b << std::endl; 14- return 0; 15. } 16. void One (int &b) 17. { 18. int a = b*2 19.b++; 20. std:: cout << a << " " << b << std::endl; 21. return; 22. } 23. void Two(int a) 24-{ 25. int b = a + 4; 26. a++ 27. std:: cout << a << " " << b << std::endl; 28. return; return; 29. } What is the output of the program?

6 3 6 4 6 4 5 8 6 4

Vector

A data type in the Standard Template Library (STL) that can hold values of any type and automatically adjusts its size as needed

Running total

Accumulated sum of numbers from each repetition of a loop

Other useful member functions

Additional member functions of vectors, such as at, capacity, reverse, resize, and swap

Two-dimensional array

An array with two dimensions, represented as rows and columns

Off-by-one error

An error that occurs when array subscripts are off by one, often caused by starting subscripts at 1 instead of 0

If you try to open a file to read from it, and the file does not exist, the program will fail to compile.

False

Read the following program: #include <iostream> void CalculateSum (int sum) { sum = 0; for (int i = 0; i <5; i++) sum += i; } int main(){ int sum = 0; CalculateSum(sum); std::cout << sum << std::endl; } The bolded variable sum is best referred to as an ___ when it is used input in the CalculateSum function call.

Argument

Read the following code: std::string name_one = "BROCK" std::string name_two = "BRANDON" if (name_one > name_two) { std::cout << "BROCK is greater than BRANDON\n"; } else { std::cout << "BRANDON is greater than BROCK\n"; } What would the output be?

BROCK is greater than BRANDON

In C++ it is possible for a function to return multiple values in a single return statement.

False

Loop

Control structure that causes a statement or statements to repeat

A file can only be read line by line in C++

False

A statement in one function can directly access a local variable in another function by name. (T or F)

False

An array cannot be initialized when it is defined. You must use a loop to initialize the elements. (T o F)

False

Use the program below to answer the following questions. (Line numbers included for reference): 1. #include <iostream> 2- 3. void One (int&); 4. void Two (int); 5. int main () 6. { 7. int b = 3; 8. int a = 6; 9. std::cout << a << " " << b << std::endl; 10- One(b); 11- std::cout << a << " " << b << std::endl: 12- Two (b); 13. std:: cout << a << " " << b << std::endl; 14- return 0; 15. } 16. void One (int &b) 17. { 18. int a = b*2 19.b++; 20. std:: cout << a << " " << b << std::endl; 21. return; 22. } 23. void Two(int a) 24-{ 25. int b = a + 4; 26. a++ 27. std:: cout << a << " " << b << std::endl; 28. return; return; 29. } Lines 3 & 4 contain...?

Function prototypes

A variable that is accessible by every function in a program, including main, is called what?

Global variable

Partial array initialization

Initializing an array with fewer initial values than its size declarator, resulting in the remaining elements being set to 0

Read the following code snippet (assume that my_file has been created and opened correctly such that it can be written to) my_file « "John" << " " << "D" << " " << "Doe"; std:: cout << std: :endl; my_file « "Sally << " " << "S" «< " " « "Sandy" << std:: cout << std::endl; What is the output?

John D Doe Sally S Sandy

In the function below, the variable csquared is known as what kind of variable? double pythag( double a, double b) { double csquared = pow(a,2) + pow(b,2); return csquared; }

Local

Nested loop

Loop inside the body of another loop

Infinite loop

Loop that repeats an infinite number of times

While loop

Loop that repeats as long as a certain condition exists

Pretest loop

Loop that tests its test expression before each iteration

Adjacent memory locations

Memory locations where values in an array are stored

Size declarator

Named constant used to specify the size of an array

Array size

Number of elements in an array

Open

Operation that creates a link between a file name and a file stream object

Close

Operation that ends the link between a file name and a file stream object

Increment operator

Operator that adds one to a variable

Prefix

Operator that increments or decrements, then returns the value of the variable

Postfix

Operator that returns the value of the variable, then increments or decrements

Decrement operator

Operator that subtracts one from a variable

In the function below, the bolded variables are known as what kind of variable? int somefunction (int value1, int value 2, int value3) {

Parameters

Array as function argument

Passing an array to a function by using the array name as the argument

Suppose the file numbers.dat does not exist. If the following snippet was in your main function, what would be the output? std::ifstream my_file; my_file.open("numbers.dat"); if ( !my_file) { std::cout << "Potato" << std::endl; } else { std::cout << "Success" << std::endl; }

Potato

Input validation

Process of inspecting data that is given to the program as input and determining whether it is valid

Use the program below to answer the following questions. (Line numbers included for reference): 1. #include <iostream> 2- 3. void One (int&); 4. void Two (int); 5. int main () 6. { 7. int b = 3; 8. int a = 6; 9. std::cout << a << " " << b << std::endl; 10- One(b); 11- std::cout << a << " " << b << std::endl: 12- Two (b); 13. std:: cout << a << " " << b << std::endl; 14- return 0; 15. } 16. void One (int &b) 17. { 18. int a = b*2 19.b++; 20. std:: cout << a << " " << b << std::endl; 21. return; 22. } 23. void Two(int a) 24-{ 25. int b = a + 4; 26. a++ 27. std:: cout << a << " " << b << std::endl; 28. return; return; 29. } On line 16, the variable b is defined as a what?

Reference paremeter

The last accessible index in the array is _____?

Size of the array -1

Range-based for loop

Specialized version of the for loop that iterates through each element in an array

Continue

Statement that goes to the end of a loop and prepares for the next repetition

Break

Statement that terminates the execution of a loop

Suppose the file numbers.dat does not exist. If the following snippet was in your main function, what would be the output? std: :ofstream my_file; my_file. open ("numbers. dat") ; if ( !my_file ){ std: : cout << "Potato" << std::endl; } else { std: :cout << "Success" << std::endl; }

Success

Use the program below to answer the following questions. (Line numbers included for reference): 1. #include <iostream> 2- 3. void One (int&); 4. void Two (int); 5. int main () 6. { 7. int b = 3; 8. int a = 6; 9. std::cout << a << " " << b << std::endl; 10- One(b); 11- std::cout << a << " " << b << std::endl: 12- Two (b); 13. std:: cout << a << " " << b << std::endl; 14- return 0; 15. } 16. void One (int &b) 17. { 18. int a = b*2 19.b++; 20. std:: cout << a << " " << b << std::endl; 21. return; 22. } 23. void Two(int a) 24-{ 25. int b = a + 4; 26. a++ 27. std:: cout << a << " " << b << std::endl; 28. return; return; 29. } On line 23, the variable 'a' is defined as a what?

Value parameter

Array

Variable that can store multiple values of the same type

Counter

Variable that is incremented or decremented each time a loop repeats

Read the following function definition: void print_initials (string first. string last) { char initial = first[0]; char initial2 = last[0]; cout <<initial << ". " << initial2 << ". " << endl: } What type of function is the definition for?

Void Function

Read the following function definition: void max_of_two(int a, int b) { What type of function is the definition for?

Void function

Which of the following operators allows you to access individual elements of an array?

[]

Read the following function: ___ isEven (int number) { if (number % 2 == 0) { return true; } return false; } What should the return type of this function be?

bool

In C++, you can compare the contents of two arrays to determine if they are equal using the == operator. For example, the following c++ code would result in being "TRUE": int arr[5] = {2,3,4,5,6} int arr2[5] = {2,3,4,5,6} if (arr== arr2) { std::cout << "TRUE" << std::endl; } else { std::cout << "False" << std::endl; }

false

Which of the following For Loop headers will correctly iterate through all valid indexes of the array?

for (int i = 0; i < 15; i++)

Which of the following would correctly access the first element of an array called grades?

grades[0]

Which of the following statements correctly closes the file object my_file that has been opened and connected to the file data.txt?

my_file.close();

Given the following statements: std::ifstream my_file; string filename = "somefile.dat"; Which of the following will open the file correctly?

my_file.open(filename);

To write data to a file, you need to create an instance (variable) of the type_______

ofstream

This statement causes a function to end immediately and optionally send a value back to the part of the program that called the function.

return

To read data from a file, you need to create an instance (variable) of the type -------

std::ifstream


Ensembles d'études connexes

Financial & Managerial Accounting Midterm

View Set

DISCOVERING MEDIA COMMUNICATION ETEXT QUIZZES EXAM 1 Korpi

View Set

Abeka 4th Grade, Health Test 2, (Ch. 4-5)

View Set

Business 263 Final Exam Study Guide

View Set