to problem set

Problem set 1 - Multiple Choice

questions pulled from : 401 fall final 2016
My tip for multiple choice in computer science would be to think of examples for an against every statement. If you can find an example that goes against what the question says, it probably isn’t right.

For each multiple choice question below, indicate all correct answers among the choices provided. More than one answer may be correct and at least one answer will be correct. Each question is worth 5 points. Selecting all of the choices or selecting none of the choices will result in no points awarded.


1. Which of these statements about Java code organization and syntax are true?
 a) A block of statements can be used anywhere an expression is needed. 
* b) A statement ends in a semicolon. 
* c) Classes organized into a package are visible to each other without an import statement. 
* d) A Java source file can only contain one public class or interface. 
e) Local variables declared within a loop are accessible outside of the loop body.

a) Definition of expression and statement below. Statements could control flow or declare variables. This would not be enough to replace an expression.

An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value.

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution.

c) we have seen this in our assignments. Things that are inside the a6novice package don’t need an import statement. If we are inside RowMajorPixelIterator we simply can make a new Pixel without having to import a6novice.Pixel

e) This has to do with scope


2. Which of these statements about Java arrays are true?
 a) Arrays are indexed starting with 1. 
* b) Arrays are objects. 
c) An array can hold elements of different types. 
* d) An array can not be resized once created. 
e) An array may not be passed to a method as a parameter.

a) 0
b) Although they can contain primitive types (int), they act like references, where the variable is a pointer to a location in memory where the array is stored.
c) we declare an array with a single type int[] a = new int[10];
e) We absolutely CAN do this


3. Which of these statements about encapsulation are true? 
* a) Using private instance fields is related to the principle of encapsulation. 
b) An object with one or more setters is immutable. 
c) A getter must be associated with a specific instance field. 
* d) An advantage of setters is the ability to validate the new value of a property. 
e) Proper encapsulation requires all methods to be marked public.

b) setters would allow you to change the object, making it mutabe.
c) derived properties are examples of a getter that don’t require a specific field.
e) sometimes you want private methods, encapsulation simply pertains to data being made private, not methods.


4. Which of these statements about polymorphism are true? 
* a) Overloaded methods are an example of polymorphism. 
b) Using the JavaBeans conventions for getters and setters is an example of polymorphism. 
c) Polymorphism requires that at most one class will implement a particular interface. 
d) Using the same local variable name in different methods is an example of polymorphism. 
* e) The "is-a" relationship between a class and the interfaces that the class implements is an example of polymorphism.

a) true because one things have many forms is polymorphism
b) nothing to do with the topic
c) the AT MOST part here is wrong
d) that is just coding in general
e) Is-A relationships are the product of polymorphism, so yes this is true.


5. Which of these statements about subclassing are true? 
a) A subclass can override private methods in its parent class. 
* b) A subclass may only have one parent class. 
c) Two subclasses of the same parent class have an is-a relationship with each other. 
d) A subclass has direct access to private fields in its parent class. 
e) A subclass must redeclare any protected fields declared in its parent class. 

a) subclasses have no access to private parent class methods
b) a fact of java
c) classes are completely separate. Private means specific to an individual class
e) subclass has direct access to fields of a parent that are not marked private.


6. Which of these statements about interfaces and subinterfaces are true? 
* a) An enumeration can be declared within an interface. 
* b) A subinterface may have multiple parent interfaces. 
c) An interface may include one or more private methods. 
* d) An extended interface adds one or more additional methods to those declared by its parent. 
* e) A public interface should be defined in a Java source file with the same name. 

a) we see this a lot actually

public interface Currency {
    enum CurrencyType{
        RUPEE,
        DOLLAR,
        POUND
    }
}

b) perfectly legal public interface Hockey extends Sports, Event
c) since interfaces are contracts that EVERY class must contain, it would not make sense to have private methods declared here (because we cannot call private methods from outside of the scope of the class).


7. Which of these statements about overriding methods are true? 
a) Every method of a parent class should be overridden by a method in a subclass. 
b) An overridden method is not able to call the version of the method as defined in the parent class. 
* c) An overridden method in a subclass has the same method signature as a method in the parent class. 
* d) Overridden methods are virtual by definition in Java. 
* e) A class can simultaneously overload and override the same method. 

b) we do this by using the super keyword
c) true, overloaded methods on the other hand do not.
e) override a method in the subclass. Then overload it by changing method signature.


8. Which of these statements about overloading methods are true? 
a) An overloaded method may not call (i.e., invoke) a different version of the overloaded method. 
* b) An overloaded method is one that is defined more than once within a class with a unique method signature associated with each definition. 
* c) A subclass can overload a method defined in its parent class. 
* d) All versions of an overloaded method must have the same access modifier (e.g., public, protected, private). 
e) All versions of an overloaded method must have the same return type.

note: overloading is when you have multiple methods of the same name but different parameters being passed into it.

An overloaded method may or may not have different return types. But return type alone is not sufficient for the compiler to determine which method is to be executed at run time. … Method Overloading means to have two or more methods with same name in the same class with different arguments.

a) this is perfectly fine, think about constructor chaining
e) read above


9. Which of these statements about the iterator design pattern are true? 
* a) A for-each loop can be used with any collection that implements the java.util.Iterable interface. 
b) An existing iterator must iterate over the entire collection before another iterator can be created for that collection. 
* c) The iterator design pattern provides an abstraction for iterating over a collection without needing to understand how the collection is maintained. 
* d) An iterator can be passed to a method as a parameter. 
e) An iterator must be a subclass of the iterable collection. 

a) This is the way that iterators are used a lot of the time. example
b) New iterators can be made at any time without changing anything about the item being iterated over. This is one of the main reasons for iterators.
e) subclassing would imply that our iterator extends the collection class. This doesn’t really make sense. Think about the iterators in assignment 6, they are completely removed from the AnyPicture class.


10. Which of these statements about the factory design pattern are true? 
a) The factory design pattern uses delegation. 
* b) A class using the factory design pattern prevents direct instantiation of objects. 
c) The factory method in the factory design pattern is usually an instance method. 
* d) The factory design pattern can be used for dynamic subclass binding. 
* e) The factory design pattern can be used to create a singleton.

This is when knowing the definitions of the design patterns and their basic principles is really important.
c) instance methods come from instances of a factory. There is supposed to be only one factory, so there shouldn’t be multiple instances of it.
d) definition
e) definition