2: Quizzes Flashcards
Which of the following is NOT a Java relational operator?
=
>=
!=
>
=
Which of the following is true?
- When using == to compare reference variables, you are testing for “identity equality” – that the two variables refer to the same object.
- The .equals() method tests for “content equality” – that the objects are considered equivalent to each other.
1 and 2 are both true.
Which of the following operators may NOT be used with operands of type boolean?
&&
||
!=
!
All of these may be used with type boolean.
What is a common use for a variable of type boolean?
Flag variable
What is a common name for a method that returns type boolean?
Predicate method
What are the valid data types for a switch statement.
int, char, String
If a break statement is not included at the end of each action (alternative) in a switch instruction, what is the result?
More than one action may be executed.
You are writing a program that will allow a user to enter a single word identifying her ‘sign’ in the Chinese zodiac (e.g. “Dog”) and then show some information about that sign. Obviously a decision must be made in this program. Which selection structure(s) could you use that will compile?
Either if statements or a switch statement could be used.
Which of the following is true in Java?
- Every else must have a preceding if.
- Every if must have a following else.
1 is true.
What kind of loop is a data validation loop?
An indefinite loop.
Which loop control structure can NOT be used if there are conditions under which the body of the loop should be executed zero times?
do…while
You know that a for loop is usually the best choice for a counting loop, but which other loop control structure(s) could possibly be used to create a counting loop?
Any of the loop control structures can be used to create a counting loop.
How are the following implemented?
- The state of an object.
- The behavior of an object.
- Through instance variables.
2. Through instance methods.
Another term for instance variable is…?
Field.
What is the keyword for the implicit parameter?
this
A method that changes the state of an object is known as….
a mutator.
A method that provides information about the state of an object is known as…
an accessor.
A piece of code that initializes the state of an object is known as…
a constructor.
What is the keyword used to encapsulate the fields of an object?
private
An object’s properties are also known as what?
Instance variables
What is another name for an object?
An instance of a class.
Which of the following statements is FALSE?
- A private method may be called from anywhere within the class in which it is declared.
- A public attribute may be examined from anywhere in the program, but may not be changed.
- The keyword void indicates that a method does not return any value.
2 is false.
Which of the following things does this statement do?
Color darkBrown = new Color( 120, 160, 0 );
- Declare a new variable.
- Construct a new object.
- Create a binding.
- All of the above.
- None of the above.
- All of the above.
An object diagram represents what?
The state of memory at a particular point during the execution of a program.
When drawing an object diagram, you evaluate a series of program statements to figure out how they change the contents of memory. Which of the following is a question you should ask as you evaluate each line of code?
- Does the line of code declare a variable?
- Does the line of code instantiate an object?
- Does the line of code create a binding?
- All of the above.
- None of the above.
- All of the above.
Which of the following valid statements declares a variable? /* 1 */ Oval dot = new Oval(); /* 2 */ dot.setColor( new Color( 0, 0, 255 ) ); /* 3 */ dot.setFilled(); /* 4 */ Oval circle; /* 5 */ circle = dot;
1 & 4.
Which of the following valid statements creates (constructs) an object? [Read very carefully!] /* 1 */ Oval dot = new Oval(); /* 2 */ dot.setColor( new Color( 0, 0, 255 ) ); /* 3 */ dot.setFilled(); /* 4 */ Oval circle; /* 5 */ circle = dot;
1 & 2
Which of the following valid statements creates a binding (assigns a value to a variable)? /* 1 */ Oval dot = new Oval(); /* 2 */ dot.setColor( new Color( 0, 0, 255 ) ); /* 3 */ dot.setFilled(); /* 4 */ Oval circle; /* 5 */ circle = dot;
1 & 5.
Which of the following valid statements creates an alias? /* 1 */ Oval dot = new Oval(); /* 2 */ dot.setColor( new Color( 0, 0, 255 ) ); /* 3 */ dot.setFilled(); /* 4 */ Oval circle; /* 5 */ circle = dot;
5
What is an orphaned object?
An object which has no references to it.
What are the different views of a class?
- Source code
- UML Class Diagram
- Javadoc comments
1, 2 and 3.
What does the keyword ‘this’ refer to when used in a statement in code?
The particular object executing the statement.
Name two access modifiers.
public and private.
What is the proper JavaDoc tag for parameter descriptions?
@param
Javadoc comments are for client programmers. True/False
True
What is the name of the package needed to use File objects.
java.io
What is the throws clause that we include in our method signature when the method instantiates a Scanner to read from a File?
throws FileNotFoundException
The proper syntax to create a Scanner object that can read from the text file words.txt is
Scanner sc = new Scanner(“file.txt”);
False
How many steps are required to successfully swap the values in two variables?
3
Given the following declaration:
double[][] y = {{ 1, 2, 3, -1}, {4, 5, 6, 0}, {7, 8, 9, 10}};
What is the proper syntax to access the -1?
y[0][3]
In order to visit every element in a 2-dimensional array, what control structure design do you need?
A loop nested within another loop
Which sort algorithm begins with the algorithm below?
start at index 1
copy array value into temporary storage
compare temporary to index 0 - if element 0 is less than element 1,
shift element 0 to element 1
put temporary value into element 0
Insertion sort
Given a 2-dimensional array of ints, what will the following code do?
int sum = 0; for (int r = 0; r < array.length; r++) { for ( c= 0; c < array[r].length; c++) sum += array[r][c]; } System.out.println(sum);
Prints out the sum of the entire array
Given a 2-dimensional array of ints, what will the following code do?
int sum = 0; for (int r = 0; r < array.length; r++, sum = 0) { for ( c= 0; c < array[r].length; c++) sum += array[r][c]; System.out.println(sum); }
Prints the sum of each row
Which sort algorithm is described below?
set top of unsorted array to 0
search array from top to end to find index of largest entry
swap value at top of array with value at index of largest entry
increment top
repeat until end of loop
Selection sort
You can swap 2 rows of a 2-dimensional array (true/false)
True
Inheritance represents what type of relationship?
Is-a relationship
What is the keyword used to indicate a class inheritance relationship?
extends
A sub class may not access any private members of the super class by name. (true/false)
True
What is the keyword used by a sub class to call a super class constructor?
super
When we implement the method: String toString() {} in our classes, we are ______ the method from the Object class.
overriding