F 1. unsigned int is a value type in Java for representing unsigned integer numeric values.
Unsigned ints are a thing in a lot of languages. It means that the number can’t store negative numbers (so it can store a larger range of positive numbers). This is not something that is supported in Java.
F 2. String is a built-in value type in Java.
Strings are built in, but because they are Objects/reference type, they are not value type.
T 3. An instance of a class is always reference type in Java.
When we make an object from class, the variable points to the location in memory where the information is stored. If something is not a value type, it has to be a reference type.
T 4. A class name usually starts with a capital letter.
A class name doesn’t have to be a capital letter, but it should be.
F 5. switch is a valid variable name.
switch is a keyword in java, so it can’t be used.
T 6. An expression can always be evaluated to produce a value.
This is the definition of an expression.
F 7. An object reference is not a valid expression.
An object reference is a valid expression because it evaluates to something that a variables could be assigned to.
public static void main(String[] args) {
int[] arr = new int[]{1, 2, 3}; //make new array from object reference
int[] a = genArray(1, 2, 3); // same thing as assigning result of method call/expression
}
public static int[] genArray(int a, int b, int c) {
return new int[]{a, b, c};
}
F 8. A variable can be assigned the result of any expression.
counter example
int x = "this is a string" + 12;
F 9. An if statement is a type of loop.
If statements control flow.
F 10. A continue statement within the body of a loop exits the loop.
It doesn’t exit the loop it just tells it to go onto the next iteration of it.
for (int i = 0; i < 3; i++) {
if (i == 1){
continue;
}
System.out.println(i);
}
output:
0
2
T 11. A local variable declared within a loop is not available outside of the body of the loop.
If this doesn’t make sense look up “java variables scope”
T 12. Several case statements within a switch statement may be associated with the same block of statements to be executed.
If you don’t have the break;
statement at the end of the case block, it will continue executing down the
chain.
F 13. A method must return a value that can be used in an expression.
Void methods don’t return anything.
T 14. To invoke (i.e., call) a method, you must provide a value for each parameter declared in the method’s signature.
If you don’t include everything that the method definition requires you will get an error.
F 15. Invoking a class method (i.e., a static method) requires a reference to a particular instance (i.e., object) of the class.
Static methods do not require instances. Think about the Math
class in java. We don’t have to make a new
object to access the methods we just say Math.method()
F 16. When a method (call it method A) is called from within another method (call it method B), the code within method A can access local variables within method B at the point where the call to method A was made.
Scope again. When method A calls method B, method B goes off and does its thing and comes back to method A. The method A has no idea what went on inside method B.
T 17. The length of an array in Java cannot be changed after the array is created.
True for arrays in java. If you want to change the size of an array you have to make a whole new array and copy over ALL the values.
F 18. A specific array can hold values of any type.
When we declare an array we give it the type it must hold
int[] a = new int[10];
T 19. The == operator should not be used to compare whether two String objects are the same on a character-by-character basis.
You should be using the .equals()
method to compare Strings. == will check to see if they share the same
location in memory.
F 20. The individual characters of a String object can be changed.
Strings are immutable, if we want to change a character we have to re-build a whole new string.
T 21. The principle of encapsulation encourages object fields to be marked as private.
definition of encapsulation i.e. Data hiding
F 22. Every method of a class should be included in at least one of the interfaces that the class implements.
If you have private methods they would not be including in an interface. Also if you had methods that are specific to a class there is no need for it to be in an interface.
F 23. Overloading a method is an example of the principle of encapsulation.
encapsulation = data hiding
overloading = polymorphism
T 24. Setters should never be provided for an immutable object.
Setters would make an object mutable (subject to change)
F 25. To adhere to the principle of encapsulation, a public method should not make use of (i.e., invoke or call) a private method declared in the same class.
Encapsulation just has to deal with keeping fields private and providing public getters/setters. The methods themselves can be public and call on private methods.
T 26. It is possible to create an instance of a class that does not explicitly define a constructor.
All classes have a ‘hidden’ constructor (that is blank). So if you dont have a constructor it can still be made an instance
public static class Foo {
int noConstructorHere = 3;
}
//executing code
Foo foo = new Foo();
System.out.println(foo.noConstructorHere); // prints 3
T 27. Defining multiple constructors that accept different types and number of parameters is an example of polymorphism.
Just a definition of polymorphism. If a class can take many forms it is ‘poly’
T 28. A constructor that chains to another constructor must do so as the first statement of the constructor body.
Just a fact that you have to know.
F 29. Two different versions of the same method can be declared to accept the same number and type of parameters as long as the parameters have been given different names.
Paremeter names have no meaning when it comes to programming languages. When the code is compiled to bytecode/assembly all this information is forgotten anyways (variables names are only there to make it easy for people to code).
T 30. All of the methods declared in an interface must be implemented as public methods.
Just a fact about methods. The interface is a contract that the class must fufill. If someone uses an object that implements an interface, they expect the methods to be there.
F 31. An interface may only extend (i.e., inherit from) a single parent interface.
Interfaces can be in a comma separated list.
F 32. Every class must implement at least one interface.
Classes dont implement interfaces all the time.
T 33. An interface may declare several different versions (i.e., overload) the same method name.
Just like in a class, overloading can be done everywhere.
F 34. An interface can only be implemented by one class.
The point of an interface that many classes implement it. The question tries to confuse by getting you to think that it is saying: a class can only extend one other class (this is true btw).
T 35. The name of an interface is a valid type name that can be used as the type of a variable.
Pixel p = new ColorPixel(.3, .3, .2);
Pixel is an interface here. So this works fine.
T 36. A class that implements an extended interface must implement all of the methods declared in the parent interface of the extended interface.
When an interface extends another, they are merged. So the class must do everything.
T 37. A subclass that extends (i.e., inherits from) a parent class automatically implements any interfaces that the parent class implements.
This is true because of inheritance. Example would be from our assignments. PictureImpl
extends AnyPicture
which implements Picture
. So PictureImpl
F 38. An instance field marked as private can be directly accessed by code outside of that class.
The word private should be your big clue here. If something is private it is not accessable outside the scope of the class.
T 39. An instance of a subclass can always be used wherever a reference to an object with the parent class type is required.
Since the subclass has EVERYTHING that the parent class has but more, the subclass would do just fine in place of the parent.
F 40. A subclass can extend from more than one parent class.
A rule of java, classes may not extend more than one parent. Reasons for why this isn’t allowed should have been discussed in class, can also be googled.