(6-7) Flashcards
Data hiding, which means critical data stored inside the object is protected from code outside the object, is accomplished in Java by:
using the private access specifier on the class fields
The scope of a public instance field is:
the instance methods and methods outside the class
Instance methods do not have the key word static in their headers
True
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
True
The java.lang package is automatically imported into all Java programs
True
A method that gets a value from the class’s field but does not change it is known as a mutator method
False
For the following code, which statement is NOT true?
public class Circle
{
private double radius;
private double x;
private double y;
}
Y is available to code that is written outside the Circle class
It is common practice in object-oriented programming to make all of a class’s:
fields private
For the following code which statement is NOT true?
public class Sphere
{
private double radius;
public double x;
private double y;
private double z;
}
Z is available to code that is written outside the circle class
Java allows you create objects of this class in the same way you would create primitive values
String
The public access specifier for a field indicates that the attribute may not be accessed by statements outside the class
False
A class in not an object, but description of an object
True
An object can store data
True
A class specifies _________ and ______ that a particular type of object has
fields; methods
Look at the following statement:
import.java.util.*;
This is an example of:
A wildcard import
A constructor:
has the same name as the class
Which of the following statements will create a reference, str, to the String, “Hello, World”?
String str = “Hello, World”;
Which of the following are classes from the Java API?
Scanner, Random, and PrintWriter
You should not define a class field that is dependent upon the values of other class fields:
in order to avoid having stale data
Methods that operate on an object’s fields are called:
instance methods
Which of the following will create a reference, str, to the string, “Hello, World?”
- String str = new String(“Hello World);
- String str = “Hello, world”;
Both 1 and 2
Class objects normally have __________ that perform useful operations on their data, but primitive values do not.
methods
One or more objects may be created from a(n)
class
What is stored by a reference variable?
A memory address
Instance methods do not have this key word in their headers:
Static
A constructor is a method that:
performs initialization or setup operation
An access specifier indicates how the class may be accessed
True
The following package is automatically imported into all Java programs
java.lang
In UML diagrams, this symbol indicates that a member is public
+
Given the following code what will be the final amount when it is displayed?
522.00
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 __________
class name; attributes or fields; methods
What does the following UML diagram entry mean?
+ setHeight(h : double) : void
A public method with a parameter of data type double that does not return a value
Overloading means multiple methods in the same class:
have the same name, but different parameter lists
In a UML diagram to indicate the data type of a variable enter:
the variable name followed by a colon and the data type
The term “no-arg-constructor” is applied to any constructor that does not accept arguments
True
When an object is passed as an argument to a method, what is passed into the method’s parameter variable?
the object’s memory address
Look at the following statement:
import java.util.Scanner;
This is an example of:
an explicit import
The scope of a private instance field is:
the instance methods of the same class
Another term for an object of a class is:
instance
A constructor is a method that is automatically called when an object is created:
True
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;
}
}
38
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];
}
This will cause the program to show an error
What will be the result of executing the following code?
int[] x = {0, 1, 2, 3, 4, 5};
An array of 6 values ranging from 0 through 5 and referenced by the variable x will be created
Declaring an array reference variable does not create an array.
True
Which of the following statements are TRUE about the following code?
final int ARRAY_SIZE = 10;
long[] array1 = new long[ARRAY_SIZE];
- 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
For the following code, what will be the value of str[2]?
String[] str = {“abc”, “def”, “ghi”, “jkl”};
A reference to the string “ghi”
Subscript/index numbering always starts at what value?
0
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”};
for (int i = 0; i < names.length(); i++ System.out.println(names[i].length);
Once an array is created, its size cannot be changed.
True
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;
a = 5
In Java, you do not use the new operator when you use a(n):
Initialization list
When an individual element of an array is passed to a method:
The method does not have direct access to the original array.
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;
}
60
When an array of objects is created with new, but not initialized, the array values are set to null.
True (check question)
Each array in Java has a public field named ______ that contains the number of elements in an array
length
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];
}
Value contains the sum of all the values in array1
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[]?
0 through 14
To compare the contents of two arrays, you must compare the elements of the two arrays one by one
True
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} };
94
To return an array of long values from a method, use this as the return type for the method.
long[]
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:
str[0].toUpperCase();
A ragged array is:
a two-dimensional array where the rows are of different lengths
What does the following statement do?
double[] array1 = new double[10];
- 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
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;
x[] = {36, 78, 12, 24} and
y[] = {36, 78, 12, 24}
When an array is passed to a method:
- 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
In memory, an array of String objects:
consists of elements, each of which is a reference to a String object
What will be returned from the following method?
public static float[] getValue(int x)
An array of float values
If numbers is a two-dimensional array, which of the following would give the length of row r?
numbers[r].length
By default, Java initializes array elements with what value?
0
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;
}
33
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];
}
x[] = {36, 78, 12, 24} and
y[] = {36, 78, 12, 24}
An array can hold multiple values of several different types of data simultaneously
False