Basics, Arrays Questions With 100% Correct Answers
Identity - ANSWER differentiates one instance from another (like an id number or
location in memory)
Class (OOP Definition #2) - ANSWER The code you write
Defines an object's "state" (as variables)
Defines an object's "behavior" (as methods, i.e. functions)
"template" or "mold" for creating new objects
Tells Java "how" to create an object
Instance (OOP Definition #2) - ANSWER Instance =
A single actual object "created" based on the template (the class) - the data+code that
is actually stored in memory
Maintains own "state"
Exhibits own "behavior"
Maintains own "identity"
Two instances, like two cookies made from the same cookiecutter, are still different
Referenced with the new keyword
Getter - ANSWER A getter returns, or "gets" an instance to the main code. For
example, in the rectangle project the user could set the value to whatever they wanted,
and the getter method just returned that to the runner. Known as an accessor
Setter - ANSWER Allows a user to set an instance, for example making the height of
rect1 200. Known as a mutator
Overloaded Constructor - ANSWER You can create an alternate Constructor that
automatically sets your instance variables to specific starting values. Instead of making
a setter for the values, you can put variables in your constructor that the user enters
after the new keyword.
String [] arr = new String [10];
how many strings are made? - ANSWER 0, all 10 strings created were set to null,
therefore they are not real strings
, Can an Object or a Primitive Type be in an array? - ANSWER Both
How do you declare a new array? - ANSWER type [ ] name = new type [number] or
type name [ ] = new type [number] or
int [ ] George = {1,2,3,4,}
What is the name of a value at specific point in the array? - ANSWER Index
nums [2] = 10;
nums is the name, the index of this is 2, the value is 10
Enhanced For Loop - ANSWER Alternative for-loop to retrieve each item in an array
one at a time - much more like for-loops in Python.
Can you reference or declare an array in an enhanced for loop? i.e.
(int arrVal : arr) {
System.out.println(arr[arrVal]);
} - ANSWER No, you can not. If you just call for printing arr, the for loop will go
through all the values of the variable and print them.
Overloaded Constructor - ANSWER A constructor that has parameters, set to the
instance variables
Default Constructor - ANSWER A constructor with no parameters that sets the instance
variables to default values. i.e.
Given an array of ints, return true if every element is a 1 or a 4.
only14([1, 4, 1, 4]) → trueonly14([1, 4, 2, 4]) → falseonly14([1, 1]) → true - ANSWER
public boolean only14(int[] nums) { for (int i = 0; i < nums.length; i++) { if (nums [i] !=
1 && nums [i] != 4) { return false;
}
}
return true;
}
Return the number of even ints in the given array. Note: the % "mod" operator
computes the remainder, e.g. 5 % 2 is 1.
countEvens([2, 1, 2, 3, 4]) → 3countEvens([2, 2, 0]) → 3countEvens([1, 3, 5]) → 0 -