23 October 2013
1. The following program just uses variables. What does the program output?
#include <iostream>
using namespace std;
int main()
{
int lisa = 5;
double bart = 5 / 2.0;
lisa++;
int maggie = lisa / 2;
maggie *= 3;
cout << lisa << ", " << bart << ", " << maggie << "\n";
return 0;
}
2. The following program uses conditions. What does the program output?
#include <iostream>
using namespace std;
int main()
{
int bilbo = 9;
int frodo = 3;
if(bilbo % 2 == 0)
{
frodo *= 2;
bilbo++;
}
else
{
bilbo--;
frodo *= 4;
}
cout << bilbo << ", " << frodo << "\n";
return 0;
}
3. The following program uses a switch. What does the program output?
#include <iostream>
using namespace std;
int main()
{
int bill = 9;
int ted = bill - 4;
switch(ted)
{
case 3: cout << "ant ";
case 2: cout << "bee "; break;
case 5: cout << "cat ";
case 4: cout << "dog "; break;
default: cout << "eel ";
}
cout << "\n";
return 0;
}
4. The following program uses a while loop. What does the program output?
#include <iostream>
using namespace std;
int main()
{
int tux = 29;
while(tux > 0)
{
cout << (tux % 3);
tux /= 3; // Hint: INTEGER division
}
cout << "\n";
return 0;
}
5. The following program uses a for loop. What does the program output?
#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i < 40; i *= 3)
{
cout << i << "\n";
}
return 0;
}
6. The following program uses nested if/else statements. Given the values of baz
and tar
as inputs, what does the program output?
baz = 6; tar = 3;
Output: baz = 3; tar = 6;
Output: baz = 6; tar = 9;
Output: baz = 9; tar = 6;
Output: baz = 3; tar = 4;
Output: #include <iostream>
using namespace std;
int main()
{
int baz, tar;
cin >> baz >> tar; // Receives values as shown above
if(baz > 4)
{
if(baz > 8)
{
cout << "Austria ";
}
if(tar < 8)
{
cout << "Belgium ";
}
cout << "Chile ";
}
else
{
cout << "Denmark ";
if(tar < 6)
{
cout << "Egypt ";
}
}
cout << "France\n";
return 0;
}
7. The following program uses nested loops. What does the program output?
#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i < 4; i++)
{
for(int j = i; j < 5; j++)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}