Java Programming - Classes and Objects
What are the four numbers outputted? public class QuizClass { public static int num1; public int num2; ... public static void main(String [] args) { QuizClass a = new QuizClass(); QuizClass b = new QuizClass(); a.num1 = 1; a.num2 = 2; b.num1 = 3; b.num2 = 4; System.out.println(a.num1 + " " + a.num2 + " " + b.num1 + " " + b.num2); } }
3, 2, 3, 4 since num1 is static and will be the same for all instances
How many objects of type A will be created? pubic class A{ public A friend; public A(){ friend = null; } public A (A other){ friend = newA(other.friend); } public void setFriend(A newFriend){ friend = newFriend; } A firstVar = new A(); firstVar.setFriend(new A()); A secondVar = new A(firstVar); }
4
What will this output? Person[] people = new Person[3]; people[0] = new Person("Bob"); people[1] = new Person("Alice"); people[2] = new Person("Jane"); // make a copy Person[] peopleCopy = new Person[3]; for (int i = 0; i < 3; i++){ peopleCopy[i] = people[i]; } // now I can change some names people[2].setName("Jane Doe"); people[0] = new Person("Michelle"); System.out.println(peopleCopy[0] + ":" + peopleCopy[1] + ":" + peopleCopy[2]);
Bob:Alice:Jane Doe
an object is
an instance of the class
Instance members must be associated with ___
an object
blueprint from which individual objects are created
class
static member variables and static member methods are sometimes called ___ variables and methods
class
class variables and methods belong to the ____ itself rather than to ____ of that class
class instances
class is visible to all classes everywhere
class declared with the modifier "public"
class only visible within its own package
class has no modifier (package-private)
To return a string with an object, you would...
create toString method
What does this code do? String [] names = new String[20];
creates an array of 20 strings
declaring a variable (does/does not) create an object
does not
immutable means
does not change
If an equals method has not been implemented, what will this code output? (assume class is called Car and constructor for make of the car has been implemented) Car firstCar = new Car("Dodge"); Car secondCar = new Car("Dodge"); System.out.println(firstCar.equals(secondCar));
false because the default equals method checks the memory location
example of? static final double PI = 3.141592653589793;
final modifier
the value of this field cannot change
final modifier
accessor methods are referred to as?
getter methods
JUnit testing should be applied to
individual methods
example of? class MountainBike extends Bicycle{ //new fields and methods defining the subclass MountainBike }
inheritance
What does the second line of code do? PairOfDice dice; //declares a variable of type PairOfDice dice = new PairOfDice();
initializes the object's instance variables and returns a reference to the object
An object created from a class is called an ______ of that class
instance
PlayerData class creates an object. that object is an ____ of the PlayerData class. name and age are ____ _____ in the object.
instance instance variables
methods that the object contains
instance methods
variables that the object contains
instance variables
public ____ getArea() { return width * height; }
int
What are the parameters of the constructor? public class PairOfDice { public int die1; public int die2; public PairOfDice(int val1, int val2){ die1 = val1; die2 = val2; } public void roll(){ die1 = (int)(Math.random()*6) + 1; die2 = (int)(Math.random()*6) + 1; } }
int val1, int val2
subroutine
method
instance variable declared private is not visible from...
methods within other classes
setter method is also referred to as?
mutator method
Objects are created with what operator?
new
Can a constructor be declared static?
no
can a constructor return void?
no
Correct? public void myMethod(){ int x; if (x>10) { System.out.println("larger than 10"); } }
no because x was never initialized
Does the code below create a new object? public Date (Date oldDate) { this(oldDate.getDay(), oldDate.getMonth(); oldDate.getYear(); } assume Date method is defined earlier in code
no, because it is a copy constructor that sets the values of the current object being created
Will this return a String of the date? Date taxDay = new Date(15, "April", 2016); System.out.println("Tay day is " + taxDay);
no, it will print the location in memory where the object taxDay is stored
Correct? public class QuizClass{ int num; String text; public static void printNice() { System.out.println(text + " " + num); } }
no, text and num are not intialized
what is an actual value stored in the variable?
null
a namespace that organizes a set of related classes and interfaces
package
when a method takes in something
parameter
To make data not seen outside the class, you declare it as ___
private
T/F arrays cannot contain primitive types
F
T/F default constructor is generated by the compiler even when another constructor defined in the class
F
T/F if a class is immutable, it needs setters
F
T/F static members can be associated with any specific instance of the class
F
T/F private variables can be seen in methods from other classes
F
T/F static variables can only be used in static methods
F
T/F boxing is the automatic conversion of a private variable to a wrapper class
F, it is the automatic conversion of a primitive type to the corresponding wrapper class
T/F a constructor can assign a value to a constant (variable defined final) with all variables
F, only for class variables. not variables defined within the constructor or within already created objects
If a class is immutable, is it necessary to perform a deep copy in the copy constructor?
No
Does this code print out the line? Date taxDay = new Date915, "April", 2016); Date alsoTaxDay = new Date(15, "April", 2016); if (taxDay.equals(alsoTaxDay)){ System.out.println("They are the same"); }
No, because the default equals method does not check the value, it checks if the two live at the same memory location
Correct? ArrayList<char> lalaString = new ArrayList<char>();
No, primitive types cannot be stored in array list. Should be character instead
Correct? public class Point { public int x; public int y; public Point(){ x = y = 0; } public static int getX(){ return x; } public static int getY(){ return y; }
No, static methods cannot access class members
A class that is not explicitly a subclass is automatically a subclass of what class?
Object
What class is not a subclass of any other class?
Object
public class PairOfDice { public int die1; public int die2; public _______(int val1, int val2){ die1 = val1; die2 = val2; }
PairOfDice
T/F ArrayLists can contain other ArrayLists
T
T/F arrays can contain ArrayLists
T
T/F arrays can contain other arrays
T
T/F encapsulation restricts access to data within a class
T
T/F if there is no constructor defined, the compiler generates a default constructor
T
T/F it is okay to have two constructors with the same name but different parameters
T
T/F the default equals method checks memory location
T
T/F you can make a class immutable by setting the variables to final
T
T/F Encapsulation controls access to variables (ie private vs public) using getters and setters
T
T/F Every class is a subclass of another class
T
T/F a constructor will control how the instance variables are initialized
T
How is the class variable different from the instance variable below? public class Bicycle { private int cadence; private int gear; private int speed; //add inst var for the obj ID private int ID: //add class var for # Bicycle obs instantiated private static int numberOfBicycles = 0; ... }
The class variable is not related to any individual object, but to the class as a whole. It is related to all Bicycle objects. the ID number instance variable is related to one specific individual object.
How would you encapsulate data?
Through getter and setter methods
Correct? public class QuizClass { public int x; public static String text; public int square (int x) { return x * x; } public String getName() { return text; } }
Yes
Can the parameter-list for a constructor be empty?
Yes if the constructor has no parameters
Correct? public class Point{ public final int x; public final int y; public Point(int newx, int newy){ x = newx; y = newy; } }
Yes, a constructor can assign value to a final/constant
Correct? public boolean equals(Date otherDate){ if (day == otherDate.getDay() && month.equals(otherDate.getMonth()) && year == otherDate.getYear()){ return true; } else{ return false; } }
Yes, use .equals to compare Strings and use == to compare integers
What is PairOfDice()? PairOfDice dice; //declares a variable of type PairOfDice dice = new PairOfDice();
a constructor
a variable holds the object, or a reference to an object?
a reference to an object
a void method does not need to contain?
a return statement
After executed, what does "dice" refer to? PairOfDice dice; //declares a variable of type PairOfDice dice = new PairOfDice();
the newly created object
'this' keyword refers to what?
the object being created
In a JUnit test, what does the code below expect? assertFalse(myMethod(5, 15));
the output of the method to be false and triggers an error otherwise
what must be the name of the constructor?
the same name of the class in which it is defined
you can define constants with?
the static modifier and the final modifier
a reference to the current object
this
What modifiers can be used on a constructor definition?
three access modifiers: public, private, and protected
The statement below is an example of int x = new Integer(5);
unboxing
make variables common to all objects
use the static modifier
method declared ____ does not return a value
void
Are strings objects?
yes
Can a constructor have a list of formal parameters?
yes
correct? if (std == null)
yes
change using 'this' public class Point{ public int x = 0; public int y = 0; //constructor public Point(int a, int b){ x = a; y = b; } }
public class Point{ public int x = 0; public int y = 0; //constructor public Point(int x, int y){ this.x = x; this.y = y; } }
a constructor has no __ type
return
when asking the user for something
scanner
the java class library is a?
set of packages
public void setTitle(String newTitle){ title = newTitle; }
setter method
two characteristics of objects
state and behavior