Final Review CS 211

Ace your homework & exams now with Quizwiz!

How many times is the string "Foo!" printed? for(int count=21; count>15;count--){ if (count%2 == 0){ System.out.println("Foo!"); } }

3

What is one benefit of using Javadoc style comments in your code along with the javadoc tool? A. Automated documentation generation B. Improved code readability with less boilerplate C. Faster compilation time D. Automated conversion of primitive datatypes to their respective wrapper classes

A.

Which of the following are stream objects that java creates by default? Mark all that apply. A. System.in B. System.out C. System.err D. System.file

A. B. C.

Which pf the following methods are you allowed to declare in a class? Mark all that apply. A. An instance method which does not return any value B. A static method which does not take any parameters C. A method with the same name as another method, but with different parameter types D. A method with the same name as another method, but with a different return value

A. B. C.

Consider the following snippet of code: for(String item : thing) { System.out.println(thing); } Which of the following could be the type of the variable 'thing'? Select any/all that apply. A. ArrayList<String> B. LinkedList<String> C. String[] D. List<String>

A. B. C. D.

What is the correct syntax for declaring a variable which can only reference a generic ArrayList parameterized by any class which inherits from Number? A. ArrayList<? extends Number> foo; B. ArrayList<I extends Number> foo; C. ArrayList<Number> foo; D. ArrayList foo;

A. ArrayList<? extends Number> foo;

What is one advantage of using ArrayLists over LinkedLists? A. ArrayLists are faster than LinkedLists when adding elements because they do not have to copy elements when they dynamically resize. B. LinkedLists have a maximum size. ArrayList do not. C. ArrayLists can directly access elements by index, LinkedLists must traverse from front or back D. ArrayLists are part of the Collections framework, LinkedLists are not.

A. ArrayLists are faster than LinkedLists when adding elements because they do not have to copy elements when they dynamically resize.

What is one advantage of using LinkedLists over ArrayLists? A. LinkedLists are faster than ArrayLists when adding elements because they do not have to copy elements when they dynamically resize. B. ArrayLists have a maximum size, LinkedLists do not. C. LinkedLists can directly access elements by index, ArrayLists must traverse from the front or back. D. LinkedLists are part of the Collections framework, ArrayLists are not.

A. LinkedLists are faster than ArrayLists when adding elements because they do not have to copy elements when they dynamically resize.

What is the difference between mergesort and quicksort? A. Mergesort runs in O(n^2) time while quicksort runs in O(n log n). B. Quicksort is recursive, but mergesort is iterative. C. Quicksort is in-place, but mergesort requires temporary variables to perform the merging step. D. Quicksort runs in O(n^2) time while mergesort runs in O(n log n).

A. Mergesort runs in O(n^2) time while quicksort runs in O(n log n).

If generics use type erasure anyway, why are they considered "safer" than using Object references? A. The compiler can insert instanceof checks automatically. B. The compiler can prevent type casting on return values from generic methods C. The compiler can check that the generic types match before erasure happens. D. The compiler can automatically generate new wrapper classes to prevent unsafe type conversions.

A. The compiler can insert instanceof checks automatically.

From which class do all other classes inherit from? A. The Class class B. The Object class C. The Reference class D. Reference classes inherit from the Class class, but primitive classes do not

B.

What does the following line of code do when placed at the top of the file? package cs211.exercise1; A. Copies the contents of the cs211.exercise1 package into the current class B. Declare that the class which follows is part of the cs211.exercise1 package C. Allows the classes in the cs211.exercise1 package to access private fields and methods in this class directly D. None of the above

B.

What is the difference between method overloading and overriding? A. Overriding involves multiple methods with the same name but different methods signatures. Overloading involves inheritance. B. Overloading involves multiple methods with the same name but different methods signatures. Overriding involves inheritance. C. Overloading and overriding both refer to the same technique. D. None of the above.

B.

Which of the following fields are you allowed to declare in a class? Mark all that apply. A. A field with no type B. A field with no access modifier C. A field with the same name as another field, but with a different type D. A field with the same name as another field, but with a different access modifer

B.

Which of the following statements are not true about enums? Mark all that apply. A. Instances of an enum type may be compared with the "==" operator B. Instances of an enum type are actually primitives, not reference C. An enum is not allowed to dine a constructor D. An enum is not allowed to define any methods or fields

B.

Consider the following classes. What is printed? public class A { public int data; public A(int a) { data = a; } } public class B extends A { public B(int a) { super(a); } public static void main(String[] args) { B b = new B(5); System.out.println(b.data); } } A. 4 B. 5 C. An error message D. None of the above

B. 5

Which of the following is true about anonymous inner classes? Mark any/all that apply. A. Anonymous inner classes can only be created from classes, not interfaces. B. Anonymous inner classes can only access method local variables that are final or effectively final. C. Anonymous inner classes can only be created from interfaces, not classes. D. Anonymous inner classes can only override a single existing method.

B. D.

What kind of mistake can result in a StackOverflowError? A. Forgetting to handle a base case. B. Making a recursive call but with modified arguments. C. Forgetting to fully comment the recursive method. D. Calling the recursive method from a different, non-recursive method.

B. Making a recursive call but with modified arguments.

The throw keyword is similar to the return keyword in many respects. In what way is throw different from return? A. Unlike throw, return can be used to halt the flow of a program. B. Only instances of objects that inherit from the Throwable class can be thrown, whereas any type may be returned. C. While the type of object that a method returns is declared in the return type, there's no way to specify the type of an object that a method throws. D. None of the above. The keywords throw and return are identical.

B. Only instances of objects that inherit from the Throwable class can be thrown, whereas any type may be returned.

What does the following code output? int[][] x = new int[2][]; System.out.println(x[1]); A. 0 B. null C. {0,0} D. none of the above

B. null

What are the different ways that the "this" keyword can be used? A. To call one constructor from within a different constructor B. To reference an instance variable within an instance method C. Both (A) and (B) D. None of the above

C.

Which of the following statements are not true about abstract classes? Mark all that apply. A. Abstract classes cannot be instantiated B. Abstract classes may contain methods with no implementation C. A class may inherit from multiple abstract classes at once. D. An abstract class must define at least one abstract constructor

C. D.

What is printed when the command "java Reverser Hello world!" is run? public class Reverser { public static void main(String[] args){ for(String s : args) { for(int i=s, s.length()-1;i--){ System.out.print(s.charAt(i)); } System.out.print(" "); } } } A. !dlrow olleH B. Hello world! C. olleH !dlrow D. world! Hello

C. The inner loop iterates through each character of the string in reverse order. However, the outer loop iterates through the arguments (strings) in the order they are provided.

Which class defines the version of the method doThing() which is executed? SomeClass foo; // ...other code here. foo.doThing(); A. The version defined in SomeClass. B. The version defined in a subclass of SomeClass. C. The version defined in SomeClass, or a superclass of SomeClass. D. It depends on the runtime type of the object referenced by foo.

D

Which of the following statements are not true about interfaces? Mark all that apply A. Interfaces cannot be instantiated B. Interfaces may contain methods with no implementation C. A class may inherit from multiple interfaces D. An interface must define at least one abstract constructor

D.

Which of the following is not true about recursion? A. If the recursive call is the last statement of the method, it may be possible to re-write as a loop using tail-call optimization. B. Calling a method recursively requires no special syntax in Java. C. Recursive solutions tend to use more memory than iterative solutions. D. A recursive method must have a non-void return value.

D. A recursive method must have a non-void return value.

Certainly, here's the formatted version: Which of the following is not true about JUnit and annotations? A. JUnit uses annotations to indicate which methods are part of a test harness. B. JUnit tests can integrate with IDEs such as Eclipse and Dr. Java, or can be run directly from the command line. C. JUnit allows you to mark a test to be ignored by adding the appropriate annotation. D. Annotations are not part of Java directly but are included with the JUnit annotation.

D. Annotations are not part of java directly but are included with the JUnit annotation.

Which of the following statements is true regarding Lists, LinkedLists, and ArrayLists? A. These three classes are unrelated to each other. B. List is a class, LinkedList is a subclass of List, and ArrayList is a subclass of LinkedList. C. List is a class, ArrayList is a subclass of List, and LinkedList is a subclass of ArrayList. D. Both ArrayList and LinkedList implement the List interface.

D. Both ArrayList and LinkedList implement the List interface.

Which is the correct way to declare a method with a generic type parameter which must be Comparable with itself? A. public <T> void foo(T num) { B. public <? extends Comparable<?>> void foo(? num) { C. public <T extends Comparable<?>> void foo(T num) { D. public <T extends Comparable<T>> void foo(T num) {

D. public <T extends Comparable<T>> void foo(T num) {

A java program executes the last method defined in its class as the main method when run on the command line in a terminal. True or false?

False

A method marked as "private" cannot access fields marked as "public". True or false?

False

All classes written in Java must explicitly call their superclass' constructor. True or false?

False

All classes written in Java must have a default constructor without arguments. True or false?

False

Because of how much faster binary search is compared to linear search, it is always faster to sort data first before searching. True or false? A True B. False

False

The Swing GUI framework completely replaces the AWT framework in modern versions of Java. True or false?

False

Which of the following are true about Java? Mark all the apply A. Java is an interpreted language B. Java is a compiled language C. Java is a grabage collected language D. Java is a strongly-typed language

Java is an interpreted language (JVM) Java is a compiled language ('javac') Java is a garbage collected language Java is a strongly-typed language

What is printed in the following code? public class Foo { public int account; public Foo(int a) { account = a; } public static void main(String[] args){ Foo f = new Foo(); f.account = 10; System.out.println(f.account); } }

The Foo class constructor expects an integer argument, but when you create an instance of Foo in the main method, you're not providing an integer argument: so the line `Foo f = new Foo();` will cause an error

What does the following code print! int[] x; System.out.println(x);

The correct answer depends on your version of java, but Arrays are reference types, so the default value for their variables is `null`, and newer versions of java may generate an error

Constructor are not inherited? True or false?

True

If a class does not expicitly define any constructor, java will create a default. True or false?

True

The Scanner class may be used to read data from both files and the terminal. True or false?

True

Unit testing is only one of several different approaches to testing. True or false? A True B. False

True


Related study sets

Chapter 5 :Defining and Managing Project Scope

View Set

BCS 260: Module 1: zyBooks Introductory Basicsflogff

View Set

HW 4 Bidding and Contract Documents 2

View Set

Life insurance, California laws and rules

View Set

Health Insurance Portability and Accountability Act (HIPAA)

View Set

Econ 2301 MacroEconomics Unit 6-8 Review

View Set

ATI The Gastrointestinal System Test 4.0

View Set

PROPERTY: TITLES - ADVERSE POSSESSION

View Set