Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 of 30 (worth 3 points) Consider the following program. What is the pu

ID: 3770873 • Letter: Q

Question

Question 1 of 30     (worth 3 points)

Consider the following program. What is the purpose of pw?

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FileUtil {

  public void writeLinesToFile(String filename,
                               String[] linesToWrite,
                               boolean appendToFile) {

    PrintWriter pw = null;

    try {

      if (appendToFile) {

        //If the file already exists, start writing at the end of it.
        pw = new PrintWriter(new FileWriter(filename, true));

      }
      else {

        pw = new PrintWriter(new FileWriter(filename));
        //this is equal to:
        //pw = new PrintWriter(new FileWriter(filename, false));

      }

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

        pw.println(linesToWrite[i]);

      }
      pw.flush();

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      
      //Close the PrintWriter
      if (pw != null)
        pw.close();
      
    }

  }

  public static void main(String[] args) {
    //TODO Write main method code here
  }
}

Check to review before finishing (will be flagged in Table of Contents)

Question 2 of 30     (worth 3 points)

Java provides file operations in which of the following?

Check to review before finishing (will be flagged in Table of Contents)

Question 3 of 30     (worth 3 points)

Once your public class is created to extend JFrame, why would you create a constructor of that class?

Check to review before finishing (will be flagged in Table of Contents)

Question 4 of 30     (worth 3 points)

Which method of the String class would you use to compare two Strings for equality?

Check to review before finishing (will be flagged in Table of Contents)

Question 5 of 30     (worth 3 points)

Which method of the String class can be used to determine the size of the String?

Check to review before finishing (will be flagged in Table of Contents)

Question 6 of 30     (worth 3 points)

Which of the following can be used to concatenate two Strings?

Check to review before finishing (will be flagged in Table of Contents)

Question 7 of 30     (worth 3 points)

The index of the first character in a String is 0, while the index of the last character is length().

True
False

Check to review before finishing (will be flagged in Table of Contents)

Question 8 of 30     (worth 3 points)

Which line of code could you use to get the character located at index 9 in a String called anotherPalindrome and store it in a char variable called aChar?

Check to review before finishing (will be flagged in Table of Contents)

Question 9 of 30     (worth 3 points)

What would happen if only one argument is passed to the substring method for retrieving a substring?

For example:

String anotherPalindrome = "Niagara. O roar again!";
String roar = anotherPalindrome.substring(11);

Check to review before finishing (will be flagged in Table of Contents)

Question 10 of 30     (worth 3 points)

Which of the following String methods returns true if the string includes the specified character sequence?

Check to review before finishing (will be flagged in Table of Contents)

Question 11 of 30     (worth 3 points)

You have a main method with the following statements. Each statement creates an object of the Platypus class, but one sends an argument in parentheses and the other does not. In which circumstances would this be allowed?

Platypus p1 = new Platypus("digger");
Platypus p2 = new Platypus();

Check to review before finishing (will be flagged in Table of Contents)

Question 12 of 30     (worth 3 points)

Consider the following partial class with constructor:

public class Filename {
    private String fullPath;
    private char pathSeparator,
                 extensionSeparator;

    public Filename(String str, char sep, char ext) {
        fullPath = str;
        pathSeparator = sep;
        extensionSeparator = ext;
    }

Which statement could be used to create an object of the Filename class?

Check to review before finishing (will be flagged in Table of Contents)

Question 13 of 30     (worth 3 points)

The array stores a fixed-size sequential collection of elements of one or more type(s).

True
False

Check to review before finishing (will be flagged in Table of Contents)

Question 14 of 30     (worth 3 points)

Assume that myList is a previously declared an initialized array. What does the following code block do?

for (int i = 0; i < myList.length; i++) {
         System.out.print(myList[i] + " ");
      }

Check to review before finishing (will be flagged in Table of Contents)

Question 15 of 30     (worth 3 points)

Which of the following would be a correct way to call (invoke) this method?

public static void printArray(int[] array) {
  for (int i = 0; i < array.length; i++) {
    System.out.print(array[i] + " ");
  }
}

printArray({3, 1, 2, 6, 4, 2});

printArray(int[]);

printArray(int[] result);

int[] result={3, 1, 2, 6, 4, 2};

printArray(result);

Check to review before finishing (will be flagged in Table of Contents)

Question 16 of 30     (worth 3 points)

java.util.Arrays class contains which of the following methods?

Check to review before finishing (will be flagged in Table of Contents)

Question 17 of 30     (worth 3 points)

Consider the following class. Which of the following is a good reason for declaring numberOfBicycles with the static keyword?

public class Bicycle {

    private int cadence;
    private int gear;
    private int speed;
    private int id;
    private static int numberOfBicycles = 0;
        ...
}

Check to review before finishing (will be flagged in Table of Contents)

Question 18 of 30     (worth 3 points)

Consider the following class:

public class Bicycle {

    private int cadence;
    private int gear;
    private int speed;
    private int id;
    private static int numberOfBicycles = 0;

    public Bicycle(int startCadence, int startSpeed, int startGear){
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;

       id = ++numberOfBicycles;
    }

  
    public int getVar() {
        //TODO return a variable
    }
    

    public static int getVariable() {
        //TODO return a variable
    }

}

Which of the following return statements should be used in the getVar and getVariable methods?

public static int getVariable() {
        return numberOfBicycles && id;
    }

public static int getVariable() {
        return id;
    }

public static int getVariable() {
        return id;
    }

public static int getVariable() {
        return numberOfBicycles;
    }

Check to review before finishing (will be flagged in Table of Contents)

Question 19 of 30     (worth 3 points)

A GUI is a user interface that provides graphical two-way communication between the user and the application.

True
False

Check to review before finishing (will be flagged in Table of Contents)

Question 20 of 30     (worth 3 points)

Which of the following are steps in Task Centered GUI Design?

Check to review before finishing (will be flagged in Table of Contents)

Question 21 of 30     (worth 3 points)

A GUI should avoid bundling actions together, because the user may not anticipate any side effects. For example, if a user chooses to cancel a request to send a note, only the send request should be cancelled. Do not bundle another action, such as deletion of the note, with the cancel request.

True
False

Check to review before finishing (will be flagged in Table of Contents)

Question 22 of 30     (worth 3 points)

Which of the following is a top-level container?

Check to review before finishing (will be flagged in Table of Contents)

Question 23 of 30     (worth 3 points)

The JButton class provides the functionality for an ordinary push button. A button can only contain text.

True
False

Check to review before finishing (will be flagged in Table of Contents)

Question 24 of 30     (worth 3 points)

Unlike radio buttons, in which many or none can be selected at once, check boxes only have one button selected at a time.

True
False

Check to review before finishing (will be flagged in Table of Contents)

Question 25 of 30     (worth 3 points)

Which search algorithm can be described as follows:

Given a collection you try every element in the collection until you have found the element or until you reach the end of the collection.

Check to review before finishing (will be flagged in Table of Contents)

Question 26 of 30     (worth 5 points)

Consider the following class:

public class BinarySearch {
  public static boolean contains(int[] a, int b) {
    if (a.length == 0) {
      return false;
    }
    int low = 0;
    int high = a.length-1;

    while(low <= high) {
      int middle = (low+high) /2;
      if (b> a[middle]){
        low = middle +1;
      } else if (b< a[middle]){
        high = middle -1;
      } else { // The element has been found
        return true;
      }
    }
    return false;
}
}

What is the purpose of iteration in the above code.

Check to review before finishing (will be flagged in Table of Contents)

Question 27 of 30     (worth 5 points)

Consider the following search method.

private static int search (int[] a, int target, int left, int right) {
   if (left > right)
      return 1?
   else {
   int midpoint = (left + right) / 2?
   if (a[midpoint] == target)
      return midpoint?
   else if (a[midpoint] < target)
      return search (a, target, midpoint + 1, right)?
   else
      return search (a, target, left, midpoint – 1)?
   }
}

Which of the following might be described as the base case(s) for this method to end the recursion?

if (a[midpoint] < target)

if (a[midpoint] > target)

if (a[midpoint] == target)

if (left > right)

Check to review before finishing (will be flagged in Table of Contents)

Question 28 of 30     (worth 5 points)

Consider both of the following approaches to the binary search:

Approach A

private static int search (int[] a, int searchValue) {
   int left = 0?
   int right = a.length1?
   while (left <= right) {
      int midpoint = (left+right)/2?
      if (a[midpoint] == searchValue)
         return midpoint?
      else if (a[midpoint] < searchValue)
         left = midpoint + 1?
      else
         right = midpoint – 1?
   }
   return 1?
}

Approach B

private static int search (int[] a, int target, int left, int right) {
   if (left > right)
      return 1?
   else {
   int midpoint = (left + right) / 2?
   if (a[midpoint] == target)
      return midpoint?
   else if (a[midpoint] < target)
      return search (a, target, midpoint + 1, right)?
   else
      return search (a, target, left, midpoint – 1)?
   }
}

Which approach uses recursion? Explain why Approach B has more parameters than Approach A.

Check to review before finishing (will be flagged in Table of Contents)

Question 29 of 30     (worth 5 points)

Consider the following class:

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
    {
     String cmd = evt.getActionCommand();
     if (cmd.equals("Find"))
      
        //Find player
        String newName = Integer.toString(player.getValue(jtfName.getText()));
      
        jtfHighScore.setText(newName);
        jbtReplace.setVisible(true);
    
    
     else (cmd.equals("Replace"))
     {
       
        //Find player
      
        int newScore = Integer.parseInt(jtfHighScore.getText());
                 
        player.replaceVal(jtfName.getText(),
                            newScore);
        String topScores= player.showAll();
         jtxtTop.setText(topScores);
       
          jtfName.setText("");
          jtfHighScore.setText("");
   
     }
     else (cmd.equals("Add"))
      {
         
          //addressBook player = new addressBook();
          int newScore = Integer.parseInt(jtfHighScore.getText());
          player.add(jtfName.getText(), newScore);
        
          jtfName.setText("");
          jtfHighScore.setText("");
        
        
         String topScores= player.showAll();
         jtxtTop.setText(topScores);  

        }

   }

Itentify two major problems with this code.

int newScore = Integer.parseInt(jtfHighScore.getText());

else (cmd.equals("Replace"))

else (cmd.equals("Add"))

String topScores

if (cmd.equals("Find"))

Check to review before finishing (will be flagged in Table of Contents)

Question 30 of 30     (worth 5 points)

You have previously defined these GUI objects needed for your JPanel.

jbtAdd

jbtFind

jbtReplace

Which code block should you use to add these objects to a new JPanel called p2 using a GridLayout with 1 row and 3 columns?

JPanel p2 = new JPanel(new GridLayout(1, 3));

A. pw is an object of the PrintWriter class. It is used to handle file writing for this program, specifically to write each line of an array to a file. B. pw is an object of the PrintWriter class. It is used to handle data reading and writing for this program, specifically to read each line of a file and write those lines to the screen. C. pw is an array that has as its data type a PrintWriter object. It is used to hold data that will ultimately be written to a file. D. pw is a method of the PrintWriter class. It is used to handle file reading and writing for this program, specifically to write each line from one file to another file.

Explanation / Answer

1 C

2.C

4. C

5.B

This method returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.

syntax

public int length()

6. C

This method appends one String to the end of another. The method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke this method.

Syntax:

public String concat (String s)

7. true

for eample consider a string PROGRAM

index of first character (P) is 0. index of last character (M) is 7 i.e length of string.

8.char aChar = anotherPalindrome.charAt(9)

the value at position 9 in palindrome is stored in achar.

9. if we pass only one argument, it retrieves the value from the specified index till the end

in the above example it returns ragain. r is at 11th index.

10. D

The java.lang.String.contains() method returns true if and only if this string contains the specified sequence of char values.

11 C it is the concept of overloding.When the Platypus class has an overloaded constructor. One constructor of this class would have no parameters. When an object is created without an argument, this constructor is called. The class also has another constructor that has a String parameter. When an object is created with a String argument, this constructor is called.

12 A Filename myHomePage = new Filename(FPATH, '/', '.');

the arguments should match

13 false   An array is a container object that holds a fixed number of values of a single type. not one or more types.

14 D

16 A   public static int binarySearch(Object[] a, Object key)

This method searches the specified array for the specified object using the binary search algorithm.

a This is the array to be searched.

key This is the value to be searched for.

17 B You need a variable that holds a constant value of zero and cannot be changed. this is one of the uses of static key word

19 A   a graphical user interface or GUI, pronounced is a type of interface that allows users to interact with electronic devices through graphical iconn

20 all are steps of task centered GUI

21 true

25 A linear search or sequential search is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted

26 A

27 A recursion can be ended when the case is false

28 B     Approach B uses recursion. binary search uses recursion to narrow the search space.

30 B

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote