Variables, Objects, and Classes Flashcards
The goal of object technology is to allow you to write software that better models the natural world. (T/F)
True
A unit within a computer program that combines data and operations
Object
Variables internal to an object that hold data defining the object’s state
Instance variable
An operation that an object can perform
Method
A plan that tells the computer how to build objects
Class
Specifies the variables that are to be included in objects of the class
Field declaration
Specifies the methods that are to be included in objects of the class
Method declaration
A special method that, when called, initializes the object being built
Constructor
A constructor that is automatically provided by the compiler
Implicit constructor
A constructor that is written out within the text of the class
Explicit constructor
Specified by a static field declaration, a variable shared by all objects of a class
Class variable
Specified by a static method declaraion, a method shared by all objects of a class
Class method
Indicates that a field or method belongs to the entire class and not to individual objects of the class
Static
Object-oriented programmers refer to an object as __________ of a class.
An instance
You can always identify an explicit constructor within a class declaration because it has the same identifier as the class. (T/F)
True
Given this Die class, which is its field?
public class Die { public int faceUp; public void roll( ) { faceUp = (int) (Math.random( ) * 6 + 1); }}
faceUp
Given this Die class, which is its method?
public class Die { public int faceUp; public void roll( ) { faceUp = (int) (Math.random( ) * 6 + 1); }}
roll( )
Given this Die class, does it have an implicit or explicit constructor?
public class Die { public int faceUp; public void roll( ) { faceUp = (int) (Math.random( ) * 6 + 1); }}
Implicit
Given this Die class, does it have an implicit or explicit constructor?
public class Die { public int faceUp; public Die ( ) { roll( ); } public void roll( ) { faceUp = (int) (Math.random( ) * 6 + 1); }}
Explicit
If a class has no constructor explicitly written out, the Java compiler automatically gives it an implicit constructor. (T/F)
True
A __________ has as its declared data type one of the eight data types (e.g. int, double, char, etc.) that are built into the Java language.
Primitive variable
A __________ has as its declared data type a Java class.
Reference variable
A __________ is a direct reference to its datum.
Primitive variable
A __________ is an indirect reference to its data.
Reference variable
__________ stores its datum immediately within the memory location.
A direct reference
__________ stores an address that the computer must travel to in order to find the data.
An indirect reference
__________ variable holds a whole number.
An integer
__________ variable holds a number with a fractional part.
A floating-point
What code fragment declares s to be a reference variable?
String s;
What code fragment declares s to be a primitive variable?
double s;
Java objects can only be accessed through an associated reference variable. (T/F)
True
A reference variable that doesn’t refer to any object is called a null pointer. (T/F)
True
Given this Die class, what Java statement correctly builds a Die object?
public class Die { public int faceUp; public Die ( ) { roll( ); } public void roll( ) { faceUp = (int) (Math.random( ) * 6 + 1); }}
Die d = new Die ( );
Which part of the code below declares the object’s reference variable?
Scanner in = new Scanner( System.in );
Scanner in
Which part of the code below calls the class constructor to initialize the object?
Scanner in = new Scanner( System.in );
Scanner( System.in );
Which part of the code below builds the object?
Scanner in = new Scanner( System.in );
new Scanner
Which part of the code below copys the address where the object is located in memory to the reference variable?
Scanner in = new Scanner( System.in );
in = new
Given this HourlyWorker class, which code fragment correctly references Sam’s hoursWorked?
public class HourlyWorker { public static double hourlyWage; public double hoursWorked; public static void setWage( double w ) { hourlyWage = w; } public void setHours( double h) { hoursWorked = h; } public double grossPay( ) { return hoursWorked * hourlyWage; }}
HourlyWorker sam = new HourlyWorker( ); sam.hoursWorked = 40.0;
Given this HourlyWorker class, which code fragment correctly references Sam’s hourlyWage?
public class HourlyWorker { public static double hourlyWage; public double hoursWorked; public static void setWage( double w ) { hourlyWage = w; } public void setHours( double h) { hoursWorked = h; } public double grossPay( ) { return hoursWorked * hourlyWage; }}
HourlyWorker sam = new HourlyWorker( ); sam.hourlyWage = 12.25; -or- HourlyWorker sam = new HourlyWorker( ); HourlyWorker.hourlyWage = 12.25;
Although the second option is the best
Given this HourlyWorker class, which code fragment correctly calls setWage?
public class HourlyWorker { public static double hourlyWage; public double hoursWorked; public static void setWage( double w ) { hourlyWage = w; } public void setHours( double h) { hoursWorked = h; } public double grossPay( ) { return hoursWorked * hourlyWage; }}
HourlyWorker sam = new HourlyWorker( ); sam.setWage( 12.25 ); -or- HourlyWorker sam = new HourlyWorker( ); HourlyWorker.setWage( 12.25 );
Although the second option is best
Given this HourlyWorker class, which code fragment correctly calls setHours?
public class HourlyWorker { public static double hourlyWage; public double hoursWorked; public static void setWage( double w ) { hourlyWage = w; } public void setHours( double h) { hoursWorked = h; } public double grossPay( ) { return hoursWorked * hourlyWage; }}
HourlyWorker sam = new HourlyWorker( ); sam.setHours( 40.0 );
Given this HourlyWorker class, which code fragment correctly calls grossPay?
public class HourlyWorker { public static double hourlyWage; public double hoursWorked; public static void setWage( double w ) { hourlyWage = w; } public void setHours( double h) { hoursWorked = h; } public double grossPay( ) { return hoursWorked * hourlyWage; }}
HourlyWorker sam = new HourlyWorker( ); . . . double pay = sam.grossPay( 40.0 );
The easiest way to compile and run a Java program using more than one class is to have all necessary class files contained in the same disk folder. (T/F)
True
An external reference is the occurrence in some file A of an identifier x that is declared in another file B. (T/F)
True
To resolve an external reference, the Java compiler automatically searches for its declaration, a process called separate compilation. (T/F)
True
Each Java class is compiled into a separate file that has __________ as its extension.
.class