Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the following Boolean variables: bool pass = true, open = false, run = tru

ID: 3820138 • Letter: G

Question

Given the following Boolean variables:

bool pass = true, open = false, run = true;

Using a truth table, which of the following Boolean expressions has the logical value “false”?

a. pass && !open || !pass && run

b. !pass || open && pass || run

c. pass && !open || pass || !run

d. !pass && !open || !pass && run

What value would the variable price hold after the following code is executed?

int price;

int tier = 2;

int year = 1995;

switch (tier)

{

case 1:

case 2:

    if (year < 1990)

      price = 300;

    else

      price = 400;

    break;

case 3:

case 4:

    if (year < 2000)

       price = 500;

    else

       price = 600;

break;

Select one:

a. 300

b. 400

What would the value of payment be after the following code is executed?

  

    char residency = 'N';

    int payment, items = 300;

    if (items > 400)

       if (residency == 'N')

          payment = 2500;

       else

          payment = 3000;

    else if (items > 200)

       if (residency == 'R')

          payment = 1500;

       else

          payment = 1000;

Select one:

a. 1000

b. 1500

c. 2500

d. 3000

Which of the following is a valid implementation of a void method named Subtract ?

A.

void Subtract (x, y)

{double z;

z = x - y;

        System.out.println("Result = ", z);

}

B.

Subtract (double x, double y)

    {

        double z = x - y;

        System.out.println("Result = ", z);

}

C.

void Subtract (double x, y)

{z = x - y;

        System.out.println("Result = ", z);

}

D.

void Subtract (x, y)

    {

        System.out.println("Result = ", x - y);

}

Which of the following correctly implements a method named Slowest ?

A.

void Slowest (double a, b, c)

    {

        if (a < b && a < c)

            System.out.println(a);

        else if (b < c)

            System.out.println(b);

        else

              System.out.println(c);

  }

B.

Slowest (double a, double b, double c)

   {

       if (a < b || a < c)

           System.out.println(a);

       else if (b < c)

           System.out.println(b);

       else

System.out.println(c);

}

C.

     

void Slowest (double a, double b, double c)

   {

       if (a < b && a < c)

           System.out.println(a);

     else if (b < c)

           System.out.println(b);

       else

System.out.println(c);

}

D.

     

void Slowest (double a, double b, double c)

   {

       if (a < b || a < c)

           System.out.println(a);

       else if (b < c)

           System.out.println(b);

       Else

            System.out.println(c);

}

Explanation / Answer

1) Given- pass=true, open=false, run=true

Note that for the && operator if any one of the operands is FALSE the result is FALSE while for the || operator if any one of the operands is TRUE the result is also TRUE. And for the ! Operator, the result is always the opposite of the operand (! has only one operand unlike the other two mentioned operators which have two operands)

Also, note the precedence of the logical operators. The Negation operator(!) has the highest precedence and so is evaluated first. The AND (&&) operator has the next precedence and will be evaluated second if the statement has both ! and && operations. The OR operator (||) is evaluated last as it has the lowest precedence amoong these three operators.

This is how we get all the below results.

a) pass && !open || !pass && run=?

First consider the Truth tables for !open and !pass as the negation operations are evaluated first:-

For !open

open

!open

FALSE

TRUE

Similarly for !pass:-

pass

!pass

TRUE

FALSE

Next, the logical AND, && operation will be evaluated according to operator precedence.

So, pass && !open and !pass && run will be evaluated next. Now, using the above results,

for pass && !open, using the 1st truth table above:-

pass

!open

pass && !open

TRUE

TRUE

TRUE

for !pass && run, using the 2nd truth table above:-

!pass

run

!pass && run

FALSE

TRUE

FALSE

(because TRUE && TRUE = TRUE and FALSE && TRUE = FALSE)

Finally, the logical OR, || operation will be evaluated. So we will take all the results obtained above and combine them with the || operator as required by:-

For, pass && !open || !pass && run, using the 3rd and 4th truth tables above,

pass && !open

!pass && run

pass && !open || !pass && run

TRUE

FALSE

TRUE

(because TRUE || FALSE == TRUE)

Therefore, pass && !open || !pass && run = TRUE.

b) !pass || open && pass || run

Once again, evaluate the negation operation first as it has the highest precedence.

for !pass:-

pass

!pass

TRUE

FALSE

Next evaluate the AND, && operation as it is next in precedence.

For open && pass:-

open

pass

open && pass

FALSE

TRUE

FALSE

Finally, we have the two || operations. These will be evaluated from left to right.

So, !pass || open && pass will be evaluated first.

For, !pass || open && pass:-

!pass

open && pass

! pass || open && pass

FALSE

FALSE

FALSE

And then the next || operation, (!pass || open && pass) || run, will be evaluated; parentheses have been added to aid understanding.

So, for !pass || open && pass || run,

! pass || open && pass

run

!pass || open && pass || run

FALSE

TRUE

TRUE

Therefore, !pass || open && pass || run = TRUE

c) pass && !open || pass || !run

Again, the ! Operations are evaluated first.

For !open:-

open

!open

FALSE

TRUE

For !run:-

run

!run

TRUE

FALSE

Next, the && operation is evaluated.

For pass && !open:-

pass

!open

pass && !open

TRUE

TRUE

TRUE

Next, the || operations are evaluated from left to right; parentheses added for understanding.

For, (pass && !open) || pass:-

pass && !open

pass

pass && !open || pass

TRUE

TRUE

TRUE

For, (pass && !open || pass) || !run:-

pass && !open || pass

!run

pass && !open || pass || !run

TRUE

FALSE

TRUE

Therefore, pass && !open || pass || !run = TRUE

d)!pass && !open || !pass && run.

Again, evaluate the ! operations first, followed by the && operations from left to right and finally the || operation.

For !pass:-

pass

!pass

TRUE

FALSE

For !open:-

open

!open

FALSE

TRUE

For, !pass && !open:-

!pass

!open

!pass && !open

FALSE

TRUE

FALSE

For, !pass && run:-

!pass

run

!pass && run

FALSE

TRUE

FALSE

Finally, for (!pass && !open) || (!pass && run).. parentheses added for understanding:-

!pass && !open

!pass && run

!pass && !open || !pass && run

FALSE

FALSE

FALSE

Therefore, !pass && !open || !pass && run=FALSE

The value 1995 is assigned to the variable, year and the value 2 is assigned to the variable, tier which later goes through the switch statement. Since the value of tier is 2, the statements after 'case 2:' are executed. Here the value of the variable, year is checked. Since the value of year (1995) is greater than 1990, the statement in the else part of the if ...else ... statement is esxecuted; which assigns the value 400 to the variable price. This is followed by the break; statement which causes the control to break out of the switch statement. So, the value of price at the end of the given code is 400.

First the values 'N' and 300 are assigned to the variables residency and items respectively.

Next, the value of items is checked. Since items is not greater than 400, the program control does not enter the if part of the first if...else... statement but rather will enter its else part., which in turn checks if the value of items is greater than 200. Since it is (value of items=300>200), the control enters the if block of this if...else statement, which itself contains another if statement checking of the value of residency is equal to 'R'. Since it isn't (residency was previously assigned the value 'N'), the control will go to the else part of this current if-else statement and execute the statement payment=1000; assigining 1000 to payment. So, the answer for this is a. 1000.

The correct syntax for defining a function is:-

<access type> <return type> <function name>(<data type1> <variable1>,<data type2> <variable2, ...<data type n> <variable n>){ ....}

So, A is not valid as data types of the parameters have not been mentioned.

B is not valid as the function's return type is not mentioned. We have to mention 'void' even if the function does not return any value.

C is not valid as the data type of the 2nd parameter is not mentioned.

D is also not valid as data types of the parameters have not been mentioned in this either.

So, none of the given options are valid.

A. is syntactically incorrect as stated above.

The purpose of this function seems to be to print the smallest of given numbers. If so, only C is correct as a will be printed only if it is greater than both the other numbers. If it is greater than any of b or c, we don't need to consider it and only need to compare b & c and print the smallest of these two. This is exactly what option C does.

Hope this helps!

open

!open

FALSE

TRUE

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote