1 Will the following string literal fit in the space allocat
1. Will the following string literal fit in the space allocated for name? Why or why not?
2. If a program contains the definition string name; indicate whether each of the following lettered program statements is legal or illegal.
A) cin >> name;
B) cin.getline(name, 20);
C) cout << name;
D) name = \"John\";
3. If a program contains the definition char name[25]; indicate whether each of the following lettered program statements is legal or illegal.
A) cin >> name;
B) cin.getline(name, 25);
C) cout << name;
D) name = \"Pikachu\";
4. Write a statement that produces a random number between 900 and 1000 and stores it in the variable luckyNumber.
5. What header file must be included
A) to perform mathematical functions like sqrt?
B) to use cin and cout?
C) to use stream manipluators like setprecision?
D) to use functions rand() and srand()?
6. The following program has some errors. Locate as many as you can.
}
7. What will each of the following program segments display?
A)
x = 2;
y = 4;
cout <<x++<<\"\"<<--y;
B)
x = 2;
y = 2*x++;
cout <<x<<\"\"<<y;
C)
D)
x = 0;
if (++x)
E)
F)
G)
for ( int count = 1; count <= 10; count++ )
{
cout << ++count << \" \"; // This is a bad thing to do!
}
H)
for (int row = 1; row <= 3; row++)
}
Solution
Question 1:
char name[4] = \"John\";
this will not work because John is decleared inside \"\" so char will not accept
char name[4] = {\'J\',\'o\',\'h\',\'n\'};
Question 2:
A) cin >> name;
// legal
B) cin.getline(name, 20);
// illegal
C) cout << name;
// legal
D) name = \"John\";
// legal
Question 3:
A) cin >> name;
// legal
B) cin.getline(name, 25);
// legal
C) cout << name;
// legal
D) name = \"Pikachu\";
// illegal
Question 4:
int luckyNumber = (rand() % 100) + 900;
(rand() % 100) // get random number 1 to 100
adds ( 1 to 100 ) + 900;
Question 5:
Answer is all:
A) to perform mathematical functions like sqrt?
B) to use cin and cout?
C) to use stream manipluators like setprecision?
D) to use functions rand() and srand()?
Question 6:
Change the code to :
#include <iostream>
using namespace std;
int main()
{
int number1, number2;
double quotient;
cout << \"Enter two numbers and I will divide\ \";
cout << \"the first by the second for you.\ \";
cin >> number1 >> number2; // reads number1 and number2
quotient = double(number1)/number2; // change to double
cout << quotient;
return 0;
}
Question 7:
A)
23
x++ is post increment and --y is pre decrement
B)
34
x++ is post increment
so 2 * 2 then x is incremented
C)
It is true!
there x is post incremented
D)
It is true!
there x is pre incremented
E)
this is infinite loop while (x < 10);
F)
10
G)
2 4 6 8 10
H)
for (int row = 1; row <= 3; row++) // loops for 3 times
{ cout << \"\ $\";
for (int digit = 1; digit <= 4; digit++) // loops for 4 time
cout << \'9\';
}
Output:
$9999
$9999
$9999


