[go: up one dir, main page]

0% found this document useful (0 votes)
45 views8 pages

C++final Revision Fall 2024

Uploaded by

kelwa3366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

C++final Revision Fall 2024

Uploaded by

kelwa3366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

a) State whether the following statements are true or false.

1. An expression containing the || operator is true if either or both of its operands


are true. (True)
2. All arguments to function calls in C++ are passed by value. (False)
3. All variables must be declared before they used. (True)
4. An array can store many different types of values.(False)
5. The default case is required in the switch selection structure. (False)
6. A C++ program that prints three lines of output must contain three output
statements using cout. (False)
7. int total; is a variable declaration statement.(True)
8. Comments cause the computer to print the text after the // on the screen when the
program is executed. (False)
9. The expression (x< y&& a<b) is true if either the expression x<y is true or a<b is
true. (False)
10.The escape sequence for a newline is \n.(True)

Identify and correct the errors in each of the following:


1. cin << value; cin>>
2. while ( z >= 0 ) infinity loop
sum += z;
3. if ( gender = = 1 )
cout << "Woman" << endl;
else ; semicolon
cout << "Man" << endl;

b) What is the output of the following programs segments?


void main( )
{ int x;
x = -3 +4 * 5 – 6; cout << “x = “ << x << endl; Sol:
x = 3 + 4 % 5 –6; cout << “x = “ << x << endl; X=11
X=1
x = -3 * 4 % -6/5; cout << “x = “ << x << endl; X=0
x = (7 + 6) % 5 /2; cout << “x = “ << x << endl; } X=1

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;

b) Chose the correct answer of the following:


1) A C++ statement that makes a decision is ______.
a. If b. for c. #include d. return

2) The error in the following code at______.

line1: if ( gender == 1 )
line2: cout << "Woman" << endl;
line3: else;
line4: cout << "Man" << endl;

a. line1 b. line2 c. line3 d. line4 d. no error

3) What is wrong with following code?

while ( z >= 0 )
sum += z;

a. While is written wrong b. Condition never break

c. += is not allowed d. Nothing wrong

4) An example on prefix increment variable “score” is ______.


a. score++; b. ++score; c. score--; d. --score;

5) Assume x = 2 and y = 3. The output of the following statement:


cout<<x<< “+=”<<y; is______.
a. 5 b. 2 c. 2+=3 d. 2”+=”3

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

3. Which of the following is a repetition structure?


a) if. b) if…else. c) do…while. d) switch.

4. Which of the following for headers is not valid?


a) for ( int i = 0; i < 10; i++ ).
b) int i = 0; for ( ; i < 10; i++ ).
c) for ( int i = 0; int j = 5; ; i++ ).
d) All of the above.

5. Which of the following is false?


a) break and continue statements alter the flow of control.
b) continue statements skip the remaining statements in current iteration of the
body of the loop in which they are embedded.
c) break statements exit from the loop in which they are embedded.
d)continue and break statements may be embedded within all C++ control
statements.
6. Which of the following statements places input into the variable "value"?

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

8. Which of the following is a repetition structure?


a) if. b) if…else. c) do…while. d) switch.

9. Which of the following for headers is not valid?

a) for ( int i = 0; i < 10; i++ ).


b) int i = 0; for ( ; i < 10; i++ ).
c) for ( int i = 0; int j = 5; ; i++ ).
d) All of the above.
a) Write a program that read two integers and then uses the conditional
expression operator to print either "multiple" or "not" according to whether one
of the integers is a multiple of the other.

Example 1: Find Largest Number Using if...else


Statement
Write a program finds out the largest number among three numbers entered

by user and displays it with a proper message.

Example to generate the multiplication table of a number (entered by the user)


using for loop.
C++ Program to Calculate Average of
Numbers Using Arrays
This program takes n number of element from user (where, n is specified by
user), stores data in an array and calculates the average of those numbers.

You might also like