Introduction To Programming: Exercise Questions
Introduction To Programming: Exercise Questions
EXERCISE QUESTIONS
Chapter 2
OBJECTIVE
1. Mark the following statements as true or false.
a. An identifier can be any sequence of digits and letters.
b. In C++, there is no difference between a reserved word and a predefined identifier.
c. A C++ identifier can start with a digit.
d. The operands of the modulus operator must be integers.
e. If a = 4; and b = 3;, then after the statement a = b; the value of b is still 3.
f. In the statement cin >> y;, y can only be an int or a double variable.
g. In an output statement, the newline character may be a part of the string.
h. The following is a legal C++ program: int main () {return 0; }
i. In a mixed expression, all the operands are converted to floating-point numbers.
j. Suppose x = 5. After the statement y = x++; executes, y is 5 and x is 6.
k. Suppose a = 5. After the statement ++a; executes, the value of a is still 5 because the value of
the expression is not saved in another variable.
9. Do a walk-through to find the value assigned to e. Assume that all variables are properly
declared.
a = 3;
b = 4;
c = (a % b) * 6;
d = c / b;
e = (a + b + c + d) / 4;
a. i = i + 5;
b. x + 2 = x;
c. x = 2.5 *x;
d. percent = 10%;
14. Suppose x, y, z, and w are int variables. What value is assigned to each of these variables
after the last statement executes?
x = 5; z = 3; y = x - z; z = 2 * y + 3; w = x - 2 * y + z; z = w - x; w++;
15. Suppose x, y, and z are int variables and w and t are double variables. What value is
assigned to each of these variables after the last statement executes?
x = 17; y = 15;
x = x + y / 4;
z = x % 3 + 4;
w = 17 / 3 + 6.5;
t = x / 4.0 + 15 % 4 - 3.5;
16. Suppose a, b, and sum are int variables and c is a double variable. What value is assigned
to each variable after each statement executes?
17. What is printed by the following program? Suppose the input is: 20 15
#include<iostream>
using namespace std;
const int NUM = 10; const double X = 20.5;
int main()
{
int a, b; double z;
char grade;
a = 25;
cout << "a = " << a << endl;
cout << "Enter two integers: "; cin >> a >> b; cout << endl;
cout << "The numbers you entered are " << a << " and " << b << endl;
z = X + 2 * a - b;
cout << "z = " << z << endl;
grade = 'A';
cout << "Your grade is " << grade << endl;
a = 2 * NUM + z;
cout << "The value of a = " << a << endl;
return 0;
}
18. What is printed by the following program? Suppose the input is: Miller 34 340
#include <iostream>
#include <string>
using namespace std;
const int PRIME_NUM = 11;
int main()
{
const int SECRET = 17;
string name; int id; int num; int mysteryNum;
cout << "Enter last name: "; cin >> name; cout << endl;
cout << "Enter a two digit number: "; cin >> num; cout << endl;
id = 100 * num + SECRET;
cout << "Enter a positive integer less than 1000: "; cin >> num; cout << endl;
mysteryNum = num * PRIME_NUM - 3 * SECRET;
cout << "Name: " << name << endl; cout << "Id: " << id << endl; cout<<myestry
number: " << mysteryNum << endl;
return 0;
}
Chapter 2
SUBJECTIVE
1. Write a C++ program that prompts the user to input the elapsed time for an event in
hours, minutes, and seconds. The program then outputs the elapsed time in seconds.
2. To make a profit, a local store marks up the prices of its items by a certain percentage.
Write a C++ program that reads the original price of the item sold, the percentage of the
marked-up price, and the sales tax rate. The program then outputs the original price of
the item, the percentage of the mark-up, the store’s selling price of the item, the sales tax
rate, the sales tax, and the final price of the item. (The final price of the item is the selling
price plus the sales tax.)
3. Write a program that prompts the user to input a length expressed in centimeters. The
program should then convert the length to inches (to the nearest inch) and output the
length expressed in yards, feet, and inches, in that order. For example, suppose the input
for centimeters is 312. To the nearest inch, 312 centimeters is equal to 123 inches. 123
inches would thus be output as:
3 Yard, 1 feet (foot), and 3 inches
4. A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships cartons of milk
to a local grocery store. The cost of producing one liter of milk is $0.38, and the profit of
each carton of milk is $0.27. Write a program that does the following:
a. Prompts the user to enter the total amount of milk produced in the morning.
b. Outputs the number of milk cartons needed to hold milk. (Round to int.)
char z = '*';
cout<< x << " " << y << " " << z << endl;
3. Suppose that x and y are int variables, z is a double variable, and ch is a char variable. Suppose
the input statement is: cin >> x >> y >> ch >> z;
What values, if any, are stored in x, y, z, and
ch if the input is: (9 marks)
a. 35 62.78
b. 86 32A 92.6
c. 12 .45A 32
Chapter 4
OBJECTIVE
1. Given following values of variables, write down the output of C++ expressions
mentioned below.
bool found = true;
int age = 20;
double hours = 45.30;
int count = 20;
char ch = 'B';
Expression value
!found
hours > 40.00
!(found && (age >= 18))
(count >= 0) && (count <=
100)
('A' <= ch && ch <= 'Z')
2. Write down the order of precedence of following operators as 1st, 2nd, 3rd, and so on. For
your guidance, the precedence of expressions in first and second row is already
mentioned as 1st and 3rd. please fill the rest of the cells in the same way.
Operator Precedence
!, +, - (unary operators) 1st
+, - 3rd
<, <=, >=, >
||
&&
==, !=
*, /, %
if (60 <= 12 * 5)
cout << "Hello";
cout << " There";
Outputs the following:
if (7 <= 7)
cout << 6 - 9 * 2 / 6 << endl;
#include <iostream>
using namespace std;
int main() {
int myNum = 10;
int yourNum = 30;
if (yourNum % myNum == 3)
{
yourNum = 3;
myNum = 1;
}//end if
else if (yourNum % myNum == 2)
{
yourNum = 2;
myNum = 2;
}// end else if
else
{
yourNum = 1;
myNum = 3;
} //end else
cout << myNum << " " << yourNum << endl;
return 0;
} //end main
2. Write the missing statements in the following program so that it prompts the user to input
two numbers. If one of the numbers is 0, the program should output a message indicating
that both numbers must be nonzero. If the first number is greater than the second
number, it outputs the first number divided by the second number; if the first number is
less than the second number, it outputs the second number divided by the first number;
otherwise, it outputs the product of the numbers.
#include <iostream>
using namespace std;
int main()
{
double firstNum, secondNum;
cout << "Enter two nonzero numbers: ";
cin >> firstNum >> secondNum;
cout << endl;
//Missing statements
return 0;
}
3. Write a program that works like a calculator. The program should take value1,
value2 as integer values and an operator optr as character value. It should then output
the values, the operator, and the result. (For division, if the denominator is zero, output
an appropriate message.) Some sample outputs follow:
3+4=7
13 * 5 = 65
12 / 0 [print: invalid input]
4. Write a program that prompts the user to input three numbers. Now, by using selection
(if, if-else) control structure, the program should then output the numbers in ascending
order.
5. A box of cookies can hold 24 cookies, and a container can hold 75 boxes of cookies. Write
a program that prompts the user to enter the total number of cookies, the number of
cookies in a box, and the number of cookie boxes in a container. The program then
outputs the number of boxes and the number of containers to ship the cookies. Note that
each box must contain the specified number of cookies, and each container must contain
the specified number of boxes. If the last box of cookies contains less than the number of
specified cookies, you can discard it and output the number of leftover cookies. Similarly,
if the last container contains less than the number of specified boxes, you can discard it
and output the number of leftover boxes.
Chapter 5
OBJECTIVE
a. In a counter-controlled while loop, it is not necessary to initialize the loop control variable.
b. It is possible that the body of a while loop may not execute at all.
c. In an infinite while loop, the while expression (the decision maker) is initially false, but
after the first iteration it is always true.
d. The while loop:
j = 0;
while (j <= 10)
j++;
terminates if j > 10.
e. A sentinel-controlled while loop is an event-controlled while loop whose termination
depends on a special value.
f. A loop is a control structure that causes certain statements to execute over and over.
Chapter 5
SUBJECTIVE
sum = 0;
while (count < 20)
cin >> num;
sum = sum + num;
count++;
int i = 0, value = 0;
while (i <= 20)
{
if (i % 2 == 0 && i <= 10)
value = value + i * i;
else if (i % 2 == 0 && i > 10)
value = value + i;
else
value = value - i;
i = i + 1;
}
cout << "value = " << value << endl;
#include <iostream>
using namespace std;
int main()
{
int x, y, z;
x = 4; y = 5;
z = y + 6;
do
{
cout << z << " ";
z = z + 7;
}
while (((z - x) % 4) != 0);
cout << endl;
return 0;
}
int count = 5;
while (--count > 0)
cout << count << " ";
cout << endl;
7. Write a program that prompts the user to input an integer and then outputs both the
individual digits of the number and the sum of the digits. For example, it should output the
individual digits of 3456 as 3 4 5 6, output the individual digits of 8030 as 8 0 3 0, output the
individual digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of 4000 as 4 0 0 0,
and output the individual digits of -2345 as 2 3 4 5.
8. Write a program that prompts the user to input a positive integer. It should then output a
message indicating whether the number is a prime number. (Note: An even number is prime
if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to
the square root of the number.)
9. Write a program that uses while loops to perform the following steps:
a. Prompt the user to input two integers: firstNum and secondNum (firstNum must be less
than secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and secondNum.
d. Output the numbers and their squares between 1 and 10.
e. Output the sum of the square of the odd numbers between firstNum and secondNum.
f. Output all uppercase letters.
Chapter 9
OBJECTIVE
1. Consider the following declaration:
double salary[10];
In this declaration, identify the following:
a. The array name.
b. The array size.
c. The data type of each array component.
d. The range of values for the index of the array.
3. Determine whether the following array declarations are valid. If a declaration is invaid,
explain why.
a. int list75;
b. int size;
double list[size];
c. int test[-10];
d. double sales[40.5];
5. What would be a valid range for the index of an array of size 50?
7. Determine whether the following array declarations are valid. If a declaration is invaid,
explain why.
a. int list75;
b. int size;
double list[size];
c. int test[-10];
d. double sales[40.5];
8. Determine whether the following array declarations are valid. If a declaration is invaid,
explain why.
a. int list75;
b. int size; double list[size];
c. int test[-10];
d. double sales[40.5];
10. Suppose list is an array of six components of type int. What is stored in
list after the following C++ code executes?
list[0] = 5;
for (int i = 1; i < 6; i++)
{
list[i] = i * i + 5;
if (i > 2)
list[i] = 2 * list[i] - list[i - 1];
}
11. Correct the following code so that it correctly initializes and outputs the
elements of the array.
myList;
int myList[10];
for (int i = 1; i <= 10; i++)
cin >> myList;
for (int i = 1; i <= 10; i++)
cout << myList[i] << " ";
cout << endl;
Chapter 9
SUBJECTIVE
1. Write a C++ program that declares an array alpha of 50 components of type double.
Initialize the array so that the first 25 components are equal to the square of the index
variable, and the last 25 components are equal to three times the index variable. Output
the array so that 10 elements per line are printed.
2. Write a program that displays the minimum and maximum value in an array along with
their index. Moreover, it is required to display the average of that array.
3. Write a program that displays the odd and even values in an array along with their
indices. Moreover, it is required to display the average of even and odd elements in that
array.