Computer science chapter 5 and 6

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following function: int strange (int x, int y) { if (x > y) return x + y; else return x - y; } What would be output/displayed by the the following statement? cout << strange(4, 5) << endl;

-1

40. What is output/displayed by the following code (which uses an automatic variable in the function)? void counter( ) { int count = 0; cout << count++ << ' '; } void main ( ) { for(int i = 0; i < 4; ++i) counter( ); }

0 0 0 0

What is stored in two-dimensional array matrix after the following code executes? int matrix[3][2]; int j, k; for (j = 0; j < 3; ++j) for (k = 0; k < 2; ++k) matrix[j][k] = j + k;

0 1 1 2 2 3

What is output/displayed by the following code (which uses a static variable in the function)? void counter( ) { static int count = 0; cout << count++ << ' '; } void main ( ) { for(int i = 0; i < 4; ++i) counter( ); }

0 1 2 3

35. #include <iostream> using namespace std; // function prototypes void fun1( int& ) ; void fun2( int ); int x = 1; void main() { int x = 2; fun1(x); cout << x << endl; fun2(x); cout << x << endl; } void fun1(int& y) { y = 5; cout << x << endl; } void fun2(int x) { x = 15; }

1 5 5

What is output/displayed by the following program? #include <iostream> using namespace std; // function prototypes void one(int, int&); void two(int&, int); void main() { int u = 1, v = 2; one(u, v); cout << u << ' ' << v << endl; two(u, v); cout << u << ' ' << v << endl; } void one(int x, int& y) { int a; a = x; x = y; y = a; } void two(int&s, int t) {int b; b = s - t; s = t + b + 2; t = 4 * b; }

1 1 3 1

What is displayed by the following code? vector<int> vect; vect.push_back(10); vect.push_back(20); vect.push_back(30); vect.push_back(40); vect.push_back(50); vect.pop_back(); vect.pop_back(); for(int i = 0; i < vect.size(); ++i) cout << vect.at(i) << " ";

10 20 30

What is the value of alpha[3] after the following code executes? int alpha[5]; int j; alpha[0] = 5; for (j = 1; j < 5; ++j) { if (j % 2 == 0) alpha[j] = alpha[j - 1] + 2; else alpha[j - 1] + 3; } (Wrong)

13

Consider the following declaration of a character array. What is the length of the longest c-string that can be stored in it? char str[16];

15

What is displayed by the following code? vector<int> vect(4); vect.at(0) = 1; vect.at(1) = 2; vect.at(2) = 3; vect.at(3) = 4; vect.pop_back(); cout << vect.size() << " " << vect.back();

3 3

Consider the following function: int mystery(int u, int v) { int a; a = u - v; u = a; v = u; return u + v; } What is output/displayed by the following statement? cout << mystery(9, 7) << endl;

4

How many swaps would it take to reverse the following list? 11 22 33 44 55 66 77 88

4

What is displayed by the following code? vector<int> vect(4); vect.at(0) = 1; vect.at(1) = 2; vect.at(2) = 3; vect.at(3) = 4; cout << vect.back();

4

What is output/displayed by the following program? #include <iostream> using namespace std; void fun( int ) ; // function prototype int x = 10; void main() { int x = 2; fun(x); cout << x << endl; } void fun(int x) { x = 5; cout << x << endl; }

5 2

What is output/displayed by the following program? #include <iostream> using namespace std; void fun( int ) ; // function prototype int x = 10; void main() { int x = 2; fun(x); cout << x << endl; } void fun(int& x) { x = 5; cout << x << endl; }

5 5

How many elements could the following 3-dimensional array hold? int arr[4][5][3];

60

Consider the following function: int next (int x) { return x + 1; } What would be output/displayed by the following statement? cout << next( next(5) ) << endl;

7

10. Given the following code, what would be displayed? int list[5] = {1, 5, 3, 10, 15}; for (int index = 0; index < 5; ++index) list[index] = (index + 2) * (index + 2); cout << list[1] << " " << list[2] << " " << list[3] << " " << list[4];

9 16 25 36

The following code correctly swaps the values for x and y. (True or False) int x = 50, y = 100; x = y; y = x;

False

The following would be a valid/legal assignment between str2 and str1. (True or False) char str1[6] = "Hello"; char str2[6]; str2 = str1;

False

15. The following code is valid for assigning/copying vect2 into vect1. (True or False) vector<int> vect1(3); vector<int> vect2(5); vect2.at(0) = 10; vect2.at(1) = 20; vect2.at(2) = 30; vect2.at(3) = 40; vect2.at(4) = 50; vect1 = vect2;

True

Consider the following declaration: vector<int> vect(5) = {0, 1, 2, 3, 4}; The following cout statement would compile without an error and display the value 2. (True or False) cout << vect[2];

True

A variable or value passed in a function call is a(n) _____ parameter. A variable in a function header is a(n) _____ parameter.

actual, formal

Consider the following declaration for integer array alpha. Which statement correctly inputs values (from the keyboard) into all three slots of the array? int alpha[3];

cin >> alpha[0] >> alpha[1] >> alpha[2];

Assuming any necessary declarations not shown have been correctly made, consider the following code: int array[MAX_ROW_SIZE][MAX_COL_SIZE]; for (x = 0; x < MAX_COL_SIZE; ++x) for (y = 0; y < MAX_ROW_SIZE; ++y) array[y][x] = 0; The above array is being processed (initialized) in _____ order.

column-major

Given the following function prototype, which of the following cout statements with a function call would be valid? double testAlpha ( int u, char v, double t );

cout << testAlpha(5, 'A', 2.0);

20. What does the following code do? const int SIZE = 5; int arr[SIZE] = {2, 8, 6, 10, 4}; int var; var = arr[0]; for (int i = 0; i < SIZE; ++i) { if (arr[i] > var) var = arr[i]; } cout << var << endl;

finds and displays the largest value in the array

Suppose that sales is an array of 50 components of type double. Which of the following correctly initializes the array?

for (int j = 0; j <= 49; ++j) sales[j] = 0.00;

Given the following declarations, which loop finds the sum of the fifth row of sale? int j; double sum = 0.00; double sale[10][7];

for (j = 0; j < 7; ++j) sum = sum + sale[4][j];

Which is a valid function call, given the following function header? void fun(int a, int b, double c = 0.0, char ch = 'x')

fun(10, 20, 'z') fun(10, 20);

25. Which of the function prototypes is valid?

int funcTest(int, int, double);

Which of the choices make the following statement false? Actual parameters and their corresponding formal parameters must match with regard to _____.

names

Which statement is true regarding the following function header? void fun(int& one, double two)

one is a reference parameter and two is a value parameter

Two-dimensional arrays are stored in _____ order.

row-major

30. The _____ of an identifier (variable) refers to where in the program that identifier is accessible (recognized).

scope

Using global variables can result in _____.

side effects

5. Consider the following declaration of a character array. Which statement correctly stores "Blue Sky" into array str? char str[15];

strcpy(str, "Blue Sky");

Arrays are automatically passed by reference so no & is needed in the formal parameter list for it . (True of False)

true

Consider the following function heading: void tryMe(int x[ ], int size) and declarations: int list[100]; double score[50]; double gpas[50]; Which function call is valid?

tryMe(list, 100);

A function called accomplish has 3 parameters: u (of type int), v (of type double), and w (of type char). The parameters u and w need to pass their values back out of the function while v only receives a value passed into it from the call. Which would be the valid function heading?

void accomplish(int& u, double v, char& w)


Conjuntos de estudio relacionados

«Биология. Этапы эмбриогенеза- формирование систем органов»

View Set

NUR 310: Complications of Diabetes PowerPoint, Exam 1

View Set

Marketing Chapter 9 Warm-Up and Quiz

View Set

Chapter 1 Mc Graw Hill Physical Science

View Set