IT 313 - Quizzes Winter 2020 * = correct answer ----------------------------------------------------- Quiz 1 -- Closed Jan 19. 1. Which token indicates the beginning of a Java comment line? a. # b. * c. % *d. // 2. Which is a legal header for the Java startup method? *a. public static void main(String[ ] args) { b. public static void startUp(String[ ] args) { c. public void static main(String[ ] args) { d. public void static startUp(String args) { 3. What is the Java name for Standard In (keyboard)? a. Input b. Scanner c. Std.in *d. System.in 4. Which statement converts the string s defined as String s = "467.987"; to the double value d? *a. double d = Double.parseDouble(s); b. double d = s.parseDouble( ); c. double d = s.toDouble; d. double d = s.toDouble( ); 5. What is the output from these statements? System.out.println("a"); System.out.print("b"); System.out.println("c"); a. abc b. a b c *c. a d. a bc b c ----------------------------------------------------- Quiz 2 -- Closed Jan 26. 1. Which statement is needed to use a Scanner object? a. import java.util; b. import java.lang; *c. import java.util.Scanner; d. import.java.lang.Scanner; 2. Which for loop prints the odd numbers from 1 to 99? a. for(int n = 1; n <= 99; i++) { System.out.println(n); } *b. for(int n = 1; n <= 99; i += 2) { System.out.println(n); } c. for(int n = 1; n <= 99; i *= 2) { System.out.println(n); } d. for(n <= 99; int n = 1; i -= 2) { System.out.println(n); } 3. What is the output? System.out.println('A' + 13 + "C" + 'D'); Hint: The ASCII code for 'A' is 65 a. 6513C68 *b. 78CD c. A13C68 d. A13CD 4. In a method definition, which annotation is used to indicate that the method overrides the method with the same name in the base class. *a. @Override b. @Overrides c. @Replace d. @Replaces 5. Which method returns the last character of the input string? *a. public static char lastChar(String inputString) { return inputString.charAt(inputString.length( ) - 1); } b. public static char lastChar(String inputString) { return inputString.charAt(inputString.length( )); } c. public static char lastChar(String inputString) { return inputString.last( ); } d. public static String lastChar(String inputString) { return last(inputString); } ----------------------------------------------------- Quiz 3 -- Closed Feb 2. 1. Which statement converts the int value n to a String value? For example, if the value of n is 294 the statement should convert it to "294". a. String s = Integer.parseInt(n); b. String s = n.toString( ); *c. String s = String.valueOf(n); d. String s = toString(n) 2. Which statement declares and instantiates a Scanner object that reads from the file "input.txt"? a. Scanner s = new File("input.txt"); *b. Scanner s = new Scanner(new File("input.txt")); c. Scanner s = new Scanner("input.txt"); d. Scanner s = Scanner.instanceOf("input.txt"); 3. Which of these choices is the built in reference variable that refers to the current object? a. current b. me c. self *d. this 4. Which statement declares and instantiates an array named arr that contains five double precision point zeros (0.0)? a. double[5] arr; *b. double[ ] arr = new double[5]; c. double[5] arr = {0.0}; d. double[ ] arr = {0.0} * 5; 5. Given the source code files A.java and Main.java, what is the output when the Main class runs? // Source code file: A.java public class A { private String s; public A(String aString) { s = "$" + aString + "$"; } public void augment( ) { s += "%"; } @Override public String toString( ) { return 25 + s; } } // Source code file: Main.java public class Main { public static void main(String[] args) { A a = new A("dog"); a.augment( ); a.augment( ); System.out.println(a); } } a. %dog%$25 b. 25%dog%$$ c. 25$dog$% *d. 25$dog$%% ----------------------------------------------------- Quiz 4 -- Closed Feb 9. 1. What is the output of these statements? String s = "mississippi"; System.out.println(s.lastIndexOf("ssi"); Look up the lastIndexOf method in the Java class library if necessary. a. 2 b. 4 *c. 5 d. -1 2. What happens if you don't close a PrintWriter object after you are finished using it? a. You can't open any other PrintWriter objects. *b. The last page of the output buffer is not written to the output file. c. Your program will not compile. d. The PrintWriter object goes into an infinite loop. 3. If the URL string s is defined as s = "http://facweb.cdm.depaul.edu/sjost/it212/index.htm"; which statement creates a scanner which reads from the webpage with the URL string defined by s? a. Scanner in1 = new Scanner(s); b. Scanner in1 = (new Scanner(s)).openStream( ); c. Scanner in1 = new Scanner(new URL(s)); *d. Scanner in1 = new Scanner(new URL(s).openStream( )); 4. Which of the following is a properly written getter for the instance variable animalType in the Pet class? a. private String getAnimalType { return animalType; } b. private String getAnimalType( ) { return animalType; } *c. public String getAnimalType( ) { return animalType; } d. public String getAnimalType( ) { System.out.println(animalType); } 5. If p is defined by Pet p = new Pet("Molly", "dog"); Which method call converts the Pet object p into a String object? a. p.str( ) *b. p.toString( ) c. str(p) d. String.valueOf(p) ----------------------------------------------------- Quiz 5 -- Closed Feb 16. 1. If the ArrayList collection col is defined as ArrayList col = new ArrayList(100); which statement inserts the string "apple" into the collection at the index 3? *a. col.add(3, "apple"); b. col.add("apple", 3); c. col.insert(3, "apple"); d. col.insert("apple", 3); 2. Which statement defines a Java derived class B that inherits from the base class A? a. public class B : A { b. public class B(A) { *c. public class B extends A { d. public class B inherits A { 3. Which Java keyword invokes a method in the base class? a. base b. extends c. self *d. super 4. In IntelliJ, a JAR file is an example of a(n) _____________________ ? *a. artifact b. asset c. resource d. widget 5. Two methods in the same class that have the same name, but different signatures are _____________________ methods. a. helper b. overloaded *c. overridden d. static ----------------------------------------------------- Quiz 6 -- Closed Mar 1. 1. What is a Java interface? It is a __________ ? a. base class b. derived class c. set of instance variables *d. specification of required methods, including method names, signatures, and return values 2. What is the required method of the Comparable interface? a. comparator b. compare *c. compareTo d. spaceship 3. What is the first line of a class A that implements the interface I? a. public class A extends I { *b. public class A implements I { c. public class A interface I { d. public interface I for class A { 4. Which choice defined a HashMap with integer keys and string values? a. HashMap hm = new HashMap( ) b. HashMap hm = new HashMap( ) c. HashMap hm = new HashMap( ) *d. HashMap hm = new HashMap( ) 5. What is the load factor for a HashMap collection? It is the maximum ___________ of the HashMap collection. a. capacity b. number of empty cells b. percentage of empty cells *d. percentage of occupied cells ----------------------------------------------------- Quiz 7 -- Closed Feb 8. 1. Which keyword or symbol is used to show that a class implements a Java interface? a. extends *b. implements c. : d. << 2. What is a required method of the Iterator interface? a. getIterator *b. hasNext c. iterator *d. next 3. A software design pattern is a *a. high level reusable software design b. geometric symmetry c. recurring substring d. repeating decimal number 4. What is the important feature of the Factory software pattern? a. Factory objects cannot be destroyed. b. Many identical objects are created by the Factory class. c. Several overloaded constructors are available for the users of the Factory class. *d. Users of the Factory class do not obtain new objects with a constructor, but with a static getInstance method. 5. Which software pattern is probably the most controversial? a. Factory b. Iterator c. Observer *d. Singleton -------------------------------------------------------