Professional Documents
Culture Documents
Soalan C++
Soalan C++
#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";
}
===========================================================================
====
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;
}
===========================================================================
====
3. What is the output of this program?
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 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;
}
===========================================================================
====
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;
}
===========================================================================
====
8. If, when you compile your program, the compile produces a warning
message,
what does that imply?
===========================================================================
====
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";
===========================================================================
====
11. What will this program output?
int main()
{
int a = 312;
int b = 213;
cout << foobar(a,b) << " " << a << " " << foobar(b,a);
}
===========================================================================
====
12. What will the following program output?
===========================================================================
====
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.
===========================================================================
====
15. When do the following two bits of code do the same thing?
(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 << "+";
}
===========================================================================
====
17. How many x's will be printed?
===========================================================================
====
19. What does the following program output?
for (n = 0; n < 5; n += 2)
{
num += n;
}
cout << num << " ";
return num;
}
===========================================================================
====
20. What's the output of the following code?
===========================================================================
====
21. What does the following code output?
===========================================================================
====
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";
}
}
===========================================================================
====
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"?
int main()
{
int size, i;
cin >> size;
for (i=0; i<size; i++)
{
starline(i+1);
}
}
(a) ***
(b) ***
***
***
(c) ***
**
*
*(d) *
**
***
===========================================================================
====
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;
===========================================================================
====
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;
===========================================================================
====
28. What will the following code output?
===========================================================================
====
29. If you want to construct a loop that will be guaranteed to execute
at least one time, which should you use:
===========================================================================
====
30. Consider the following function:
return 0; //finished
}
===========================================================================
====
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";
===========================================================================
====
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;
}