Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections Practice Questions

Work through AP Computer Science A practice questions on Data Collections, then check each answer against a worked solution.

Free · no sign-up Open questions need no account. Sign up or log in to open the rest.

Subtopic: Using Text Files Clear filter

Questions 171–180 of 181
  1. 171
  2. 172
  3. 173
  4. 174
  5. 175
  6. 176
  7. 177
  8. 178
  9. 179
  10. 180

Question 171

Multiple choice Using Text Files
Using the Employee class defined above.
public class Employee {
private String name;
private int hours;
private double rate;
public Employee(String n, int h, double r) { name=n; hours=h; rate=r; }
public String getName() { return name; }
public double getPay() { return hours * rate; } }
A text file contains:
Alice,10,25.0
Bob,8,30.0
What is printed?

ArrayList<Employee> list = new ArrayList<Employee>();
while (sc.hasNext()) {
String[] p = sc.nextLine().split(",");
list.add(new Employee(p[0], Integer.parseInt(p[1]), Double.parseDouble(p[2])));
}
sc.close();
double diff = list.get(1).getPay() - list.get(0).getPay();
System.out.println(diff);
Pick an answer, then check it. The solution opens in a window.

Question 172

Multiple choice Using Text Files
Consider the following class. public class Record { private String subject; private String name; private int score; public Record(String sub, String n, int s) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 173

Multiple choice Using Text Files
Suppose the file "numbers.txt" contains: 8 -3 12 -1 4 public static int averagePositive(String filename) { Scanner scan = new Scanner(new File(filename)); int…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 174

Multiple choice Using Text Files
Suppose the file 'words.txt' contains: hello world java code public static String readOddLines(String filename) { Scanner scan = new Scanner(new…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 175

Multiple choice Using Text Files
A text file "items.txt" contains the following lines, one per line. apple,3 banana,5 cherry,2 Assume the file is opened successfully and the java.util.Scanner…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 176

Multiple choice Using Text Files
A text file "scores.txt" contains the following two lines. 85 90 78 92 88 95 Assume the file is opened successfully and the java.util.Scanner and java.io.File…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 177

Multiple choice Using Text Files
A text file contains the following exact contents (one record per line, comma-separated):
name,grade,present
Ali,87,true
Bea,72,false
Cyd,91,true
Dax,54,true
A program creates a properly constructed Scanner sc reading that file, then runs the following code. Assume any imports needed are present.
sc.nextLine();
int total = 0;
int n = 0;
while (sc.hasNext())
{
    String line = sc.nextLine();
    String[] parts = line.split(",");
    if (parts[2].equals("true"))
    {
        total = total + Integer.parseInt(parts[1]);
        n = n + 1;
    }
}
sc.close();
double avg = total * 1.0 / n;
What is the value of avg when the segment finishes?
Pick an answer, then check it. The solution opens in a window.

Question 178

Multiple choice Using Text Files
A text file contains the following integers, separated by spaces. 6 2 9 3 8 Assume sc is a properly constructed Scanner that reads the file. What is printed by…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 179

Multiple choice Using Text Files
Consider the following class. public class ScoreEntry { private String name; private int score; public ScoreEntry(String n, int s) { name = n; score = s; }…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 180

Multiple choice Using Text Files
A text file "orders.txt" contains the following data. red 3 blue 5 green 2 Assume the file is opened successfully and the java.util.Scanner and java.io.File…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 171 of 181
Page 18 of 19