Table of Contents
Java Programming: Solving Problems with Software Week 1 Quiz Answers
Quiz:Getting Started with BlueJ
Q 1. Suppose you have written a Java program that has one class and one method.
Which one of the following best describes what you need to do in order to run the program?
- Compile the program, create a new object for the class, and call the method.
- Compile the program, call the method, and then create a new object for the class.
- Create a new object for the class, call the method, and then compile the program.
- Create a new object for the class, compile the program, and call the method.
Q 2. Which one of the following best describes what happens when one compiles a Java program in BlueJ on their computer?
- The .java file is translated into a .class file that can be run on that computer.
- A Java object is created that can be run on that computer.
- Your Java program opens and processes the data files specified in your program.
- The .class file is translated into a file that can be run on that computer.
Q 3. Which one of the following best describes BlueJ?
- BlueJ is an object-oriented programming language.
- BlueJ is a programming environment.
- BlueJ is software that organizes data files.
- BlueJ is a word processor for editing code.
Quiz:Variables and Mathematical Operators
Q 1. Consider the following Java statements.
int a = 5;
int b = 3;
int c = 4;
c = a + b ;
What is the value of c after these lines execute?
Answer:
8
Q 2. Consider the following Java statements.
int a = 1 ;
int b = a + 2 ;
int c = b + b ;
What is the value of c after these lines execute?
Answer:
6
Q 3. Consider the following Java statements.
int a = 3 ;
int b = 1 ;
int c = 2 + a * 5 - b ;
What is the value of c after these lines execute?
Answer:
16
Quiz:Functions and Conditionals
Q 1. Consider the following Java methods.
int func2(int w) {
return w * 3;
}
int func1 (int a, int b) {
int n = a + b;
return 2 + func2(n);
}
int start() {
int z = 4;
return func1(z, 1) - 3;
}
Answer:
14
Q 2. Before you start writing your own Java programs, it is important you fully understand what your Java code will do. Try solving the following problems in this quiz by hand.
Consider the following Java methods.
int f(int x) {
return x*2 - 1;
}
int h() {
int a = 3;
int b = f(a) + f(4);
return b;
}
Answer:
12
Q 3. Consider the following method g.
int g (int a) {
if (a < 9) {
return 9;
}
if (a < 7) {
return 7;
}
if (a < 4) {
return 4;
}
return 0;
}
What is the value returned from the call g(5)?
Answer:
12
Q 4. Consider the following method k.
int k (int a, int b) {
if (a < b) {
if (b > 4) {
return 0 ;
}
else {
return 1;
}
}
else {
if (a > 4) {
return 2;
}
else {
return 3;
}
}
}
For which of the values a and b would 2 be the return value?
Answer:
a=6, b=6
Q 1. Consider the following method play, and assume appropriate import statements are there for classes used in this function.
public void play () {
Frog fred = new Frog();
Cat jiang = new Cat();
fred.hop(4);
jiang.jump(5, fred);
String greet = That is all";
fred.say(greet, 3);
}
Without seeing any details of classes used in this program and their methods, but assuming the methods shown are valid methods for these classes, which one of the following are methods in the Frog class?
- Only say
- hop and say
- Only jump
- hop, jump and say
- Only hop
Q 2. Consider the following Java class
public class Thing {
private int a;
public Thing(int x) {
a = x;
}
public int geta() {
return a;
}
public void print() {
int b = 4;System.out.println(geta() + " " + b);
}
}
What is (or are) the instance variable(s) in this class?
- a and x
- x
- b
- a
- a, x, and b
Q 3. Consider the following Java class.
public class Thing {
private int a;
public Thing(int x) {
a = x;
}
public int geta() {
return a;
}
}
And consider the following code segment that uses the class Thing.
Thing f = new Thing(3);
Thing g = new Thing(5);
Thing h = f;
Thing j = h;
How many Thing objects are created?
Answer:
2
Q 4. Consider the following code segment that uses the edu.duke FileResource class. The method .length() calculates how many characters are inside a string. For example, for the string “puppy”, .length() would calculate a value of 5. We will learn more about strings later in this course.
FileResource f = new FileResource("words.txt");
for (String g : f.lines()) {
if (g.length() > 5) {
System.out.println(g);
}
}
Suppose the file words.txt contains the following lines:
cat
elephant
monkey
tiger
lion
Which one of the following would be the output from this code segment?
Hint: Be sure to review the documentation for FileResource if you do are not sure what this class does or what methods it contains: http://www.dukelearntoprogram.com/course2/doc/.
Answer:
elephant
monkey
Q 5. Consider the following Java class.
public class Thing {
private int a;
public Thing(int x) {
a = x;
}
public int geta() {
return a ;
}
public void print() {
int b = 4 ;
System.out.println(geta() + " " + b);
}
}
And consider the following code segment that uses the Thing class.
Thing f = new Thing(4);
System.out.println(f.geta());
Which one of the following is NOT a primitive type?
- b
- a
- x
- f
Quiz: Calculating the Perimeter of a Shape
Q1. What is the perimeter of the shape made from the file datatest4.txt whose contents are shown below (Truncate your answer to two decimal places)?
-3, 9
-8, 7
-12, 4
-6, -2
-4, -6
2, -8
6, -5
10, -3
8, 5
4, 81
- correct Answers =59.45
Q2. What is the average length of a side in the shape made from the file datatest4.txt whose contents are shown below (Truncate your answer to two decimal places)?
-3, 9
-8, 7
-12, 4
-6, -2
-4, -6
2, -8
6, -5
10, -3
8, 5
4, 81
- correct Answers= 5.94
Q3. What is the longest side in the shape made from the file datatest4.txt whose contents are shown below (Truncate your answer to two decimal places)?
-3, 9
-8, 7
-12, 4
-6, -2
-4, -6
2, -8
6, -5
10, -3
8, 5
4, 81
- Correct Answers= 8.48
Q4. What is the largest perimeter of a shape made from the shapes in files dataset1.txt, dataset2.txt, dataset3.txt, dataset4.txt, dataset5.txt, and dataset6.txt (Truncate your answer to two decimal places)?
- correct Answers = dataset5.txt
Q5. What is the name of the file that has the shape with the largest perimeter from the four files example1.txt, example2.txt, example3.txt and example4.txt? 1 point
- example1.txt
- example2.txt
- example3.txt
- example4.txt
Q6. The method getNumPoints returns the number of points in a Shape s.
Which one of the following is NOT a correct implementation of getNumPoints?
public int getNumPoints (Shape s) {
int count = 0;
for (Point p : s.getPoints()) {
int newPoint = 1;
count = count + newPoint;
}
return count;
}
public int getNumPoints (Shape s) {
int count = 0;
for (Point p : s.getPoints()) {
count = count + 1;
}
return count;
}
- correctAnswers :
public int getNumPoints (Shape s) {
int count = 0;
for (Point p : s.getPoints()) {
count = count + count;
}
return count;
}
Q7. Consider the following code for the function mysteryShape that has one parameter a Shape s and calls the function getNumPoints from the assignment.
public double mysteryShape (Shape s) {
double tmp = 0;
for (Point p : s.getPoints()) {
if (p.getX() > 0) {
if (p.getY() < 0) {
tmp = tmp + 1;
}
}
}
return tmp / getNumPoints(s);
}
- Which one of the following best describes the purpose of this function?
- The function computes the sum of those points from the Shape s that have a positive X or a negative Y.
- The function computes the percentage of those points from the Shape s that have a positive X and a negative Y.
- The function computes the percentage of those points from the Shape s that have a positive X or a negative Y.
- The function computes the sum of those points from the Shape s that have a positive X and a negative Y.
Java Programming: Solving Problems with Software Week 2 Quiz Answers
Finding a Gene in DNA
Q 1. Consider the assignment that goes with this lesson and its algorithm for finding a gene with stop codon TAA.
Consider the following DNA string.
- “AAATGCCCTAACTAGATTAAGAAACC”
Which one of the following is the gene returned using that algorithm?
- ATGCCCTAACTAGATTAA
- The empty string.
- ATGCCCTAA
Note: This is the correct answer.
- CCC
- ATGCCC
Q 2. Which one of the following replaces the String variable dna with the lowercase version of this string?
Answer:
dna = dna.toLowerCase();
Q 3. After adding two additional parameters to findSimpleGene for the startCodon and stopCodon, which of the following is another change that must be made for the program to compile?
- Two return statements must be added to findSimpleGene
- Two if statements must be added to findSimpleGene.
- Two arguments must be added to the call to findSimpleGene.
- Two if statements must be added to testSimpleGene
Q 4. Suppose that stringa is in stringb at position pos. Which one of the following returns the part of stringb that comes after stringa in the method lastPart?
Answer :
return stringb.substring(pos+stringa.length());
Q 5. In the method twoOccurrences, suppose pos is the value of the first occurrence of stringa when stringa is found. Which of the following lines of code assigns pos2 to the second occurrence of stringa if there is a second occurrence?
Answer:
pos2 = stringb.indexOf(stringa, pos+1);
Q 6. Consider assignment 2 for this lesson. Consider the code to find and print all the YouTube URL links.
URLResource file = new URLResource("http://someURL");
for (String item : file.words()) {
String itemLower = item.toLowerCase();
int pos = itemLower.indexOf("youtube.com");
if (pos != -1) {
MISSING CODE
}
}
Which one of the following is the missing code to find and print all the YouTube URLs?
Answer:
int beg = item.lastIndexOf("\"",pos);
int end = item.indexOf("\"", pos+1);
System.out.println(item.substring(beg+1,end));
Quiz:Finding All Genes in DNA
Q 1. Which one of the following is the first gene for this strand of DNA where you want to consider all three of the stop codons?
“AATGCTAACTAGCTGACTAAT”
- ATGCTAACTAGCTGA
- The empty string
- ATGCTAACTAG
- ATGCTAA
- CTAACTAGC
- ATGCTA
Q 2. Consider the following code segment to count the number of times the string “TG” occurs in the string dna, which does not work correctly.
String dna = "CTGCCTGCATGATCGTA";
int pos = dna.indexOf("TG");
int count = 0;
while (pos >= 0) {
count = count + 1;
pos = dna.indexOf("TG",pos);
}
System.out.println(count);
Which of the following best describes the error?
- The variable pos should be initialized to 0 in line 2 and only set with indexOf inside the while loop.
- Each time pos is reset in the while loop it finds the same “TG”.
- The two lines in the body of the while loop should be swapped so the count happens after pos is given a new value.
- The count is off by 1. The count should be initialized to 1 to take into account the first “TG” that is found.
Q 3. Consider the following segment of code from a program.
while (count < 3) {
count += 1;
newDna = newDna + dna.substring(startPos,pos);
startPos = pos+1;
pos = dna.indexOf("T", startPos);
if (pos == -1) {
break;
}
}
How many different ways are there to break out of this loop?
- 2
- 0, it is an infinite loop
- 1
- 3
Q 4. Which one of the following conditionals correctly evaluates to true if the integer num is an odd number and to false if it is an even number.
Answer:
if (num % 2 == 1)
Quiz:Debugging: Part 1
Q 1. Your friend is trying to solve the following problem using Java:
Write a method that finds each occurrence of “abc_” in a String input (where _ is a single character) and prints “bc_” for each such occurrence. For example, findAbc(“abcdefabcghi”) should print:
bcd
bcg
You friend has just finished writing a solution and needs help testing it.
Create a new BlueJ project. Create a class and copy and paste the two methods (below) into it.
public void findAbc(String input) {
int index = input.indexOf("abc");
while (true) {
if (index == -1) {
break;
}
String found = input.substring(index+1, index+4);
System.out.println(found);
index = input.indexOf("abc", index+4);
}
}
public void test() {
//no code yet
}
Test method findAbc() by adding a line in the test() method:
findAbc("abcd");
Do this example by hand. What should the output be if the method is working correctly?
Answer:
note: if you know the answer the answer please comments.
Q 2. Compile your class and run the test() method. What is the output?
Answer:
bcd
Q 3. So far so good! Comment out the line findAbc(“abcd”) and add a new line in the test() method:
findAbc("abcdabc");
What should the output be if the method is working correctly?
Answer:
bcd
Q 4. Compile and run your code. Which two of the following are results?
Answer:
- “bcd” is printed.
- A StringIndexOutOfBoundsException is thrown.
Q 5. Which line of the program is causing the error?
Answer:
String found = input.substring(index+1, index+4);
Q 6. What is the index that is out of range?
Answer:
8
Q 7. What is the length of input for this method call?
Answer:
7
Q 8. At the time this error is produced, what are the values of index+1 and index+4? You may want to add print statements to your code and run it again to see what these values are.
Enter your answer as numbers separated by a comma: int1, int2. For example if index+1 were 1 and index+4 were 5, you would enter:
1,5
Answer:
note : comments the answer:
Q 9. Why does the program throw an index out of bounds exception for this input?
- The method substring() is trying to access index 8 but the String input is only 7 characters long.
- The method findAbc() never works when the String input is longer than 6 characters.
- The program finds the wrong index for the second occurrence of “abc”.
- The method substring() is trying to access index 5, but the value of the int index is 4.
Q 10. Imagine your friend wants to get help from Coursera classmates on the discussion forums.
Which one of the following would be the most helpful way to describe the problem so that others can easily help?
Answer:
My method findAbc() has an index out of bounds exception at the line
String found = input.substring(index+1, index+4);
Q 11. Which of the following are examples of input that would also throw this exception? Check all that are correct.
Answer:
- “abcbbbabcdddabc”
- “aaaaabc”
- “yabcyabc”
Q 12. Add an if statement, such that the while loop breaks if the index is out of bounds. Compile the code and run it again. It should now print “bcd” without throwing an exception. Try it on the examples you selected the previous question to make sure it works.
What are possible conditions for this if statement? (Choose all that are correct.)
Answer:
index >= input.length() - 3
index > input.length() - 4
Quiz:Debugging: Part 2
Q 1. You have fixed one bug in your friend’s code, so that it no longer throws an index out of bounds exception, by adding the condition index >= input.length() – 3:
public void findAbc(String input){
int index = input.indexOf("abc");
while (true){
if (index == -1 || index >= input.length() - 3){
break;
}
String found = input.substring(index+1, index+4);
System.out.println(found);
index = input.indexOf("abc",index+4);
}
}
public void test(){
//findAbc("abcd");
findAbc("abcdabc");
}
Let’s test some more. Run the above code with input “abcdkfjsksioehgjfhsdjfhksdfhuwabcabcajfieowj”. What is the output?
Enter your answer with the printed strings separated by commas, str1, str2. For example, if the output were “bcq”, “bcd”, and “bcu”, you would write: bcq, bcd, bcu.
Answer:
bcd, bca
Q 2. Check this answer – do the problem by hand. What should be the correct output for input “abcdkfjsksioehgjfhsdjfhksdfhuwabcabcajfieowj”?
Enter your answer with the printed strings separated by commas, str1, str2. For example, if the output were “bcq”, “bcd”, and “bcu”, you would write: bcq, bcd, bcu
Answer:
bcd,bca,bca
Q 3. You will have to do some debugging, since the output wasn’t what you were expecting. Let’s see which occurrences of “abc” the program is finding. Add a line to print the index before found is calculated.
What are the indices printed? Select all that are correct.
Answer:
- 0
- 30
Q 4. You can see that the program is finding the first two occurrences of “abc” but not the third. The while loop is breaking without finding this occurrence. So we know that when the variable index is updated after finding the second occurrence of “abc” at index 30, it must be updated either to -1 or to something greater than or equal to the length of input – 3. Let’s see which it is.
Add a print statement. You might find it helpful to distinguish this from the print statement you added earlier so you can more easily see which is the index before updating and which is the index after. For example, you might do something like:
System.out.println("index " + index);
//code
System.out.println("index after updating " + index);
What is the value of index after updating for the last time?
Answer :
-1
Q 5. Now we can tell that the code isn’t finding the last occurrence of “abc” even though we can see that a third occurrence exists. At what index should the third occurrence of “abc” be found?
Answer : 33
comment the correct answer
Q 6. After the program finds the 2nd occurrence of “abc”, at what index does it start searching for the 3rd occurrence?
Hint: look at the line index = input.indexOf(“abc”,index+4);
Answer:
34
Q 7. What are some other examples of input that would also have this problem? Select all that are correct.
Answer:
- “kdabcabcjei”
- “abcabcabcabca”
Q 8. Imagine your friend wants to get help from Coursera classmates on the discussion forums. Which of the following would be the most helpful way to describe the problem so that others can easily help?
- My method findAbc() isn’t working.
- My method findAbc() is giving me the wrong answer.
- My method findAbc() works on “abcdabc” but is giving me the wrong answer with input “abcdkfjsksioehgjfhsdjfhksdfhuwabcabcajfieowj”.
- My method findAbc() works on “abcdabc” but is giving me the wrong answer with input “abcdkfjsksioehgjfhsdjfhksdfhuwabcabcajfieowj”: it prints “bcd, bca” when it should print “bcd, bca, bca”. It also gives the wrong answer with input “kdabcabcjei”, it prints “bca” when it should print “bca, bcj”.
Q 9. What is causing this bug?
- When the character following “abc” is “a” the program misses the next “abc”
- When one occurrence of “abc” is followed immediately by another occurrence of “abc”, the while loop breaks
- When one occurrence of “abc” is followed immediately by another occurrence of “abc”, the method does not find the second “abc” because it starts searching at the “b” rather than at the “a” following the first “abc”
- The method will never find any occurrences of “abc” after the second one
Q 10. Which change needs to be made to fix the bug?
Answer:
index = input.indexOf("abc",index+4);
Q 11. Make the change. Test your method on the input options that would have caused the bug (see question 7). What is the output when you run it with input “abcabcabcabca”?
Enter your answer with the printed strings separated by commas, str1, str2. For example, if the output were “bcq”, “bcd”, and “bcu”, you would write: bcq, bcd, bcu
Answer: bca, bca, bca, bca
note: if you know the answer please comments
Quiz: Using StorageResource
Q 1. How many genes are there in the file brca1line.fa?
Answer:
1
Q 2. How many genes are there in the file brca1line.fa that are longer than 60?
Answer:
1
Q 3. How many genes are there in the file brca1line.fa that have a C-G-ratio greater than 0.35?
Answer:
1
Q 4. If dna is a String and cgcount is the integer number of C’s and G’s in the String, which one of the following is the correct return statement to return the decimal number of the ratio of C’s and G’s in the string to the length of the string?
Answer:
return ((double) countcg)/dna.length();
Q 5. Consider the following code where dna is a String. This code attempts to count the number of “C”’s in dna but has an error. It has forgotten to update the variable start
int countcg = 0;
int start = 0;
while (true) {
int pos = dna.indexOf("C", start);
if (pos == -1) {
break;
}
countcg += 1;
}
Which one of the following best describes how to fix this error?
- The code start = pos – 1; should go right before the line containing if (pos == -1).
- The code start = pos – 1; should go immediately after the line containing countcg += 1.
- The code start = pos; should go right before the line containing if (pos == -1).
- The code start = pos + 1; should go right before the line containing if (pos == -1).
- The code start = pos + 1; should go immediately after the line containing countcg += 1;.
- The code start = pos; should go immediately after the line containing countcg += 1;
Q 6. Consider that storeAll is a method that returns a StorageResource of Strings. Which one of the following is a valid statement that would be useful in the storeAll method?
Answer:
StorageResource store = storeAll(s);
Quiz: Strings in Java
Q1. Consider finding all the YouTube links on a web page. What is the video for the second YouTube link on the web page http://www.dukelearntoprogram.com/course2/data/manylinks.html about?
- Turkeys, Ducks and Quails
Q2. Consider the following mystery method.
Question 2
public String mystery(String dna) {
int pos = dna.indexOf("T");
int count = 0;
int startPos = 0;
String newDna = "";
if (pos == -1) {
return dna;
}
while (count < 3) {
count += 1;
newDna = newDna + dna.substring(startPos,pos);
startPos = pos+1;
pos = dna.indexOf("T", startPos);
if (pos == -1) {
break;
}
}
newDna = newDna + dna.substring(startPos);
return newDna;
}
- The method mystery is given a string and it returns a string which is the same string but with the first three T’s removed
Q3. Use the following data file to answer this question: http://www.cs.duke.edu/~rodger/GRch38dnapart.fa. How many genes are in this file?
- 69
Q4. Use the following data file to answer this question: http://www.cs.duke.edu/~rodger/GRch38dnapart.fa. How many genes in this file are longer than 60?
- 23
Q5. Use the following data file to answer this question: http://www.cs.duke.edu/~rodger/GRch38dnapart.fa. How many genes in this file have cgRatio greater than 0.35?
- 40
Q6. Use the following data file to answer this question which represents one long strand of DNA: http://www.cs.duke.edu/~rodger/GRch38dnapart.fa. How many times does the codon CTG appear in this strand of DNA?
- 224
Q7. Use the following data file to answer this question: http://www.cs.duke.edu/~rodger/GRch38dnapart.fa. What is the length of the longest gene in the collection of genes found in this file?
- 489
Java Programming: Solving Problems with Software Week 3 Quiz Answers
Quiz: Which Countries Export…?
Q 1. Which one of the following is a correct version of the code for comparing the name of a country that is in a CSV file that the CSVParser variable parser is processing to see if it is the same name as the String variable country?
Answer:
for (CSVRecord record : parser) {
if (record.get("Country").equals(country)) {
// do something
}
}
Q 2. Suppose there is a CSV file with a column named Exports. A CSVRecord is used to get a String named export, representing the data in this column.
Which one of the following is the correct code for the if condition to determine if both the Strings exportItem1 and exportItem2 are in this CSVRecord in the export column?
Answer:
if (export.contains(exportItem1) && export.contains(exportItem2)) { }
Q 3. Run your program on the file exportdata.csv. What is the name of the country that is listed as the third country that exports both fish and nuts?
Answer: Panama
note: If you know the Answer please comments
Q 4. Run your program on the file exportdata.csv. How many countries export sugar?
Answer: 21
note: If you know the Answer please comments
Q 5. Run your program on the file exportdata.csv. Call the method countryInfo with the country Nauru. Which one of the following items is listed as an export from this country?
- Nuts
- Bananas
- Wood
- Phosphates
- Oil
Q 6. Run your program on the file exportdata.csv. What is the name of the second economy (that is, what is listed on the second line of the output) listed that exports one trillion dollars or more?
Hint: The country’s Value (dollars) string in the CSV file should have a greater length than the string “$999,999,999,999”.
Answer: European Union.
Quiz: Weather Data
Q 1. If cr is a CSVRecord from one of the weather data files, which one of the following is a correct way to add a temperature from the file to an accumulating double variable named total?
Answer:
double temp = Double.parseDouble(cr.get(“TemperatureF”));
total += temp;
Q 2. Suppose the name of the file that has the coldest temperature is obtained with this line of code:
filenameWithColdestTemp = something.getName();
Which one of the following must be true about something?
- something must be of type CSVRecord
- something must be of type CSVParser
- something must be of type File
- something must be of type DirectoryResource
Q 3. Run your program on weather data from May 1, 2014 in the file weather-2014-05-01.csv.
What was the coldest temperature on this day?
- Answer: 64
Q 4. Run your program on the data files for the year 2014.
What was the coldest temperature in this year?
Answer:
Comment the Answer:
Q 5. Run your program on weather data from April 1, 2014 in the file weather-2014-04-01.csv.
At what time of day did the lowest humidity occur (from the DateUTC column)?
- 11:51:00
- 13:51:00
- 16:51:00
- 18:51:00
- 20:51:00
Q 6. Run your program on weather data from 2014.
What was the lowest humidity in that year?
Answer:
Comment the answer.
Q 7. Run your program on weather data from 2014.
At what time of day did the lowest humidity in that year occur?
- 11:51:00
- 13:51:00
- 16:51:00
- 18:51:00
- 20:51:00
Q 8. Run your program on weather data from June 1, 2014 in file weather-2014-06-01.csv.
What was the average temperature on this day?
(Give your answer to four decimal places, and truncate the rest. For example, if the average temperature was 989.777874, you would enter 989.7778 as your answer.)
Answer: comment the Answer
Q 9. Run your program on weather data from March 30, 2014 in file weather-2014-03-30.csv.
What was the average temperature in Fahrenheit for those temperature readings when the humidity is greater than or equal to 80?
(Give your answer to three decimal places, and truncate the rest. For example, if the average temperature was 989.777874, you would enter 989.777 as your answer.)
- Answer: 58.775
Quiz: CSV Files and Basic Statistics in Java
Q 1. Questions 1–3 refer to the data file exportdata.csv, available in the .zip download here.
Run your program from the first lesson programming exercise Parsing Export Data on the file exportdata.csv.
What is the name of the country that is listed as the second country that exports both cotton and flowers?
Answer:
Zambia:
Q 1.1. Run your program from the first lesson programming exercise Parsing Export Data on the file exportdata.csv.
How many countries export cocoa?
Note: You should only enter a one- or two-digit number representing the number of countries, with no other information included.
- 17
Q2. Run your program from the first lesson programming exercise Parsing Export Data on the file exportdata.csv.
What is the name of the third country (on the third line of the output) listed whose exports are valued at one trillion US dollars or more?
(Hint: Their value in the CSV file should be greater than $999,999,999,999.)
- Germany
Q3. Run your program developed in Parsing Weather Data to determine the lowest humidity in the file for June 29th, 2014 (weather-2014-06-29.csv).
What was the lowest humidity reading on that day?
Note: You should only enter your two-digit number result, with no other additional information included.
- 40
Q4. Run your program from programming exercise Parsing Weather Data to determine the lowest humidity in the file for July 22nd, 2014 (weather-2014-07-22.csv).
At what time of day did that humidity occur?
(Refer to the time from the DateUTC column.)
- 20:51:00
Q5. Run your program from programming exercise Parsing Weather Data to determine the lowest humidity reading in the entire year of 2013.
What was the lowest humidity reading?
Note: You should only enter your two-digit number result, with no other additional information included.
- 16
Q6. Run your program from programming exercise Parsing Weather Data to determine the average temperature in Fahrenheit on August 10, 2013 (weather-2013-08-10.csv).
Give your answer with four decimal digits and truncate the rest.
- 80.1964
Q7. Run your program from programming exercise Parsing Weather Data to determine the average temperature in Fahrenheit for those temperature readings when the humidity is greater than or equal to 80 on September 2, 2013 (weather-2013-09-02.csv).
Give your answer with three decimal digits and truncate the rest.
- 72.593
Q8. Run your program from programming exercise Parsing Weather Data to determine which day of the year had the coldest temperature in 2013.
- January 23, 2013
Q9. Run your program from programming exercise Parsing Weather Data on 2013 data.
What was the coldest temperature recorded in 2013?
Give your answer with one decimal digit. (For example: 10.0)
- 19.0
Java Programming: Solving Problems with Software Week 4 Quiz Answers
Quiz: Baby Names
Q1. What is the rank of the girl’s name “Emily” in 1960?
- 251
Q2. What is the rank of the boy’s name “Frank” in 1971?
- 54
Q3. What is the girl’s name of rank 350 in 1980?
- Mia
Q4. What is the boy’s name of rank 450 in 1982?
- Forrest
Q5. Suppose Susan was born in 1972. Based on her name’s rank in 1972, what would her name be if she were born in 2014 (that is, what name in 2014 had the same rank that “Susan” had in 1972)?
- Addison
Q6. Suppose Owen was born in 1974. Based on his name’s rank in 1974, what would his name be if he were born in 2014 (that is, what name in 2014 had the same rank that “Owen” had in 1974)?
- Leonel
Q7. In which year from 1880 to 2014 does the girl’s name “Genevieve” have the highest rank (over all the data files)?
If there is more than one year with the highest rank, choose the earliest one.
- 1914
Q8. In which year from 1880 to 2014 does the boy’s name “Mich” have the highest rank (over all the data files)?
If there is more than one year with the highest rank, choose the earliest one.
- 1960
Q9. What is the average rank of the girl’s name “Susan” over all the data files?
Give the answer to two decimal places.
- 173.51
Q10. What is the average rank of the boy’s name “Robert” over all the data files?
Give the answer to two decimal places.
- 10.75
Q11. What is the total number of girls born in 1990 with names ranked higher than the girl’s name “Emily” in 1990?
Give the answer without any commas or decimal points.
- Ans:- 323200
Q12. What is the total number of boys born in 1990 with names ranked higher than the boy’s name “Drew” in 1990?
Give the answer without any commas or decimal points.
- 1498074
Batch Grayscale Images
Q 1. Consider writing code to list out files in one of your folders using DirectoryResource. The following attempt at writing such code has an error.
Which one of the following best explains what the error is?
- The dr that is highlighted should be listFiles.
- The dr that is highlighted should be dir.
- The dr that is highlighted should be File.
- The dr that is highlighted should be DirectoryResource.
Q 2. Shown below is the code that was developed in one of the videos to convert many images to grayscale and to display those grayscale images.
Consider adding additional code to this program to save each of the new grayscale images created as a file.
Which one of the following is the best method to modify to make this change?
- Modify the makeGray method.
- Create a new method to read each image from a file and save the image.
- Modify the selectAndConvert method.
Q 3. Suppose one wants to convert a given image to grayscale and then display and save the resulting grayscale image as a file.
The code below has been started for you. The variable f is a file of an image and the method makeGray returns an image that is the grayscale image of the original image.
ImageResource original = new ImageResource(f);
ImageResource grayscale = makeGray(original);
[MISSING CODE]
Which one of the following is the missing code that will convert the original image into a new file that is a grayscale version of the original image?
Answer:
String fname = original.getFileName();
String newName = “grayscale-” + fname;
grayscale.setFileName(newName);
grayscale.draw();
grayscale.save();
Q 4. Consider writing a program to create new images that are photographic negatives (or inverted images) of selected images.
In inverting an image, a pixel’s red, blue and green components are modified to be the exact opposite within the 0 to 255 range. That is, if a pixel’s red, blue, and green values are (34, 198, 240) then that same pixel in the inverted image would have the red, blue, and green values of (221, 57, 15). Note that 255 – 34 is 221, 255 – 198 is 57, and 255 – 240 is 15.
For example, these images show the original and inverse images of Robert.
Suppose a pixel has RGB values of (100, 30, 250).
Which one of the following shows the correct RGB values for the inverted pixel?
- The RGB values would be (200, 230, 50).
- The RGB values would be (126, 126, 126).
- The RGB values would be (100, 30, 250).
- The RGB values would be (225, 155, 5).
- The RGB values would be (155, 225, 5).
Consider writing a program to create new images that are photographic negatives (or inverted images) of selected images.
Suppose we have a Pixel named pxInvert and a Pixel named pxOriginal.
What is the line of code to change pxInvert’s red color to the inverted red color of pxOriginal?
Hints: Start with pxInvert.setRed
Remember the semi colon at the end
Answer:
pxInvert.setRed(255-pxOriginal.getRed());
Q 6. Consider writing a program to create new images that are photographic negatives (or inverted images) of selected images.
Suppose we have an ImageResource variable named picture whose current value is for an image file named dragon.png. See the following code segment below.
ImageResource invertImage = makeInverted(picture);
String fname = picture.getFileName();
invertImage.setFileName(“inv-” + fname);
invertImage.draw();
invertImage.save();
What is the name of the resulting file?
Answer:
inv-dragon.png
Get all Course Quiz Answers of Object Oriented Programming in Java Specialization
Course 01: Java Programming: Solving Problems with Software Quiz Answers
Course 02: Java Programming: Arrays, Lists, and Structured Data Quiz Answers
Course 03: Object Oriented Programming in Java Quiz Answers
Course 04: Data Structures and Performance Quiz Answers
Week 3- Which Country Exports? – Question number 3 answer is Panama.
Thank You for Correction, We’ve made changes