0% found this document useful (0 votes)
105 views

Introduction To Programming: Exercise Questions

The document contains questions about C++ programming concepts such as variables, data types, operators, input/output statements, and basic programs. It includes multiple choice and true/false questions to test understanding of fundamental C++ topics as well as exercises asking to write code for tasks like calculating profit amounts, converting between measurement units, and reading/processing user input. The questions cover a wide range of foundational C++ programming skills and knowledge.

Uploaded by

rana uzair
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Introduction To Programming: Exercise Questions

The document contains questions about C++ programming concepts such as variables, data types, operators, input/output statements, and basic programs. It includes multiple choice and true/false questions to test understanding of fundamental C++ topics as well as exercises asking to write code for tasks like calculating profit amounts, converting between measurement units, and reading/processing user input. The questions cover a wide range of foundational C++ programming skills and knowledge.

Uploaded by

rana uzair
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

INTRODUCTION TO PROGRAMMING

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.

2. Which of the following are valid C++ identifiers?


a. myFirstProgram b. MIX-UP c. C++Program2
d. quiz7 e. ProgrammingLecture2 f. 1footEquals12Inches
g. Mike'sFirstAttempt h. Update Grade i. 4th
j. New_Student

3. Which of the following is a reserved word in C++?


a. Const b. include c. Char
d. void e. int f. Return

4. What is the difference between a keyword and a user-defined identifier?


5. Are the identifiers firstName and FirstName the same?
6. Evaluate the following expressions.
a. 25 / 3 b. 20 - 12 / 4 * 2 c. 32 % 7
d. 3 - 5 % 7 e. 18.0 / 4 f. 28 - 5 / 2.0
g. 17 + 5 % 2 - 3 h. 15.0 + 3.0 * 2.0 / 5.0
7. If x = 5, y = 6, z = 4, and w = 3.5, evaluate each of the following statements, if possible. If it
is not possible, state the reason.
a. (x + z) % y b. (x + y) % w c. (y + w) % x
d. (x + y) *w e. (x % y) % z f. (y % z) % x
g. (x *z) % y h. ((x *y) *w) *z

8. Given: int num1, num2, newNum; double x, y;


Which of the following assignments are valid? If an assignment is not valid, state the reason.
When not given, assume that each variable is declared.
a. num1 = 35;
b. newNum = num1 – num2;
c. num1 = 5; num2 = 2 + num1; num1 = num2 / 3;
d. num1 * num2 = newNum;
e. x = 12 * num1 - 15.3;
f. num1 * 2 = newNum + num2;
g. x / y = x * y;
h. num2 = num1 % 2.0;
i. newNum = static_cast<int> (x) % 5;
j. x = x + y - 5;
k. newNum = num1 + static_cast<int> (4.6 / 2);

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;

10. Which of the following variable declarations are correct?


If a variable declaration is not correct, give the reason(s) and provide the correct variable
declaration.
n = 12; //Line 1
char letter = ; //Line 2
int one = 5, two; //Line 3
double x, y, z; //Line 4
11. Which of the following are valid C++ assignment statements? Assume that i, x, and
percent are double variables.

a. i = i + 5;
b. x + 2 = x;
c. x = 2.5 *x;
d. percent = 10%;

12. Write C++ statement(s) that accomplish the following.


a. Declare int variables x and y. Initialize x to 25 and y to 18.
b. Declare and initialize an int variable temp to 10 and a char variable ch to 'A'.
c. Update the value of an int variable x by adding 5 to it.
d. Declare and initialize a double variable payRate to 12.50.
e. Copy the value of an int variable firstNum into an int variable tempNum.
f. Swap the contents of the int variables x and y. (Declare additional variables, if necessary.)
g. Suppose x and y are double variables. Output the contents of x, y, and the expression x + 12 /
y - 18.
h. Declare a char variable grade and set the value of grade to 'A'.
i. Declare int variables to store four integers.
j. Copy the value of a double variable z to the nearest integer into an int variable x

13. Write each of the following as a C++ expression.


a. 32 times a plus b
b. The character that represents 8
c. The string that represents the name Julie Nelson.
d. (b2 - 4ac) / 2a
e. (a + b)/c(ef)-gh
f. (-b + (b2 - 4ac)) / 2a

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?

Suppose a = 3, b = 5, and c = 14.1.

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.)

c. Outputs the cost of producing milk.

d. Outputs the profit for producing milk


Chapter 3
OBJECTIVE
1. Mark the following statements as true or false:
i. >>is used with cout and << is used with cin.
ii. Attempting to read invalid data into a variable causes the input stream to enter the
fail state. 

iii. In the statement cin >> x; x must be a variable. 

iv. The statement cin >> num; is equivalent to the statement num >> cin;. 

v. To use cin and cout, the program must include the header file string.
vi. An output stream is a stream from a source to a computer. 

vii. An input stream is a stream from a computer to a destination. 

viii. When inputting data into a variable, the operator>>skips all leading whitespace
characters. 

ix. The statement cout<<Hello; would print Hello on the screen.
x. To move the output to next line, endline expression is used.
Chapter 3
SUBJECTIVE
1. Suppose x and y are int variables and ch is a char variable.
Consider the 
following input: 
5 28 36

What value (if any) is assigned to x, y, and ch after each of the following 
statements
executes? (9 marks)

Cin >> x >> y >> ch; 


Cin >> ch >> x >> y;

Cin >> x >> ch >> y; 


2. Given the input: 46 A 49 
and the C++ code: 



int x = 10, y = 18;


 char z = '*';


cin>> x >> y >> z;


cout<< x << " " << y << " " << z << endl; 


What is the output?

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
<, <=, >=, >
||
&&
==, !=
*, /, %

3. Solve following objective statements


a. Circle the best answer

if (60 <= 12 * 5)
cout << "Hello";
cout << " There";
Outputs the following:

(i) HelloThere (ii) Hello (iii) Hello (iv) ThereThere


b. Circle the best answer

if (7 <= 7)
cout << 6 - 9 * 2 / 6 << endl;

Outputs the following:

(i) -1 (ii) 3 (iii) 3.0 (iv) None of these


4. Write down the output of following program

#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

5. Rewrite following programs using conditional (ternary) operator


a. if (x >= y)
z = x - y;
else
z= y - x;

b. if (hours >= 40.0)


wages = 40 * 7.50 + 1.5 * 7.5 * (hours - 40);
else
wages = hours * 7.50;
Chapter 4
SUBJECTIVE
1. Write down C++ program for the problem mentioned as following.
Suppose that overSpeed and fine are double variables. Assign the value to fine as follows: If
0 < overSpeed <= 5, the value assigned to fine is $20.00; if 5 < overSpeed <= 10, the value
assigned to fine is $75.00 if 10 < overSpeed <= 15, the value assigned to fine is $150.00; if
overSpeed > 15, the value assigned to fine is $150.00 plus $20.00 per mile over 15.

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

1. Mark the following statements as true or false:

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

2. Write output of the following C++ code segments:


int num = 5;
while (num > 5)
num = num + 2;
cout << num << endl;

3. Correct the following code so that it finds the sum of 20 numbers:

sum = 0;
while (count < 20)
cin >> num;
sum = sum + num;
count++;

4. Rewrite the following as a for loop.

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;

What is the output of this loop?


5. What is the output of the following program?

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

6. What is the output of the following program segment?

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.

2. Identify error(s), if any, in the following array declarations.


a. int list[10];
b. constint size = 100; double list[SIZE];
c. int numList[0..9];
d. string names[20];
e. scores[50] double;

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?

6. Write C++ statements to do the following:


a. Declare an array alpha of 15 components of type int.
b. Output the value of the tenth component of the array alpha.
c. Set the value of the fifth component of the array alpha to 35.
d. Set the value of the ninth component of the array alpha to the sum of
the sixth and thirteenth components of the array alpha.
e. Set the value of the fourth component of the array alpha to three times the value of the
eighth component minus 57.
f. Output alpha so that five components per line are printed.

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];

9. Suppose list is an array of five components of type int. What is stored in


list after the following C++ code executes?
for (int i = 0; i < 5; i++)
{
list[i] = 2 * i + 5;
if (i % 2 == 0)
list[i] = list[i] - 3;
}

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.

You might also like