Sem 2 CSA CB Units (3,4)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 7:25 PM on 5/27/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

30 Terms

1
New cards

Consider the design of a  Player  class that will contain two  int  attributes and a constructor. The class will also contain a method  getScore  that can be accessed from outside the class. A partial definition of the  Player  class is shown.

 

public class Player 
{
   /* missing code */ 
}

Which of the following replacements for  /* missing code */  is the most appropriate implementation of the class?

A

private int score;
private int id; 

public Player(int playerScore, int playerID)
{ /* implementation not shown */ }

private int getScore() 
{ /* implementation not shown */ }

B

private int score; 
private int id; 

public Player(int playerScore, int playerID) 
{ /* implementation not shown */ } 

public int getScore() 
{ /* implementation not shown */ }

C

public int score; 
public int id; 

public Player(int playerScore, int playerID) 
{ /* implementation not shown */ } 

private int getScore() 
{ /* implementation not shown */ }

D

public int score; 
public int id; 

public Player(int playerScore, int playerID) 
{ /* implementation not shown */ } 

public int getScore() 
{ /* implementation not shown */ }

private int score; 
private int id; 

public Player(int playerScore, int playerID) 
{ /* implementation not shown */ } 

public int getScore() 
{ /* implementation not shown */ }

2
New cards

Consider the following class definition.

 

public class WordClass 
{
   private final String word;
   private static String maxWord = "";

   public WordClass(String s)
   {
      word = s;

      if (word.length() > maxWord.length())
      {
         maxWord = word;
      }
   }
}

Which of the following is a true statement about the behavior of  WordClass  objects?

A

Every time a  WordClass  object is created, the  maxWord  variable is referenced.

B

Every time a  WordClass  object is created, the value of the  maxWord  variable changes.

C

No two  WordClass  objects can have their  word  length equal to the length of  maxWord.

D

The value of the  maxWord  variable cannot be changed once it has been initialized.

A

Every time a  WordClass  object is created, the  maxWord  variable is referenced.

3
New cards

A city transportation office has a data set that contains information about bus ridership in the city. Each row in the data set corresponds to one rider’s information for a single week. A sample portion of the data set is shown.

Rider ID

Age Group

Primary Route Used

Has Discount? (Yes/No)

20038

Teen (13 to 19)

Route A

Yes

31932

Adult (20 to 64)

Route B

No

29938

Senior (65 and older)

Route C

Yes

25571

Adult (20 to 64)

Route A

No

38860

Teen (13 to 19)

Route B

No

Which of the following questions can be answered using the data set?

A

Which rider is entitled to a discount based on age?

B

Which riders without a discount most frequently use Route A?

C

What time of day are teens most likely to use Route B?

D

Which age group prefers Route C because of shorter travel times?


Which riders without a discount most frequently use Route A?

4
New cards

Consider the following class definition.

 

public class RentalCar 
{ 
   private double dailyRate;     // the fee per rental day 
   private double mileageRate;   // the fee per mile driven 
 
   public RentalCar(double daily, double mileage) 
   { 
      dailyRate = daily; 
      mileageRate = mileage; 
   } 
 
   public double calculateFee(int days, int miles) 
   { 
      /* missing code */ 
   } 
}

 

The  calculateFee  method is intended to calculate the total fee for renting a car. The total fee is equal to the number of days of the rental,  days,  times the daily rental rate plus the number of miles driven,  miles,  times the per mile rate.

Which of the following code segments can be used to replace  /* missing code */  so that the  calculateFee  method works as intended?

A

return dailyRate + mileageRate;

B

return (daily * dailyRate) + (mileage * mileageRate);

C

return (daily * days) + (mileage * miles);

D

return (days * dailyRate) + (miles * mileageRate);

return (days * dailyRate) + (miles * mileageRate);

5
New cards

A software developer created an application that suggests little-known interesting neighborhood locations that tourists can visit. The application has become very popular and soon local residents are overwhelmed by the sudden growth in visitors to the neighborhood.

Which of the following statements best explains the impact of the application?

A

The application contains run-time errors because it fails to use appropriate abstractions. 

B

The application has poor system reliability, causing inconsistent recommendations.

C

The application is beneficial to its users but has unintended effects on neighborhood residents.

D

The application raises intellectual property concerns because it was not published as open source.

The application is beneficial to its users but has unintended effects on neighborhood residents.

6
New cards

Consider the following code segment, where  num  is an integer variable.

 

int[][] arr = {{11, 13, 14, 15}, 
               {12, 18, 17, 26}, 
               {13, 21, 26, 29}, 
               {14, 17, 22, 28}}; 
 
for (int j = 0; j < arr.length; j++) 
{ 
   for (int k = 0; k < arr[0].length; k++) 
   { 
      if (arr[j][k] == num) 
      { 
         System.out.print(j + k + arr[j][k] + " "); 
      } 
   } 
} 

What is printed when  num  has the value  14?

A

14 14 

B

16 17 

C

17 16 

D

18 19 

16 17 

7
New cards

A widely used translation application collects and stores users’ voice input to improve its speech-recognition algorithms. Which of the following is the most significant privacy concern raised by this practice?

A

The application may perform less accurately for speakers with regional accents. 

B

Storing large amounts of voice data increases the risk that the data will become inaccurate. 

C

The quality of the voice collection is dependent on a user’s device, which may cause inconsistent results. 

D

Users might assume their conversations are private, even though their voice data is being stored and used to train future models. 

Users might assume their conversations are private, even though their voice data is being stored and used to train future models. 

8
New cards

Consider the following class definition.

 

public class Thing
{
   private int number; 
   
   public Thing()
   { 
      number = 7; 
   } 
 
   public void increment()
   { 
      number++; 
   } 
} 

 

The following method appears in a class other than  Thing.

 

public void doSomething(Thing item, int value)
{
   item.increment();
   value++;
}   

 

The following code segment appears in a method is the same class as  doSomething.

 

Thing aThing = new Thing();
int amt = 11;
doSomething(aThing, amt);

Which of the following best describes the result of executing the code segment?

A

The  aThing  object’s instance variable  number  and the  int  variable  amt  have both been incremented by  1.

B

The  aThing  object’s instance variable  number  has been incremented by  1,  but the  int  variable  amt  is unchanged.

C

The  aThing  object’s instance variable  number  is unchanged, but the  int  variable  amt  has been incremented by  1.

D

The  aThing  object’s instance variable  number  and the  int  variable  amt  are both unchanged.

B

The  aThing  object’s instance variable  number  has been incremented by  1,  but the  int  variable  amt  is unchanged.

9
New cards

A teacher is developing a program to manage student information. The program must store each student’s name, ID, and grade point average (GPA). It must also support displaying the student’s information. 

Which of the following is the most appropriate design? 

A

Define a  StudentName  class with a  String  instance variable, a  StudentID  class with a  String  instance variable, and a  GPA  class with a  double  instance variable. Each class will include a method to print its instance variable.

B

Define a  StudentName  class with a  String  instance variable, a  StudentID  class with a  String  instance variable, and a  GPA  class with a  double  instance variable. Define a  Student  class with no instance variables that includes methods to print the instance variables of the three other classes.

C

Define a  Student  class with two  String  instance variables, one for the student name and one for the ID. Define a  GPA  class with a  double  instance variable for the GPA. Each class will include a method to print its instance variables.

D

Define a  Student  class with a  String  instance variable for the student name, a  String  instance variable for the ID, and a  double  instance variable for the GPA. The class will include a method to print its instance variables. 

Define a  Student  class with a  String  instance variable for the student name, a  String  instance variable for the ID, and a  double  instance variable for the GPA. The class will include a method to print its instance variables. 

10
New cards

Consider the following method, which correctly implements a recursive binary search.

 

/**
 *  Returns an index in arr where the value x appears if x appears
 *   in arr between arr[left] and arr[right], inclusive;
 *   otherwise returns -1.
 *   Preconditions: arr is sorted in ascending order.
 *                 left >= 0, right < arr.length, arr.length > 0
 */
public static int bSearch(int[] arr, int left, int right, int x)
{
   if (right >= left)
   {
      int mid = (left + right) / 2;
      if (arr[mid] == x)
      {
         return mid;
      }
      else if (arr[mid] > x)
      {
         return bSearch(arr, left, mid - 1, x);
      }
      else
      {
         return bSearch(arr, mid + 1, right, x);
      }
   }
   return -1;
}

 

The following code segment appears in a method in the same class as   bSearch.

 

int[] nums = {10, 20, 30, 40, 50};
int result = bSearch(nums, 0, nums.length - 1, 40);

How many times will the  bSearch  method be called as a result of executing the code segment, including the initial call?

A

1

B

2

C

3

D

4


2

11
New cards

A school district wants to know the average number of hours students spend reading for pleasure each week. The district sends an online survey to students who are members of the district’s reading club and asks them to respond.

Which of the following factors is most important for the district to consider before making generalizations from the data obtained from the survey?

A

The survey results may be unreliable because the data might contain personal information.

B

The survey results may be inaccurate if some students rounded their reported time reading for pleasure.

C

The survey results may be inconsistent because students completed the survey at different times during the week.

D

The survey results may not represent all students because only those who are members of the reading club and choose to respond are included.

The survey results may not represent all students because only those who are members of the reading club and choose to respond are included.

12
New cards

Consider the following method.

 

/** Precondition: j <= k */ 
public static void mystery(int j, int k) 
{
   System.out.println(j);
   if (j < k)
   {
      mystery(j + 1, k);
   } 
}

Which of the following best describes the behavior of the  mystery  method?

A

It repeatedly prints the value  j  due to infinite recursion.

B

It prints the initial value of  j  a total of  k  times.

C

It prints the integers from  j  to  k,  inclusive, in order from least to greatest.

D

It prints the integers from  j  to  k,  inclusive, in order from greatest to least.

C. It prints the integers from  j  to  k,  inclusive, in order from least to greatest.

13
New cards

A company develops an application that helps users find rental housing. It recommends apartments and homes based on income, past search history, and stated preferences. As the application is being used by clients, the company finds that the application produces different recommendations even when the housing needs are similar.

Which of the following is the most likely reason for this behavior? 

A

The application contains algorithmic bias that is creating unfair outcomes for specific groups of users.

B

The application does not protect the users’ data, allowing it to be viewed by other users.

C

The application encapsulates the data as it is being entered, resulting in invalid data.

D

The application uses data that has not been sorted, causing some housing options to be overlooked.

A

The application contains algorithmic bias that is creating unfair outcomes for specific groups of users.

14
New cards

A text file named  colorFile.txt  has the following contents. Assume that the file contains no spaces.

blue
yellow
red
green
orange
brown

 

In the following code segment, a valid  Scanner  object named  scan  is created to read from the text file.

 

File colors = new File("colorFile.txt");
Scanner scan = new Scanner(colors);
int value = 0;

while (scan.hasNext())
{
   if (scan.next().length() > 4)
   {
      value++;
   }
}
System.out.println(value);

 

 

 

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

A

1

B

4

C

5

D

6

4

15
New cards

Consider the following method.

 

public static int getValue(int[] data, int j, int k) 
{ 
   return data[j] + data[k]; 
} 

Which of the following code segments, when appearing in another method in the same class as  getValue,  will print the value  70?

A

int[] arr = {40, 30, 20, 10, 0}; 
System.out.println(getValue(arr, 1, 2)); 

B

int[] arr = {50, 40, 30, 20, 10}; 
System.out.println(getValue(arr, 1, 2)); 

C

int arr = {40, 30, 20, 10, 0}; 
System.out.println(getValue(arr, 2, 1)); 

D

int arr = {50, 40, 30, 20, 10}; 
System.out.println(getValue(arr, 2, 1)); 

int[] arr = {50, 40, 30, 20, 10}; 
System.out.println(getValue(arr, 1, 2)); 

16
New cards

A student is writing a computer program and finds some code online that is neither published as free to use nor published as open source. Which of the following best describes the most responsible choice for using the code?

A

Asking for permission or buying the code before using it

B

Rewriting the code by changing method names and then using it

C

Using the code but including comments describing the code and its functionality

D

Using the code but giving credit to the person who wrote it

Asking for permission or buying the code before using it

17
New cards

Consider the following  mystery  method, which has been defined in a class other than  Something.

 

public static void mystery(Something obj)
{ /* implementation not shown */ }

 

Which of the following initial conditions, if any, must be met for the method to be called in a class other than  Something?

A

The method can only be called from the  Something  class.

B

The method must be called by a  Something  object using the dot operator.

C

The result of the method call must be assigned to a variable.

D

Something  object must be created to pass as an argument to the method.

D

Something  object must be created to pass as an argument to the method.

18
New cards

Consider the following class definitions.

 

public class MenuItem 
{ 
   private double price;
 
   public MenuItem(double p) 
   { 
      price = p; 
   }
   
   public double getPrice()
   {
      return price;
   }
 
   public void makeItAMeal() 
   { 
      Combo meal = new Combo(this); 
      price = meal.getComboPrice(); 
   } 
} 

 

public class Combo
{
   private double comboPrice;
 
   public Combo(MenuItem item)
   {
      comboPrice = item.getPrice() + 1.5;
   }
 
   public double getComboPrice()
   {
      return comboPrice;
   }
} 

 

The following code segment appears in a class other than  MenuItem  or  Combo.

 

MenuItem one = new MenuItem(5.0); 
one.makeItAMeal(); 
System.out.println(one.getPrice()); 

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

A

5.0

B

6.5

C

8.0

D

Nothing is printed because the code does not compile.

6.5

19
New cards

The Java API provides one general method for raising a number to an exponent:  Math.pow(double base, double exponent).  

 

Consider an alternate API that provides separate methods such as  Math.square(double base)Math.cube(double base),  or  Math.pow4(double base)  for raising a number to an exponent. 

Which of the following best explains why providing one general method is considered a better design than creating separate methods?

A

It hides the implementation details, providing better encapsulation.

B

It increases flexibility by handling any exponent with only one method.

C

It makes a program run faster because fewer methods are needed.

D

It reduces the risk of a program encountering a run-time error.


It increases flexibility by handling any exponent with only one method.

20
New cards

The following method is a correct implementation of the insertion sort algorithm. The method correctly sorts the elements of  arr  so that they appear in order from least to greatest.

 

public static void insertionSort(int[] arr)
{
   for (int j = 1; j < arr.length; j++) 
   {
      int temp = arr[j]; 
      int possibleIndex = j; 
      while (possibleIndex > 0 && temp < arr[possibleIndex - 1])
      { 
         arr[possibleIndex] = arr[possibleIndex - 1]; 
         possibleIndex--;                       // Line 10 
      } 
      arr[possibleIndex] = temp; 
   } 
} 

 

The following code segment appears in a method in the same class as  insertionSort.

 

int[] numbers = {4, 12, 4, 7, 19, 6}; 
insertionSort(numbers); 

How many times is the statement in line 10 of the method executed as a result of the call to  insertionSort?

A

2

B

3

C

4

D

5

D

5

21
New cards

Consider the following class definition.

 

public class IceCreamCone
{
   private double cost;
   private String flavor;
   private int numScoops;

   /* missing constructor */
}

 

The following statement appears in a class other than  IceCreamCone.

 

IceCreamCone treat = new IceCreamCone(3.50, "Caramel", 2);

Which of the following is the most appropriate replacement for  /* missing constructor */  so that the object  treat  is correctly created?

A

public IceCreamCone(double ct, String fl, int sc)
{
   double ct = cost;
   String fl = flavor;
   int sc = numScoops;
}

B

public IceCreamCone(double ct, String fl, int sc)
{
   double cost = ct;
   String flavor = fl;
   int numScoops = sc;
}

C

public IceCreamCone(double ct, String fl, int sc)
{
   ct = cost;
   fl = flavor;
   sc = numScoops;
}

D

public IceCreamCone(double ct, String fl, int sc)
{
   cost = ct;
   flavor = fl;
   numScoops = sc;
}

public IceCreamCone(double ct, String fl, int sc)
{
   cost = ct;
   flavor = fl;
   numScoops = sc;
}

22
New cards

A university wants to determine which major has the highest average GPA among students graduating in a given year. The university has the following data sets available.

  • Data set 1 contains an entry for each student. Each entry includes the student’s ID, declared major, and graduation year.

  • Data set 2 contains an entry for each student. Each entry includes the student’s ID, declared major, and assigned faculty advisor.

  • Data set 3 contains an entry for each student. Each entry includes the student’s ID, course name, and grade received.

  • Data set 4 contains an entry for each student. Each entry includes the student’s ID and final GPA.

Which two data sets can be combined and analyzed to determine the information the university seeks?

A

Data sets 1 and 2

B

Data sets 1 and 4

C

Data sets 2 and 3

D

Data sets 3 and 4


Data sets 1 and 4

23
New cards

A programmer is developing an online catalog of video games. The catalog will display the name and average user rating of each video game in the catalog. Users will be able to rate a video game on a scale from 0.0 to 5.0. 

Which of the following is the most appropriate design to allow the programmer to maintain the catalog?

A

Create a  Catalog  class. For each video game in the catalog, the class should have one  String  attribute for the video game’s name and one double attribute for the video game’s rating. The class should include one method to display a video game’s name and rating.

B

Create  Display  and  Update  classes. The  Display  class should have a method that displays the name and rating of a video game. The  Update  class should have a method that allows a user to update a video game’s rating.

C

Create  Name  and  Rating  classes. The  Name  class should include a method to display the names of the video games. The  Rating  class should contain a method to display the ratings of the video games and a method to allow users to submit a video game’s rating.

D

Create a  VideoGame  class. The class should have a  String  attribute that represents the video game’s name and a  double  attribute that represents its rating. The class should include one method to display the name and rating, and another method that allows users to update a video game’s rating.

Create a  VideoGame  class. The class should have a  String  attribute that represents the video game’s name and a  double  attribute that represents its rating. The class should include one method to display the name and rating, and another method that allows users to update a video game’s rating.

24
New cards

In the following code segment, a valid  Scanner  object named  scan  has been created to read from a text file named  "data.txt".

 

File inputFile = new File("data.txt");
Scanner scan = new Scanner(inputFile);

String name = scan.next();
int score1 = scan.nextInt();
int score2 = scan.nextInt();

Which of the following could be the contents of  "data.txt"  so that the variables  name,  score1,  and  score2  are each assigned a valid value by the code segment?

A

Brody 85 Carmen 90

B

Brody 85 90

C

Brody 85.7 Carmen 90.6

D

Brody 85.7 90.6


Brody 85 90

25
New cards

Consider the following method, which is intended to remove duplicate consecutive elements contained in  nums.

 

public static void remDups(ArrayList<Integer> nums) 
{ 
   for (int j = 0; j < nums.size() - 1; j++) // Line 3
   { 
      int first = nums.get(j);
      int second = nums.get(j + 1);
      if (first == second)                   // Line 7
      { 
         nums.remove(j);                     // Line 9
         j++;                                // Line 10
      } 
   } 
} 

 

For example, if  nums  contains  {1, 2, 2, 3, 4, 3, 5, 5, 6},  then after  remDups(nums)  is executed,  nums  should contain  {1, 2, 3, 4, 3, 5, 6}.

 

The method does not always work as intended.

Which of the following changes can be made so that the method always works as intended?

A

In line 3, changing the Boolean expression to  j < nums.size()

B

In line 7, changing the Boolean expression to  first != second

C

In line 9, changing the expression to  nums.remove(j + 1)

D

In line 10, changing the expression to  j--


In line 10, changing the expression to  j--

26
New cards

Consider the following class definition.

 

public class Circle 
{ 
   private double radius; 
 
   public double computeArea() 
   { 
      private double pi = 3.14159; 
      public double area = pi * radius * radius; 
      return area; 
   } 
 
   // Constructor not shown. 
} 

Which of the following best explains why the  computeArea  method will not compile?

A

Local variables declared inside a method cannot be declared as  public  or  private.

B

Local variables declared inside a method must all be  private.

C

Local variables declared inside a method must all be  public.

D

Local variables used inside a method must be declared before the method header.

A

Local variables declared inside a method cannot be declared as  public  or  private.

27
New cards

Consider the following code segment, which is intended to declare and initialize the two-dimensional  String  array  things.

 

/* missing code */ = {{"spices", "garlic", "onion", "pepper"}, 
		      {"clothing", "hat", "scarf", "gloves"}, 
		      {"plants", "tree", "bush", "flower"}, 
		      {"vehicles", "car", "boat", "airplane"}}; 

Which of the following could replace  /* missing code */  so that things is properly declared?

A

new String[][] things

B

new(String[][]) things

C

String[] String[] things

D

String[][] things

String[][] things

28
New cards

Consider the following code segment.

 

ArrayList<Double> dataValues = new ArrayList<Double>();
dataValues.add(9.84);
dataValues.add(8.93);
dataValues.add(7.65);
dataValues.add(6.24);
dataValues.remove(2);
dataValues.set(2, 7.63);

Which of the following represents the contents of  dataValues  after executing the code segment?

A

[9.84, 7.63, 6.24]

B

[9.84, 7.63, 7.65, 6.24]

C

[9.84, 8.93, 7.63]

D

[9.84, 8.93, 7.63, 6.24]

[9.84, 8.93, 7.63]

29
New cards

Consider the following method  countNegatives,  which returns the number of elements in  numList  that are less than zero. 

 

public static int countNegatives(ArrayList<Integer> numList)
{
   int count = 0;
   for (int j = 0; j < numList.size(); j++)       // Line 4
   {
      if (numList.get(j) < 0)
      {
         count++;
      }
   }
   return count;
}

Suppose the Boolean expression  j < numList.size()  in line 4 of the method is replaced with  j <= numList.size() - 1.  Which of the following best explains the effect of this change?

A

It has no effect on the behavior of the method.

B

It causes the method to ignore the last element in numList.

C

It causes an IndexOutOfBoundsException  to be thrown.

D

It changes the number of times the loop executes, but all elements in numList will still be accessed.


It has no effect on the behavior of the method.

30
New cards

The  Car  class will be used to represent information about a car. Each instance of the class will maintain two string attributes for the car’s make and model. This information is unique to each car and should not be directly accessible outside the  Car  class. The  Car  class will also contain a single constructor.

 

public class Car
{
   /* missing code */
   
   /* There may be other methods that are not shown. */
}

Which of the following replacements for  /* missing code */  is the most appropriate partial implementation of the class?

A

public String make;
public String model; 

public Car(String myMake, String myModel)
{ /* implementation not shown */ }

B

public String make;
public String model;

private Car(String myMake, String myModel)
{ /* implementation not shown */ }

C

private String make;
private String model;

public Car(String myMake, String myModel)
{ /* implementation not shown */ }

D

private String make;
private String model;

private Car(String myMake, String myModel)
{ /* implementation not shown */ }

private String make;
private String model;

public Car(String myMake, String myModel)
{ /* implementation not shown */ }