Java Programming: Arrays, Lists, and Structured Data Quiz Answers 2024

Java Programming: Arrays, Lists, and Structured Data Week 1 Quiz Answers

Quiz: Cryptography Quiz Answers

Q1. Encrypt the following phrase with the Caesar Cipher algorithm, using key 15.

Can you imagine life WITHOUT the internet AND computers in your pocket?

What is the encrypted string?

Rpc ndj xbpvxct axut LXIWDJI iwt xcitgcti PCS rdbejitgh xc ndjg edrzti?
correct Answers

Q2. Encrypt the following phrase with the algorithm described for using two Caesar Cipher keys, with key1 = 21 and key2 = 8.

Can you imagine life WITHOUT the internet AND computers in your pocket?

What is the encrypted string?

Q3. Consider the Caesar Cipher two-key algorithm described in this course. Every other character, starting with the first, will use the Caesar Cipher algorithm with key1, and every other character, starting with the second, will use the Caesar Cipher algorithm with key2.

Assume shiftedAlphabet1 is the shifted alphabet using key1 and shiftedAlphabet2 is the shifted alphabet using key2, both are of type String.

If i is the location of the current character in the message, and idx is the integer variable of the location of the current character in the original alphabet, which one of the following segments of code correctly gets the corresponding encrypted character?

if (i % 2 != 0)  {                           
newChar = shiftedAlphabet1[idx];            
}            
else {                        
newChar = shiftedAlphabet2[idx];            
 if (i % 2 == 0)  {                      
newChar = shiftedAlphabet1[idx];            
}            
else {                        
newChar = shiftedAlphabet2[idx];           
 }
 
 if (i % 2 == 0)  {                           
newChar = shiftedAlphabet1.charAt(idx);            
}            
else {                        
newChar = shiftedAlphabet2.charAt(idx);           
 } 
  • correct Aswesr

Q4. Consider the file errors.txt.

What is the most common word length (ignoring the punctuation of the first and last character of each group of characters)?

Q5. Consider the file manywords.txt.

What is the most common word length (ignoring the punctuation of the first and last character of each group of characters)?

  • 7

Q6. The following phrase was encrypted with the two-key encryption method we discussed using the two keys 14 and 24. What is the decrypted message?

Hfs cpwewloj loks cd Hoto kyg Cyy.

Q7. The following phrase was encrypted with the two-key encryption method described in this course. You will need to figure out which keys were used to encrypt it.

Aal uttx hm aal Qtct Fhljha pl Wbdl. Pvxvxlx!

What is the original message?

Q8. Decrypt the encrypted file mysteryTwoKeysQuiz.txt.

This file is encrypted with the two-key encryption method we discussed. You’ll need to decrypt the complete file by figuring out which keys were used to decrypt it.

What are the first five decrypted words?

Q9. Decrypt the encrypted file mysteryTwoKeysQuiz.txt.

This file is encrypted with the two-key encryption method we discussed. You’ll need to decrypt the complete file by figuring out which keys were used to decrypt it.

What are the two keys used to encrypt it?

Q10. Which of the following is the best choice for adding additional private fields to the CaesarCipherTwo class created in the last lesson to make it easier to call decrypt on a string that was encrypted using an object of this class?

  • Create 26 shifted alphabets, one for each possible key.
  • As key1 and key2 of type int, which are parameters to the constructor.
  • Create 26 possible key variables.
  • Create an array of shifted alphabets, one shifted alphabet for each key.

Q11. Should the halfOfString method in the TestCaesarCipherTwo class be public or private?1 point

  • public
  • private
Q12. Consider the following two classes Simple and TestSimple for the remaining questions.
 public class Simple{      
private String word;      
private String phrase;      
public Simple(int number, String w) {             
word = w;                
phrase = mystery(number, w);               
}        
private String mystery(int num, String s) {                 
String answer = "";                f
or (int k=0; k<num; k++) {                       
answer = answer + s;               
}              
return answer;      
}       
public String toString() {              
return phrase + " is " + word + " repeated";     
}
}
 public class TestSimple{     
public void print() {              
Simple item = new Simple(3, "blue");                   
System.out.println(item);                  
}      
} 

Why is there no return type for the method Simple?

  • It should not have a return type.
  • The user forgot to put the return type, it should be int.
  • The user forgot to put the return type, it should be void.
  • The user forgot to put the return type, it should be String.
Q13.public class Simple{      
private String word;      
private String phrase;      
public Simple(int number, String w) {             
word = w;                
phrase = mystery(number, w);              
 }        
private String mystery(int num, String s) {                 
String answer = "";                
for (int k=0; k<num; k++) {                       
answer = answer + s;               
}              
return answer;      
}       
public String toString() {              
return phrase + " is " + word + " repeated";     
}
}
 public class TestSimple{     
public void print() {              
Simple item = new Simple(3, "blue");                   
System.out.println(item);                  
}      
} 

What is printed when the print method in TestSimple is called?

  • 3, blue
  • blue, 3
  • blue
  • blueblueblue is blue repeated
  • blueblueblue
Q14. public class Simple{     
private String word;      
private String phrase;      
public Simple(int number, String w) {             
word = w;                
phrase = mystery(number, w);               
}        
private String mystery(int num, String s) {                 
String answer = "";                
for (int k=0; k<num; k++) {                       
answer = answer + s;              
 }              
return answer;      
}       
public String toString() {              
return phrase + " is " + word + " repeated";    
 }
}
 public class TestSimple{     
public void print() {              
Simple item = new Simple(3, "blue");                   
System.out.println(item);                 
 }      
} 

Suppose the following line is added as the last line in the print method of the class TestSimple. 1System.out.println(item.mystery(5, "ho"));

How does this line affect what happens with the code in the print method?1 point

  • An error occurs when the program is run, because mystery is private and cannot be called by the TestSimple class.
  • When the program runs, an additional line is printed that reads “hohohohoho is ho repeated”.
  • When the program runs, an additional line is printed that reads “5, ho”.
  • A compile error occurs when compiling this class, because mystery is private and cannot be called by the TestSimple class.
  • When the program runs, an additional line is printed that reads “ho”.
  • When the program runs, an additional line is printed that reads “hohohohoho”.

Java Programming: Arrays, Lists, and Structured Data Week 2 Quiz Answers

Quiz: GladLibs

Q1. Consider the first version of GladLibs we saw in this lesson, which stores label substitutions in ArrayLists. Assume an ArrayList named wordsUsed will keep track of words used as replacements so no replacement word will be used more than once. The code below was used as part of a program by a learner in the method processWord. The learner’s program runs but still results in duplicate words sometimes.

String sub = getSubstitute(w.substring(first+1,last));
while (true) {    
if (wordsUsed.contains(sub)) {        
sub = getSubstitute(w.substring(first+1,last));        
break;    
}    
else {        
wordsUsed.add(sub);    
}
}

     

Which one of the following best explains why this code still returns duplicates sometimes?

  • The “if condition” is always false the first time in the loop, so the else part is executed the first time through the loop. This means the while loop always executes its body at least twice so some words may be used a second time.
  • The “if condition” is always false in the loop, so the else part is always executed. This always results in a second random word.
  • If a word is a repeated word, then this code gets another random word and uses that second word without checking to see if it is a repeated word.
  • Repeated words are also put into wordsUsed, so the call to getSubstitute may choose a repeated word.

Q2. Consider the first version of GladLibs we saw in this lesson, which you modified so there would not be duplicate words chosen for the story. Assume an instance variable is used to keep track of the total number of word tags that are replaced.

Which one of the following methods is most likely where that variable is updated?

  • The GladLibs constructor.
  • myStory
  • processWord
  • getSubstitute

Q3. Consider the class WordFrequencies, which you wrote in an assignment, that can determine facts about words in a file.

How many unique words are in the file errors.txt?

(You should lowercase all words and include the punctuation as part of a word. Thus, “end.” is different than “end”, but “All” is the same as “all”.)

  • 3721

Q4. Consider the class WordFrequencies, which you wrote in an assignment, that can determine facts about words in a file.

Which word occurs the most often in the file errors.txt?

(You should lowercase all words and include the punctuation as part of a word. Thus, “end.” is different than “end”, but “All” is the same as “all”.)

  • of

Q5. Consider the class WordFrequencies, which you wrote in an assignment, that can determine facts about words in a file.

Find the word that occurs the most often in the file errors.txt.

(You should lowercase all words and include the punctuation as part of a word. Thus, “end.” is different than “end”, but “All” is the same as “all”.)

How many times does the most common word occur?

  • 609

Q6. Consider the class CharactersInPlay, which you wrote in an assignment, that determines who the characters were in one of Shakespeare’s plays and also how many lines they had.

Of the characters who have fewer than 100 lines in the file errors.txt, which of these characters has the most speaking parts?

  • ADRIANA

Q7. Consider the class CharactersInPlay, which you wrote in an assignment, that determines who the characters were in one of Shakespeare’s plays and also how many lines they had.

Find the name of the character with the third most speaking parts in the file errors.txt. How many speaking parts does this person have?

  • 79

Q8. Consider the class CharactersInPlay, which you wrote in an assignment, that determines who the characters were in one of Shakespeare’s plays and also how many lines they had.

How many characters in the file errors.txt have at least 10 speaking parts, but no more than 15 speaking parts?

  • 3

Q9. Consider the class you wrote to find out how many times each codon occurs in a strand of DNA based on reading frames. The file dnaMystery2 represents a long strand of DNA.

How many unique codons are there if you use a reading frame that starts at position 1?

  • 32

Q10. Consider the class you wrote to find out how many times each codon occurs in a strand of DNA based on reading frames. The file dnaMystery2 represents a long strand of DNA.

What is the number of occurrences of the codon that occurs the most often using a reading frame that starts at position 2?

  • 12

Q11. Consider the class you wrote to find out how many times each codon occurs in a strand of DNA based on reading frames. The file dnaMystery2 represents a long strand of DNA.

Using a reading frame that starts at position 0, which of the following codons occur 7 times? (Select all that are correct.)

  • CAG
  • ATG
  • GAT
  • GCC
  • TGT
  • CAA

Q12. Consider the class WordsInFiles, which you wrote in an assignment, that determines which words occur in several files, and for each word, which files they occur in.

Consider the seven files: caesar.txt, confucius.txt, errors.txt, hamlet.txt, likeit.txt, macbeth.txt, and romeo.txt.

How many words are there that each occur in all seven files?

  • 570

Q13. Consider the class WordsInFiles, which you wrote in an assignment, that determines which words occur in several files, and for each word, which files they occur in.

Consider the seven files: caesar.txt, confucius.txt, errors.txt, hamlet.txt, likeit.txt, macbeth.txt and romeo.txt.

How many words are there that each occur in four of the seven files?

  • 826

Q14. Consider the class WordsInFiles, which you wrote in an assignment, that determines which words occur in several files, and for each word, which files they occur in.

Consider the seven files: caesar.txt, confucius.txt, errors.txt, hamlet.txt, likeit.txt, macbeth.txt and romeo.txt.

In which file does the word “laid” NOT appear?1 point

  • caesar.txt
  • confucius.txt
  • errors.txt
  • hamlet.txt
  • likeit.txt
  • macbeth.txt
  • romeo.txt

Q15. Consider the class WordsInFiles, which you wrote in an assignment, that determines which words occur in several files, and for each word, which files they occur in.

Consider the seven files: caesar.txt, confucius.txt, errors.txt, hamlet.txt, likeit.txt, macbeth.txt and romeo.txt.

In which of the following files does the word “tree” appear? (Choose all that apply.)

(Consider only the exact lowercase string “tree”. “TREE” or “tree.” would be different words.)1 point

  • caesar.txt
  • confucius.txt
  • errors.txt
  • hamlet.txt
  • likeit.txt
  • macbeth.txt
  • romeo.txt

Q16. Consider the map version of GladLibs where a map is created that maps a category to a list of words in that category.

In which method are the individual ArrayLists of words for categories created?

  • initializeFromSource
  • readIt
  • They are created in the constructor.
  • They are not created in a method but are automatically created as part of the definition of the private HashMap variable of <String> to <ArrayList<String>>.
  • makeStory

Q17. Consider the map version of GladLibs where a map is created that maps a category to a list of words in that category. In which method are these individual ArrayLists of words placed into the HashMap?

  • initializeFromSource
  • Not in a method, but rather, they are placed in automatically as part of the definition of the private HashMap variable of <String> to <ArrayList<String>>.
  • in the constructor
  • readIt
  • makeStory

Q18. Consider the map version of GladLibs and consider the method totalWordsInMap that returns the total number of words in all the ArrayLists in the HashMap myMap.

Which two of the following code possibilities compute this sum of total number of words in the variable sum?

int sum = 0;
for (ArrayList<String> wordlist : myMap.keySet()) {    
for (String word : wordlist) {        
sum += 1;    
}
}
 int sum = 0;        
for (String category : myMap.keySet()) {    
sum += myMap.get(category);
} 
 int sum = 0;        
for (String category : myMap.keySet()) {    
ArrayList<String> words = myMap.get(category);    
sum += words.size();
} 
  • correct Answer
 int sum = 0;
for (String category : myMap.keySet()) {   
 ArrayList<String> words = myMap.get(category);    
sum += words;
} 
 int sum = 0;
for (ArrayList<String> wordlist : myMap.keySet()) {   
 sum += wordlist.size();
} 
 int sum = 0;
for (String category : myMap.keySet()) {   
 sum += myMap.get(category).size();
} 

Q19. Consider the map version of GladLibs and consider the method totalWordsConsidered that returns the total number of words in the ArrayLists of the categories that were used for a particular GladLib. Assume a private variable of type ArrayList<String> and named categoriesUsed is used to store the unique categories found as the GladLib is created.

In which method would we put a category into this ArrayList?

  • makeStory
  • processWord
  • getSubstitute
  • fromTemplate
  • readIt
  • totalWordsConsidered

Java Programming: Arrays, Lists, and Structured Data Week 3 Quiz Answers

Quiz: Web Server Logs

Q1. Suppose a web log is modified to now have a sixth piece of information, a priority, that can be represented as a String.

Which one of the following is the least likely change to the LogEntry class to accommodate this new part of a web log?

  • A new String field is initialized in the constructor.
  • A new getPriority method is created to return the priority.
  • The toString method is modified to include the new priority as part of the return String.
  • A new String parameter is added to the constructor.
  • The toString method is modified to include a String parameter.
  • A new private variable named priority is created.

Q2. Consider the following code for the readFile method of the LogAnalyzer class.6}

In the Tester class, readFile is called with a correct filename, and then printAll is called, but nothing is printed.

Which one of the following is likely the best reason why? 1 point

  • In readFile, a System.out.println statement is missing that should be right after the for loop.
  • In readFile, the log entries were not stored in records.
  • In readFile, a System.out.println statement is missing from the body of the for loop.
  • In readFile, the wrong parameter is sent to parseEntry.
  • In readFile, a System.out.println statement is missing that should be right before the for loop.

Q3. Consider the following code for the method printAllHigherThanNum with one integer parameter num. This method should print all the logs that have a status code higher than num.

Which one of the following would be the best choice for suitable code for this method?

if (le.getStatusCode() > num) {    
for (LogEntry le : records) {        
System.out.println(le);    
}
}
 if (le.getStatusCode() > num) {    
for (LogEntry le : records) {        
System.out.println(le);    
}
}
else {    
System.out.println();
}
 for (LogEntry le : records) {    
if (le.getStatusCode() > num) {        
System.out.println(le);    
}    
else {        
System.out.println();    
}
}  
  • correct Answers
 if (le.getStatusCode() > num) {    
for (LogEntry le : records) {        
if (le.getStatusCode() > num) {            
System.out.println(le);        
}    
}
}         

Q4. Run the method countUniqueIPs on the file weblog2_log.

How many unique IP addresses are in the file?

  • 45

Q5. Run the method uniqueIPVisitsOnDay(“Sep 27”) on the file weblog2_log.

What size is the ArrayList that is returned?

  • 23

Q6. Run the method countUniqueIPsInRange(200,299) on the file weblog2_log.

What number is returned?

  • 13

Q7. Run the method mostNumberVisitsByIP after a HashMap has been created from the method countVisitsPerIP on the file weblog2_log.

What number is returned?

Q9. Run the method iPsMostVisits after a HashMap has been created from the method countVisitsPerIP on the file weblog2_log.

What single IP address is returned in the ArrayList?

  • 103.57.41.178
  • 188.162.84.63
  • 200.69.213.251
  • 210.4.104.99
  • 212.128.74.248

Q9. Run the method dayWithMostIPVisits with a HashMap has been created from the method iPsForDays on the file weblog2_log.

What day is returned?

  • Sep 24
  • Sep 26
  • Sep 28
  • Sep 30

Q10. Run the method iPsWithMostVisitsOnDay with two parameters—one, a HashMap that has been created from the method iPsForDays on the file weblog2_log and two, the day “Sep 29”.

One IP address is returned in the ArrayList—what is it?

  • 103.57.41.178
  • 188.162.84.63
  • 210.4.104.99
  • 212.128.74.248
  • 212.185.210.111

Java Programming: Arrays, Lists, and Structured Data Week 4 Quiz Answers

Quiz: reaking the Vigenère Cipher

Q1. What is the language of secretmessage3.txt?1 point

  • Danish
  • Dutch
  • English
  • French
  • German
  • Italian
  • Portuguese
  • Spanish

Q2. What is the first line of the message you decrypted from secretmessage3.txt?

  • La chambre à coucher de Juliette.

Q3. What is the language of secretmessage4.txt?

  • Danish
  • Dutch
  • English
  • French
  • German
  • Italian
  • Portuguese
  • Spanish

Q4. What is the first line of the message you decrypted from secretmessage4.txt?

Q5. Recall that the choice to test key lengths 1–100 was arbitrary, and that the longer a key is with respect to an encrypted message, the less likely this Vigenère decryption algorithm is to work.

How long would a key have to be, so that you could gain no information about an original message by analyzing the encrypted message?

  • 1/26 the message length
  • Half the message length
  • Exactly the message length
  • There is no key length that ensures an unbreakable Vigenère implementation.

Q6. The Vigenère decryption algorithm used in this lesson has limitations.

In which of the following scenarios could the program return incorrect results? (Select all that are correct.)

  • The language of the original message has multiple letters of comparable high frequency.
  • The language of the original message when written in sentences has different letter frequencies than the sum of individual words in the dictionary.
  • The language of the original message has many characters not in the English alphabet.
  • The language of the original message is not included in the dictionaries.
  • The language of the original message has an unusual letter that is highest in frequency (not ‘e’ or ‘a’).

Q7. Consider the Vigenère decryption algorithm described in this lesson.

Which one of the following could be a problem?

  • The key length is long compared with the message.
  • The message is long compared with the key length.
  • Neither—the key length compared with the message is unimportant.
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

Team Networking Funda
Team Networking Funda

We are Team Networking Funda, a group of passionate authors and networking enthusiasts committed to sharing our expertise and experiences in networking and team building. With backgrounds in Data Science, Information Technology, Health, and Business Marketing, we bring diverse perspectives and insights to help you navigate the challenges and opportunities of professional networking and teamwork.

Leave a Reply

Your email address will not be published. Required fields are marked *