3. What is the output of the following statements? a. if(\'b\' < \'a\') cout <<
ID: 3829645 • Letter: 3
Question
3. What is the output of the following statements?
a. if('b' < 'a') cout << "b is less than a"; cout << "the end" << endl;
b. if(10 < 2 * 5) cout << "10 "; cout << "2 * 5"; cout << endl;
c. if('a' < 'B') { // lowercase vs. uppercase… cout << 'a'; cout << 'A'; } cout << endl;
d. if("C++" >= "C--") // check the ASCII tables… cout << "C++" << endl; cout << "C--" << endl;
e. if("Hank" <= "Dean") cout << "Go Team Venture!" << endl; cout << "Minions, to the cocoon!" << endl;
7. Rewrite the following expressions using the conditional operator. (Assume that all variables are declared properly.)
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;
c) if (score >= 60) str = "Pass"; else str = "Fail";
8. Suppose that beta is an int variable. Consider the following C++ code:
cin >> beta;
switch (beta % 7) { case 0: case 1: beta = beta * beta;
break;
case 2: beta++;
break;
case 3: beta = static_cast(sqrt(beta * 1.0));
break;
case 4: beta = beta + 4; case 6: beta = beta--;
break;
default:
beta = -10; }
a. What is the output if the input is 11?
b. What is the output if the input is 12?
c. What is the output if the input is 14?
d. What is the output if the input is 16?
9. The following program contains errors. Correct them so that the program will run and output:
w = 22
Don’t delete any statements, and correct for style issues (except for short variable names), syntax errors and logic errors.
#include
using namespace std;
const int SECRET = 5
main (){
int x, y, w, z;
z = 11;
if z > 10
x = 12; y = 5, w = x + y + SECRET;
else
x = 12;
y = 4,
w = x + y + SECRET;
cout << "w = " << w << endl;
return 0; }
10. 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. Otherwise if the first number is greater than the second number, it outputs the first number divided by the second number. Otherwise 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
using namespace std;
int main()
{ double firstNum, secondNum;
cout << "Enter two nonzero numbers: ";
cin >> firstNum >> secondNum;
cout << endl;
//Missing statements
return 0; }
Explanation / Answer
3)
a) if('b' < 'a') cout << "b is less than a"; cout << "the end" << endl;
Output : the end
In this code, ascii value of b(98) is compared with ascii values of a(97). It means actual comparision is 98<97, this is not true. Hence, next line is skipped.
b) . if(10 < 2 * 5) cout << "10 "; cout << "2 * 5"; cout << endl;
Output : 2 * 5
In this case, 10 < 2*5 ,i.e., 10<10 which is false. Hence next line after if is skipped. And, 2 * 5 is printed.
c) if('a' < 'B') { // lowercase vs. uppercase…
cout << 'a'; cout << 'A'; } cout << endl;
Output :
In this case, ascii value of a is compared with the ascii value of B ,i.e., 97<66 which is false. Hence, entire if block is skipped and nothing is printed.
d) if("C++" >= "C--") // check the ASCII tables… cout << "C++" << endl; cout << "C--" << endl;
Output: C--
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.