Exam 2 Flashcards

(21 cards)

1
Q

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; }
A

100
0
0
100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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; }
A

100
0
50
100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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;
}
A

100
0
50
100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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;
}
A

100
0
0
100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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; }
A

2
4
0
4
0
2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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; }
A

2
4
0
8
0
8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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; }
A

2
4
0
4
20
2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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; }
A

2
4
0
8
20
8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A
int max(int num) {
   static int largest = 0; 
   if (num > largest)
      largest = num;
   return largest;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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; }
A

1 4 7
5 4 7
4 8 7
2 5 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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;
A

2
14
8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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]?

A

3
0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The arrays numberArray1 and numberArray2 have 100 elements.

Write code that copies the values in numberArray1 to numberArray2.

A
for (int i = 0; i < 100; i++) {
   numberArray2[i] = numberArray1[i];
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A
float total = 0;

for (int i = 0; i < 10; i++) {  
   for (int j = 0; j < 20; j++) {
      total += values[i][j];
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What will the following code output?

int numbers[] = {2, 4, 6, 8, 10};
cout << *(numbers+3) << endl;
17
Q

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; 
18
Q

What will the following code output?

int number = 22; 
int *var = &number; 
cout << *var << endl;
19
Q

What will the following code output?

int number = 22; 
int *var = &number; 
cout << var << endl;
A

the memory address of number

20
Q

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

a) 30
b) 30
c) 0
d) 0
e) 20
f) 10
g) 10
h) 20
i) 1
j) 10

21
Q

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

A

for (int i = 0; i < 5; i++) {
total += *(ptrGrades + i);
}