Transcript Final Exam

After the execution of the following code, what
is the value of sum?
sum = 0;
num = 10;
if (num > 0)
sum = sum + 10;
else if (num > 5)
sum = num + 15;
30. After the execution of the following code, what
will be the value of num if the input value is
5?
cin >> num;
if (num > 0)
num = num + 10;
else
if (num > 5)
num = num + 15;
Suppose x and y are int variables. Consider
the following statements.
if (x > 5)
y = 1;
else if (x < 5)
{
if (x < 3)
y = 2;
else
y = 3;
}
else
y = 4;
What is the value of y if x = 6?
Suppose x and y are int variables. Consider
the following statements.
if (x > 5)
y = 1;
else if (x < 5)
{
if (x < 3)
y = 2;
else
y = 3;
}
else
y = 4;
What is the value of y if x = 3?
Suppose x and y are int variables. Consider
the following statements.
if (x > 5)
y = 1;
else if (x < 5)
{
if (x < 3)
y = 2;
else
y = 3;
}
else
y = 4;
If the value of y is found to be 2, what is a possible
value of x?
34.
What is the value of alpha after the
following code executes if the input value is 4?
int num;
int alpha = 10;
cin >> num;
switch (num)
{ case 3:
alpha++; break;
case 4:
case 6:
alpha = alpha + 3;
case 8:
alpha = alpha + 4; break;
default:
alpha = alpha + 5;
35.
What is the output of the following
code?
char lastInitial = 'S';
switch (lastInitial)
{ case 'A':
cout << "section 1" <<endl; break;
case 'B':
cout << "section 2" <<endl; break;
case 'D':
cout << "section 3" <<endl; break;
default:
cout << "section 4" <<endl; }
36. What is the output of the following code?
if ( 6 > 8)
{
cout << " ** " << endl ;
cout << "****" << endl;
}
else if (9 == 4)
cout << "***" << endl;
else
cout << "*" << endl;
37. What is the output of the following C++ code?
int x = 55, y = 5;
switch (x % 7)
{ case 0: case 1: y++;
case 2: case 3: y = y + 2;
case 4: break;
case 5: case 6: y = y – 3;}
cout << y << endl;
38. What is the value of x after the following
statements execute?
1, 1, 2, 3, 5, 8, 13, 21, ...
52. What is the output of the following C++ code?
count = 1;
num = 25;
while (count < 25)
{
num = num - 1;
count++;
}
cout << count << " " << num << endl;
54. What is the output of the following C++ code?
What is the value of counter after the
num = 0;
while (num < 5)
{ cout << num << " ";
num = num + 1; }
0 1 2 3 4 5
1 2 3 4 5
following statements execute?
counter = 0;
while (counter <= 50)
counter = counter + 3;
0 1 2 3 4
1 2 3 4
56. What is the output of the following program
segment?
int x = 14, y = 60;
while (((y - x) % 3) != 0)
{ cout << y << " ";
y = y - 5; }
57. Assume all variables are properly declared.
What is the output of the following C++ code?
num = 100;
while (num <= 150)
num = num + 5;
cout << num << endl;
58. Suppose sum and num are int variables,
and the input is 18 25 61 6 -1. What is the
output of the following code?
sum = 0;
cin >> num;
while (num != -1)
{ sum = sum + num;
cin >> num; }
cout << sum << endl;
59. Consider the following code.
const int LASTVAL = -99;
int entry;
cin >> entry;
while (entry != LASTVAL)
{ triple = entry * 3;
cout << triple << endl;
cin >> entry; }
This code is an example of a(n) ____ while loop.
counter-controlled
60. Which of the following is the initial statement in
the following for loop? (Assume that all
variables are properly declared.)
int i;
for (i = 1; i < 20; i++)
cout << "Hello World";
cout << "!" << endl;
i = 1;
i++;
i < 20;
i=1; i < 20; i++
62. What is the output of the following code?
int x = 0, i;
for (i = 0; i < 4; i++);
x++;
if (x == 3)
cout << "*";
64. What is the output of the following C++ code?
int x = 1;
int j;
for (j = 0; j <= 2; j++)
x = x * j;
cout << x << endl;
sentinel-controlled
EOF-controlled
61. Consider the following for loop.
int i;
for (i = 1; i < 20; i++)
cout << "Hello World";
cout << "!";
cout << endl;
How many times will ! be printed by this for loop?
63. What is the output of the following C++ code?
int j;
for (j = 10; j <= 10; j++)
cout << j << " ";
cout << j << endl;
65. Consider the following statements.
int x = 5, y;
do
x = x * 2;
while (x < y);
If y = 0, how many times would the do...while
loop execute?
Suppose that the input is 4 7 –8 5 2. What
is the output of the following C++ code?
int sum = 0 , num, j;
for (j = 1; j <= 5; j++)
{ cin >> num;
if (num < 0)
break;
sum = sum + num; }
cout << sum << endl;
67. Suppose sum, num, and j are int variables,
and the input is 4 7 12 9 -1. What is the
output of the following code?
cin >> sum;
cin >> num;
for (j = 1; j <= 3; j++)
{ cin >> num;
sum = sum + num; }
cout << sum << endl;
68. What is the value of x after the following
statements execute?
int x = 5;
int y = 30;
do
x = x * 2;
while (x < y);
69. What is the output of the following loop?
count = 5;
cout << 'St';
do
{ cout << 'o';
count--; }
while (count <= 5);
70. What is the output of the following C++ code?
int x = 7;
bool found = false;
do
{ cout << x << " ";
if (x <= 2)
found = true;
else
x = x – 5; }
while (x > 0 && !found);
71. Suppose that the input is 5 3 4 –6 8. What
is the output of the following C++ code?
int sum = 0, num, j;
for (j = 1; j <= 5; j++)
{ cin >> num;
if (num < 0)
continue;
sum = sum + num; }
cout << sum << endl;
In C++, the scope resolution operator is ____
96. The ____ of an identifier refers to where in the program an identifier is accessible (visible).
____ identifiers are not accessible outside of the function (block).
Memory for ____ variables remains allocated as long as the program executes.
99. A variable for which memory is allocated at block entry and deallocated at block exit is called a(n) ____
variable
102.
Given the following function :
int strange(int x, int y)
{ if (x > y)
return x + y;
else
return x – y; }
what is the output of the following statement:?
cout << strange(4, 5) << endl;
103.
Given the following function:
int mystery(int u, int v)
{ int a;
a = u – v;
u = a;
v = u;
return u + v; }
choose the output of the following statement:
cout << mystery(9, 7) << endl;
104.
Suppose that you have the following
function.
void mystery(int& one, int two)
{ int temp
temp = one;
one = two;
two = temp;}
What are the values of x and y after the following
statements?
x = 10;
y = 15;
mystery(x, y);
105.
Consider the following function
definition.
void strange(int& u, char& ch)
{ int a;
a = u++;
u = 2 * u;
a = static_cast<int>(ch);
a++;
ch = static_cast<char>(a); }
What are the values of one and let after the
following statements execute?
int one =5;
char let = 'A';
strange(one, let);
What is the output of the following C++
code?
int alpha = 5, beta = 10;
alpha = alpha + 5;
{ int alpha = 20;
beta = beta + 5 ; }
cout << alpha << " " << beta <<endl ;
107.
What is the output of the following C++
code?
int x = 4;
int y = 5;
{ x = x + 2;
int y = 4; }
cout << x << " " << y << endl;
109.
Consider the following function
prototypes.
int nextElement(int);
char nextElement(char);
Which of the following statements is valid in C++?
(i) cout << nextElement(10) << endl;
(ii) cout << nextElement('A') << endl;
(iii) cout << nextElement(7.5) << endl;

