Semester 1 Unit 4

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/35

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

36 Terms

1
New cards

Consider the following code segment.

for (int r = 3; r > 0; r--)
{
int c;
for (c = 1; c < r; c++)
{
System.out.print("-");
}
for (c = r; c <=3; c++)
{
System.out.print("*");
}
System.out.println();
}

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

 

  • *--

    **-

    ***

  • ***

    -**

    --*

     

  • ***

    **-

    *--

  • --*

    -**

    ***

  • --*

    ***

    --*

  • --*

    -**

    ***

2
New cards

Consider the following code segment.

int outerMax = 10;
int innerMax = 5;

for (int outer = 0; outer < outerMax; outer++)
{
for (int inner = 0; inner <= innerMax; inner++)
{
System.out.println(outer + inner);
}
}

How many values will be printed when the code segment is executed?

  • 45

  • 66

  • 50

  • 55

  • 60

  • 60

3
New cards

Consider the following code segment.

for (int k = 0; k < 20; k = k + 2)
{
if (k % 3 == 1)
{
System.out.print(k + " ");
}
}

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

  • 4 16

  • 1 4 7 10 13 16 19

  • 4 10 16

  • 0 2 4 6 8 10 12 14 16 18

  • 0 6 12 18

  • 4 10 16

4
New cards

Consider the following code segment.

for (int k = 1; k <= 7; k += 2)
{
    System.out.print(k);
}

Which of the following code segments will produce the same output as the code segment above?

 

for (int k = 0; k <= 7; k += 2 )
{
    System.out.print (k) ;
}

 

for (int k = 1; k < 7; k += 2)
{
    System.out.print(k + 1);
}

 

for (int k = 1; k <= 8; k += 2)
{
    System.out.print(k);
}

 

for (int  k = 0; k < 7; k += 2)
{
    System.out.print (k);
}

 

for (int k = 0; k <= 8; k += 2)
{
    System.out.print(k + 1);
}

for (int k = 1; k <= 8; k += 2)
{
    System.out.print(k);
}

5
New cards

Consider the following code segment.

int counter = 0;

for (int x = 10; x > 0; x--)
{
    for (int y = x; y <= x; y++)
    {
        counter++; // line 6
    }
}

How many times will the statement in line 6 be executed as a result of running the code segment?

  • 1

  • 20

  • 10

  • 11

  •   10

6
New cards

Consider the following code segment.

int num = 1;

for (int k = 2; k < 10; k++)
{
    num = 0;
    num = num + k;
}

What will be the value of num after the loop is executed?
  • 9

  • 44

  • 2

  • 45

  • 10

9

7
New cards

Consider the following methods.

/** Precondition: Precondition: a > 0 and b > 0 */

public static int methodOne(int a, int b)
{
    int loopCount = 0;
    for (int i = 0; i < a / b; i++)
    {
        loopCount++;
    }
    return loopCount;
}

/** Precondition: Precondition: a > 0 and b > 0 */

public static int methodTwo(int a, int b)
{
    int loopCount = 0;
    int i = 0;
    while (i < a)
    {
        loopCount++;
        i += b;
    }
    return loopCount;
}

Which of the following best describes the conditions under which methodOne and methodTwo return the same value?

  • When a%b is equal to one

  • When a and b are both odd 

  • When a and b are both even

  • When a is even and b is odd 

  • When a%b is equal to zero

  • When a%b is equal to zero

8
New cards

The question refer to the following code segment.

int k = a random number such that 1 <= k <= n;

for (int p = 2; p <= k; p++)

for (int r = 1; r < k; r++)

System.out.println("Hello");

What is the minimum number of times that Hello will be printed?

  • 2

  • n - 1

  • 1

  • 0

  • n - 2

  • 0

9
New cards

Consider the following code segment.

int x = 1;

while ( /* condition */)

{

if (x % 2 == 0)

{

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

}

x = x + 2;

}

The following conditions have been proposed to replace /* condition */ in the code segment.

I. x < 0

II. x <= 1

III. x < 10

For which of the conditions will nothing be printed?

  • II only

  • I and II only

  • I only

  • I and III only

  • I, II, and III

  • I, II, and III

10
New cards

Which of the following code segments will print all multiples of 5 that are greater than 0 and less than 100 ?

I.

for (int k = 1; k < 100; k++)

{

if (k % 5 == 0)

{

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

}

}

II.

for (int k = 1; k < 100; k++)

{

if (k / 5 == 0)

{

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

}

}

III.

int k = 5;

while (k < 100)

{

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

k = k + 5;

}

  • I and III

  • II only

  • III only

  • I only

  • II and III

  • I and III

11
New cards

Consider the following method definition. The method printAllCharacters is intended to print out every character in str, starting with the character at index 0.

public static void printAllCharacters(String str)

{

for (int x = 0; x < str.length(); x++) // Line 3

{

System.out.print(str.substring(x, x + 1));

}

}

The following statement is found in the same class as the printAllcharacters method.

printAllCharacters("ABCDEFG");

Which choice best describes the difference, if any, in the behavior of this statement that will result from changing x < str.length () to x <= str.length () in line 3 of the method?

  • The method call, which worked correctly before the change, will now cause a run-time error because it attempts to access a character at index 8 in a string whose last element is at index 7.

  • The behavior of the code segment will remain unchanged.

  • The method call will print more characters than it did before the change because the loop will iterate more times.

  • The method call, which worked correctly before the change, will now cause a run-time error because it attempts to access a character at index 7 in a string whose last element is at index 6 .

  • The method call will print fewer characters than it did before the change because the loop will iterate fewer times.

  • The method call, which worked correctly before the change, will now cause a run-time error because it attempts to access a character at index 7 in a string whose last element is at index 6 .

12
New cards

Consider the following code segment.

int count = 0;

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

{

count++;

}

System.out.println(count);

Which of the following code segments will produce the same output as the code segment above?

int count = 0;

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

{

count++;

}

System.out.println(count);

int count = 1;

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

{

count++;

}

System.out.println(count);

int count = 0;

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

{

count++;

}

System.out.println(count);

int count = 0;

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

{

count++;

}

System.out.println(count);

int count = 1;

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

{

count++;

}

System.out.println(count);

int count = 0;

for (int k = 9; k >= 0; k--)
{
count++;
}
System.out.println(count);

13
New cards

Consider the following method.

public int mystery(int num)

{

int x = num;

while (x > 0)

{

if (x / 10 % 2 == 0)

return x;

x = x / 10;

}

return x;

}

What value is returned as a result of the call mystery(1034)?

  • 4

  • 103

  • 1034

  • 10

  • 34

  • 103

14
New cards

Consider the following method.

public int compute(int n, int k)

{

int answer = 1;

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

answer *= n;

return answer;

}

Which of the following represents the value returned as a result of the call compute(n, k)?

  • n*k

  • n!

  • kn

  • 2k

  • n^k

  • n^k

15
New cards

Consider the following method.

public static String changeStr(String str)

{

String result = "";

for (int i = str.length() - 1; i >= str.length() / 2; i -= 2)

{

result += str.substring(i, i + 1);

}

return result;

}

What value is returned as a result of the method call changestr ("12345") ?

  • "543"

  • "531"

  • "53"

  • "4"

  • "54321"

  • "53"

16
New cards

Consider the following code segment.

int num = 1;

int count = 0;

while (num <= 10)

{

if (num % 2 0 && num % 3 0) { count++; } num++;

}

What value is stored in the variable count as a result of executing the code segment?

  • 3

  • 8

  • 1

  • 5

  • 7

  • 1

17
New cards

Consider the following code segment.

for (int outer = 0; outer < n; outer++)

{

for (int inner = 0; inner <= outer; inner++)

{

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

}

}

If n has been declared as an integer with the value 4 , what is printed as a result of executing the code segment?

  • 0 0 1 0 1 2 0 1 2 3

  • 0 0 1 0 1 2

  • 0 1 2 3

  • 0 1 1 2 2 2 3 3 3 3

  • 0 1 2 2 3 3 3

  • 0 1 1 2 2 2 3 3 3 3

18
New cards

Consider the following code segment.

int k = 1;

while (k < 20)

{

if ((k % 3) == 1)

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

k++;

}

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

  • 1 4 7 10 13 16 19

  • 3 6 9 12 15 18

  • 1 3 5 7 9 11 13 15 17 19

  • 2 4 6 8 10 12 14 16 18 20

  • 2 5 8 11 14 17

  • 1 4 7 10 13 16 19

19
New cards

Consider the following output.

1 1 1 1 1

2 2 2 2

3 3 3

4 4

5

Which of the following code segments will produce this output?

for(int j = 1; j <= 5; j++)

{

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

{

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

}

System.out.println();

}

for(int j = 1; j <= 5; j++)

{

for(int k = 1; k <= 5; k++)

{

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

}

System.out.println();

}

for(int j = 1; j <= 5; j++)

{

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

{

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

}

System.out.println();

}

for(int j = 1; j <= 5; j++)

{

for(int k = 5; k >= j; k--)

{

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

}

System.out.println();

}

for(int j = 1; j <= 5; j++)

{

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

{

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

}

System.out.println();

}

for(int j = 1; j <= 5; j++)
{
 for(int k = 5; k >= j; k--)
{
System.out.print(j + " ");
}
  System.out.println();
}

20
New cards

Consider the following code segment.

int sum = 0;

int k = 1;

while(sum < 12 || k < 4)

sum += k;

System.out.println(sum);

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

  • 15

  • 10

  • Nothing is printed due to an infinite loop.

  • 6

  • 12

  • Nothing is printed due to an infinite loop.

21
New cards

Consider the following method.

/* Precondition: num > 0 */

public static int doWhat(int num)

{

int var = 0;

for(int loop = 1; loop <= num; loop = loop + 2)

{

var += loop;

}

return var;

}

Which of the following best describes the value returned from a call to doWhat?

  • No value is returned because of an infinite loop.

  • The sum of all even integers between 1 and num, inclusive

  • num

  • The sum of all odd integers between 1 and num, inclusive

  • The sum of all integers between 1 and num, inclusive

  • The sum of all odd integers between 1 and num, inclusive

22
New cards

The following method is intended to print the number of digits in the parameter num.

public int numDigits(int num)

{

int count = 0;

while (/* missing condition */)

{

count++;

num = num / 10;

}

return count;

}

Which of the following can be used to replace / missing condition / so that the method will work as intended?

  • num != 0

  • count > 0

  • num >= 0

  • count != 0

  • num == 0

  • num != 0

23
New cards

Which of the following code segments will print all multiples of 5 that are greater than 0 and less than 100 ?

I.

for (int k = 1; k < 100; k++)

{

if (k % 5 == 0)

{

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

}

}

II.

for (int k = 1; k < 100; k++)

{

if (k / 5 == 0)

{

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

}

}

III.

int k = 5;

while (k < 100)

{

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

k = k + 5;

}

  • II and III

  • I only

  • II only

  • I and III

  • III only

  • I and III

24
New cards

Consider the following code segment, which is intended to print the sum of all the odd integers from 0 up to and including 101.

int r = 0;

int sum = 0;

/* missing loop header */

{

if (r % 2 == 1)

{

sum += r;

}

r++;

}

System.out.println(sum);

Which of the following could replace /* missing loop header */ to ensure that the code segment will work as intended?

  • while (r <= 100)

  • while (r <= 101)

  • while (sum <= 100)

  • while (sum <= 101)

  • while (r < 101)

  • while (r <= 101)

25
New cards

Consider the following code segment. Assume that num3 > num2 > 0.

int num1 = 0;

int num2 = /* initial value not shown */;

int num3 = /* initial value not shown */;

while (num2 < num3)

{

num1 += num2;

num2++;

}

Which of the following best describes the contents of num1 as a result of executing the code segment?

  • The sum of all integers from num2 to num3 - 1 , inclusive

  • The sum of num2 and num3

  • The product of num2 and num3

  • The sum of all integers from num2 to num3, inclusive

  • The product of num2 and num3 - 1

  • The sum of all integers from num2 to num3 - 1 , inclusive

26
New cards

Consider the following code segment.

int k = 0;

/* missing loop header */

{

k++;

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

}

Which of the following can be used as a replacement for /* missing loop header */ so that the code segment prints out the string "1 2 3 4 "?

  • while (k < 3)

  • while (k <= 5)

  • while (k <= 4)

  • while (k < 4)

  • while (k < 5)

  • while (k < 4)

27
New cards

Consider the following code segment.

int counter = 0;

for (int x = 10; x > 0; x--)

{

for (int y = x; y <= x; y++)

{

counter++; // line 6

}

}

How many times will the statement in line 6 be executed as a result of running the code segment?

  • 0

  • 10

  • 20

  • 1

  • 11

  • 10

28
New cards

Consider the following code segment.

int j = 1;

while (j < 5)

{

int k = 1;

while (k < 5)

{

System.out.println(k);

k++;

}

j++;

}

Which of the following best explains the effect, if any, of changing the first line of code to int j = 0; ?

  • There will be four more values printed because the outer loop will iterate one additional time.

  • There will be no change to the output of the code segment.

  • There will be one more value printed because the outer loop will iterate one additional time.

  • There will be four fewer values printed because the outer loop will iterate one fewer time.

  • There will be one less value printed because the outer loop will iterate one fewer time.

  • There will be four more values printed because the outer loop will iterate one additional time.

29
New cards

Which of the following code segments produces the output "987654321" ?

int num = 10;

while (num > 1)

{

num--;

System.out.print(num);

}

int num = 10;

while (num >= 0)

{

System.out.print(num);

num--;

}

int num = 10;

while (num > 0)

{

System.out.print(num);

num--;

}

int num = 0;

while (num <= 9)

{

System.out.print(10 - num);

num++;

}

int num = 10;

while (num >= 1)

{

num--;

System.out.print(num);

}

int num = 10;

while (num > 1)
{
num--;
System.out.print(num);
}

30
New cards

Consider the following code segment.

int count = 5;

while (count < 100)

{

count = count* 2;

}

count = count + 1;

What will be the value of count as a result of executing the code segment?

  • 100

  • 161

  • 101

  • 321

  • 160

  • 161

31
New cards

Consider the following code segment.

int x = 1;

while ( /* missing code */)

{

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

x = x + 2;

}

Consider the following possible replacements for /* missing code */.

I. x < 6

II. x != 6

III. x < 7

Which of the proposed replacements for /* missing code */ will cause the code segment to print only the values 1 3 5 ?

  • I and III only

  • I, II, and III

  • I and II only

  • II only

  • I only

 

I and III only

32
New cards

Directions: Select the choice that best fits each statement. The following question(s) refer to the following method.

public static int mystery (int n)

{

int x = 1;

int y = 1;

// Point A

while (n > 2)

{

x = x + y;

// Point B

y = x – y;

n--;

}

// Point C

return x;

}

What value is returned as a result of the call mystery (6)?

  • 5

  • 1

  • 6

  • 8

  • 13

  • 8

33
New cards

Consider the following code segment.

for (int j = 1; j < 10; j += 2)

}

System.out.print(j);

}

Which of the following code segments will produce the same output as the code segment above?

int j = 1;

while (j < 10)

{

System.out.print(j);

j += 2;

}

int j = 1;

while (j <= 10)

{

j += 2;

System.out.print(j);

}

int j = 1;

while (j >= 10)

{

System.out.print(j);

j += 2;

}

int j = 1;

while (j < 10)

{

j += 2;

System.out.print(j);

}

int j = 1;

while (j >= 10)

{

j += 2;

System.out.print(j);

}

int j = 1;

while (j < 10)

{

System.out.print(j);

j += 2;

}

34
New cards

Consider the following output.

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

Which of the following code segments will produce the output shown above?

for (int j = 1; j <= 6; j++)

{

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

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

System.out.println();

}

for (int j = 1; j < 6; j++)

{

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

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

System.out.println();

}

for (int j = 1; j < 6; j++)

{

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

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

System.out.println();

}

for (int j = 1; j <= 6; j++)

{

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

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

System.out.println();

}

for (int j = 1; j <= 6; j++)

{

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

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

System.out.println();

}

for (int j = 1; j <= 6; j++)

{

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

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

System.out.println();

}

35
New cards

Directions: Select the choice that best fits each statement. The following question(s) refer to the following method.

public static int mystery (int n)

{

int x = 1;

int y = 1;

// Point A

while (n > 2)

{

x = x + y;

// Point B

y = x – y;

n--;

}

// Point C

return x;

}

Which of the following is true of method mystery ?

  • x will sometimes be 1 at // Point B.

  • n will always be greater than 2 at // Point B.

  • n will sometimes be greater than 2 at // Point C.

  • x will never be 1 at // Point C.

  • n will never be greater than 2 at // Point A.

  • n will always be greater than 2 at // Point B.

36
New cards