You are on page 1of 11

1.

Consider a program whose purpose is to allow the user to enter a


number
and then output whether or not it is equal to five. Here is code
that
does not work. Which line contains the error?

#include <iostream>
int main()
{
int i, j;
cout << "Enter a number: ";
cin >> i;

if (i = 5)
cout << "The number is equal to 5\n";
else
cout << "The numbers is not equal to 5\n";
}

(a) #include <iostream>


(b) int main()
(c) cout << "Enter a number: ";
(d) cin >> i;
*(e) if (i = 5)

===========================================================================
====
2. What will this code output:

int main() {
int x, y, z;

y = z = 0;
for (x=0; x < 10; x++)
{
y = y + 1;
}
z = x + y + z;

cout << z;
}

(a) 0 (b) 10 *(c) 20 (d) 100 (e) 121

===========================================================================
====
3. What is the output of this program?

void foobar( int b, int a ) {


if (a == b)
cout << "lemon curry ";
cout << a - b << endl;
}

int main()
{
int a, b;
a = 42;
b = 23;
foobar(a,b);
}

(a) nothing (b) 19 *(c) -19 (d) lemon curry 19 (e) lemon curry -19

===========================================================================
====
4. What is the output of this program?

int dumb_function( int &a)


{
a = 55;
return 123;
}

int main()
{
int a, b;

b = 99;
a = 12;
a = dumb_function(b);
cout << a + b;
}

(a) 456 *(b) 178 (c) 110 (d) 154 (e) 221

===========================================================================
====
5. What will the following code print?

int a = 99;

switch (a)
{
case 1:
a += 5;
case 2:
a += 23;
case 99:
a += 12;
case 100:
a += 10;

}
cout << a;

(a) 99 (b) 111 (c) 149 *(d) 121 (e) None of the above

===========================================================================
====
6. What value will the variable "a" have following the execution of this
code?

int a, b;
for (b = 0; b < 5; b++ )
{
a = a + 1;
}

(a) 0 (b) 1 (c) 5 (d) 15 *(e) impossible to determine

===========================================================================
====
7. How many asterisks will be printed?

int a, b;
for (a = 0; a < 5; a++ ) {
for (b = 0; b < a; b++ )
{
cout << "*";
}
cout << endl;
}

(a) 0 (b) 1 (c) 5 *(d) 10 (e) 21

===========================================================================
====
8. If, when you compile your program, the compile produces a warning
message,
what does that imply?

(a) The program will not run correctly


(b) The syntax in your program is problematic and the program will not
compile
(c) The program will sometimes crash your computer
*(d) You wrote code that possibly does something different than you
intended
(e) The indentation of the code does not follow conventions

===========================================================================
====
9. What will the following program output?

int x, y;
x = 5;
y = 4;
if (x+y > 9 || x-y > 1)
cout << "Hello ";
else if (x * y - 1 > 10 && x == 5)
cout << "Goodbye ";
else
cout << "Hello Goodbye";

(a) Hello
*(b) Goodbye
(c) Hello Goodbye
(c) Hello Goodbye Hello Goodbye
(d) Cannot be determined with given information

===========================================================================
====
10. What will the following code output?
int z = 4;
if (++z == 5)
cout << "X";
if (z++ == 6)
cout << "Y";
if (z == 6)
cout << "Z";

(a) X (b) Y (c) YZ (d) XY *(e) XZ

===========================================================================
====
11. What will this program output?

int foobar(int a, int b)


{
if (a > b)
return 123;
else
return 321;
}

int main()
{
int a = 312;
int b = 213;
cout << foobar(a,b) << " " << a << " " << foobar(b,a);
}

(a) 123 312 123


*(b) 123 312 321
(c) 321 312 123
(d) 321 312 321
(e) The program won't compile

===========================================================================
====
12. What will the following program output?

int main( void ) {


int f, c;
f = 42;
c = ( f - 32 ) * 5 / 9;
cout << c << endl;
}

(a) 5.555 (b) 5.000 (c) 4 *(d) 5 (e) cannot be determined

===========================================================================
====
13. What will the following code output?

char c = 'a';
switch ( c )
{
case 'a': case 'b':
c++;
cout << "hello " << c ;
break;
case 'A': case 'B':
cout << " oops";
break;
default:
cout << " doh";
}

(a) hello (b) hello a *(c) hello b (d) hello c oops (e) hello c
oops doh

===========================================================================
====
14. Determine the output.

int n1, n2;


n1 = 4;
n2 = 5;
cout << n2 *= n1 << endl;

*(a) 20 (b) 4 (c) 5 (d) n2 *= n1 (e) cannot be determined

===========================================================================
====
15. When do the following two bits of code do the same thing?

while (n < 10) do


{ {
cout << n; cout << n;
++n; ++n;
} } while (n < 10);

(a) always
(b) cannot be determined
*(c) if the initial value of n is smaller than 10
(d) if the initial value of n is larger than 10
(e) if the initial value of n is equal to 10

===========================================================================
====
16. What does the following code output?

int i = 10;
while( i > 1 )
{
i /= 2;
cout << "+";
}

(a) + (b) ++ *(c) +++ (d) ++++ (e) +++++

===========================================================================
====
17. How many x's will be printed?

for ( i = 0; i < 10; i++ )


{
for ( i = 0; i < 10; i++ )
{
cout << "x";
}
}
(a) 0 (b) 100 *(c) 10 (d) 81 (e) 11
===========================================================================
====
18. which of the following loops will execute one or more times?

*(a) for (i=10; i<=10; --i) { ... }


(b) for (i=j=0; j>10; ++j) { ... }
(c) for (i=10; i>20; ++i) { ... }
(d) two or more of the above
(e) none of the above

===========================================================================
====
19. What does the following program output?

int increment(int num)


{
int n;

for (n = 0; n < 5; n += 2)
{
num += n;
}
cout << num << " ";
return num;
}

int main( void ) {


int number = 10;
increment( number );
cout << number << endl;

(a) 0 0 (b) 0 10 (c) 6 10 *(d) 16 10 (e) 6 16

===========================================================================
====
20. What's the output of the following code?

void sum(int n1, int n2, int &s)


{
n1 = n1 * n1;
n2 = n2 * n2;
s = n1 + n2;
}

int main( void ) {


int n1 = 10, n2 = 11;
int total = 0;
sum ( n1, n2, total );
cout << n1 << ", " << n2 << ", " << total << endl;
}
(a) 100, 121, 221 (b) 10, 11, 21 (c) 10, 121, 21 *(d) 10, 11, 221
(e) 100, 121, 0

===========================================================================
====
21. What does the following code output?

int cowboy( int x, int y)


{
int z, i;
z = 0;
for (i = 0; i < y; i++ )
{
z += x;
}
return z;
}

int main( void )


{
cout << cowboy(7,1) - cowboy(3, 2) << endl;
}

*(a) 1 (b) 2 (c) 6 (d) 13 (e) 25

===========================================================================
====
22. What will be output here?

int x, y;
x = 3;
y = 5;
if ( x * y + x * (x - y) > x + y )
{
if ( x - (-y) * x < x - (x * y) )
cout << "A";
else
cout << "B";
}
else
{
if ( x * y + (x * x) - y < y * y - x * x)
cout << "C";
else
cout << "D";
}
}

(a) A *(b) B (c) C (d) D (e) none of the above

===========================================================================
====
23. When the following program runs, what will be output when the user
types "10 12 13 6 7 a f g 7" followed by a newline (the user does NOT type
the quotes)?

int main()
{
char c;
int x;

do
{
cin >> x;
cout << x+2 << " ";
} while (x != 6);
do
{
cin >> c;
cout << c << " ";
} while (c != 'g');
}

(a) 10 12 13 6 7 a f g
*(b) 12 14 15 8 7 a f g
(c) 10 12 13 6 a f g
(d) 12 14 15 8 9 a f g
(e) none of the above

===========================================================================
====
24. What will this program output?

int talking_frog(int x)
{
int i = 5;
x = x + i;
cout << i << " " << x << " ";
return 0;
}

int main()
{
int i = 1;
int x = 3;
cout << i << " " << x << " ";
talking_frog(x);
cout << i << " " << x << " ";
}

*(a) 1 3 5 8 1 3
(b) 1 3 5 8 5 8
(c) 1 3 5 8 1 8
(d) 1 3 5 8 5 3
(e) none of the above

===========================================================================
====
25. What will the following code output if the user inputs "3"?

void starline(int length)


{
int j;
for (j=0; j<length; j++)
{
cout << "*";
}
cout << endl;
}

int main()
{
int size, i;
cin >> size;
for (i=0; i<size; i++)
{
starline(i+1);
}
}
(a) ***

(b) ***
***
***

(c) ***
**
*

*(d) *
**
***

(e) none of the above

===========================================================================
====
26. If the user inputs "35 36" to the following program, what will the
output
of this code be?

int num1;
char num2;
cin >> num1 >> num2;
cout << num1 << " " << num2 << endl;

(a) 35 36 *(b) 35 3 (c) 35 (d) 3 36 (e) 3 5

===========================================================================
====
27. Consider this code which asks the user if they want to continue, reads
their input, and responds:

char c;
cout << "Do you want to continue? ";
cin >> c;
if ((c == "y") || (c == "Y"))
cout << "On to the next problem..." << endl;
else
cout << "Good bye" << endl;

(a) This code should compile and run as intended


*(b) The double quotes in the "if" statement should be turned into single
quotes.
(c) The strings "y" and "Y" should be replaced by "yes" and "YES"
(d) The two cout statements should be exchanged.
(e) The two cout statements must be enclosed by curly braces.

===========================================================================
====
28. What will the following code output?

int num1, num2;


double real1, real2;
num1 = 3;
real1 = 4.34;
num2 = real1 - num1;
real2 = real1 - num2;
cout << num1 << " " << num2 << " " << real1 << " " << real2 << endl;
(a) 3 1.34 4.34 3.34
(b) 3 1.34 4.34 3
(c) 3 1 4.34 3.43
(d) 3 1 4.34 3
*(e) none of the above

===========================================================================
====
29. If you want to construct a loop that will be guaranteed to execute
at least one time, which should you use:

(a) a for loop


(b) a while loop
*(c) a do-while loop
(d) two or more of the above will work
(e) none of the above will work

===========================================================================
====
30. Consider the following function:

int oddcounter (int num1, int num2)


{
int tmp;
if ((num1<0) || (num2<0) || (num1==num2))
return -1; //error

if (num1 > num2)


{
tmp = num2;
num2 = num1;
num1 = tmp;
}
if (num1 % 2 == 0)
num1++;
do {
cout << num1 << " ";
num1 += 2;
} while (num1 < num2);

return 0; //finished
}

Which statement most accurately describes its purpose?


(a) Prints the larger of two positive numbers
(b) Prints the smaller of two positive numbers
(c) Prints all even numbers between two positive numbers
*(d) Prints all odd numbers between two positive numbers
(e) Prints all numbers between two numbers

===========================================================================
====
31. Given the following definition of the function below, what does
"glorch(glorch(2)+1)" evaluate to?

int glorch(int q)
{
return q * (q-1);
}
(a) 1 (b) 3 (c) 4 (d) 5 *(e) 6

===========================================================================
====
32. What will the following code output? Warning: You need to pay
attention
to the code, not the indentation, or lack thereof.

int i, j;
i = 3;
j = 4;
if (i+j*2 > 7)
if (i+4 == j+3)
cout << "X";
else
cout << "Y";
else if (i > j)
cout << "Z";

*(a) X (b) Y (c) Z (d) XZ (e) YZ

===========================================================================
====
33. What is the last value that will be output by this code?

int i = 100;
int c = 0;
int d = 1;
while (i > 0)
{
cout << i << endl;
for (c=0; c<2; c++)
{
d *= 2;
}
i -= d;
}

(a) -240 (b) 1 *(c) 16 (d) 38 (e) 80

You might also like