Question

In: Computer Science

A1.State whether each of the following is true or false. If false, explain why: a.In C++...

A1.State whether each of the following is true or false. If false, explain why:

a.In C++ the operator for raising to a power has lower precedence than the multiplication operator

b.In C++ the statement
x = y = x = 0;
is illegal.

c.In C++ the expression
x < y < z
is illegal.

d.In C++ the expression
x <= y = z
is legal.

A2.Consider the following program:
void main() {
enum direction { EAST, WEST, NORTH, SOUTH };
direction dir = NORTH;
int num = 2;
s1: num = dir;
s2: dir = (direction) num;
}
Why a type cast is not necessary in the statement labelled s1, but it is necessary in the statement labelled s2?

A3.List two advantages of using named constants as opposed to literals

A4.What is the purpose of a function prototype?

A5.Why is it necessary to double the backslash character in a string literal?

A6.In the table below fill in the type and the fixed format value of each of the listed literals. If it is not a valid literal, fill in “INVALID” for the type.

TYPE || Value in fixed format
2.5E2
2.5F2
2E2
2E2F
2F2E
-2E-2F

A7.What is the output of the following code snippet?

int num = 0;
int* ptr = &num;
num = 5;
*ptr = *ptr + 5;
cout << num << " " << *ptr << endl;
a)5 5
b)5 10
c)10 5
d)10 10

A8.What will be printed by the following function:

void printBalance()
{
int balance=90;
while (!(balance < 9))
balance -= 9;
cout << balance << end1;
}

A9.Answer True or False
a) The expression:
!(amount > 100 && amount <= 200)
always has the same Boolean value as the expression
amount <= 100 || amount > 200
b) The expression:
!(amount > 200 || amount <= 100)
always has the same Boolean value as the expression
amount > 100 && amount <= 200

A10.Show the output of the following program in the box on the right:

#include <iostream>
using namespace std;
int getTotal();
int main()
{
cout << "Total is " << getTotal();
system("pause");
}
int getTotal(){
int x, y, total = 0;
x = 1;
while (x < 6){
y = x*x;
cout << y << endl;
total = total + y;
++x;
}
return total;
}

A11.What is the error in the following code snippet?
[2 points]
int x = 3;
int y = 3;
switch (x+3){
case 3: y = 3;
break;
case 6: y = 1;
default: y += 1;
}
int * ptr;
* ptr = y;
cout << &ptr << " " << *ptr << end1;

A12.What is the error in the following code snippet?

#include <iostream>
using namespace std;
void main() {
for (int cnt = 1; ; cnt++){
int * a = new int;
*a = cnt;
if (cnt >= 5) return;
double d = 1.0;
d -= 1.5 * 3 + * a++;
cout<< d << end1;
system("pause");
}
}

State whether each of the following is true or false. If false, explain why:
a.In C++ the operator for raising to a power has lower precedence than the multiplication operator

b.In C++ the statement
x = y = x = 0;
is illegal.

c.In C++ the expression
x < y < z
is illegal.

d.In C++ the expression
x <= y = z
is legal.

Solutions

Expert Solution

1.

a) False : Exponents or powers have a higher precedence over multiplication/ division.

b) False: x=y=x=0 is a legal statement in C++ it both x and y have been declared previously.

c) False : x < y < z is a legal statement but it will work like this

(x<y) < z

d) True : x <= y = z is an illegal statement because <= is a comparison operator and = is a assignment operator.

4. Function Prototype

  • Tells the return type
  • Idea about the number of arguments.
  • Tells about the data type
  • Tells about the order of argument.

5. It is necessary to double the backslash character in string literal because C++ assigns a special meaning to a single backslash hence it is not read as a character inside the string. To avoid that, two backslashes are used.

7. Output for the code:

int num = 0;
int* ptr = &num;
num = 5;
*ptr = *ptr + 5;
cout << num << " " << *ptr << endl;

is 10,10(d)

this is because ptr is a pointer and is storing the memory address of the num variable

so whenever a change is made to *ptr it will be reflected to the variable also. Hence, num changed from 5 to 10 and the output was 10 10.

Please upvote if my answer helped. Thanks and stay safe.


Related Solutions

State whether the following statement is true or false AND explain why: "An increase in the...
State whether the following statement is true or false AND explain why: "An increase in the interest rate paid on excess reserves will always cause an increase in the federal reserve funds rate."
Please state whether each of the following statements is TRUE or FALSE, and then explain your...
Please state whether each of the following statements is TRUE or FALSE, and then explain your choice briefly. Please start your answer by writing either TRUE or FALSE. Answers without that clear statement at the beginning, and with excessively long (more than 2-3 short sentences) explanations will be penalized. Banks can still influence the money supply if they are required to hold all deposits in reserve. In the months of November and December, people in the U.S. hold a larger...
Determine whether each of the following statements is true or false, and explain why in a...
Determine whether each of the following statements is true or false, and explain why in a few sentences. 1. The mean, median, and mode of a normal distribution are all equal. 2. If the mean, median, and mode of a distribution are all equal, then the distribution must be a normal distribution. 3. If the means of two distributions are equal, then the variance must also be equal. 4. The sample mean is not the same as the population mean....
Exercise 4.8. For each of the following, state whether it is true or false. If true,...
Exercise 4.8. For each of the following, state whether it is true or false. If true, prove. If false, provide a counterexample. (i) LetX beasetfromRn. ThesetX isclosedifandonlyifX isconvex. (ii) Let X and Y be sets from Rn. If X ∩Y is closed and convex then Xand Y are both closed and convex sets. (iii) LetX beanopensetandY ⊆X. IfY ≠∅,thenY isaconvexset. (iv) SupposeX isanopensetandY isaconvexset. IfX∩Y ⊂X then X∪Y isopen.
State whether EACH of the following statements are true or false. A. A fraction A of...
State whether EACH of the following statements are true or false. A. A fraction A of the Sun’s radiation gets reflected off the atmosphere. (T/F) B. The Earth surface radiates both visible and infrared radiation. (T/F)                            [ C. Most of the visible radiation from the sun is absorbed by the atmosphere. (T/F)             D. The atmosphere emits thermal infrared radiation. (T/F) E. The greenhouse effect is due to the thermal...
QUESTION 4 State whether each of the following are TRUE or FALSE, and explain your reasoning...
QUESTION 4 State whether each of the following are TRUE or FALSE, and explain your reasoning for the answer: The binomial distribution is defined by its mean and standard deviation. As the sample size increases, the standard error of the mean decreases. If the population distribution is unknown, in most cases the sampling distribution of the mean can be approximated by the normal distribution if the samples contain at least 10 observations. (b)       Suppose the following trend line and seasonal...
Indicate whether each of the following statements is true or false, and explain why. A competitive...
Indicate whether each of the following statements is true or false, and explain why. A competitive firm that is incurring a loss should immediately cease operations. A pure monopoly does not have to worry about suffering losses because it has the power to set its prices at any level it desires. In the long run, firms operating in perfect competition and monopolistic competition will tend to earn normal profits. Assuming a linear demand curve, a firm that wants to maximize...
True/False (3 pts each) Indicate whether the statement is true or false. If true, explain why...
True/False (3 pts each) Indicate whether the statement is true or false. If true, explain why it is true and/or give examples to support it. If false, explain why it is false. 2.​An increase in plasma osmolality will cause more secretion of ADH.
State whether each of the following six propositions is true or false. In 2-3 sentences, explain...
State whether each of the following six propositions is true or false. In 2-3 sentences, explain why. Use diagrams if helpful. a. Suppose the government passes a law requiring “profit-sharing”; every year, firms must give at least 50% of their profits to their workers. T, F, and Explain: As long as wages are flexible, wages will fall, leaving workers’ well-being unchanged. b. T, F, and Explain: With a volunteer army, wars are bad for taxpayers. With conscription, wars are bad...
State whether each of the following six propositions is true or false. In 2-3 sentences, explain...
State whether each of the following six propositions is true or false. In 2-3 sentences, explain why. Use diagrams if helpful. a. Suppose the government passes a law requiring “profit-sharing”; every year, firms must give at least 50% of their profits to their workers. T, F, and Explain: As long as wages are flexible, wages will fall, leaving workers’ well-being unchanged. b. T, F, and Explain: With a volunteer army, wars are bad for taxpayers. With conscription, wars are bad...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT