Professional Documents
Culture Documents
C++ Practical Exercise - 2021 Final
C++ Practical Exercise - 2021 Final
1. What output would be produced by the following two lines (when embedded in a
complete and correct program)?
//cout << "Hello from";
cout << "Self-Test Exercise";
2. Write an if-else statement that outputs the word High if the value of the variable score
is greater than 100 and Low if the value of score is at most 100. The variable score is of
type int.
3. Write an if-else statement that outputs the word Passed provided the value of the
variable exam is greater than or equal to 60 and the value of the variable
programs_done is greater than or equal to 10. Otherwise, the if-else statement outputs
the word Failed. The variables exam and programs_done are both of type int
4. What is the output of the following cout statements embedded in these if-else
statements? You are to assume that these are embedded in a complete correct
program. Explain your answer.
if (0) if (1) if (-1)
cout << "0 is true"; cout << "0 is true"; cout << "0 is true";
else else else
cout << "0 is false"; cout << "0 is false"; cout << "0 is false";
cout << endl; cout << endl; cout << endl;
5. What is the output produced by the following (when embedded in a correct program
with x declared to be of type int)?
x = 10; x = 10;
while (x > 0) do {
{ cout << x << endl;
cout << x << endl; x = x - 3;
x = x - 3; } while (x > 0);
}
6. What is the most important difference between a while statement and a do-while
statement?
7. What is the output of the following (when embedded in a complete program)?
int count = 3; int count = 3; int n = 1; int n = 1;
while (count–– > 0){ while (--coun > 0){ do { do {
cout << count << " "; cout << count << " cout << n << " "; cout << n << " ";
} "; }while (n++ <= 3); }while (++n <= 3);
}
15. What is the maximum length of a string that can be placed in the string variable
declared by the following declaration? Explain. char s[6];
16. How many characters are in each of the following character and string constants?
a. '\n'
b. 'n'
c. "Mary"
d. "M"
e. "Mary\n"
17. What string will be output when this code is run? (Assume, as always, that this code is
embedded in a complete, correct program.)
char song[10] = "I did it ";
char franks_song[20];
strcpy( franks_song, song );
strcat( franks_song, "my way!");
cout << franks_song << endl;
18. Since character strings are just arrays of char, why does the text caution you not to
confuse the following declaration and initialization?
char short_string[] = "abc";
char short_string[] = {'a', 'b', 'c'};
21. Write a piece of code which prints the characters in a cstring in a reverse order.
char s[10] = "abcde";
char* cptr;
// WRITE YOUR CODE HERE
22. Consider the following statements:
int *p;
int i;
int k;
i = 42;
k = i;
p = &i;
After these statements, which of the following statements will change the value of i to
75?
a. k = 75;
b. *k = 75;
c. p = 75;
d. *p = 75;
e. Two or more of the answers will change i to 75.
23. Write a C++ program to count all the vowels in a given string.
Sample Output:
Sample Input: eagerer
Sample output: number of vowels -> 4