For the next 2 questions consider the execution of the following program:
(Hint – trace the program carefully.)
void main()
{
int
LoopCounter, Count=0;
for
(LoopCounter = 0; LoopCounter < 10; LoopCounter++)
{
LoopCounter++;
Count++;
}
cout <<
LoopCounter;
cout <<
Count;
}
1) What value is printed for the variable LoopCounter:
1) 1 2)
2 3) 3 4) 4 5) 5
6) 6 7)
0 8)10 9) none of the above
2) What value is printed for the variable Count:
1) 1 2)
2 3) 3 4) 4 5) 5
6) 6 7) 0 8)10 9) none of the above
Consider executing the following program:
void main ()
{
int i, j, count=0, Answer;
Answer = 1;
for (i = 1; i < 3; i=i+1) {
for (j = 1; j < 5; j=j+2){
count++;
Answer = Answer * i * j;
}
}
cout
<< Answer << endl << count << endl;
}
3) What value is printed for the variable Answer:
1) 0 2) 1 3) 2 4) 15 5) 18 6) 36
7) 72 8) none of the above
4) What value is printed for the variable count :
1) 0 2) 1 3) 2 4) 15 5) 18 6) 36
7) 72 8) none of the above
void main()
{
int Count
= 0;
int number = 2;
bool done
= false;
while (!done)
{
Count++;
number = number * 2;
done = (number > 64);
}
cout << Count << endl;
}
5) What is printed out when the above code is executed?
1) 1 2)
2 3) 3 4) 4 5) 5
6) 6 7) 0 8)10 9)
none
of the above
void main()
{
int i=1, sum, j;
while (i <= 5)
{
sum = 0;
j = 1;
while (j<=i)
{
sum = sum + j;
j++;
}
i++;
}
cout << sum << endl;
}
6) What is printed out for sum when the above code is executed?
1)
0 2) 1 3) 5 4) 14 5) 15
6)
none
of the above
void main() {
int i, j, A[2][4] = {1,2,3,4,5,6,7,8};
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++)
cout
<< A[j][i] << endl;
}
7) What is the value printed on the fifth line of output?
1) 1 2) 2 3) 3 4) 4 5) 5 6) 6
7) 7 8) 8 9) none of the above
For the next 2 questions consider the following code:
void
main ()
{
int Value1 = 1;
int Value2 = 3;
Mystery (Value1, Value2);
cout << Value1<< ‘ ‘ <<
Value2 << endl;
}
void
Mystery (int Param1, int& Param2)
{
Param1 = Param1 * 5;
Param2 = Param2 * 2;
}
8) What is printed out for Value1?
1) 1 2) 2 3) 3 4) 4 5) 5
6) 6 7)
0 8)10 9)
none
of the above
9) What is printed out for Value2?
1) 1 2)
2 3) 3 4) 4 5) 5
6) 6 7) 0 8)10 9)
none
of the above
10) Given the function heading
int
FindIt( int& howMany,
float& alpha,
float beta )
which of the following is a valid function prototype for FindIt?
1) int
FindIt( int& howMany, float& alpha, float beta );
2) void FindIt(
int&, float&, float );
3) int FindIt( int,
float&, float& );
4) 1 and 2 above
5) 1, 2, and 3 above
_______________________________________________________________________________________
11)
Consider the function definition
void
DoThis( int& alpha,
int
beta )
{
int temp;
alpha = alpha + 100;
temp = beta;
beta = 999;
}
Suppose that the caller has
integer variables gamma and delta whose values are 10 and 20, respectively.
What are the values of gamma
and delta after return from the following function
call?
DoThis(gamma,delta);
|
1) gamma = 10 and delta = 20 2) gamma = 110 and delta = 20 3) gamma = 10 and delta = 999 |
4) gamma = 110 and delta = 999 5) none of the above |
_______________________________________________________________________________________
12) What is the output of the following code fragment? (All variables are of type int.)
int
alpha, beta;
alpha = 3;
beta
= 20;
if
(beta > 10)
{
int alpha = 5;
beta = beta + alpha;
cout << alpha << ' ' << beta << endl;
}
cout
<< alpha << ' ' << beta << endl;
|
1) 3 20 |
2) 3 25 3
25 |
3) 5 25 5
25 |
4) 5 25 3 25 |
5) 5 25 3
20 |
13) What is the output of the following code fragment if the input value is 4? (Be careful here.)
int
num;
int
alpha = 10;
cin
>> num;
switch (num)
{
case 3 : alpha++;
case 4 : alpha = alpha + 2;
case 8 : alpha = alpha + 3;
default : alpha = alpha + 4;
}
cout
<< alpha << endl;
1) 10 2)
14 3) 12 4) 19 5)
15
14) What is the output of the following code fragment? (beta is of type int.)
beta
= 5;
do
{
switch (beta)
{
case 1 : cout << 'R';
break;
case 2 :
case 4 : cout << 'O';
break;
case 5 : cout << 'L';
}
beta--;
}
while (beta > 1);
cout
<< 'X' << endl;
1) X 2) ROOLX 3) LOOX 4)
LOORX 5) ROOX
15)
Which of the loops below produces the same number of loop
iterations as the following loop?
(count
is of type int)
for
(count = 1; count <= 10; count++)
DoSomething();
1) for (count = 10;
count >= 1; count--)
DoSomething();
2) for (count = 0;
count < 10; count++)
DoSomething();
3) for (count = 10;
count >= 0; count--)
DoSomething();
4) 1 and 2 above
5) 1, 2, and 3 above
16)
What is the content of the array arr
after the for loop?
int
arr[5];
int j
= 0;
for
(i = 0; i < 5; i++)
{
arr[i] = i+j;
j = j + 1;
}
1) 0 1 2
3 4 3) 0
2 4 6 8
2) 1
2 3 4 5 4) 1
3 5 7 9
For the next 5 questions, consider the incomplete function definition given below:
// CountAs takes an array of characters and count the number
of times
// the character ‘A’ appears in the list.
//
// Parameters:
// CharacterList[] array containing characters.
// NumOfCharacters number of characters to check
// TotalA number of As in the array
void CountAs(
char _________, int NumOfCharacters, int& TotalA)// Line A
{
int LoopCounter;
for(LoopCounter=0;________;LoopCounter++) //Line B
{
if ( _________ == 'A') //Line C
TotalA=TotalA+________; //Line D
}
cout<<"The number of 'A's is =
"<<_________; //Line E
}
17) How should the blank for the first parameter in line A be filled?
1) CharacterList 2) CharacterList[] 3) ‘A’
4) it should be left blank 5) none of these
18) How should the blank in line B be filled?
1) LoopCounter < 100 2)
LoopCounter <= NumOfCharacters
3) LoopCounter <= 100 4) LoopCounter <
NumOfCharacters
5) none of these
19) How should the blank in line C be filled?
1) CharacterList[NumOfCharacters] 2) CharacterList[LoopCounter]
3) LoopCounter 4) none of these
5) NumOfCharacters
20) How should the blank in line D be filled?
1) 1 2) CharacterList
3) LoopCounter 4) none of these
5) NumOfCharacters
21) How should the blank in line E be filled?
1) TotalA 2) CharacterList[LoopCounter]
3) LoopCount 4) none of these
5) NumOfCharacters
Consider the following code:
|
void silly
(int); int main() { int x,y; // do
something silly x=10; y=11; silly(x); silly(y);
// values here return 0; } |
void silly
(int x) { int y; y = x + 2; x = x * 2; } // end
silly |
22) What are the values of main function variables x and y at the point marked // values here?
1) x=10 and y=11 2) x=20 and y=22
3) x=11 and y=10 4) x=40 and y=11
5) x=20 and y=12 6) x=40 and y=14
23) What are the values of the main function’s variables x and y if x was passed by reference to the silly function at the point marked // values here?
1) x=10 and y=11 2) x=20 and y=22
3) x=11 and y=10 4) x=40 and y=11
5) x=20 and y=12 6) x=40 and y=14
For the next 3 questions consider the following code:
for (int i=1;
i<10; i++)
{
for (int j=0; j<=i; j++)
cout << setw (4) << (i + j); //first
cout << endl; //second
}
24) How many times does the first cout statement execute?
1) 100 2) 56
3) 10 4) 55
5) 11 6) cannot be determined
25) How many times does the second cout statement execute?
1) 100 2) 56
3) 10 4) 55
5) 11 6) cannot be determined
26) What is the last value displayed?
1) 10 2) 19
3) 11 4) 20
5) 18 6) none of the above
Consider the following code:
int main()
{
int x[8];
// line 1
for (int i=0;i<9;i++) // line 2
x[i] = i;
// line 3
return 0;
// line 4
}
27) Which line of code should be changed to prevent a possible runtime error in the program?
1) 1 2) 2 3) 3 4) 4
5) either 1 or 2 6) either 1 or 3 7) either 2 or 3 8) either 1 or 4
num = 345;
do
{
cout << ' ' << num % 10;
num = num /10;
}
while (num > 0);
28) What
does the above code segment display?
1)
5 .5 0 2) 3 4 5
3)
.5 0 0 4) 5 4 3
5) 34.5 3.45 .345 6) none of the above
int
X; // 1
void
main() {
int Y; // 2
while(Y < 10) {
int X = 0; // 3
X++;
Y = Y + X;
}
cout << X << endl;
}
29)
Choose the correct statement about the above code:
1) The lifetime of the variable X in declaration 1 is the entire execution of the program.
2) The variable X in declaration 3 is initialized to 0 during each iteration of the while loop.
3) The scope of the variable X in declaration 1 is the entire body of main() except for the while loop.
4) The variable Y in declaration 2 should be initialized; otherwise , the content of Y is unpredictable.
5) All of the above.
6) None of the above.
30)
Choose the correct statement about parameter passing by
value:
1) Actual and formal parameters must be of similar types.
2) Formal parameters must be declared as local variables in the function body.
3) Actual parameters in a function call cannot be constants or expressions; they must be variables.
4) Parameter passing by value can be used both for input and output communication purposes.
5) All of the above are correct.
6) None of the above is
correct.