1. ( 1) Which of the following is an invalid C++ identifier?
ID: 3540397 • Letter: 1
Question
TwoForOne A_+_B two_for_one A_plus_B2. ( 1) Which of the following identifies syntax errors in programs written in a high-level language? Assembler Compiler Preprocessor Linker
3. ( 1) For the values given, what will c contain after executing the following? int a = 9, b = 4, c = -1; c -= a % b;
-1 -2 -3 1
4. ( 1) For the values given, what will c contain after executing the following? int a = 9, b = 4, c = -1; c += b++ * a;
45 36 44 35
5. ( 1) A slash sign (/) is called the separation operator. the divide operator. the slash operator. None of the above
6. ( 1) What is the result of 56 % 5? 1 11 11.2 12
7. ( 1) What is the output for the following code fragment? int var1 = 20; cout << --var1; cout << ++var1;
1920 1921 2020 2021
8. ( 1) Which operation in the following expression will be performed first? c = a++ / b + 5;
a++ a / b b + 5 assignment to c
9. ( 2) The endl manipulator _______. requires #include <iomanip> is used with cout to display a new line only works with cin statements All of the above
10. ( 2) A user inputs his name in response to a prompt for a numeric value. The subsequent cin statement tries to get a numeric value and store it into an integer variable. How does a program detect this input error and clear it? The cin input stream is broken, the application must be restarted. cin.fail() detects a failure and cin.clear() clears the failure. Use cin.fail() to detect the failure and cin.ignore() to clear the failure. Use cin.ignore() to detect and clear the failure.
11. ( 2) Which statement outputs a double value in a field of six characters with three digits after the decimal point? cout << 6chars << 3digits << 1.234; cout << fixed << setprecision(3) << 1.234; cout << setprecision(3) << setw(6) << 1.2345678; cout >> setprecision(3) >> 6chars >> 1.234;
1. ( 2) What are the values of the variables after the code fragment below executes if the input data is 37 86.56 32? double x, y; int z; cin >> x; cin >> z; cin >> y;
x = 37.0, y = 32, z = 86 x = 37.0, y = 56, z = 86 x = 37.0, y = 32, z = 86.56 x = 37.0, y = 0.56, z = 86
2. ( 10) For readability, all statements inside an if statement body should be indented the same distance as the if statement. surrounded by an open and closing parenthesis. indented by one additional tab stop more than the if statement. written on the same line.
3. ( 10) Which of the following correctly adds a comment to the end of a line of code? a = b * c; \ comment here a = b * c; /* comment here a = b * c; // comment here a = b * c; * comment here
4. ( 3) Based on input of a single digit code, your program must output the full name of a US state plus other information about the state based on its population category. The best selection structure to use to program this situation is _______. a SWITCH statement multiple IF statements nested IF statements multiple IF ELSE statements
5. ( 3) Which statement correctly tests char variable keepgoing for the upper or lower case letter A? if(keepgoing = 'a' || keepgoing = 'A') if(keepgoing = 'a' || 'A') if(keepgoing == 'a' && keepgoing == 'A') if(keepgoing == 'a' || keepgoing == 'A')
6. ( 3) What is the output of the following code snippet? int a = 9, b = 4, c = -1; if (a < b && (a = c) > 0) { cout << "TRUE" << a << b << c; } else { cout << "FALSE" << a << b << c; }
FALSE 9 4 -1 TRUE -1 4 -1 FALSE -1 4 -1 None of the above
7. ( 3) What is the value of beta after the following code executes if the input is 5? int beta; cin >> beta; switch(beta) { case 3: beta += 3; case 1: beta++; break; case 5: beta += 5; case 4: beta += 4; }
14 9 10 5
8. ( 4) Which looping construct guarantees that the loop body always executes at least one time? for do while while any of the above
9. ( 4) How many times does the following loop body execute? int count = 52; for(int i = 0; i > 26; i++) { cout << count << endl; --count; }
26 52 25 None of the above
10. ( 4) When the _______ statement executes in a loop body, control immediately exits from the loop. break continue exit done
11. ( 4) Which of the following expressions is correct if you want to end a while-loop when the character variable answer is anything other than the character 'y' in either upper or lower case? while(answer == 'y' && answer == 'Y') while(answer == "y" && answer == "Y") while(answer == 'y' || answer == 'Y') while(answer == "y" || answer == "Y")
1. ( 4) What is the output from the following loop? int value = 1; for( int j = 10; j < 50; j = j + 10) { value = value * j; cout << value << " "; } cout << endl;
2. ( 3) A program has a char variable gender that has been assigned a value. Write a switch statement that outputs "Male" if the variable contains lower or upper case 'm', "Female" if it contains lower or upper case 'f', or "Invalid Input" for any other value. 3. ( 3) Write a short program that gets three integer values from the user and outputs the largest number. Make sure you include everything you need, and declare all the variables you need. 4. ( 3) Write a complete C++ console mode program (#includes, etc., but no prologue) that solves a simple DC circuit problem. Given a DC voltage source of some magnitude, find the voltage drops across the 2 resistors. The user will provide the values for the voltage source and the 2 resistor values. The formula that you will need is: VRx = Vsupply * Rx / (Rx + Ry) // Voltage Divider rule %u2022Use appropriate data types. %u2022Valid input is any positive real number. If the user input is zero or negative, your program should print an error message and not try to compute any results. %u2022Be sure to display explanatory text to make the input and output clear to the user. Results should be output with 3 digits following the decimal point. 5. ( 4) Create a C++ program that uses a while-loop to display the odd numbers between 15 and 30 including 15. The numbers should be displayed one per line 6. ( 4) Write a complete C++ program that meets the following criteria:
a. Use a while loop. Explicitly ask the user if they have more data to enter. Use their answer to decide when the loop is done. b. The purpose of the loop is to calculate and display the amount of interest for one year given an initial deposit and an interest rate. (interest = deposit * rate) c. The output from the loop should explain what values are being displayed and display the interest with 2 digits after the decimal point. d. The loop must ask the user to input the values for the initial deposit and the interest rate. Both values should be treated as real numbers. e. If either input value is negative, your program should output a message indicating that the input values must be positive numbers. The interest amount should not be displayed in this case. f. Declare all needed variables using appropriate data types. Initialize as needed.
Explanation / Answer
1. ( 1) Which of the following is an invalid C++ identifier?
A_+_B
2. ( 1) Which of the following identifies syntax errors in programs written in a high-level language?
Compiler
3. ( 1) For the values given, what will c contain after executing the following?
int a = 9, b = 4, c = -1;
c -= a % b;
-2 is answer
c = c - (a%b) = -1 - (9%4) = -1-1 = -2
4. ( 1) For the values given, what will c contain after executing the following?
int a = 9, b = 4, c = -1;
c += b++ * a;
35
c = c+ (b++)*a = -1 + (4*9) = -1 + 36 = 35 is answer.
5. ( 1) A slash sign (/) is called
the divide operator.
6. ( 1) What is the result of 56 % 5?
1 (56%5 = 1)
7. ( 1) What is the output for the following code fragment?
int var1 = 20;
cout << --var1; (predecremnt operator so var1 will be 19)
cout << ++var1; (postdecremnt operator so var1 will be 20)
1920
8. ( 1) Which operation in the following expression will be performed first?
c = a++ / b + 5;
a++
9. ( 2) The endl manipulator _______.
is used with cout to display a new line
10. ( 2) A user inputs his name in response to a prompt for a numeric value.
The subsequent cin statement tries to get a numeric value and store it into an integer variable.
How does a program detect this input error and clear it?
Use cin.fail() to detect the failure and cin.ignore() to clear the failure.
11. ( 2) Which statement outputs a double value in a field of six characters with three digits after the decimal point?
cout << fixed << setprecision(3) << 1.234;
1. ( 2) What are the values of the variables after the code fragment below executes if the input data is 37 86.56 32?
double x, y;
int z;
cin >> x;
cin >> z;
cin >> y;
x = 37.0, y = 0.56, z = 86
2. ( 10) For readability, all statements inside an if statement body should be
indented by one additional tab stop more than the if statement.
3. ( 10) Which of the following correctly adds a comment to the end of a line of code?
a = b * c; // comment here
4. ( 3) Based on input of a single digit code, your program must output the full name of a US state plus other information
about the state based on its population category. The best selection structure to use to program this situation is _______.
a SWITCH statement
5. ( 3) Which statement correctly tests char variable keepgoing for the upper or lower case letter A?
if(keepgoing == 'a' || keepgoing == 'A')
6.
( 3) What is the output of the following code snippet?
int a = 9, b = 4, c = -1;
if (a < b && (a = c) > 0) // since a is not less than b else will be executed.
{
cout << "TRUE" << a << b << c;
}
else
{
cout << "FALSE" << a << b << c; // FALSE 9 4 -1
}
FALSE 9 4 -1
7.
( 3) What is the value of beta after the following code executes if the input is 5?
int beta;
cin >> beta;
switch(beta)
{
case 3:
beta += 3;
case 1:
beta++;
break;
case 5:
beta += 5;
case 4:
beta += 4;
}
beta = 5 + 5;
beta = 10 + 4 = 14.
8. ( 4) Which looping construct guarantees that the loop body always executes at least one time?
do while
9.
( 4) How many times does the following loop body execute?
int count = 52;
for(int i = 0; i > 26; i++)
{
cout << count << endl;
--count;
}
None of the above // since 0 is not greater than 26 loop will not be executed.
10. ( 4) When the _______ statement executes in a loop body, control immediately exits from the loop.
break
11. ( 4) Which of the following expressions is correct if you want to end a while-loop when the character variable answer
is anything other than the character 'y' in either upper or lower case?
while(answer == 'y' || answer == 'Y')
1.
( 4) What is the output from the following loop?
int value = 1;
for( int j = 10; j < 50; j = j + 10)
{
value = value * j;
cout << value << " ";
}
cout << endl;
10 200 6000 240000
2. ( 3) A program has a char variable gender that has been assigned a value.
Write a switch statement that outputs "Male" if the variable contains lower or upper case 'm', "Female" if it contains lower or upper case 'f',
or "Invalid Input" for any other value.
switch(gender)
{
case 'm':
case 'M': cout << "Male" << endl; break;
case 'f':
case 'F': cout << "Female" << endl; break;
default: cout << "Invalid Input << endl; break;
}
3. ( 3) Write a short program that gets three integer values from the user and outputs the largest number.
Make sure you include everything you need, and declare all the variables you need.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a,b,c;
cin >> a >> b >> c;
int max = (a>b)?a:b;
max = (max>c)?max:c;
cout << max << endl;
return 0;
}
4. ( 3) Write a complete C++ console mode program (#includes, etc., but no prologue) that solves a simple DC circuit problem.
Given a DC voltage source of some magnitude, find the voltage drops across the 2 resistors.
The user will provide the values for the voltage source and the 2 resistor values. The formula that you will need is:
VRx = Vsupply * Rx / (Rx + Ry) // Voltage Divider rule
#include<iostream>
using namespace std;
int main()
{
double Vsupply,Rx,Ry;
double VRx = Vsupply * Rx / (Rx + Ry);
cout << VRx << endl;
return 0;
}
%u2022Use appropriate data types.
%u2022Valid input is any positive real number.
If the user input is zero or negative, your program should print an error message and not try to compute any results.
%u2022Be sure to display explanatory text to make the input and output clear to the user.
Results should be output with 3 digits following the decimal point.
5. ( 4) Create a C++ program that uses a while-loop to display the odd numbers between 15 and 30 including 15.
The numbers should be displayed one per line
#include<iostream>
using namespace std;
int main()
{
int count=15;
while(count<30)
{
cout << count << endl;
count = count+2;
}
return 0;
}
6. ( 4) Write a complete C++ program that meets the following criteria:
a. Use a while loop. Explicitly ask the user if they have more data to enter. Use their answer to decide when the loop is done.
b. The purpose of the loop is to calculate and display the amount of interest for one year given an initial deposit and an interest rate.
(interest = deposit * rate)
c. The output from the loop should explain what values are being displayed and display the interest with 2 digits after the decimal point.
d. The loop must ask the user to input the values for the initial deposit and the interest rate. Both values should be treated as real numbers.
e. If either input value is negative, your program should output a message indicating that the input values must be positive numbers.
The interest amount should not be displayed in this case.
f. Declare all needed variables using appropriate data types. Initialize as needed.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char answer = 'y';
double deposit,rate,interest;
while(answer == 'y' || answer == 'Y')
{
cout << "Enter initial deposit :";
cin >> deposit;
cout << endl;
cout << "Enter initial rate :";
cin >> rate;
cout << endl;
if(deposit<=0 || rate<=0)
{
cout << "INput numbers should be positive" << endl;
}
else
{
interest = deposit * rate;
cout << " for deposit " << deposit << " Interest given by " << setprecision(2) << interest << endl;
}
cout << " do you want to continue (y or n) " ;
cin >> answer;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.