CS520 Final

¡Supera tus tareas y exámenes ahora con Quizwiz!

b. Instantiating Employee with new Employee("Alice") is incorrect. c. The computeSalary() method is incomplete and would cause a compiler error.

Assuming that the following classes are in their own files and exist within the same package, what is wrong with this code? Select all statements that are true. a. The companyName variable is accessed without first instantiating Employee. b. Instantiating Employee with new Employee("Alice") is incorrect. c. The computeSalary() method is incomplete and would cause a compiler error. d. Because the computeSalary() method is marked private, it cannot be used by another method that is called from outside the class.

b. polymorphism

Assuming that these classes are all in the same package, which object-oriented principle is demonstrated here? The output of PublicationTest is : Monthly Daily a. encapsulation b. polymorphism c. overloading d. casting

b. A, B and D C is false because Cat can have many constructors regardless of how many its superclasses have.

Consider the following classes: If a class called Cat is created that extends Feline, which of the following statements would be true? A. The walk() method could be called on an instance of Cat. B. Cat must declare a void method called eat(). C. Cat cannot include multiple constructors because its superclasses use the implicit constructor only. D. An instance of Cat could be created with a base type of Animal, Feline or Cat. a. A, B and C b. A, B and D c. A, C and D d. B, C and D e. A, B, C and D

d. None of these choices

Consider the following line of code: String str = "Hello"; Which of the following method invocations modifies the contents of str? a. str.substring(2); b. str.replace('H', 'M'); c. str.indexOf("H"); d. None of these choices

c. 11

Consider the following segment of code: What is the minimum value that can be assigned to idx on line 3 so that sum would not be changed inside the loop? a. 9 b. 10 c. 11 d. None of these choices, because a while loop cannot be totally skipped.

c. Calling instruments[4] would result in an exception.

Consider the following statement: String[] instruments = new String[] { "piano", "violin", "guitar", "drum" }; Which of the following is correct? a. The element at instruments[2] is "violin". b. Calling instruments.get(0) would return "piano". c. Calling instruments[4] would result in an exception. d. Calling instruments.length() would return 4.

b. B and C

Consider the following statement: String[][] myArray = new String[5][3]; Which of the following are false? A. When visualized, the elements of the array form a rectangle. B. Because 5 and 3 are unequal, the array would be considered irregular or "jagged". C. myArray[1][1] would reference the first element of the first inner array. D. myArray contains a total of 15 elements. a. A and B b. B and C c. C and D d. A, B, C and D are all true

c. When the double value is cast to an int, it loses the decimal portion. The code casts y, which is 0.4, to an int, but as int is a whole-number type, the decimal portion is truncated.

Given the following code: When run, the code will print 0. Why? a. The code has an error, and Java always sets int variables back to zero when errors occur. b. The value of z is never actually defined. c. When the double value is cast to an int, it loses the decimal portion. d. x and y are considered equal because 3.5 and 3.1 are within the same value range.

d. None of the given options

Given the following code: Which of the following, inserted at line 5, will prevent line 7 (y++;) from executing? a. y = x; b. y = 10; c. y = 11; d. None of the given options

b. salary = 100 * age; Assigning age as salary / 100, salary - 10000, or salary * age assigns a double value to the integer variable age, which is not permitted by the type casting rules.

Given the following declarations: int age = 30; double salary = 25000; which of the following is a valid assignment statement? a. age = salary / 100; b. salary = 100 * age; c. age = salary - 10000; d. age = salary * age;

a. instance

In the following class: Which term best describes the scope of the computeSalary() method? a. instance b. protected c. global d. static

a. It creates an endless loop. The loop adds 1 to i each time it runs, as dictated by the for statement (i++), but within the loop, --i decreases the value of i each time, so i will never be greater than 0, causing an endless loop.

What is the potential problem with this code? a. It creates an endless loop. b. Because i is treated as an int and then as a boolean, it needs to have a type cast. c. The syntax of the for loop statement is incorrect. d. The main() method accepts arguments but does not use them.

d. None of the above.

What is the problem with this (legal) code? a. The order of elements in the list is not guaranteed to always be the same. b. myList should be declared as ArrayList rather than List for the names to be displayed in the correct order. c. HashSet is a better choice than ArrayList for elements that are referenced with get(). d. None of the above.

d. None of the above.

What is the problem with this code? String[] myArray = new String[10]; int length = myArray.length; a. The length property is being read on a null value. b. The variable name length is invalid because it is a reserved Java keyword. c. length is being called as a property instead of as the method length(). d. None of the above.

b. B only

What problem(s) will result when this code, which compiles successfully, is executed? A. The code within the if block will never execute. B. The message in result will show the wrong number of days in September because a and b are concatenated as strings rather than added mathematically. C. The substring() method could cause an exception. a. A only b. B only c. C only d. A and B e. A and C f. B and C g. A, B and C h. None of A, B or C

b. 12

When this code is run, which line will set y to 1? a. 9 b. 12 c. 15 d. The code will not compile because the switch statement is incorrect.

b. false private hides variables/methods from all other classes, even subclasses.

When used on an instance variable or method, the access modifier private signifies that the variable or method may only be accessed by an instance of the class and by instances of its subclasses. a. true b. false

a. super

Which keyword can be used in a class to reference a method or variable in its superclass? a. super b. override c. this d. static

c. B and C The == operator should never be used to compare strings.

Which of the following are reliable approaches to comparing the contents of two strings, string1 and string2 ? A. string1 == string2 B. string1.equals(string2) C. string1.compareTo(string2) a. A and B b. A and C c. B and C d. A, B and C

c. ListIterator can loop bidirectionally.

Which of the following best describes an advantage of ListIterator over Iterator? a. ListIterator can move through a collection without needing a polymorphic reference token. b. Iterator is unsuited for looping through collections of type List. c. ListIterator can loop bidirectionally. d. Unlike ListIterator, Iterator can only loop through a collection one item at a time.

d. The length of an "inner" array cannot be declared before the length of its "outer" array is declared.

Which of the following best describes why this code statement is illegal? int[][] myArray = new int[][5]; a. The "inner" array type is inconsistent with the type of the "outer" array. b. The length of both the "inner" and "outer" arrays must be declared when a multidimensional array is declared. c. The length of the array needs to be declared on both sides of the = sign. d. The length of an "inner" array cannot be declared before the length of its "outer" array is declared.

c. Static variables in a class may be accessed without having to first create an instance of the class.

Which of the following describes a primary difference between static and instance variables? a. Static variables cannot be changed but instance variables can be changed. b. Static variables are unique to each class instance, while instance variables are shared by all instances. c. Static variables in a class may be accessed without having to first create an instance of the class. d. Static variables are only used for primitive types like int, but instance variables may hold any type.

a. Arrays are intended to contain primitives while collections are intended to contain proper Java objects. Java must always know the length of an array when it is created, but the size of a collection does not have to be explicitly declared.

Which of the following is not a difference between arrays and collections? a. Arrays are intended to contain primitives while collections are intended to contain proper Java objects. b. Arrays are of fixed length but collections may grow and shrink. c. Java automatically allocates a value (or null) at all positions within an array, but collections may be empty

d. Deciding between while and do-while depends on whether the loop should always run at least once. do-while is always guaranteed to run at least once through the loop because the boolean test is at the end of the loop block.

Which of the following is true about while and do-while loops? a, Both will continue looping until the while condition becomes true, which causes the loop to end. b. Using do-while is more efficient than while because it defers the boolean test until the end. c. while is only appropriate for short loops, but do-while has more capacity for larger loops. d. Deciding between while and do-while depends on whether the loop should always run at least once.

c. public, protected, default, private public and private are the most and least restrictive. default is more restrictive than protected because protected includes subclasses in a different package while default does not.

Which of the following lists Java's access modifiers in descending order of accessibility (most accessible to least accessible)? a. public, private, default, protected b. public, default, private, protected c. public, protected, default, private d. public, default, protected, private

d. A, B and C

Which of the following methods could be used to loop through the values in a list called myList of type ArrayList<String> ? A. A "for" loop beginning with for (int i = 0; i < myList.size(); i++) { B. A "for-each" loop beginning with for (String nextString : myList) { C. The Iterator that can be obtained from myList.iterator(); a. A and B b. B and C c. C and D d. A, B and C

c. Every Java class must contain a main() method. Every application must contain a class with a main() method, but typically only one class has a main() method within the application.

Which of the following statements about Java classes is false? a. Classes may contain both variables and methods. b. If a class is named MyClass, the file that contains it must be called MyClass.java. c. Every Java class must contain a main() method. d. Java requires that every programming module is a class.

d. A, B & C

Which of the following statements about Java input and output are true? A. The Scanner class allows a program to prompt a user for input. B. When System.out.println("hello") is called, "hello" is considered an argument sent to the println method. C. User input may need to be converted to a different variable type in order to work with it. a. A & B b. A & C c. B & C d. A, B & C

b. An interface can provide default functionality that can be overridden by an implementing class. Interfaces do not contain any functionality, just rules regarding the methods that implementing classes must contain.

Which of the following statements about Java interfaces is false? a. Multiple interfaces may be implemented by a class. b. An interface can provide default functionality that can be overridden by an implementing class. c. All of the methods named in an interface must be present in implementing classes. d. Interfaces do not have constructors.

b. A and C

Which of the following statements about Map are true? A. Some types of Map have an unpredictable sort order. B. Map provides an implementation that automatically sorts its members by their values. C. It is possible to have a Map with keys and values of the same variable type. a. A and B b. A and C c. B and C d. None of the above

b. The values in a Map must all be unique.

Which of the following statements about Map is false? a. Each key is associated with one, and only one, value. b. The values in a Map must all be unique. c. It is possible to loop through the values in a Map without using its keys. d. To add an element to a Map, both the key and its associated value must be provided.

b. A and C

Which of the following statements about Strings are true? A. The substring() method may be invoked with argument(s) to return a selected portion of the string. B. String variables can be allocated using either single or double quote marks. C. If not otherwise defined, the default value of a String variable is null. a. A and B b. A and C c. B and C d. A, B and C

d. It is impossible for a method with a return type of int[] to return null.

Which of the following statements about arrays is false? a. Arrays are passed to methods by reference. b. If the members of the array are provided at the same time at which it is created, the length does not have to be indicated in the square brackets. c. The only way to mix objects of type String and Employee objects in the same array would be to declare the array as type Object[]. d. It is impossible for a method with a return type of int[] to return null.

b. Arrays are empty until they are populated with values.

Which of the following statements about arrays is false? a. Arrays can be created of any Java object type. b. Arrays are empty until they are populated with values. c. Java must know the size of an array when it is created. d. Arrays can be passed to methods.

d. Classes without explicitly defined constructors are considered static.

Which of the following statements about constructors is false? a. If the programmer does not write any constructors for a class, Java will assume an implicit no-argument constructor. b. A class may have many constructors, but the list of arguments must be different for each one. c. super() may be used to call a constructor of the superclass. d. Classes without explicitly defined constructors are considered static.

a. A and B A: Since toString() is an inherited method from java.lang.Object, all classes will have toString(). B: toString() may be overridden in any class.

Which of the following statements about toString() are true? A. toString() may always be invoked on any instances of any Java class. B. Classes may declare their own custom versions of toString(). C. Invoking the toString() method will change the instance of a class. a. A and B b. A and C c. B and C d. A, B and C

a. A and B

Which of the following statements are true about Set? A. A Set cannot contain duplicate entries. B. The sort order of entries within a HashSet is not guaranteed. C. TreeSet orders entries by the order in which they were added. a. A and B b. B and C c. A and C d. A, B and C

a. A

Which of the following statements are true about arrays? A. Their size is fixed and they cannot shrink or grow. B. Employee e = new Employee[4]; will allocate four empty Employee instances in the array. C. An int[] array is considered a primitive type. a. A b. B c. C d. A and B e. A and C f. B and C g. A, B and C h. None

b. A and C

Which of the following statements are true about exceptions? A. Exceptions and errors are technically two different things in Java. B. When properly used, exception handling can help catch syntax errors in your code. C. NullPointerException occurs when trying to invoke a method on a null variable. a. A and B b. A and C c. B and C d. None of these choices

a. One of its advantages is that it handles string data as a proper object rather than as a primitive type.

Which of the following statements is false about StringBuffer? a. One of its advantages is that it handles string data as a proper object rather than as a primitive type. b. It has methods to search for a string within the StringBuffer's contents. c. One may choose to employ StringBuffer instead of String for performance reasons. d. It has both destructive and non-destructive methods.

c. The static parser methods in the String class can convert strings to other variable types.

Which of the following statements is false? a. If a String is not explicitly assigned a value, it will be null. b. Strings are immutable. c. The static parser methods in the String class can convert strings to other variable types. d. Combining strings with + is less computationally efficient than doing so using StringBuffer's append() method.

c. Because the boolean primitive type can hold false or true values, it can also be assigned a value of 0 or 1. The boolean type can only contain true or false and can never hold numeric values.

Which of the following statements is false? a. The types double and float are the only primitives that can contain decimal values. b. The integer types byte, short, int and long are primarily differentiated by the range of numbers that they can hold. c. Because the boolean primitive type can hold false or true values, it can also be assigned a value of 0 or 1. d. It would be possible to create three distinct variables named myValue, myvalue and MYVALUE.

b. Source code is written in .java files and the compiler automatically creates corresponding .class files. The compiler makes .java files into compiled .class files.

Which of the following statements is true about Java? a. The JVM is responsible for converting source code into bytecode. b. Source code is written in .java files and the compiler automatically creates corresponding .class files. c. Code compilation is only required if the program's classpath is empty. d. The more classes in a Java program, the slower it will run because of lengthy compilation routines.

d. A static variable is shared by all the instances of the class

Which of the following statements is true about the static modifier? a. A static variable cannot change its value. b. A static method is often written in a non-Java language and exists outside of JVM. c. A static method can access instance variables d. A static variable is shared by all the instances of the class

d. overloading Test() is constructor, and you can ONLY invoke constructor using new keyword to create a new instance.

Which principle is demonstrated here? a. overriding b. polymorphism c. encapsulation d. overloading

b. concatenation

Which principle is demonstrated here? a. lexicographical difference determination b. concatenation c. destructive method invocation d. indexing

d. None of the above At first, this may look like a method override, but because goToWork() is private in the superclass, the version in the subclass is not an override but a completely separate method that just happens to have the same name.

Which principle is demonstrated in the following classes? a. overloading b. overriding c. encapsulation d. None of the above

b. Though the strings look different to us, Java considers them equal because of their characters.

Which statement about the following code is false? String string1 = "Java"; String string2 = "JAVA"; String string3 = "jAvA"; a. You could test whether all three strings have the same value by evaluating this expression: string1.equals(string2) && string2.equals(string3); b. Though the strings look different to us, Java considers them equal because of their characters. c. Comparing any two of them to one another using equalsIgnoreCase() will return true. d. Calling trim() on all three would have no discernible effect.

d. It identifies a variable or resource as belonging to the current instance of the class.

Which statement correctly describes the function of the this keyword? a. It ensures encapsulation by limiting access to private variables. b. It enables method inheritance. c. It provides simple access to static variables and methods. d. It identifies a variable or resource as belonging to the current instance of the class.

d. All of the above

Which type of Set will disallow duplicate entries? a. HashSet b. LinkedHashSet c. TreeSet d. All of the above

d. 6 Loop #1: balance = 100, 100 >= 50 so continue Loop #2: balance = 90, 90 >= 50 so continue Loop #3: balance = 80, 80 >= 50 so continue Loop #4: balance = 70, 70 >= 50 so continue Loop #5: balance = 60, 60 >= 50 so continue Loop #6: balance = 50, 50 >= 50 so continue Loop #6: balance is less than 50

Within the following code: How many times is "continue" executed? a. 0 b. 1 c. 5 d. 6

b. The do-while loop

For which of the following loop statements will the loop be executed at least once? a. The while loop b. The do-while loop c. The for loop d. None of the choices

c. 11 ++b will evaluate first to 21. The value of a is then subtracted from 21, resulting in 11. (Note that a++ is post-increment operator, so it is not incremented for the expression. However, ++b is a pre-increment operator and it is incremented for the expression.)

Given the following code sample: int a = 10; int b = 20; int c = ++b - a++; What will be the value of c ? a. 10 b. 12 c. 11 d. 9

d. There is no possible user input that could cause this outcome. Based on the logic of the first two if-else tests, there is no way the final else block could be reached.

Given the following code: What user input would cause the program to print "I'm not sure if you can vote"? a. 18 b. -1 c. Eighteen d. There is no possible user input that could cause this outcome.


Conjuntos de estudio relacionados

NYELVÉSZET ÁLLAMVIZSGA POSSIBLE KÉRDÉSEK

View Set

Scuba Diving - Open Water Diver Manual: Exercises

View Set

HIstory and Geography 800 Unit 6 Quiz 1

View Set

Skill 5-6: Administering an Intradermal Injection

View Set

Ch 43: Assessment and Management of Patients with Hepatic Disorders

View Set