CB 7 Test Study Guide 2

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/24

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.

25 Terms

1
New cards

Consider the following code segment.

int num1 = 0;

int num2 = 3;

while ((num2 != 0) && ((num1 / num2) >= 0))

{

  num1 = num1 + 2;

  num2 = num2 - 1;

}

What are the values of numl and num2 after the while loop completes its execution?

Responses

A

num1 = 0, num2 = 3

B

num1 = 8, num2 = -1

C

num1 = 4, num2 = 1

D

num1 = 6, num2 = 0

num1 = 6, num2 = 0

2
New cards

Consider the following code segment.

 

int num = 245;
int temp = num;

while (temp > 0)
{
   System.out.print(temp % 10 + " ");
   temp /= 10;
}

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

Responses

A

2 4 5 

B

4 5 

C

5 4 

D

5 4 2 

D

5 4 2 

3
New cards

Consider the following code segment.

 

String str = "ABCDEF";
String result = "";
/* missing code */
System.out.println(result);

Which of the following can be used to replace /* missing code */ so that this code segment prints the string "EFCDAB"?

Responses

A

for (int j = str.length() - 1; j >= 0; j -= 1)
{
   result += str.substring(j, j + 1);
}

B

for (int j = str.length() - 1; j >= 0; j -= 2)
{
   result += str.substring(j, j + 2);
}

C

for (int j = str.length() - 2; j >= 0; j -= 1)
{
   result += str.substring(j, j + 2);
}

D

for (int j = str.length() - 2; j >= 0; j -= 2)
{
   result += str.substring(j, j + 2);
}

for (int j = str.length() - 2; j >= 0; j -= 2)
{
   result += str.substring(j, j + 2);
}

4
New cards

Consider the following method.

public int someCode(int a, int b, int c)

{

  if ((a < b) && (b < c))

    return a;

  if ((a >= b) && (b >= c))

    return b;

  if ((a b) || (a c) || (b == c))

    return c;

}

Which of the following best describes why this method does not compile?

Responses

A

It is possible to reach the end of the method without returning a value.

B

The if statements must have else parts when they contain return statements.

C

Methods cannot have multiple return statements.

D

The third if statement is not reachable.

It is possible to reach the end of the method without returning a value

5
New cards

Consider the following code segment.

int x = 7;

if (x < 7)

{

x = 2 * x;

}

if (x % 3 == 1)

{

x = x + 2;

}

System.out.print(3 * x);

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

Responses

A

9

B

14

C

21

D

27

D. 27

6
New cards

Which of the following best explains why programmers often include open source code in their programs?

Responses

A

To utilize algorithms that eliminate the need to include comments in program code

B

To utilize algorithms that have already been implemented while reducing the risk of raising intellectual property concerns

C

To utilize algorithms that minimize the need for abstraction in a program

D

To utilize algorithms that remove the requirement to design a program before implementing it

To utilize algorithms that have already been implemented while reducing the risk of raising intellectual property concerns

7
New cards

A certain device requires a passcode to be used. The passcode must meet both of the following requirements. 

  • The passcode must be 4 digits long. 

  • The passcode must not start with a zero (0) digit.

 

Assume that  pass  is a properly declared and initialized  int  variable. 

Which of the following expressions will evaluate to  true  if  pass  is an acceptable passcode?

Responses

A

pass >= 1000 || pass < 10000

B

pass > 1000 || pass <= 10000

C

pass >= 1000 && pass < 10000

D

pass > 1000 && pass < 10000

C

pass >= 1000 && pass < 10000

8
New cards

A company is designing a program to evaluate the strength of a user’s password. The program needs to indicate if the password is “weak,” “medium,” or “strong” based on the following guidelines.

  • A password with fewer than 8 characters is classified as “weak.”

  • A password with 8 or more characters is classified as “medium,” where all characters are either numbers or letters. 

  • A password with 8 or more characters plus at least one special character (like !, @, or #) is classified as “strong.”

Which of the following algorithms will correctly classify a password?

Responses

A

Step 1: If the password has fewer than 8 characters, label it as “weak” and skip Steps 2 and 3. Otherwise, go to Step 2.

 

Step 2: If the password has more than 8 characters, label it as “medium” and skip Step 3. Otherwise, go to Step 3.

 

Step 3: Label the password as “strong.”

B

Step 1: If the password has fewer than 8 characters, label it as “weak” and skip Steps 2 and 3. Otherwise, go to Step 2.

 

Step 2: If the password contains at least one special character, label it as “strong” and skip Step 3. Otherwise, go to Step 3.

 

Step 3: Label the password as “medium.”

C

Step 1: If the password has 8 or more characters, label it as “medium” and skip Steps 2 and 3. Otherwise, go to Step 2.

 

Step 2: If the password has more than 8 characters and contains at least one special character, label it as “strong” and skip Step 3. Otherwise, go to Step 3.

 

Step 3: Label the password as “weak.”

D

Step 1: If the password has 8 or more characters and contains at least one special character, label it as “strong” and skip Steps 2 and 3. Otherwise, go to Step 2.

 

Step 2: If the password does not contain at least one special character, label it as “medium” and skip Step 3. Otherwise, go to Step 3.

 

Step 3: Label the password as “weak.”

Step 1: If the password has fewer than 8 characters, label it as “weak” and skip Steps 2 and 3. Otherwise, go to Step 2.

 

Step 2: If the password contains at least one special character, label it as “strong” and skip Step 3. Otherwise, go to Step 3.

 Step 3: Label the password as “medium.”

9
New cards

Consider the following class definition.
 

public class Person
{
   private String name;
   /* missing constructor */
}

 

The following statement, which is located in a method in a different class, creates a new Person object with its attribute name initialized to "Washington".

 

Person p = new Person("Washington");

Which of the following can be used to replace /* missing constructor */ so that the object p is correctly created?

Responses

A

private Person(String n)
{
   name = n;
}

B

public Person()
{
   name = n;
}

C

public Person(String n)
{
   name = n;
}

D

public Person(String name)
{
   String n = name;
}

public Person(String n)
{
   name = n;
}

10
New cards

A student is developing a class that will store information about pets, including each pet’s name and age. Which of the following sets of attributes is most appropriate for this class?

Responses

A

One  String  attribute that includes the pet’s name and age

B

One  String  attribute for the pet’s name and one  String  attribute for the pet’s age

C

One  String  attribute for the pet’s name and one  int  attribute for the pet’s age

D

One  int  attribute for the pet’s name and one  int  attribute for the pet’s age

One  String  attribute for the pet’s name and one  int  attribute for the pet’s age

11
New cards

Consider the following code segment.

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

{

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

{

System.out.println("Fun");

}

}

Which of the following best explains how changing the outer for loop header to for (int j = 0; j <= 3; j++) affects the output of the code segment?

Responses

A

The output of the code segment will be unchanged.

B

The string "Fun" will be printed more times because the outer loop will execute more times.

C

The string "Fun" will be printed more times because the inner loop will execute more times in each iteration of the outer loop.

D

The string "Fun" will be printed fewer times because the outer loop will execute fewer times.


The string "Fun" will be printed more times because the outer loop will execute more times.

12
New cards

Consider the following code segment.

 

for (int j = 4; j > 0; j--)
{
   for (/* missing code */)
   {
       System.out.print(k + " ");
   }
   System.out.println();
}

 

This code segment is intended to produce the following output.

 

0 1 2 3 
0 1 2 
0 1 
0 

Which of the following can be used to replace /* missing code */ so that this code segment works as intended?

Responses

A

int k = 0; k < j; k++

B

int k = 0; k <= j; k++

C

int k = j; k > 0; k--

D

int k = j; k >= 0; k--


int k = 0; k < j; k++

13
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?

Responses

A

0

B

1

C

2

D

n - 1


0

14
New cards

The  Profile  class will be used to represent users of a social media website. Each instance of the class will maintain the user’s name, username, and number of followers. This information is unique to each user and should not be directly accessible outside the  Profile  class. 

Which of the following declarations is the most appropriate for the  Profile  class?

Responses

A

private class Profile
{ 
   private String name; 
   private String username; 
   private int followers;
}

B

public class Profile
{ 
   public String name; 
   public String username; 
   public int followers;
}

C

public class Profile
{ 
   private String name; 
   private String username; 
   private int followers;
}

D

private class Profile
{ 
   private String name; 
   private String username; 
   private int followers;
}

public class Profile
{ 
   private String name; 
   private String username; 
   private int followers;
}

15
New cards

Consider the following method, which returns an int based on its parameter x.

public static int puzzle(int x)

{

if (x > 20)

{

x -= 2;

}

else if (x % 2 == 0) // Line 7

{

x += 4;

}

return x;

}

Consider a modification to the method that eliminates the else from line so that line becomes

if (x % 2 == 0) // Modified line 7

For which of the following values of x would the return values of the original method and the modified method differ?

Responses

A

 5

B

14

C

22

D

25

22

16
New cards

A programmer wants to speed up the development of a program by reducing the amount of testing performed on the program. Which of the following is most likely to be a consequence of this approach?

Responses

A

Decreased intellectual property concerns

B

Decreased system reliability

C

Increased intellectual property concerns

D

Increased system reliability


Decreased system reliability

17
New cards

Assume that a and b are variables of type int. The expression

!(a < b) && !(a > b)

is equivalent to which of the following?

Responses

A

true

B

false

C

a == b

D

a != b

a == b

18
New cards

Consider the following code segment.
 

boolean a = true;
boolean b = true;
System.out.print((b || (!a || b)) + " ");
System.out.print(((!b || !a) && a) + " ");
System.out.println(!(a && b) && b);

What output is produced when this code segment is executed?

Responses

A

true true true 

B

true false true 

C

true false false 

D

false true false 


true false false

19
New cards

Consider the following code segment.

int val = 48;

int div = 6;

while ((val % 2 == 0) && div > 0)

{

if (val % div == 0)

{

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

}

val /= 2;

div--;

}

What is printed when the code segment is executed?

Responses

A

48 12 6

B

48 12 6 3

C

48 12 6 3 1

D

48 24 12 6


48 12 6

20
New cards

A programmer is creating a Movie class that will contain attributes for a movie’s title and its average user rating on a scale from  0.0 to 5.0.  The class must also contain methods to allow the attributes to be accessed outside the class.

 

A class design diagram for the Movie class will contain three sections. The first section contains the class name, the second section contains two instance variables and their data types, and the third section contains two methods and their return types. The + symbol indicates a public designation, and the - symbol indicates a private designation. 
 

Which of the following diagrams represents the most appropriate design for the Movie class?  

Responses

A

Class diagram. Refer to long description.
  • The class diagram has 3 sections with values as follows:

    • Section 1:

      • Movie

    • Section 2:

      • + title : String

      • + rating : double

    • Section 3:

      • - getTitle() : String

      • - getRating() : double

B

Class diagram. Refer to long description.
  • The class diagram has 3 sections with values as follows:

  • Section 1:

    • Movie

  • Section 2:

    • + title : String

    • + rating : int

  • Section 3: 

    • - getTitle() : String

    • - getRating() : int

C

Class diagram. Refer to long description.
  • The class diagram has 3 sections with values as follows:

    • Section 1:

      • Movie

    • Section 2:

      • - title : String

      • - rating : double

    • Section 3:

      • + getTitle() : String

      • + getRating() : double

D

Class diagram. Refer to long description.
  • The class diagram has 3 sections with values as follows:

    • Section 1:

      • Movie

    • Section 2:

      • - title : String

      • - rating : int

    • Section 3:

      • + getTitle() : String

      • + getRating() : int

Class diagram. Refer to long description.

21
New cards

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


Code Segment 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);
}


Code Segment 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?

Responses

A

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

B

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

C

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

D

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

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

22
New cards

Consider the following code segment, in which m and n are properly declared and initialized int variables.

 

int result = 0; 
for (int i = 0; i < n; i++)  
{ 
   for (int j = m; j < n; j++)  
   { 
      result++; 
   } 
} 

Which of the following best describes the conditions that must be true of m and n so that after this code segment executes, the value of result is greater than 0?

Responses

A

n > 0  and  m > 0

B

n > 0  and  m > n

C

n > 0  and  n > m

D

m > 0  and  m > n


n > 0  and  n > m

23
New cards

Consider the following code segment.

int[] numbers = new int[5];

numbers[0] = 2;

numbers[1] = numbers[0] + 1;

numbers[numbers[0]] = numbers[1];

 

for (int x = 3; x < numbers.length; x++)

{

numbers[x] = numbers[x - 1] * 2;

}

Which of the following represents the contents of the array numbers after the code segment is executed?

Responses

A

{2, 3, 0, 0, 0}

B

{2, 3, 1, 2, 4}

C

{2, 3, 3, 6, 9}

D

{2, 3, 3, 6, 12}

D. {2, 3, 3, 6, 12}

24
New cards

Consider the following two code segments, which are intended to produce identical outputs. 

 

Code Segment 1

for (int i = 0; i < 10; i++) 
{ 
   System.out.println("counting " + i); 
}

 

Code Segment 2

int x = 0; 
while (x < 10) 
{ 
   x = x + 1; 
   System.out.println("counting " + x); 
}  

Which of the following changes can be made to Code Segment 2 so that the outputs of the two code segments are identical as intended?

Responses

A

Move the statement  x = x + 1;  so that it follows the print statement inside the loop body.

B

Rename the variable  x  to be  i  so that it is the same variable name as in Code Segment 1.

C

Remove the statement  x = x + 1;  because this statement is not in the loop body in Code Segment 1.

D

Change the statement  x = x + 1;  to be  x++;  so that the increment of the loop control variable is the same in both code segments.

Move the statement  x = x + 1;  so that it follows the print statement inside the loop body.

25
New cards

The Fruit class contains attributes for a fruit’s type, color, and quantity. The class also contains methods to allow the attributes to be accessed outside the class.

 

A class design diagram for the Fruit class contains three sections. The first section contains the class name, the second section contains three instance variables and their data types, and the third section contains three methods and their return types. The + symbol indicates a public designation, and the - symbol indicates a private designation.

 

A class design diagram. Refer to long description.
  • The diagram has 3 sections containing text as follows:

    • Section 1:

      • Fruit

    • Section 2:

      • − type : String

      • − color : String

      • (missing instance variable)

    • Section 3:

      • + getType() : String

      • + getColor() : String

      • (missing method)

Which of the following are the most appropriate replacements for  (missing instance variable)  and  (missing method)?

Responses

A

Replacing  (missing instance variable)  with  - quantity : String  and replacing  (missing method)  with  + getQuantity() : String

B

Replacing  (missing instance variable)  with  - quantity : int  and replacing  (missing method)  with  + getQuantity() : int

C

Replacing  (missing instance variable)  with  - quantity : int  and replacing  (missing method)  with  + getQuantity() : String

D

Replacing  (missing instance variable)  with  - quantity : String  and replacing  (missing method)  with  + getQuantity() : int

Replacing  (missing instance variable)  with  - quantity : int  and replacing  (missing method)  with  + getQuantity() : int