Java Test 1
To determine if two arrays are equal, you must compare each of the elements of the two arrays.
True
Unicode is an international encoding system that is extensive enough to represent all the characters of all the world's alphabets.
True
Unlike a console program, a program that uses JOptionPane does not automatically stop executing when the end of the main method is reached.
True
When you open a file with the PrintWriter class, the class can potentially throw an IOException.
True
When a character is stored in memory, it is actually the ________ that is stored.
Unicode number
Which of the following is not involved in finding the classes when developing an object-oriented application?
Write the code.
It is common practice in object-oriented programming to make all of a class's
fields private.
The JVM periodically performs this process to remove unreferenced objects from memory.
garbage collection
A constructor
has the same name as the class.
Overloading is
having two or more methods with the same name, but different signatures.
When working with the PrintWriter class, which of the following import statements should you have near the top of your program?
import java.io.*;
This statement tells the compiler where to find the JOptionPane class and makes it available to your program.
import javax.swing.JOptionPane;
A(n) ________ is a dialog box that prompts the user for input.
input dialog
When an object is created, the attributes associated with the object are called
instance fields.
Methods that operate on an object's fields are called
instance methods.
A search algorithm
is used to locate a specific item in a larger collection of data.
When an array is passed to a method
it is passed just as any other object would be. the method has direct access to the original array. a reference to the array is passed. All of these
To return an array of long values from a method, use this as the return type for the method.
long[]
A method
may have zero or more parameter variables
Class objects normally have ________ that perform useful operations on their data, but primitive variables do not.
methods
By default, a reference variable that is an instance field is initialized to the value
null
UML diagrams do not contain
object names.
In the blueprint/house analogy, think of a class as a blueprint that describes a house and ________ as instances of the house built from the blueprint.
objects
Whereas ________ is centered on creating procedures, ________ is centered on creating objects.
procedural programming, object-oriented programming
This is a set of programming language statements that, together, perform a specific task.
procedure
Computers can do many different jobs because they are
programmable.
Software refers to
programs.
Which of the following is a correct method header for receiving a two-dimensional array as an argument?
public static void passMyArray(int[][] myArray)
Byte code instructions are
read and interpreted by the JVM.
The ________ method removes an item from an ArrayList at a specific index.
remove
A ________ is a value that signals when the end of a list of values has been reached.
sentinel
A(n) ________ is a special value that cannot be mistaken as a member of a list of data items and signals that there are no more data items from the list to be processed.
sentinel
You can use the ________ method to replace an item at a specific location in an ArrayList.
set
You use this method to determine the number of items stored in an ArrayList object.
size
If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal?
str1.equals(str2)
You can use this method to determine whether a file exists.
the File class's exists method
What will be returned from the method, if the following is the method header? public Rectangle getRectangle()
the address of an object of the Rectangle class
In order to do a binary search on an array,
the array must first be sorted in ascending order.
Internally, the central processing unit (CPU) consists of two parts:
the control unit and the arithmetic and logic unit (ALU). the control unit and main memory. the arithmetic and login unit (ALU) and main memory.
You may use this to compare two enum data values.
the equals and compareTo methods
When an argument is passed by value,
the parameter variable holds the address of the argument.
Application software refers to
the programs that make the computer useful to the user.
A message dialog is a quick and simple way to ask the user to enter data.
False
A sorting algorithm is used to locate a specific item in a larger collection of data.
False
Both character literals and string literals can be assigned to a char variable.
False
Colons are used to indicate the end of a Java statement.
False
Compiled byte code is also called source code.
False
Java source files end with the .class extension.
False
The names of the enum constants in an enumerated data type must be enclosed in quotation marks.
False
The term "default constructor" is applied to the first constructor written by the author of the class.
False
When an array of objects is declared, but not initialized, the array values are set to 0.
False
In an if-else statement, if the boolean expression is false
the statement or block following the else is executed.
The only limitation that static methods have is
they cannot refer to nonstatic members of the class.
This method returns a string representing all of the items stored in an ArrayList object.
toString
When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.
False
When two strings are compared using the String class's compareTo method, the comparison is case-insensitive.
False
You can declare an enumerated data type inside of a method.
False
Which of the following statements opens a file named MyFile.txt and allows you to read data from it?
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
Which of the following statements opens a file named MyFile.txt and allows you to append data to its existing contents?
FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);
Which of the following is not a rule that must be followed when naming identifiers?
Identifiers can contain spaces.
A for loop normally performs which of these steps?
update the control variable during each iteration test the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value initialize a control variable to a starting value All of these
Data hiding, which means that 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 primitive data types only allow a(n) ________ to hold a single value.
variable
Java provides a mechanism known as ________, which makes it possible to write a method that takes a variable number of arguments.
variable-length argument lists
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?
while (inputFile.hasNext())
The "has a" relationship is sometimes called a(n) ________ because one object is part of a greater whole.
whole-part relationship
The binary search algorithm
will cut the portion of the array being searched in half each it fails to locate the search value.
A partially filled array is normally used
with an accompanying integer value that holds the number of items stored in the array.
Key words are
words that have a special meaning in the programming language.
If object1 and object2 are objects of the same class, to make object2 a copy of object1
write a method for the class that will make a field by field copy of object1 data members into object2 data members.
Which of the following expressions will determine whether x is less than or equal to y?
x <= y
What will be displayed after the following statements have been executed? int x = 15, y = 20, z = 32 x += 12; y /= 6; z -= 14; System.out.println("x = " + x + ", y = " + y + ", z = " + z);
x = 27, y = 3, z = 18
What output will be displayed as a result of executing the following code? int x = 5, y = 20; x += 32; y /= 4; System.out.println("x = " + x + ", y = " + y);
x = 37, y = 5
What does <String> specify in the following statement? ArrayList<String> nameList = new ArrayList<String>();
It specifies that only String objects may be stored in the ArrayList object.
Named constants are initialized with a value, and that value cannot change during the execution of the program.
True
Programming style includes techniques for consistently putting spaces and indentation in a program so visual cues are created.
True
Shadowing is the term used to describe where the field name is hidden by the name of a local or parameter variable.
True
The DecimalFormat class is part of the Java API, but it is not automatically available to your programs.
True
The Java API provides a class named Math, which contains numerous methods that are useful for performing complex mathematical operations.
True
The Java Virtual Machine is a program that reads Java byte code instructions and executes them as they are read.
True
The Java compiler does not display an error message when it processes a statement that uses an invalid subscript.
True
The String[] args parameter in the main method header allows the program to receive arguments from the operating system command line.
True
The System.out.printf method allows you to format output in a variety of ways.
True
The do-while loop is ideal in situations where you always want the loop to iterate at least once.
True
The if-else statement will execute one group of statements if its boolean expression is true, or another group if its boolean expression is false.
True
Logical errors are mistakes that cause the program to produce erroneous results.
True
Which of the following strings could be passed to the DecimalFormat constructor to display 12.78 as 12.8%?
"##0.0%"
When saving a Java source file, save it with an extension of
.java.
How many times will the following do-while loop be executed? int x = 11; do{ x += 20; } while (x > 100);
1
What will be the value of bonus after the following code is executed? int bonus, sales = 10000; if(sales < 5000) bonus = 200; else if(sales < 7500) bonus = 500; else if(sales < 20000) bonus = 1000; else bonus = 1250;
1000
How many times will the following for loop be executed? for(int count = 10; count <= 21; count++){ System.out.println("Java is great!"); }
12
What will be the value of x after the following code is executed? int x, y = 15; x = y--;
15
What would be the value of x after the following statements were executed? int x = 10; switch(x){ case 10: x += 15; case 12: x -= 5; break; default: x *= 3; }
20
What will be the value of x after the following code is executed? int x = 10; for(int y = 5; y < 20; y += 5) x += y;
40
What will be displayed when the following code is executed? double x = 456783.259; DecimalFormat formatter = new DecimalFormat("#,###,##0.00"); JOptionPane.showMessageDialog(null,formatter.format(x));
45,678.26
What will be displayed when the following code is executed? double x = 45678.259 DecimalFormat formatter = new DecimalFormat("#,##0.0"); JOptionPane.showMessageDialog(null,formatter.format(x));
45,678.3
How many times will the following do-while loop be executed? int x = 11; do{ x += 20; } while(x <= 100);
5
CRC stands for
Class, Responsibilities, Collaborations.
final int x = 22, y = 4; y += x; System.out.println("x = " + x + ", y = " + y);
Nothing. There is an error in the code.
Assuming the following declaration exists: enum Tree { OAK, MAPLE, PINE } What will the following code display? System.out.println(Tree.OAK);
OAK
This is a special language used to write computer programs.
Programming language
________ is a cross between human language and a programming language.
Pseudocode
If you have defined a class SavingsAccount with a public static method getNumberOfAccounts, and created a SavingsAccount object referenced by the variable account20, which of the following will call the getNumberOfAccounts method?
SavingsAccount.getNumberOfAccounts
Which of the following statements correctly creates a Scanner object for keyboard input?
Scanner keyboard = new Scanner(System.in);
To print "Hello, world" on the monitor, use the following Java statement:
System.out.println("Hello, world");
Assume the class BankAccount has been created, and the following statement correctly creates an instance of the class: BankAccount account = new BankAccount(5000.0); What is true about the following statement? System.out.println(account);
The account object's toString method will be implicitly called.
What is the value of ans after the following code has been executed? int x = 40; int y = 40; int ans = 0; if( x = y) ans = x + 10;
The code contains an error and will not compile.
Given the following two-dimensional array declaration, which statement is true? int[][] numbers = new int[6][9]
The numbers array has 6 rows and 9 columns.
What will be the result of the following code? int[] numbers = {50,10,15,20,25,100,30}; int value = 0; for(int i = 0; i < numbers.lenght; i++){ value += number[i]; }
The value variable will contain the sum of all the values in the numbers array.
For the following code, which statement is not true? public class Circle{ private double radius; public double x; private double y; }
The y field is available to code that is written outside the Circle class.
For the following code, which statement is not true? public class Sphere{ private double radius; public double x; private double y; private double z; }
The z field is available to code that is written outside the Sphere class.
If the following is from the method section of a UML diagram, which of the following statements is true?
This is a public method that accepts a Stock object as its argument and returns a boolean value.
Look at the following declaration: enum Tree { OAK, MAPLE, PINE } What is the fully-qualified name of the PINE enum constant?
Tree.PINE
A local variable's scope always ends at the closing brace of the block of code in which it is declared.
True
All it takes for an OR expression to be true is for one of the subexpressions to be true.
True
An ArrayList object automatically expands in size to accommodate the items stored in it.
True
An important style rule you should adopt for writing if statements is to write the conditionally executed statement on the line after the if statement.
True
Any items typed on the command-line, separated by space, after the name of the class are considered to be one or more arguments that are to be passed into the main method.
True
Encapsulation refers to the combining of data and code into a single object.
True
If a class has a method named finalize, it is called automatically just before an instance of the class is destroyed by the garbage collector.
True
If the expression on the left side of the && operator is false, the expression on the right side will not be checked.
True
If you use a flag in a format specifier, you must write the flag before the field width and the precision.
True
Enclosing a group of statements inside a set of braces creates
a block of statements.
You cannot use the fully-qualified name of an enum constant for
a case expression.
A runtime error is usually the result of
a logical error.
A computer program is
a set of instructions that enable the computer to solve a problem or perform a task.
What is a ragged array?
a two-dimensional array where the rows have a different number of columns
What is the following statement an example of? import java.util.*;
a wildcard import statement
The variable used to keep the running total is called a(n)
accumulator
A static field is created by placing the key word static
after the access specifier and before the field's data type.
If a loop does not contain within itself a way to terminate, it is called
an infinite loop.
When you write an enumerated type declaration, you
are actually creating a special kind of class. do not enclose the enum constants in quotation marks. should use the standard convention of writing the enum constants in uppercase. All of these
The data contained in an object is known as
attributes.
If you do not provide initialization values for a class's numeric fields, they will
be automatically initialized with 0.
After the header, the body of the method appears inside a set of
braces, {}.
Which of the following expressions determines whether the char variable chrA is not equal to the letter 'A'?
chrA != 'A'
One or more objects may be created from a(n)
class
A Java program must have at least one
class definition.
A loop that repeats a specific number of times is known as a(n) ________ loop.
count-controlled
What does the following statement do? double[] array1 = new double[10];
declares array1 to be a reference to an array of double values will allow valid subscripts in the range of 0 through 9 creates an instance of an array of 10 double values All of these
Which of the 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 of 10 long values will allow valid subscripts in the range of 0 through 9 All of these
Which of the following is not part of the programming process?
design/model debugging/correcting errors testing All these are parts of the programming process.
Which of the following statements is invalid?
double r = 2.9X106;