a) What output would occur for each of the following code fragments
cout << setw(6) << setfill(‘a’) << 321 << endl;
cout << 3 + 4 * 7 ‐ 7;
cout << 28 % 6

b) What would be the value of z displayed by each of the following code fragments

i.
int x = 16, y = 5;
double z;
z = x / y;
cout << z << endl;

ii.
int x = 14, y = 6, z;
x * = 2;
z = x % y;
cout << z << endl;

c) Re-write each of the following code fragments correcting all syntax errors

i.
int difference;
if { x ! = y }
difference = { x – y };
cout >> “Values differ by ”
>> difference >> endl;
else
cout >> “Zero Difference\n”;

ii.
int x;
cin >> x;
switch if x
[
case 1
cout << “Option 1\n”;
break;
case 2
cout << “Option 2\n”;
break;
otherwise
cout << “Invalid option\n”;
]

iii.
int x = 0;
cout << “Counting\n”;
do
{
x++;
cout << “count = “ << x << endl;
} until (x == 10)

iv.
cout << “Displaying odd
numbers\n”;
for (y = 1, y <= 10, y++)
{
if y % 2 != 0
cout << y << endl;
}

d) What values will be displayed when each of the following code fragments is executed
i.
for (int x = 24; x > 10; x – = 4 )
{
cout << x << endl;
}

ii.
for ( int row = 0; row < 3; row++ )
for ( int col = 0; col < 3; col++)
cout << row * col << “ “;
[6 marks]

iii.
int x = 36;
while ( x > 0 )
{
cout << x << endl;
x /= 3;
}

iv.
for ( int x = 19; x > 0; x – = 3 )
{
cout << x << “ “;
if ( x % 5 = = 0 )
break;
}

e) What output would be produced by each of the following if the user inputs a value of 30
cin >> value;
if ( value < 10 )
cout << “AA”;
else if ( value < 20 )
cout << “BB”;
else if ( value < 30 )
cout << “CC”;
else
cout << “DD”;
cin >> value;
value %= 4;
switch ( value )
{
case 1:
cout << “AA”;
case 2:
cout << “BB”;
default:
cout << “DD”;
}

f) How would you declare an array to hold the price of 12 shopping items? Write the code to populate the contents of the array from the keyboard.

 

 

This question has been answered.

Get Answer