Exam 2 Flashcards
(21 cards)
What is the output of the following program?
#include <iostream> using namespace std; void myFunc(); int main() { int var = 100; cout << var << endl; myFunc(); myFunc(); cout << var << endl; return 0; } void myFunc() { int var = 0; cout << var << endl; var= 50; }
100
0
0
100
What is the output of the following program?
#include <iostream> using namespace std; void myFunc(); int main() { int var = 100; cout << var << endl; myFunc(); myFunc(); cout << var << endl; return 0; } void myFunc() { static int var = 0; cout << var << endl; var= 50; }
100
0
50
100
What is the output of the following program?
#include <iostream> using namespace std; void myFunc(); int var = 100; int main() { cout << var << endl; myFunc(); myFunc(); cout << var << endl; return 0; } void myFunc() { static int var = 0; cout << var << endl; var= 50; }
100
0
50
100
What is the output of the following program?
#include <iostream> using namespace std; void myFunc(); int var = 100; //global variable int main() { cout << var << endl; myFunc(); myFunc(); cout << var << endl; return 0; } void myFunc() { int var = 0; cout << var << endl; var= 50; }
100
0
0
100
What will the following codes display?
#include <iostream> using namespace std; void doSomething(int); int main() { int x = 2; cout << x << endl; doSomething(x); doSomething(x); cout << x << endl; return 0; } void doSomething(int x) { int num = 0; x *= 2; cout << x << endl; cout << num << endl; num = 20; }
2
4
0
4
0
2
What will the following codes display?
#include <iostream> using namespace std; void doSomething(int &); int main() { int x = 2; cout << x << endl; doSomething(x); doSomething(x); cout << x << endl; return 0; } void doSomething(int & x) { int num = 0; //local variable x *= 2; cout << x << endl; cout << num << endl; num = 20; }
2
4
0
8
0
8
What will the following code display?
#include <iostream> using namespace std; void doSomething(int); int main() { int x = 2; cout << x << endl; doSomething(x); doSomething(x); cout << x << endl; return 0; } void doSomething(int x) { static int num = 0; x *= 2; cout << x << endl; cout << num << endl; num = 20; }
2
4
0
4
20
2
What will the following code display?
#include <iostream> using namespace std; void doSomething(int&); int main() { int x = 2; cout << x << endl; doSomething(x); doSomething(x); cout << x << endl; return 0; } void doSomething(int& x) { static int num = 0; x *= 2; cout << x << endl; cout << num << endl; num = 20; }
2
4
0
8
20
8
Write the definition of a function named max that receives an int parameter and returns the largest value that it has been called with so far.
So, if you make these calls to max, max(5), max(3), max(12), max(4), the values returned will be(respectively): 5, 5, 12, and 12.
int max(int num) { static int largest = 0; if (num > largest) largest = num; return largest; }
What is the output of the following program?
#include <iostream> using namespace std; void test(int = 0, int = 2, int = 4); int main() { test(); test(4); test(3, 6); test (1, 3, 5); return 0; } void test(int x, int y, int z) { x += 1; y += 2; z += 3; cout << x << " " << y << " " << z << " " << endl; }
1 4 7
5 4 7
4 8 7
2 5 8
Consider the following array
definition: int values[5] = { 4, 7, 6, 8, 2 };
What do the following statements display?
cout << values[4] << endl; cout << (values[2] + values[3]) << endl; cout << ++values[1] << endl;
2
14
8
Look at the following array definition:
int numbers[5] = { 1, 2, 3 };
What value is stored in numbers[2]?
What value is stored in numbers[4]?
3
0
The arrays numberArray1 and numberArray2 have 100 elements.
Write code that copies the values in numberArray1 to numberArray2.
for (int i = 0; i < 100; i++) { numberArray2[i] = numberArray1[i]; }
values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total.
float total = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { total += values[i][j]; } }
After the code shown executes, which of the following statements is true?
int numbers[] = {0, 1, 2, 3, 4};
int *ptr = numbers;
ptr++;
a. ptr will hold the address of numbers [0]
b. ptr will hold the address of the second byte within the element numbers [0]
c. ptr will hold the address of numbers [1]
d. this code will not compile
c
What will the following code output?
int numbers[] = {2, 4, 6, 8, 10}; cout << *(numbers+3) << endl;
8
What will the following code output?
int *numbers = new int[5]; for (int i = 0; i <= 4; i++) *(numbers + i) = i; cout << numbers[2] << endl;
2
What will the following code output?
int number = 22; int *var = &number; cout << *var << endl;
22
What will the following code output?
int number = 22; int *var = &number; cout << var << endl;
the memory address of number
Given the variable initializations
int a[5] = {0, 10, 20, 30, 40};
int k = 3;
int *p = a + 1;
Determine the output from each of the following statements:
a) cout << a[k];
b) cout << *(a+k);
c) cout << *a;
d) cout << a[*a];
e) cout << a[*a + 2];
f) cout << *p;
g) cout << p[0];
h) cout << p[1];
i) cout << ++a[0];
j) cout << a[1]++;
a) 30
b) 30
c) 0
d) 0
e) 20
f) 10
g) 10
h) 20
i) 1
j) 10
Given the following declarations:
int grades[5] = { 100, 90, 95, 93, 85 };
int *ptrGrades = grades,
total = 0;
Write the statements to SUM all the grades by using a for loop and pointer notation
for (int i = 0; i < 5; i++) {
total += *(ptrGrades + i);
}