Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

C++ Programming Exercise-7:

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);
}

8. What is the output of the following (when embedded in a complete program)?


for (int count = 1; count < 5; count++) for (int n = 10; n > 0; n = n - 2)
cout << (2 * count) << " "; {
cout << "Hello "; cout << n << endl;
}
9. Rewrite the following loops as for loops.
int i = 1; int i = 1; long m = 100;
while (i <= 10) { while (i <= 10) do {
if (i < 5 && i != 2) { cout << 'X'; m = m + 100;
cout << 'X'; i++; cout << 'X'; i = i + 3;
} } } while (m < 1000);

10. Identify any errors in the following array declarations.


a. int x[4] = { 8, 7, 6, 4, 3 };
b. int x[ ] = { 8, 7, 6, 4 };
c. const int SIZE = 4;
d. int x[SIZE];
11. What is the output of the following code?
char symbol[3] = {'a', 'b', 'c'};
for (int index = 0; index < 3; index++)
cout << symbol[index];
12. What is the output of the following code?
double a[3] = {1.1, 2.2, 3.3};
cout << a[0] << " " << a[1] << " " << a[2] << endl;
a[1] = a[2];
cout << a[0] << " " << a[1] << " " << a[2] << endl;

13. What is wrong with the following piece of code?


int sample_array[10];
for (int index = 1; index <= 10; index++)
sample_array[index] = 3 * index;

14. What (if anything) is wrong with the following code?


char string_var[] = "Hello";
strcat(string_var, " and Good-bye.");
cout << string_var;
Assume that the code is embedded in a complete program and that an include directive
for<string.h> is in the program file.

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'};

19. Assume the definitions and initializations:


char c = 'T', d = 'S';
char *p1 = &c;
char *p2 = &d;
char *p3;
Assume further that the address of c is 6940, the address of d is 9772, and the address
of e is 2224. What will be printed when the following statements are executed
sequentially?
p3 = &d;
cout << "*p3 = " << *p3 << endl; // (1)
p3 = p1;
cout << "*p3 = " << *p3 // (2)
<< ", p3 = " << p3 << endl; // (3)
*p1 = *p2;
cout << "*p1 = " << *p1 // (4)
<< ", p1 = " << p1 << endl; // (5)
20. Explain the error.
char c = 'A';
double *p = &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

24. Write a C++ program to reverse a given string.


Sample Output:
Sample Input: computer
Sample Output: retupmoc

25. Write a C++ program to check if a given string is a Palindrome or not.


A palindrome is a word, number, phrase, or other sequence of characters which reads
the same backward as forward, such as madam, racecar.
Sample Output:
Sample Input: madam
Sample Output: True

May 11, 2021


HiLCoE
Problem Solving with Computer Programming I

You might also like