2: Quizzes Flashcards

1
Q

Which of the following is NOT a Java relational operator?

=
>=
!=
>

A

=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following is true?

  1. When using == to compare reference variables, you are testing for “identity equality” – that the two variables refer to the same object.
  2. The .equals() method tests for “content equality” – that the objects are considered equivalent to each other.
A

1 and 2 are both true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which of the following operators may NOT be used with operands of type boolean?

&&
||
!=
!

A

All of these may be used with type boolean.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a common use for a variable of type boolean?

A

Flag variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a common name for a method that returns type boolean?

A

Predicate method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the valid data types for a switch statement.

A

int, char, String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If a break statement is not included at the end of each action (alternative) in a switch instruction, what is the result?

A

More than one action may be executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

Either if statements or a switch statement could be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the following is true in Java?

  1. Every else must have a preceding if.
  2. Every if must have a following else.
A

1 is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What kind of loop is a data validation loop?

A

An indefinite loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which loop control structure can NOT be used if there are conditions under which the body of the loop should be executed zero times?

A

do…while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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?

A

Any of the loop control structures can be used to create a counting loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How are the following implemented?

  1. The state of an object.
  2. The behavior of an object.
A
  1. Through instance variables.

2. Through instance methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Another term for instance variable is…?

A

Field.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the keyword for the implicit parameter?

A

this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A method that changes the state of an object is known as….

A

a mutator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

A method that provides information about the state of an object is known as…

A

an accessor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

A piece of code that initializes the state of an object is known as…

A

a constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the keyword used to encapsulate the fields of an object?

A

private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

An object’s properties are also known as what?

A

Instance variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is another name for an object?

A

An instance of a class.

22
Q

Which of the following statements is FALSE?

  1. A private method may be called from anywhere within the class in which it is declared.
  2. A public attribute may be examined from anywhere in the program, but may not be changed.
  3. The keyword void indicates that a method does not return any value.
A

2 is false.

23
Q

Which of the following things does this statement do?

Color darkBrown = new Color( 120, 160, 0 );

  1. Declare a new variable.
  2. Construct a new object.
  3. Create a binding.
  4. All of the above.
  5. None of the above.
A
  1. All of the above.
24
Q

An object diagram represents what?

A

The state of memory at a particular point during the execution of a program.

25
Q

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?

  1. Does the line of code declare a variable?
  2. Does the line of code instantiate an object?
  3. Does the line of code create a binding?
  4. All of the above.
  5. None of the above.
A
  1. All of the above.
26
Q
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;
A

1 & 4.

27
Q
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;
A

1 & 2

28
Q
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;
A

1 & 5.

29
Q
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;
A

5

30
Q

What is an orphaned object?

A

An object which has no references to it.

31
Q

What are the different views of a class?

  1. Source code
  2. UML Class Diagram
  3. Javadoc comments
A

1, 2 and 3.

32
Q

What does the keyword ‘this’ refer to when used in a statement in code?

A

The particular object executing the statement.

33
Q

Name two access modifiers.

A

public and private.

34
Q

What is the proper JavaDoc tag for parameter descriptions?

A

@param

35
Q

Javadoc comments are for client programmers. True/False

A

True

36
Q

What is the name of the package needed to use File objects.

A

java.io

37
Q

What is the throws clause that we include in our method signature when the method instantiates a Scanner to read from a File?

A

throws FileNotFoundException

38
Q

The proper syntax to create a Scanner object that can read from the text file words.txt is

Scanner sc = new Scanner(“file.txt”);

A

False

39
Q

How many steps are required to successfully swap the values in two variables?

A

3

40
Q

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?

A

y[0][3]

41
Q

In order to visit every element in a 2-dimensional array, what control structure design do you need?

A

A loop nested within another loop

42
Q

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

A

Insertion sort

43
Q

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);
A

Prints out the sum of the entire array

44
Q

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);
}
A

Prints the sum of each row

45
Q

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

A

Selection sort

46
Q

You can swap 2 rows of a 2-dimensional array (true/false)

A

True

47
Q

Inheritance represents what type of relationship?

A

Is-a relationship

48
Q

What is the keyword used to indicate a class inheritance relationship?

A

extends

49
Q

A sub class may not access any private members of the super class by name. (true/false)

A

True

50
Q

What is the keyword used by a sub class to call a super class constructor?

A

super

51
Q

When we implement the method: String toString() {} in our classes, we are ______ the method from the Object class.

A

overriding