Java Programming Principles Software Design Quiz
Week- 1
Earthquakes: Programming and Interfaces
1.
Question 1
For the assignment you wrote the method quakesOfDepth to print all the earthquakes from a data source whose depth is between a given minimum and maximum value, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with depth between -12,000.0 and -10,000.0, exclusive.
How many such earthquakes are there?
1 point
Enter answer here
127 |
2.
Question 2
For the assignment you wrote the method quakesOfDepth to print all the earthquakes from a data source whose depth is between a given minimum and maximum value, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with depth between -4000.0 and -2000.0, exclusive.
How many such earthquakes are there?
1 point
Enter answer here
157 |
3.
Question 3
For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “Quarry Blast” at the start of the earthquake’s title.
How many such earthquakes are there?
1 point
Enter answer here
19 |
4.
Question 4
For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “Alaska” at the end of the earthquake’s title.
How many such earthquakes are there?
1 point
Enter answer here
364 |
5.
Question 5
For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “Can” as a substring anywhere in the earthquake’s title.
How many such earthquakes are there?
1 point
Enter answer here
58 |
6.
Question 6
For the assignment you wrote the method findLargestQuakes to print a given number of earthquakes from a data source in order from largest magnitude. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for the 20 earthquakes with the largest magnitude.
What was the magnitude of the 20th largest earthquake?
1 point
Enter answer here
5.10 |
7.
Question 7
For the assignment you wrote the method findLargestQuakes to print a given number of earthquakes from a data source in order from largest magnitude. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for the 50 earthquakes with the largest magnitude.
What country had the 50th largest earthquake?
1 point
Solomon Islands
- Greece
- Kyrgyzstan
- Indonesia
- Japan
7.
For the assignment you wrote the method quakesWithFilter in the class EarthQuakeClient2 to filter earthquakes using two criteria. Modify that criteria to be those that are 1,000,000 meters (1,000 km) from Denver, Colorado whose location is (39.7392, -104.9903), and that end with an ‘a’ in their title (for example, that might be an earthquake in Nevada as that ends in ‘a’). Run your program on the file nov20quakedata.atom (a file with information on 1518 quakes). You should also print out the number of such earthquakes found.
How many such earthquakes are there?
74 |
8.
Question 8
For the assignment you wrote the method quakesWithFilter in the class EarthQuakeClient2 to filter earthquakes using two criteria. Modify that criteria to be those that are 1,000,000 meters (1,000 km) from Denver, Colorado whose location is (39.7392, -104.9903), and that end with an ‘a’ in their title (for example, that might be an earthquake in Nevada as that ends in ‘a’). Run your program on the file nov20quakedata.atom (a file with information on 1518 quakes). You should also print out the number of such earthquakes found.
How many such earthquakes are there?
1 point
Enter answer here
74 |
9.
Question 9
For the assignment you wrote the method quakesWithFilter in the class EarthQuakeClient2 to filter earthquakes using two criteria. Modify that critieria to be magnitude between 3.5 and 4.5 inclusive and depth between -55,000.0 and -20,000.0 inclusive. Run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with this criteria. You should also print out the number of such earthquakes found.
How many such earthquakes are there?
1 point
Enter answer here
15 |
10.
Question 10
For the assignment you wrote the method testMatchAllFilter in the class EarthQuakeClient2 to filter earthquakes using three criteria. Modify that criteria to be those with magnitude between 1.0 and 4.0 inclusive, to test the depth between -180,000.0 and -30,000.0 inclusive, and if the letter “o” is in the title. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with the same criteria. You should also print out the number of such earthquakes found.
How many such earthquakes are there?
1 point
Enter answer here
187 |
11.
Question 11
For the assignment you wrote the method testMatchAllFilter2 in the class EarthQuakeClient2 to filter earthquakes using three criteria. Modify that criteria to be those with magnitude between 0.0 and 5.0 inclusive, to test for the distance from Billund, Denmark at location (55.7308, 9.1153) is less than 3,000,000 meters (3000 km), and if the letter “e” is in the title. Run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with the same criteria. You should also print out the number of such earthquakes found.
How many such earthquakes are there?
1 point
Enter answer here
17 |
12.
Question 12
Suppose one adds the getName( ) method to the Filter interface and adds a String parameter to the constructor of all the classes that implement a Filter to include the name of the Filter. Suppose one then creates the following code segment after reading in earthquake data into the ArrayList of QuakeData called list.
6
MatchAllFilter maf = new MatchAllFilter();
maf.addFilter(new MagnitudeFilter(3.0,5.0,”Magnitude”));
Location city = new Location(51.7308,-34.1153);
maf.addFilter(new DistanceFromFilter(3000*1000, city, “Distance”));
maf.addFilter(new PhraseFilter(“any”, “i”, “Phrase”));
System.out.println(“Filters used are: ” + maf.getName());
There are no compile errors—the DistanceFromFilter class includes “implements Filter” in the first line of the class definition, the program runs and the output is:
Filters used are: Magnitude null Phrase
Which of the following is most likely the error as to why “null” was printed instead of Distance?
1 point
- In the DistanceFromFilter class, a private variable for the name was not assigned the value of the parameter representing the name of the filter in the Constructor method.
- The getName( ) method in the MatchAllFilter class had a logic error in it.
- The filter method in the EarthQuakeClient2 class has a logic error.
- There was not a getName( ) method in the DistanceFromFilter class.
Week- 2
Earthquakes: Sorting Algorithms
1.
Question 1
For an assignment you wrote the method sortByLargestDepth in the class QuakeSortInPlace to sort earthquakes by their depth from largest depth to smallest depth using the selection sort algorithm. Modify this method to do exactly 70 passes and then modify testSort to run this method on the file earthQuakeDataDec6sample2.atom. The file may not be completely sorted as there are many quakes in the file.
After running your program of 70 selection sort passes on this file, what is the depth of the last earthquake in the ArrayList?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct number of sort passes!
1 point
Enter answer here
-100000.00 |
2.
Question 2
For an assignment you wrote the method sortByMagnitudeWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the selection sort algorithm, and stopping with passes once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataWeekDec6sample1.atom.
How many passes are needed to sort this file?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
1 point
- 1282
- 1292
- 1298
- 1299
- 1301
- 1319
3.
Question 3
For an assignment you wrote the method sortByMagnitudeWithBubbleSortWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the bubble sort algorithm, and stopping with passes once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataWeekDec6sample2.atom. Make sure you are using the updated (1/12/16) version of the EarthQuakeParser class.
How many passes are needed to sort this file?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
1 point
- 1226
- 1233
- 1240
- 1255
- 1260
- 1267
4.
Question 4
Consider an ArrayList of following six integers.
2 4 5 9 8 1
What does this ArrayList look like after two passes of selection sort that sorts the elements in numeric order from smallest to largest?
1 point
- 1 2 4 9 8 5
- 1 2 5 4 9 8
- 1 2 5 9 8 4
- 1 4 5 9 8 2
- 2 4 5 9 8 1
- 4 1 5 2 8 9
4. For an assignment you wrote the method sortByMagnitudeWithBubbleSortWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the bubble sort algorithm, and stopping with passes once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataWeekDec6sample1.atom.
How many passes are needed to sort this file?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
1241 |
5.
Question 5
Consider an ArrayList of following six integers.
4 2 5 9 8 1
What does this ArrayList look like after two passes of bubble sort that sorts the elements in numeric order from smallest to largest?
1 point
- 2 4 1 5 8 9
- 2 4 5 1 8 9
- 2 4 5 8 1 9
- 4 2 5 1 8 9
- 4 2 5 8 1 9
- 4 2 5 9 8 1
6.
Question 6
For an assignment, you modified the compareTo operator in the class QuakeEntry to sort earthquakes by their magnitude first, from smallest magnitude to largest magnitude, and to break ties by their depth, from largest depth to smallest depth. Then you wrote the method sortWithCompareTo in the DifferentSorters class using the Collections.sort method. Modify this method to print out the QuakeEntry in position 600 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataWeekDec6sample2.atom.
What is the depth of the earthquake that is in position 600 after the earthquakes are sorted by the above method?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
1 point
Enter answer here
–53600.00 |
7.
Question 7
For an assignment, you wrote the TitleAndDepthComparator to sort earthquakes by their title first, in alphabetical order, and to break ties by their depth, from smallest depth to largest depth. You then used the Collections.sort method with the TitleAndDepthComparator. Modify the sortByTitleAndDepth method in the DifferentSorters class to print out the QuakeEntry in position 500 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataWeekDec6sample2.atom.
What is the depth of the earthquake that is in position 500 after the earthquakes are sorted by the above method?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
1 point
Enter answer here
-7630.00 |
8.
Question 8
For an assignment, you wrote the TitleLastAndMagnitudeComparator to sort earthquakes by the last word in their title first, in alphabetical order, and to break ties by their magnitude, from smallest to largest. You then used the Collections.sort method with the TitleLastAndMagnitudeComparator. Modify the sortByLastWordInTitleThenByMagnitude method in the DifferentSorters class to print out the QuakeEntry in position 500 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataWeekDec6sample2.atom.
What is the depth of the earthquake that is in position 500 after the earthquakes are sorted by the above method?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
1 point
Enter answer here
-1490.00 |
Week- 3
N-Grams: Predictive Text
1.
Question 1
Here is the code for runMarkovZero.
11
1 public void runMarkovZero() {
2 FileResource fr = new FileResource();
3 String st = fr.asString();
4 st = st.replace(‘\n’, ‘ ‘);
5 MarkovZero markov = new MarkovZero();
6 markov.setTraining(st);
7 for(int k=0; k < 3; k++) {
8 String text = markov.getRandomText(500);
9 printOut(text);
10 }
Suppose the line
1
markov.setRandom(18);
is put between lines 7 and 8, as the first line in the body of the for loop. Which one of the following options best describes how this line affects the program?
1 point
- Each of three randomly generated texts will all be identical.
- You will get three distinct randomly generated texts.
- You will get three randomly generated texts, but the second generated text has one new letter and then is identical to the first text minus one letter, and the third generated text has one new letter and then is identical to the second text minus two letters.
- You will get three randomly generated texts, but the second generated text has the first line with new random text, but the remaining lines are identical to the first n-1 lines where the first text had n lines. The third generated text has the first line with new random text, but the remaining lines are identical to the first n-1 lines of the second text.
2.
Question 2
In the MarkovRunner class in the runMarkovZero method, set the random seed to 1024 and run this method on the file confucius.txt.
What is the first line of text output when this program runs?
1 point
Enter answer here
eeuefmespwhsfoyu, s giowhersa eell: bma s.7shni:.at.ttdr.w aknf |
3.
Question 3
In the Tester class, run the program testGetFollowsWithFile with the string “o” and the file confucius.txt.
What is the size of the ArrayList of characters that follow “o”?
1 point
Enter answer here
10453 |
4.
Question 4
In the Tester class, run the program testGetFollowsWithFile with the string “he” and the file confucius.txt.
What is the size of the ArrayList of characters that follow “he”?
1 point
Enter answer here
3715 |
5.
Question 5
After the completion of the MarkovOne class to generate random text by finding all the characters that follow one character, set the random seed in the runMarkovOne method in the MarkovRunner class to 365. Then run runMarkovOne on the file romeo.txt.
What is the first line generated?
1 point
Enter answer here
y O wirs bloay Ger. fo. tifthy The, A My; st- ie d, s. bloulate, |
6.
Question 6
After the completion of the MarkovFour class to generate random text by finding all the characters that follow four characters, set the random seed in the runMarkovFour method in the MarkovRunner class to 715. Then run runMarkovFour on the file romeo.txt.
What is the first line of text generated?
1 point
Enter answer here
man in a green, for that haste, for a foot in her from Tybalt! |
7.
Question 7
After the completion of the MarkovModel class to generate random text by finding all the characters that follow N characters, set the random seed in the runMarkovModel method in the MarkovRunner class to 953 and pass in N as 7. Then run runMarkovModel on the file romeo.txt.
What is the first line of text generated?
1 point
Enter answer here
e uncle Capulet’s orchard. Enter an officer, and light- more |
8.
Question 8
For the class EfficientMarkovModel, which one of the following is the best place to call the buildMap method?
1 point
- In the setTraining method.
- In the constructor.
- In the getRandomText method before the for loop.
- In the getRandomText method as the first line in the for loop.
- In the setRandomText method after myRandom is set.
9.
Question 9
In the EfficientMarkovModel class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunnerWithInterface class call the runModel method on an EfficientMarkovModel of order 6 with seed 792 on the file confucius.txt.
How many keys are in the HashMap?
1 point
Enter answer here
10.
Question 10
In the EfficientMarkovModel class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunnerWithInterface class call the runModel method on an EfficientMarkovModel of order 5 with seed 531 on the file confucius.txt.
What was the size of the largest ArrayList in the HashMap?
1 point
Enter answer here
70162 |
10.
In the EfficientMarkovModel class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunnerWithInterface class call the runModel method on an EfficientMarkovModel of order 5 with seed 531 on the file confucius.txt.
What was the size of the largest ArrayList in the HashMap?
1549 |
Week- 4
Java: Tools and Libraries for Everyone
1.
Question 1
What is the name of the method that starts a Java program?
1 point
- firstMethod
- _start
- main
- begin
2.
Question 2
What argument type should the method that starts a Java program take?
1 point
- an int and a String
- an ArrayList<String>
- an array of Strings
- no arguments
3.
Question 3
What effect comes from declaring a field “static”?
1 point
- The value in the field cannot be changed once it is initialized.
- The value in the field can only be changed by code inside the class.
- The value in the field can only increase, never decrease.
- There is only one copy of that field for the entire class, not one per instance.
4.
Question 4
In Java, how is the keyword “throw” used?
1 point
- To make an exception occur when the program detects a problematic circumstance that it cannot directly handle.
- To transfer data across a network connection.
- To handle an exception that has already occurred.
- To open a file for writing.
5.
Question 5
Creating a new socket, as with:
1
Socket s = new Socket(addr,port);
can throw an IOException according to the documentation of the java.net.Socket class.
Which of the following structures is the best way to create a socket while handling the exception?
1 point
try {
Socket s = new Socket(addr, port);
//code that uses s
}
catch(IOException ioe) {
//code to handle the exception
}
6.
Question 6
In Java, how is the keyword “finally” used?
1 point
- In exception handling to specify code that should be executed regardless of whether an exception happened or not.
- To specify code to run when the program exits.
- To specify a piece of code to execute immediately after the end of the current loop.
- To indicate that you are finally done debugging a particularly complicated piece of code.
7.
Question 7
If you wanted to read the contents of a file without using the edu.duke package, you might call Files.newBufferedReader.
What would you pass into Files.newBufferedReader?
1 point
- a Comparator
- an IOException
- a Path
- a FileResource
8.
Question 8
If you need to read data from a website without using the edu.duke package, you would probably want to use classes found in which package?
1 point
- java.internet
- java.lolcats
- java.net
- java.urls
9.
Question 9
You saw that a BufferedReader can be used to read data from a file on the local computer, as well as from a website. You could also use it with other sources of data, as long as you have an appropriate Reader class to access the data.
These capabilities are a great example of the benefits of which programming principles? Select the two best options.
1 point
- Open/Closed Principle: the BufferedReader class is designed such that it can have its functionality expanded (to read from new data sources) without having to modify its code
- Everything Is A Number: the BufferedReader class can read data of any type, and return it as a Java object of an appropriate class
- Abstraction: the BufferedReader class can work with any class that conforms to a specific interface, and does not need to know the details of how/where it reads data
- Astrachan’s Law: you can only use the BufferedReader class to solve interesting problems, never for something boring or that you could do by hand
- Write Robust Code: the BufferedReader class never throws an exception, instead it deals with every situation