Question

In: Computer Science

What is the output of the following statements? a. cout << ("Jack" <= "John" && "Business"...

What is the output of the following statements?

a. cout << ("Jack" <= "John" && "Business" >= "Accounting") << endl;
b.cout << boolalpha << ((10 <= 15 - 2) && (15 >= 20) || (15 - 2 != 20)) << endl;
c.cout << !(30 > 10) << endl;
d.cout << showpos << (6 <= 5 || 3 < 15) << endl;
e.cout << boolalpha << ("bill" > "billy") << endl;

Solutions

Expert Solution

Above codes are written in C++ programming language:

cout is used to display the output in console in C++.

There are few C++ mainpulator functions used in the above code:

endl : A pre-defined function used to insert a new line. It works same as "\n"

boolapha : A pre-defined function used to displaye the output of boolean values in string : true or false

true is displayed when output is 1

false is displayed when output is 0

showpos : A pre-defined function used to display plus/positive sign "+" with boolean flag

+1 is displayed when output is 1

+0 is displayed when output is 0

a. cout << ("Jack" <= "John" && "Business" >= "Accounting") << endl;

OUTPUT : 0

Explanation:

To compare two strings , we compare each alphabet of string from left to right side one by one

1. If first alphabet of one string while comparing become greater or less than first alphabet of other string, we stop the comparison there and it becomes the final result.

Example : "Hello" and "Jam" are two strings to compare

We will start with first alphabet:

'H' and 'J' , as H< J, comparison will end here.

, Hello is less than Jam , and Jam will become greater string

2. If first or more alphabets of one string is equal to first or more alphabets of other string, then we move to the next succedding alphabet of both strings for comparing values.

Example : "Hello" and "Hi" are two strings to compare

We will start with first alphabet:

'H' and 'H' , as both are equal ,

We will move to next alphabet for comparison:

'e' and 'i' , as e< i , comparison will end here.

, Hello is less than Hi , and "Hi " will become greater string

3. If one string's length is greater than other string's length and alphabates are same for both string , except for the last alphabet for string which is having more length , then string with greater length will become the greater string

Example : "Hello" and "Helloy" are two strings to compare

We will start with first alphabet:

'H' and 'H' , as both are equal ,

We will move to next alphabet for comparison:

'e' and 'e' , as both are equal ,

Similarly till 'o', strings are equal

"Helloy" is string with greater length

Hence; "Hello" <"Helloy" and "Helloy" will become greater string

Note : Loop of comparing alphabet of both strings donot stop until , final result is achieved

Now, Firstly , we will compare strings:

"Jack" <= "John" --> True

Lets , start with first alphabets 'J' and 'J' -> both are equal , move to next alphabet

'a' and 'o' -> Since, "a" is smaller than 'o' , "a" <'o' ,

that means , "Jack" will become less than "John", hence , comparison is true

Similarly , "Business" >= "Accounting" -> False

Here, && -> Logical AND operator

if one value is False , when comparing with AND operator ; result will become False

("Jack" <= "John" && "Business" >= "Accounting") -->

True && False --> False

[if one value is False , when comparing with AND operator ; result will become False]

Boolean value of false is 0

Therefore, final result will be 0

___________________________________________

b. cout << boolalpha << ((10 <= 15 - 2) && (15 >= 20) || (15 - 2 != 20)) << endl;

OUTPUT : true

Here, && -> Logical AND operator

if one value is False , when comparing with AND operator ; result will become False

|| -> Logical OR operator

if one value is True , when comparing with OR operator ; result will become True

Firstly we compare the arithematic expressions:

10 <= 15 - 2 -> 10 <=13 -> True

15 >= 20 -> False

15 - 2 != 20 -> 13 != 20 -> True

Now : , we will start from left to right side for logical comparison

(10 <= 15 - 2) && (15 >= 20) -> True && False -> False

[if one value is False , when comparing with AND operator ; result will become False]

((10 <= 15 - 2) && (15 >= 20) || (15 - 2 != 20))

From previous equation :

False || (15 - 2 != 20) - > False || True -> True

[if one value is True , when comparing with OR operator ; result will become True]

Boolean value of True is 1

We have used boolalpha function here , so string value of boolean value will be displayed in result

Therefore, final result will be true

____________________________________________

c. cout << !(30 > 10) << endl;

OUTPUT : 0

Explanation:

! operator is used to Logical Not operator , It is used to complement the value :

if value is true , it will make it false

if value is false, it will convert it to true

Firstly we compare the arithematic expressions:

30 > 10 -> true

!(30 > 10) - > ! (true) -> false  

Boolean value of false is 0

Therefore, final result will be 0

__________________________

d. cout << showpos << (6 <= 5 || 3 < 15) << endl;

OUTPUT : +1

Explanation:

Here, || -> Logical OR operator

if one value is True , when comparing with OR operator ; result will become True

Firstly we compare the arithematic expressions:

6<=5 -> False

3<15 -> True

False || True -> True [ if one value is True , when comparing with OR operator ; result will become True]

Boolean value of True is 1

We have used showpos function here , so plus sign will be appended in front of the result

Therefore, final result will be +1

____________________________________________

e. cout << boolalpha << ("bill" > "billy") << endl;

OUTPUT : false

Explanation:

To compare two strings , we compare each alphabet of string from left to right side one by one

1. If first alphabet of one string while comparing become greater or less than first alphabet of other string, we stop the comparison there and it becomes the final result.

Example : "Hello" and "Jam" are two strings to compare

We will start with first alphabet:

'H' and 'J' , as H< J, comparison will end here.

, Hello is less than Jam , and Jam will become greater string

2. If first or more alphabets of one string is equal to first or more alphabets of other string, then we move to the next succedding alphabet of both strings for comparing values.

Example : "Hello" and "Hi" are two strings to compare

We will start with first alphabet:

'H' and 'H' , as both are equal ,

We will move to next alphabet for comparison:

'e' and 'i' , as e< i , comparison will end here.

, Hello is less than Hi , and "Hi " will become greater string

3. If one string's length is greater than other string's length and alphabates are same for both string , except for the last alphabet for string which is having more length , then string with greater length will become the greater string

Example : "Hello" and "Helloy" are two strings to compare

We will start with first alphabet:

'H' and 'H' , as both are equal ,

We will move to next alphabet for comparison:

'e' and 'e' , as both are equal ,

Similarly till 'o', strings are equal

"Helloy" is string with greater length

Hence; "Hello" <"Helloy" and "Helloy" will become greater string

Note : Loop of comparing alphabet of both strings donot stop until , final result is achieved

Now, Firstly , we will compare strings:

"bill" > "billy" -> false

Here, "billy" is word with greater length

and both the string have same alphabets except last alphabet for string with larger length

So, "billy" is the greater string

which , makes this arithematic expression , "bill" > "billy" -> false

Boolean value of false is 0

We have used boolalpha function here , so string value of boolean value will be displayed in result

Therefore, final result will be false


Related Solutions

What is the output of the code below? After each line that has a cout, write...
What is the output of the code below? After each line that has a cout, write what the output of the code is. See example in code below. int i = 2, j = 1, k = i + j; cout << k << endl; ➔ 3 double d = 2.99, e = 1, f = d + e; cout << f << endl; double q = 1.50; cout << q/3 << endl; cout << 6.0/4 << endl; char c...
John and Jack found a coin on the sidewalk. They argued about the fairness of the...
John and Jack found a coin on the sidewalk. They argued about the fairness of the coin. John claimed 40% to have Heads according to his careful observation of the coin. Jack doubted and in order to infer the fairness of the coin, he tossed the coin for 50 times and got the results as shown below with 1 representing as heads and 0 as tails: ## [1] 0 0 0 1 1 1 0 0 0 0 0 0...
John and Jack found a coin on the sidewalk. They argued about the fairness of the...
John and Jack found a coin on the sidewalk. They argued about the fairness of the coin. John claimed 40% to have Heads according to his careful observation of the coin. Jack doubted and in order to infer the fairness of the coin, he tossed the coin for 50 times and got the results as shown below with 1 representing as heads and 0 as tails: ## [1] 0 0 0 1 1 1 0 0 0 0 0 0...
C++ i want .00 at the output cout << fixed << setprecision (2);where should i put...
C++ i want .00 at the output cout << fixed << setprecision (2);where should i put this line? quesstion 13. Array of Payroll Objects Design a PayRoll class that has data members for an employee’s hourly pay rate and number of hours worked. Write a program with an array of seven PayRoll objects. The program should read the number of hours each employee worked and their hourly pay rate from a file and call class functions to store this information...
Jack and Jills, a retail business, has supplied the following information in relation to their actual...
Jack and Jills, a retail business, has supplied the following information in relation to their actual sales in 2017 and planned sales for the first quarter of 2018.                                                                                                   Cash         Credit                                               Total                                                                                                           $                    $                                                   $ 2017 Actual Sales for                      November       25,000        25,000                                               50,000                                                                  December       35,000        40,000                                               75,000 2018 Estimated Sales for              January            15,000        20,000                                               35,000                                                                 February          18,000        30,000                                               48,000                                                                 March                22,000        40,000                                               62,000 Past records indicate that expected receipts collected from debtors...
Jack and Jills, a retail business, has supplied the following information in relation to their actual...
Jack and Jills, a retail business, has supplied the following information in relation to their actual sales in 2017 and planned sales for the first quarter of 2018.                                                                                                   Cash         Credit                                               Total                                                                                                           $                    $                                                   $ 2017 Actual Sales for                      November       25,000        25,000                                               50,000                                                         December       35,000        40,000                                               75,000 2018 Estimated Sales for January          15,000        20,000 35,000                                                         February        18,000 30,000 48,000 March            22,000        40,000    62,000 Past records indicate that expected receipts collected from debtors will be:               60% in the...
Jack, a geologist, opened a business organized as a C corporation called Geo-Jack (GJ) in January...
Jack, a geologist, opened a business organized as a C corporation called Geo-Jack (GJ) in January of this year. Jack is the sole shareholder. Assume GJ reports on a calendar year and uses the accrual method of accounting. For each item below, indicate its effect on Jack’s taxable income and you must clearly indicate whether it is positive or negative. In January, GJ rented a small business office about 12 miles from Jack’s home. GJ paid $14,000, which represented a...
Jack, a geologist, opened a business organized as a C corporation called Geo-Jack (GJ) in January...
Jack, a geologist, opened a business organized as a C corporation called Geo-Jack (GJ) in January of this year. Jack is the sole shareholder. Assume GJ reports on a calendar year and uses the accrual method of accounting. For each item below, indicate its effect on Jack’s taxable income and you must clearly indicate whether it is positive or negative. In an attempt to get his name and new business recognized, GJ paid $9,000 for a one-page ad in the...
The following events occurred at Jack Company during its first year of business: a. To establish...
The following events occurred at Jack Company during its first year of business: a. To establish the company, the two owners contributed a total of $60,000 in exchange for common stock. b. Grooming service revenue for the first year amounted to $175,000, of which $50,000 was on account. c. Customers owe $15,000 at the end of the year from the services provided on account. d. At the beginning of the year, a storage building was rented. The company was required...
A hydraulic jack is designed to lift a car by exerting an output force 100 times...
A hydraulic jack is designed to lift a car by exerting an output force 100 times as large as the input (see class notes). (a) What must be the ratio of the cylinder diameter on the output side to that on the input side? (b) If the input side is pushed down by 10 cm, how much does the output side move up? (The fluid volume lost on the input side is gained on the output side.) (c) Show that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT