Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following code segment. boolean a = true; boolean b = false; System.out.print((a == !b) != false); What is printed as a result of

Consider the following code segment.

boolean a = true;

boolean b = false;

System.out.print((a == !b) != false);

What is printed as a result of executing this code segment?

  • false

  • true

  • 0

  • 1

  • Nothing is printed because the expression (a == !b) != false is an invalid parameter to the System.out.print method.

  • The following code segment prints one or more characters based on the values of boolean variables b1 and b2. Assume that b1 and b2 have been properly declared and initialized.

    if (!b1 || b2)

    {

    System.out.print("A");

    }

    else

    {

    System.out.print("B");

    }

    if (!(b1 || b2))

    {

    System.out.print("C");

    }

    else

    {

    System.out.print("D");

    }

    if (b1 && !b1)

    {

    System.out.print("E");

    }

    If b1 and b2 are both initialized to true, what is printed when the code segment has finished executing?

  • ABCD

  • ABD

  • AD

  • BD

  • BDE

  • Consider the following code segment.

    String str1 = new String("Happy");

    String str2 = new String("Happy");

    System.out.print(str1.equals(str2) + " ");

    System.out.print(str2.equals(str1) + " ");

    System.out.print(str1 == str2);

    What is printed as a result of executing the code segment?

  • true true true

  • true true false

  • false true false

  • false false true

  • false false false

  • Consider the following two code segments. Assume that variables x and y have been declared as int variables and have been assigned integer values.

    I.

    int result = 0;

    if (x > y)

    {

    result = x - y;

    System.out.print(result);

    }

    else if (x < y)

    {

    result = y - x;

    System.out.print(result);

    }

    else

    {

    System.out.print(result);

    }

    II.

    if (x < y)

    {

    System.out.print(y - x);

    }

    else

    {

    System.out.print(x - y);

    }

    Which of the following correctly compares the outputs of the two code segments?

  • Code segment I and code segment II produce the same output for all values of x and y.

  • Code segment I and code segment II produce the same output only when x is equal to y.

  • Code segment I and code segment II produce the same output only when x is not equal to y.

  • Code segment I and code segment II produce the same output only when x is less than y.

  • Code segment I and code segment II do not produce the same output for any values of x and y.

  • Consider the following two code segments, which are both intended to determine the longest of the three strings "pea", "pear", and "pearl" that occur in String str. For example, if str has the value "the pear in the bowl", the code segments should both print "pear" and if str has the value "the pea and the pearl", the code segments should both print "pearl". Assume that str contains at least one instance of "pea".

    I.

    if (str.indexOf("pea") >= 0)

    {

    System.out.println("pea");

    }

    else if (str.indexOf("pear") >= 0)

    {

    System.out.println("pear");

    }

    else if (str.indexOf("pearl") >= 0)

    {

    System.out.println("pearl");

    }

    II.

    if (str.indexOf("pearl") >= 0)

    {

    System.out.println("pearl");

    }

    else if (str.indexOf("pear") >= 0)

    {

    System.out.println("pear");

    }

    else if (str.indexOf("pea") >= 0)

    {

    System.out.println("pea");

    }

    Which of the following best describes the output produced by code segment I and code segment II?

  • Both code segment I and code segment II produce correct output for all values of str.

  • Neither code segment I nor code segment II produce correct output for all values of str.

  • Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pear" but not "pearl".

  • Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pearl".

  • Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".

  • Consider the following code segment.

    String first = new String("duck");

    String second = new String("duck");

    String third = new String("goose");

    if (first == second)

    {

    System.out.print("A");

    }

    else if (second == third)

    {

    System.out.print("B");

    }

    else if (first.equals(second))

    {

    System.out.print("C");

    }

    else if (second.equals(third))

    {

    System.out.print("D");

    }

    else

    {

    System.out.print("E");

    }

    What is printed as a result of executing the code segment?

  • A

  • B

  • C

  • D

  • E

  • In the following expression, sweet, salty, and sour are properly declared and initialized boolean variables.

    sweet && (salty || sour)

    Which of the following expressions is equivalent to the expression above?

  • (sweet && salty) || sour

  • (sweet && salty) || (sweet && sour)

  • (sweet && salty) && (sweet && sour)

  • (sweet || salty) && sour

  • (sweet || salty) && (sweet || sour)

  • Consider the following code segment.

    int m = 8;

    int n = 3;

    if (m + n > 10)

    {

    System.out.print(m + n);

    }

    if (m - n > 0)

    {

    System.out.print(m - n);

    }

    What, if anything, is printed as a result of executing the code segment?

  • Nothing is printed.

  • 5

  • 11

  • 115

  • 511

  • Consider the following code segment, which is intended to set the Boolean variable inRange to true if the integer value num is greater than min value and less than max value. Otherwise inRange is set to false. Assume that inRange, num, min, and max have been properly declared and initialized.

    boolean isBigger;

    boolean isSmaller;

    boolean inRange;

    if (num < max)

    {

    isSmaller = true;

    }

    else

    {

    isSmaller = false;

    }

    if (num > min)

    {

    isBigger = true;

    }

    else

    {

    isBigger = false;

    }

    if (isBigger == isSmaller)

    {

    inRange = true;

    }

    else

    {

    inRange = false;

    }

    Which of the following values of num, min, and max can be used to show that the code does NOT work as intended?

  • num = 20, min = 30, max = 50

  • num = 30, min = 20, max = 40

  • num = 40, min = 10, max = 40

  • num = 50, min = 50, max = 50

  • num = 60, min = 40, max = 50

  • In the following expression, sunny and windy are properly declared and initialized boolean variables.

    !sunny && !windy

    Which of the following is equivalent to the expression above?

  • sunny || windy

  • !sunny || !windy

  • !(sunny || windy)

  • !(sunny && windy)

  • !(sunny && !windy)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions