(6-7) Flashcards

1
Q

Data hiding, which means critical data stored inside the object is protected from code outside the object, is accomplished in Java by:

A

using the private access specifier on the class fields

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

The scope of a public instance field is:

A

the instance methods and methods outside the class

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

Instance methods do not have the key word static in their headers

A

True

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

A method that stores a value in a class’s field or in some other way changes the value of a field is known as a mutator method

A

True

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

The java.lang package is automatically imported into all Java programs

A

True

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

A method that gets a value from the class’s field but does not change it is known as a mutator method

A

False

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

For the following code, which statement is NOT true?

public class Circle
{
private double radius;
private double x;
private double y;
}

A

Y is available to code that is written outside the Circle class

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

It is common practice in object-oriented programming to make all of a class’s:

A

fields private

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

For the following code which statement is NOT true?

public class Sphere
{
private double radius;
public double x;
private double y;
private double z;
}

A

Z is available to code that is written outside the circle class

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

Java allows you create objects of this class in the same way you would create primitive values

A

String

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

The public access specifier for a field indicates that the attribute may not be accessed by statements outside the class

A

False

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

A class in not an object, but description of an object

A

True

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

An object can store data

A

True

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

A class specifies _________ and ______ that a particular type of object has

A

fields; methods

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

Look at the following statement:

import.java.util.*;

This is an example of:

A

A wildcard import

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

A constructor:

A

has the same name as the class

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

Which of the following statements will create a reference, str, to the String, “Hello, World”?

A

String str = “Hello, World”;

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

Which of the following are classes from the Java API?

A

Scanner, Random, and PrintWriter

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

You should not define a class field that is dependent upon the values of other class fields:

A

in order to avoid having stale data

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

Methods that operate on an object’s fields are called:

A

instance methods

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

Which of the following will create a reference, str, to the string, “Hello, World?”

  1. String str = new String(“Hello World);
  2. String str = “Hello, world”;
A

Both 1 and 2

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

Class objects normally have __________ that perform useful operations on their data, but primitive values do not.

A

methods

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

One or more objects may be created from a(n)

A

class

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

What is stored by a reference variable?

A

A memory address

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

Instance methods do not have this key word in their headers:

A

Static

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

A constructor is a method that:

A

performs initialization or setup operation

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

An access specifier indicates how the class may be accessed

A

True

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

The following package is automatically imported into all Java programs

A

java.lang

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

In UML diagrams, this symbol indicates that a member is public

30
Q

Given the following code what will be the final amount when it is displayed?

31
Q

In your textbook the general layout of a UML diagram is a box that is divided into three sections. The top section has the ________; the middle section holds ___________; the bottom section holds __________

A

class name; attributes or fields; methods

32
Q

What does the following UML diagram entry mean?

+ setHeight(h : double) : void

A

A public method with a parameter of data type double that does not return a value

33
Q

Overloading means multiple methods in the same class:

A

have the same name, but different parameter lists

34
Q

In a UML diagram to indicate the data type of a variable enter:

A

the variable name followed by a colon and the data type

35
Q

The term “no-arg-constructor” is applied to any constructor that does not accept arguments

36
Q

When an object is passed as an argument to a method, what is passed into the method’s parameter variable?

A

the object’s memory address

37
Q

Look at the following statement:

import java.util.Scanner;

This is an example of:

A

an explicit import

38
Q

The scope of a private instance field is:

A

the instance methods of the same class

39
Q

Another term for an object of a class is:

40
Q

A constructor is a method that is automatically called when an object is created:

41
Q

What will be the value of x[1] after the following code is executed?

int[] x = {22, 33, 44};
arrayProcess(x);

public static void arrayProcess(int[] a)
{
for (int k = 0; k < 3; k++)
{
a[k] = a[k] + 5;
}
}

42
Q

What would be the results of the following code?

final int SIZE = 25;
int[] array1 = new int[SIZE];
// Code that will put values in array1
int value = 0;
for (int a = 0; a <= array1.length; a++)
{
value += array1[a];
}

A

This will cause the program to show an error

43
Q

What will be the result of executing the following code?

int[] x = {0, 1, 2, 3, 4, 5};

A

An array of 6 values ranging from 0 through 5 and referenced by the variable x will be created

44
Q

Declaring an array reference variable does not create an array.

45
Q

Which of the following statements are TRUE about the following code?

final int ARRAY_SIZE = 10;
long[] array1 = new long[ARRAY_SIZE];

A
  • Declares array1 to be a reference to an array of long values
  • Creates an instance of an array with 10 long values
  • will allow valid subscripts in the range of 0-9

Answer - all of the above

46
Q

For the following code, what will be the value of str[2]?

String[] str = {“abc”, “def”, “ghi”, “jkl”};

A

A reference to the string “ghi”

47
Q

Subscript/index numbering always starts at what value?

48
Q

Which of the following for loops(to print the size of each string) is valid given the following declaration?

String[] names = {“abc”, “def”, “ghi”, “jkl”};

A

for (int i = 0; i < names.length(); i++ System.out.println(names[i].length);

49
Q

Once an array is created, its size cannot be changed.

50
Q

What would be the results of the following code?

int[] x = { 55, 33, 88, 22, 99,
11, 44, 66, 77};
int a = 10;
if( x[2] > x[5])
a = 5;
else
a = 8;

51
Q

In Java, you do not use the new operator when you use a(n):

A

Initialization list

52
Q

When an individual element of an array is passed to a method:

A

The method does not have direct access to the original array.

53
Q

What will be the value of x[8] after the following code has been executed?

final int SUB = 12;
int[] x = new int[SUB];
int y = 20;
for(int i = 0; i < SUB; i++)
{
x[i] = y;
y += 5;
}

54
Q

When an array of objects is created with new, but not initialized, the array values are set to null.

A

True (check question)

55
Q

Each array in Java has a public field named ______ that contains the number of elements in an array

56
Q

What would be the results of the following code?

final int SIZE = 25;
int[] array1 = new int[SIZE];
// Code that will put values in array1
int value = 0;
for (int a = 0; a < array1.length; a++)
{
value += array1[a];
}

A

Value contains the sum of all the values in array1

57
Q

If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]?

A

0 through 14

58
Q

To compare the contents of two arrays, you must compare the elements of the two arrays one by one

59
Q

What is the value of scores[2][3] in the following array?

int [] [] scores = { {88, 80, 79, 92}, {75, 84, 93, 80}, {98, 95, 92, 94}, {91, 84, 88, 96} };

60
Q

To return an array of long values from a method, use this as the return type for the method.

61
Q

Given that String[] str has been initialized, to get a copy of str[0] with all characters converted to upper case, use the following statement:

A

str[0].toUpperCase();

62
Q

A ragged array is:

A

a two-dimensional array where the rows are of different lengths

63
Q

What does the following statement do?

double[] array1 = new double[10];

A
  • Declares array1 to be a reference to an array of double values
  • Creates an instance of an array of 10 double values
  • Will allow subscripts in the range of 0-9

Answer - all of the above

64
Q

What would be the results after the following code is executed?

int[] x = {23, 55, 83, 19};
int[] y = {36, 78, 12, 24};

x = y;
y = x;

A

x[] = {36, 78, 12, 24} and
y[] = {36, 78, 12, 24}

65
Q

When an array is passed to a method:

A
  • A reference to the array is passed
  • It is passed just as an object
  • The method has direct access to the original

Answer - all of the above

66
Q

In memory, an array of String objects:

A

consists of elements, each of which is a reference to a String object

67
Q

What will be returned from the following method?

public static float[] getValue(int x)

A

An array of float values

68
Q

If numbers is a two-dimensional array, which of the following would give the length of row r?

A

numbers[r].length

69
Q

By default, Java initializes array elements with what value?

70
Q

What will be the value of x[1] after the following code is executed?

int[] x = {22, 33, 44};
arrayProcess(x[1]);
public static void arrayProcess(int a)
{
a = a + 5;
}

71
Q

What would be the results after the following code is executed?

int[] x = {22, 55, 83, 19};
int[] y = {36, 78, 12, 24};
for (int a = 0; a < x.length; a++)
{
x[a] = y[a];
y[a] = x[a];
}

A

x[] = {36, 78, 12, 24} and
y[] = {36, 78, 12, 24}

72
Q

An array can hold multiple values of several different types of data simultaneously