Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following code segment. int j = 1; while (j 1; k--) { System.out.println(ha); // line 6 } j++; } How many times will

Consider the following code segment.

int j = 1;

while (j <= 5)

{

for (int k = 4; k > 1; k--)

{

System.out.println("ha"); // line 6

}

j++;

}

How many times will the print statement on line 6 execute?

  • 15

    A

  • 16

    B

  • 20

    C

  • 24

    D

  • 25

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following method.

    public String mystery(String word, int num)

    {

    String result = "";

    for (int k = num; k >= 0; k--)

    {

    result += word.substring(0, k);

    }

    return result;

    }

    What is returned as a result of the call mystery("computer", 3) ?

  • ccocom

    A

  • comcoc

    B

  • ccocomcomp

    C

  • compcomcoc

    D

  • comcomcomcom

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the method digitSum below, which takes a positive integer parameter as input.

    public int digitSum(int num)

    {

    int total = 0;

    while (num > 0)

    {

    total += num % 10;

    num /= 10;

    }

    return total;

    }

    Which of the following code segments could replace the while loop in the method digitSum without changing the value returned by the method?

    I.

    for (int h = 0; h < num; h++)

    {

    total += num % 10;

    num /= 10;

    }

    II.

    for (int j = num; j > 0; j--)

    {

    total += j % 10;

    }

    III.

    for (int k = num; k > 0; k /= 10)

    {

    total += k % 10;

    }

  • I only

    A

  • II only

    B

  • III only

    C

  • I and II

    D

  • II and III

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5.

    int num = 12345;

    int sum = 0;

    /* missing loop header */

    {

    sum += num % 10;

    num /= 10;

    }

    System.out.println(sum);

    Which of the following should replace /* missing loop header */ so that the code segment will work as intended?

  • while (num > 0)

    A

  • while (num >= 0)

    B

  • while (num > 1)

    C

  • while (num > 2)

    D

  • while (num > sum)

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    int count = 0;

    int number = 20;

    while (number > 0)

    {

    number = number / 2;

    count++;

    }

    What will be the value of count after executing the code segment?

  • 6

    A

  • 5

    B

  • 4

    C

  • 1

    D

  • 0

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segments, which differ only in their loop header.

    Code Segment I

    for (int i = 0; i < 10; i++)

    {

    System.out.print( "*" );

    }

    Code Segment II

    for (int i = 1; i <= 10; i++)

    {

    System.out.print( "*" );

    }

    Which of the following best explains how the difference in the two loop headers affects the output?

  • The output of the code segments is the same because the loops in each code segment terminate when i is 10.

    A

  • The output of the code segments is the same because the loops in both code segments iterate 10 times.

    B

  • The output of the code segments is different because code segment I iterates from i = 0 to i = 9 and code segment II iterates from i = 1 to i = 10.

    C

  • The output of the code segments is different because code segment I iterates from i = 0 to i = 10 and code segment II iterates from i = 1 to i = 11.

    D

  • Neither code segment produces output because both loop conditions are initially false.

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    for (int j = 0; j < 4; j++)

    {

    for (int k = 0; k < j; k++)

    {

    System.out.println("hello");

    }

    }

    Which of the following best explains how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment?

  • The output of the code segment will be unchanged.

    A

  • The string "hello" will be printed three fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop.

    B

  • The string "hello" will be printed four fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop.

    C

  • The string "hello" will be printed three additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

    D

  • The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    for (int j = 0; j < 4; j++)

    {

    for (int k = 0; k < j; k++)

    {

    System.out.println("hello");

    }

    }

    Which of the following best explains how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment?

  • The output of the code segment will be unchanged.

    A

  • The string "hello" will be printed three fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop.

    B

  • The string "hello" will be printed four fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop.

    C

  • The string "hello" will be printed three additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

    D

  • The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    String str = "AP-CSA";

    for (int i = 0; i < str.length(); i++)

    {

    if (str.substring(i, i + 1).equals("A"))

    {

    System.out.print(i + " ");

    }

    }

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

  • 0

    A

  • 5

    B

  • 0 5

    C

  • 0 6

    D

  • 1 6

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    int total = 0;

    for (int k = 0; k <= 100; k += 2)

    {

    total += k;

    }

    Which of the following for loops could be used to replace the for loop in the original code segment so that the original and the revised code segments store the same value in total?

  • for (int k = 0; k < 100; k += 2)

    {

    total += k + 1;

    }

    A

  • for (int k = 1; k < 101; k += 2)

    {

    total += k - 1;

    }

    B

  • for (int k = 0; k <= 101; k += 2)

    {

    total += k + 1;

    }

    C

  • for (int k = 1; k <= 101; k += 2)

    {

    total += k + 1;

    }

    D

  • for (int k = 1; k <= 101; k += 2)

    {

    total += k - 1;

    }

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    int a = 100;

    while (a > 1)

    {

    System.out.println("$");

    a /= 2;

    }

    How many $s are printed as a result of executing the code segment?

  • 0

    A

  • 5

    B

  • 6

    C

  • 7

    D

  • 50

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    int a = 1;

    String result = "";

    while (a < 20)

    {

    result += a;

    a += 5;

    }

    System.out.println(result);

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

  • 21

    A

  • 161116

    B

  • 161161

    C

  • 16111621

    D

  • Nothing is printing because of an infinite loop.

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following method.

    public static int mystery(String string1, String string2)

    {

    String temp = string1;

    int position = 0;

    int result = 0;

    while(temp.indexOf(string2) >= 0)

    {

    position = temp.indexOf(string2);

    result++;

    temp = temp.substring(position + 1);

    }

    return result;

    }

    The following code segment appears in another method in the same class.

    System.out.println(mystery("Mississippi", "si"));

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

  • 0

    A

  • 1

    B

  • 2

    C

  • 3

    D

  • Nothing is printed.

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    for (int k = 0; k < 4; k++)

    {

    /* missing loop header */

    {

    System.out.print(k);

    }

    System.out.println();

    }

    The code segment is intended to produce the following output.

    0

    11

    222

    3333

    Which of the following can be used to replace /* missing loop header */ so that the code segment will work as intended?

  • for (int h = 0; h < k; h++)

    A

  • for (int h = 1; h < k + 1; h++)

    B

  • for (int h = 0; h < 3; h++)

    C

  • for (int h = k; h >= 0; h--)

    D

  • for (int h = k; h <= 0; h--)

    E

  • ___________________________________________________________________________________________________________________________________

  • Consider the following code segment.

    int n = 6;

    for (int i = 1; i < n; i = i + 2) // Line 2

    {

    System.out.print(i + " ");

    }

    Which of the following best explains how changing i < n to i <= n in line 2 will change the result?

  • An additional value will be printed because the for loop will iterate one additional time.

    A

  • One fewer value will be printed because the for loop will iterate one fewer time.

    B

  • There will be no change to the program output because the loop will iterate the same number of times.

    C

  • An infinite loop will occur because the loop condition will never be false.

    D

  • The body of the loop will not execute at all because the loop condition will initially be false.

    E

  • ___________________________________________________________________________________________________________________________________

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

Write a letter asking them to refund your $1,500 down payment.

Answered: 1 week ago