C++final Revision Fall 2024
C++final Revision Fall 2024
a) What will be the output after the following C++ statements have been
executed?
int a, b, c, d;
a = 4; b = 12;
c = 37; d = 51;
if ( a < b )
cout << "a < b" << endl;
if ( a > b )
cout << "a > b" << endl;
if ( d <= c )
cout << "d <= c" << endl;
if ( c != d )
cout << "c != d" << endl;
answer: a < b
c != d
a) Write a single C++ statement (or set of C++ statements) to accomplish each of
the following: (10 Points)
1. If the variable number is not equal to 7, print "the Variable number is not equal
to 7".
If (number !=7 )
Cout<<"the Variable number is not equal to 7";
2. Calculate the value of 8 raised to power 4 using function pow. Print the result
with a precision of 2 in a field width of 10 positions.
Cout<<fixed<<setprecision (2);
Cout<<setw(10)<<pow(8,4);
3. The function header of “ smallest ” that take three integers x, y and z, and returns
with an integer
Int smallest (int x,y,z)
4. Use a for structure to print the elements of array numbers
for ( int i = 0; i < 10; i++ )
cout << numbers[ i ] << endl;
5. Add variable x to variable sum and assign the result to variable sum (write this
statement two different ways).
Sum+=x;
Sum=sum+x;
line1: if ( gender == 1 )
line2: cout << "Woman" << endl;
line3: else;
line4: cout << "Man" << endl;
while ( z >= 0 )
sum += z;
1. Which of the following statements places input into the variable "value"?
a) value >> cin; b)cin >> value; c)value << cin; d)cin << value;
2. Assuming that the user inputs a value of 25 for the price and 10 for the discount
rate in the following code snippet, what is the output?
int main()
{
cout << "Enter the price: "; double price;
cin >> price; cout << "Enter the discount rate: ";
double discount; cin >> discount; cout << "The new price is ";
cout << price - price * (discount / 100.0) << endl; return 0;
}
a) The new price is 25 b) The new price is 15 c) The new price is 22.5
d) The new price is 20.0
b) value >> cin; b)cin >> value; c)value << cin; d)cin << value;
7. Assuming that the user inputs a value of 25 for the price and 10 for the discount
rate in the following code snippet, what is the output?
int main()
{
cout << "Enter the price: "; double price;
cin >> price; cout << "Enter the discount rate: ";
double discount; cin >> discount; cout << "The new price is ";
cout << price - price * (discount / 100.0) << endl; return 0;
}
a) The new price is 25 b) The new price is 15 c) The new price is 22.5
d) The new price is 20.0