Intro to Java Sample Final
What will be printed when the following code is executed? double x = 45678.259; System.out.printf("%,.2f", x);
45,678.26
What is the value of z after the following code is executed? int x = 5, y = 28; float z; z = (float) (y / x);
5.0
A(n) ________ can be thought of as a blueprint that can be used to create a type of ________.
class, object
You can use the ________ method to replace an item at a specific location in an ArrayList.
set
The scope of a public instance field is ________.
the instance methods and methods outside the class
A parameter variable's scope is ________.
the method in which the parameter is declared
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
What will be returned from the following method? public static double methodA() { double a = 8.5 + 9.5; return a; }
18.0
The following statement is an example of ________. import java.util.*;
a wildcard import statement
Which of the following is the not equal operator? a. b. NOT c. *& d. !=
d. !=
Which of the following cannot be used as identifiers in Java? a. variable names b. class names c. key words d. objects
c. key words
________ operators are used to determine whether a specific relationship exists between two values.
Relational
Which type of method performs a task and sends a value back to the code that called it? a. value-returning b. void c. complex d. local
a. value-returning
Which of the following statements correctly creates a Scanner object for keyboard input? a. Scanner kbd = new Scanner(System.keyboard); b. Scanner keyboard = new Scanner(System.in); c. Scanner keyboard(System.in); d. Keyboard scanner = new Keyboard(System.in);
b. Scanner keyboard = new Scanner(System.in);
One or more objects may be created from a(n) ________.
class
Which symbol indicates that a member is public in a UML diagram? a. - b. * c. # d. +
d. +
In the following code, Integer.parseInt(str) is an example of ________. int num; string str = "555"; num = Integer.parseInt(str) + 5;
a value-returning method
In the following code, System.out.println(num) is an example of ________. double num = 5.4; System.out.println(num); num = 0.0;
a void method
RAM is usually ________.
a volatile type of memory, used for temporary storage
In all but very rare cases, loops must contain, within themselves ________.
a way to terminate
Which symbol indicates that a member is private a UML diagram? a. - b. * c. # d. +
a. -
Java performs ________, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for the array.
array bounds checking
The ________ indicates the number of elements the array can hold.
array's data type
Which of the following is not a valid Java comment? a. /** Comment one */ b. */ Comment two /* c. // Comment three d. /* Comment four */
b. */ Comment two /*
Overloading means that multiple methods in the same class ________.
have the same name but different parameter lists
The variable that controls the number of times a loop iterates is known as a ________.
loop control variable
Each different type of CPU has its own ________.
machine language
A method ________.
may have zero or more parameters
A reference variable stores a(n) ________.
memory address
A ________ is a part of a method that contains a collection of statements that are performed when the method is executed.
method body
Class objects normally have ________ that perform useful operations on their data, but primitive variables do not.
methods
The switch statement is a ________.
multiple alternative decision structure
A(n) ________ is a software entity that contains data and procedures.
object
Most of the programming languages used today are ________.
object-oriented
A group of related classes is called a(n) ________.
package
A ________ loop will always be executed at least once.
posttest
When you work with a ________, you are using a storage location that holds a piece of data.
primitive variable
A(n) ________ is used to write computer programs.
programming language
A UML diagram does not contain ________.
the object names
A flag may have the values ________.
true or false
An expression tested by an if statement must evaluate to ________.
true or false
The boolean expression in an if statement must evaluate to ________.
true or false
The ________ loop allows the user to decide on the number of iterations.
user controlled loop
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
In Java, ________ must be declared before they can be used.
variables
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 would be the result 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}
Subscripting always starts with ________.
0
A characteristic of ________ is that only an object's methods are able to directly access and make changes to an object's data.
data hiding
Each repetition of a loop is known as a(n) ________.
iteration
In an if-else statement, if the boolean expression is false then ________.
the statement or block following the else is executed
To indicate the data type of a variable in a UML diagram, you enter ________.
the variable name followed by a colon and the data type
A ________ type of method performs a task and then terminates.
void
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
What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++;
x = 33, y = 9
A Java source file must be saved with the extension ________.
.java
By default, Java initializes array elements to ________.
0
What would 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.
Which of the following expressions will determine whether x is less than or equal to y? a. x <= y b. x => y c. x >= y d. x =< y
a. x <= y
What would be displayed as a result of executing the following code? final int x = 22, y = 4; y += x; System.out.println("x = " + x + ", y = " + y)
Nothing. There is an error in the code.
Java allows you to create objects of the ________ class in the same way you would create primitive variables.
String
Values that are sent into a method are called ________.
arguments
What would be displayed as a result of executing the following code? int x = 578; System.out.print("There are " + x + 5 + "\n" + "hens in the hen house.");
There are 5785 hens in the hen house.
To display the output on the next line, you can use the println method or use the ________ escape sequence in the print method.
\n
Which of the following statements will create a reference, str, to the String "Hello, World"? a. String str = "Hello, World"; b. string str = "Hello, World"; c. String str = new "Hello, World"; d. str = "Hello, World";
a. String str = "Hello, World";
Which of the following will format 12.7801 to display as $12.78? a. System.out.printf("$%,.2f", 12.7801); b. System.out.printf("%f", 12.7801); c. System.out.printf("%.2f$$", 12.7801); d. System.out.printf("$d", 12.7801);
a. System.out.printf("$%,.2f", 12.7801);
Which of the following expressions could be used to perform a case-insensitive comparison of two String objects named str1 and str2? a. str1.equalsIgnoreCase(str2) b. str1.equalsInsensitive(str2) c. str1 != str2 d. str1 || str2
a. str1.equalsIgnoreCase(str2)
Which of the following statements is invalid? a. double r = 9.4632E15; b. double r = 9.4632e15; c. double r = 2.9X106; d. double r = 326.75;
c. double r = 2.9X106;
If numbers is a two-dimensional array, which of the following would give the number of columns in row r? a. numbers.length b. numbers.length[r] c. numbers[r].length d. numbers[r].length[r]
c. numbers[r].length
Which of the following is not part of the programming process? a. defining and modeling the problem b. entering code and compiling it c. testing and debugging d. All of these are parts of the programming process
d. All of these are parts of the programming process
When an array is passed to a method ________. a. it is passed just as any other object would be passed b. the method has direct access to the original array c. a reference to the array is passed d. All of these are true
d. All of these are true
Local variables can be initialized with ________. a. constants b. parameter values c. the result of an arithmetic operation d. Any of these.
d. Any of these.
Given the following method header, which of these method calls is incorrect? public void displayValue(int x, int y); a. displayValue(a, b); // where a is a short and b is a byte b. displayValue(a, b); // where a is an int and b is a byte c. displayValue(a, b); // where a is a short and b is a long d. All of these would give an error.
c. displayValue(a, b); // where a is a short and b is a long
To compile a program named First you would use which of the following commands? a. java First.java b. javac First c. javac First.java d. compile First.javac
c. javac First.java
Which of the following will compile a program called ReadIt? a. java ReadIt.java b. java ReadIt.javac c. javac ReadIt.java d. javac ReadIt.javac
c. javac ReadIt.java
To return an array of long values from a method, which return type should be used for the method? a. long[ARRAY_SIZE] b. array c. long[] d. long
c. long[]
A loop that repeats a specific number of times is known as a(n) ________ loop.
count-controlled
Variables are classified according to their ________.
data types
A constructor ________.
has the same name as the class
Methods that operate on an object's fields are called ________.
instance methods
When an argument is passed to a method ________.
its value is copied into the method's parameter variable
Variables are ________.
symbolic names made up by the programmer that represent memory locations
The central processing unit (CPU) consists of two parts which are ________.
the control unit and the arithmetic and logic unit (ALU)
The header of a value-returning method must specify ________.
the data type of the return value
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);
1 time
________ refers to combining data and code into a single object.
Encapsulation
________ refers to the physical components that a computer is made of.
Hardware
Java was developed by ________.
Sun Microsystems
In Java, when a character is stored in memory, it is actually the ________ that is stored.
Unicode number
In a general sense, a method is ________.
a collection of statements that perform a specific task
If you attempt to use a local variable before it has been given a value, ________.
a compiler error will occur
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
A computer program is ________.
a set of instructions that allow the computer to solve a problem or perform a task
Which of the following is a value that is written into the code of a program? a. a literal b. an assignment statement c. an operator d. a variable
a. a literal
Which of the following statements determines whether the variable temp is within the range of 0 through 100 (inclusive)? a. if (temp >= 0 && temp <= 100) b. if (temp > 0 && temp < 100) c. if (temp >= 0 || temp <= 100) d. if (temp > 0 || temp < 100)
a. if (temp >= 0 && temp <= 100)
Which Scanner class method reads a String? a. nextLine b. charAt c. nextString d. getLine
a. nextLine
Character literals are enclosed in ________ and string literals are enclosed in ________. a. single quotes, double quotes b. double quotes, single quotes c. single quotes, single quotes d. double quotes, double quotes
a. single quotes, double quotes
The following statement is an example of ________. import java.util.Scanner;
an explicit import statement
Values stored in local variables ________.
are lost between calls to the method in which they are declared
Which of the following is not a part of a method call? a. method name b. return type c. parentheses d. All of these
b. return type
What does the following statement do? double[] array1 = new double[10]; a. It declares array1 to be a reference to an array of double values. b. It will allow valid subscripts in the range of 0 through 9. c. It creates an instance of an array of ten double values. d. It does all of these.
d. It does all of these.
It is common practice in object-oriented programming to make all of a class's ________.
fields private
A class specifies the ________ and ________ that a particular type of object has.
fields, methods
It is common practice to use a ________ variable as a size declarator.
final
A ________ is a boolean variable that signals when some condition exists in the program.
flag
The ________ loop is ideal in situations where the exact number of iterations is known.
for
You should always document a method by writing comments that appear ________.
just before the method's definition
A value that is written into the code of a program is a(n) ________.
literal
The lifetime of a method's local variable is ________.
only while the method is executing
In the header, the method name is always followed by ________.
parentheses
Software refers to ________.
programs
A cross between human language and a programming language is called ________.
pseudocode
One type of design tool used by programmers when creating a model of a program is ________.
pseudocode
The ________ method removes an item from an ArrayList at a specific index.
remove
The end of a Java statement is indicated by a(n) ________.
semicolon (;)
A ________ is a value that signals when the end of a list of values has been reached.
sentinel
________ works like this: If the expression on the left side of the && operator is false, the expression the right side will not be checked.
short-circuit evaluation
Instance methods do not have the ________ key word in their headers.
static
The scope of a private instance field is ________.
the instance methods of the same class
When an individual element of an array is passed to a method ________.
the method does not have access to the original array
Two or more methods in a class may have the same name as long as ________.
they have different parameter lists
A ragged array is ________.
a two-dimensional array where the rows have different numbers of columns
If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array? a. for (int row = 1; row < numbers.length; row++) { for (int col = 1; col < numbers.length; col++) total += numbers [row][col]; b. for (int row = 0; row < numbers.length; row++) { for int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; } c. for (int for = 0; numbers.length; row++) { for (int col = 0; col < numbers.length; col++) total += numbers[row][col]; } d. for (int row = 0; row < numbers[row].length; row++) { for (int col = 0; col < numbers.length; col++) total += numbers[row][col]; }
b. for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; }
Which is a control structure that causes a statement or group of statements to repeat? a. block b. loop c. prefix mode d. body
b. loop
Which of the following is a correct method header for receiving a two-dimensional array as an argument? a. public static void passMyArray(int[1, 2]) b. public static void passMyArray(int[][]) c. public static void passMyArray[1][2]) d. public static void passMyArray(int[],int[] myArray)
b. public static void passMyArray(int[][])
In Java, you do not use the new operator when you use a(n) ________.
initialization list
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
Which of the following statements will correctly convert the data type, if x is a float and y is a double? a. x = float y; b. (blank) c. x = (float)y; d. x = y;
c. x = (float)y;
If x has been declared an int, which of the following statements is invalid? a. x = 0; b. x = -59832; c. x = 1,000; d. x = 592
c. x = 1,000;
In memory, an array of String objects ________.
consists of an array of references to String objects
A value-returning method must specify ________ as its return type in the method header. a. an int b. a double c. a boolean d. Any valid data type
d. Any valid data type
Which of the following is not a primitive data type? a. short b. long c. float d. string
d. string
When an object is created, the attributes associated with the object are called ________.
instance fields
What is the value of z after the following statements have been executed? int x = 4, y = 33; double z; z = (double) (y / x);
8.0
What will be the result after the following code executes? int num; string str = "555"; num = Integer.parseInt(string str) + 5;
The last line of code will cause an error.
When an object, such as a String, is passed as an argument it is ________.
actually a reference to the object that is passed
Which of the following statements opens a file named MyFile.txt and allows you to read data from it? a. Scanner inputFile = new Scanner("MyFile.txt"); b. File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); c. File Scanner = new File("MyFile.txt"); d. PrintWriter inputFile = new PrintWriter("MyFile.txt");
b. 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? a. FileWriter fwriter = new FileWriter("MyFile.txt"); PrintWriter outFile = new PrintWriter(fwriter); b. FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter); c. PrintWriter outfile = new PrintWriter("MyFile.txt", true); d. PrintWriter outfile = new PrintWriter(true, "MyFile.txt");
b. 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? a. After the first character, you may use the letters a-z, A-Z, an underscore, a dollar sign, or the digits 0-9. b. Identifiers can contain spaces. c. Uppercase and lowercase characters are distinct. d. The first character must be one of the letters a-z, A-Z, an underscore, or a dollar sign.
b. Identifiers can contain spaces.
Which of the following is not a rule that must be followed when naming identifiers? a. The first character must be one of the letters a-z, A-Z, and underscore or a dollar sign. b. Identifiers can contain spaces. c. Uppercase and lowercase characters are distinct. d. After the first character, you may use the letters a-z, A-Z, the underscore, a dollar sign, or digits 0-9.
b. Identifiers can contain spaces.
Which of the following is a correct method header for receiving a two-dimensional array as an argument? a. public static void passArray(int[1,2]) b. public static void passArray(int [][]) c. public static void passArray(int[1],[2]) d. public static void passArray(int[], int[])
b. public static void passArray(int [][])
Which of the following is not involved in identifying the classes to be used when developing an object-oriented application? a. a description of the problem domain b. the code c. a refined list of nouns that include only those relevant to the problem d. all the nouns are identified
b. the code
The boolean data type may contain which of the following range of values? a. -128 to + 127 b. true or false c. -2,147,483,648 to +2,147,483,647 d. -32,768 to +32,767
b. true or false
Given the following two-dimensional array declaration, which statement is true? int[][] numbers = new int[6][9]; a. The numbers array has 54 rows. b. The numbers array has 15 rows. c. The numbers array has 6 rows and 9 columns. d. The numbers array has 6 columns and 9 rows.
c. The numbers array has 6 rows and 9 columns.
If you prematurely terminate an if statement with a semicolon, the compiler will ________. a. not display an error message b. assume you are placing a null statement there c. both not display an error message and assume you are placing a null statement there d. none of these
c. both not display an error message and assume you are placing a null statement there
A class's responsibilities include ________. a. the things a class is responsible for knowing b. the things a class is responsible for doing c. both of these d. neither of these
c. both of these
Which of the following for loops 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); b. for (int i = 0; i < names.length(); i++) System.out.println(names[i].length); c. for (int i = 0; i < names.length; i++) System.out.println(names[i].length()); d. for (int i = 0; i < names.length(); i++) System.out.println(names[i].length());
c. for (int i = 0; i < names.length; i++) System.out.println(names[i].length());
When working with the PrintWriter class, which of the following import statements should you have near the top of your program? a. import javax.swing.*; b. import javac.io.*; c. import java.io.*; d. import java.file.*;
c. import java.io.*;
Given the following statement, which statement will write the string "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt"); a. System.out.println(diskOut, "Calvin"); b. PrintWriter.println("Calvin"); c. DiskFile.println("Calvin"); d. diskOut.println("Calvin");
d. diskOut.println("Calvin");
Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file? a. int number = inputFile.next(); b. int number = inputFile.integer(); c. int number = inputFile.readInt(); d. int number = inputFile.nextInt();
d. int number = inputFile.nextInt();
Which of the following is not part of a method header? a. return type b. method name c. parentheses d. semicolon
d. semicolon
The ________ statement is used to create a decision structure which allows a program to have more than one path of execution.
if
If a loop does not contain, within itself, a valid way to terminate, it is called a(n) ________ loop
infinite
Each array in Java has a public field named ________ that contains the number of elements in the array.
length
A set of programming language statements that perform a specific task is a(n) ________.
procedure
Before entering a loop to compute a running total, the program should first ________.
set the accumulator variable to an initial value, often zero
Given that String[] str has been initialized, to get a copy of str[0] with all the characters converted to uppercase, you would use the ________ statement.
str[0].toUpperCase();
Internally, the central processing unit (CPU) consists of two parts which are ________.
the control unit and the arithmetic/logic unit (ALU)
The process of breaking a problem down into smaller pieces is sometimes called ________.
the divide and conquer method
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
When you pass an argument to a method you should be sure that the argument's type is compatible with ________.
the parameter variable's data type
Application software refers to ________.
the programs that make the computer useful to the user
When a method tests an argument and returns a true or false value, it should return ________.
a boolean value
A runtime error is usually the result of ________.
a logical error
Every Java application program must have ________.
a method named main
Java requires that the boolean expression being tested by an if statement be enclosed in ________.
a set of parentheses
A special variable that holds a value being passed into a method is called a(n) ________.
parameter
When the + operator is used with strings, it is known as the ________.
string concatenation operator
A(n) ________ is used as an index to pinpoint a specific element within an array.
subscript
A loop that executes as long as a particular condition exists is called a(n) ________ loop.
conditional
To print "Hello, world" on the monitor, which of the following Java statements should be used? A. System.out.println("Hello, world"); B. System Print = "Hello, world"; C. SystemOutPrintln('Hello, world'); D. system.out.println(Hello, world);
A. System.out.println("Hello, world");
A block of code is enclosed in a set of ________.
braces, { }
What is syntax?
The rules that must be followed when writing a program
If the following Java statements are executed, what will be displayed? System.out.println("The top three winners are\n"); System.out.print("Jody, the Giant\n"); System.out.print("Buffy, the Barbarian"); System.out.println("Adelle, the Alligator");
The top three winners are Jody, the Giant Buffy, the BarbarianAdelle, the Alligator
Methods are commonly used to ________.
break a program down into small manageable pieces
Which of the following is a valid declaration for a ragged array with five rows but no columns? a. int[][] ragged = new int[5]; b. int[][] ragged = new int[][5]; c. int[][] ragged = new int[5][]; d. int[] ragged = new int[5];
c. int[][] ragged = new int[5][];
If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal? a. str1 = str2 b. str1 && str2 c. str1.equals(str2) d. str1 += str2
c. str1.equals(str2)
Which of the following is a named storage location in the computer's memory? a. a literal b. an operator c. a constant d. a variable
d. a variable
When an argument value is passed to a method, the receiving parameter variable is ________.
declared in the method header inside the parentheses
To create a method, you must write its ________.
definition
An item that separates other items is known as a ________.
delimiter
The ________ loop is ideal in situations where you always want the loop to iterate at least once.
do-while
Which of the following is not one of the major components of a typical computer system? a. the CPU b. input/output devices c. main memory d. secondary storage devices e. All of these are major components
e. All of these are major components
Variables of the boolean data type are useful for ________.
evaluating conditions that are either true or false
There are ________ bits in a byte.
8
A Java program must have at least one of the following. A. a comment B. a class definition C. a System.out.println(); statement D. a variable declaration
B. a class definition
When writing documentation comments for a method, you can provide a description of each parameter by using a ________.
@param tag
To document the return value of a method you can use a ________.
@return tag
The variable used to keep a running total in a loop is called a(n) ________.
accumulator
Which of the following statements will create an object from the Random class? a. randomNumbers() = new Random(); b. Random myNumber = new Random(); c. myNumber = new Random(); d. Random = new randomNumbers();
b. Random myNumber = new Random();
Which of the following is a software entity that contains data and procedures? a. a method b. an object c. a class d. a program
b. an object
Which of the following expressions determines whether the char variable, chrA, is not equal to the letter 'A'? a. chrA == 'A' b. chrA != 'A' c. chrA || 'A' d. chrA.notEquals(A)
b. chrA != 'A'
Assume that the following method header is for a method in class A. public void displayValue(int value) Assume that the following code segments appear in another method, also in class A. Which contains a legal call to the displayValue method? a. int x = 7; void displayValue(x); b. int x = 7; displayValue(x); c. int x = 7; displayValue(int x); d. int x = 7; displayValue(x)
b. int x = 7; displayValue(x);
Which of the following is the correct boolean expression to test for: int x being a value between, but not including, 500 and 650, or int y not equal to 1000? a. ((x >= 500 && x <= 650) && (y != 1000)) b. ((x > 500 AND x < 650) OR !(y.equal(1000))) c. ((x > 500 && x < 650) || (y != 1000)) d. ((x < 500 && x > 650) || !(y == 1000))
c. ((x > 500 && x < 650) || (y != 1000))
While ________ is centered on creating procedures, ________ is centered on creating objects.
procedural programming, object-oriented programming
Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops is the correct way to read data from the file until the end of the file is reached? a. while (inputFile.nextLine == " ") { ... } b. while (inputFile != null) { ... } c. while (!inputFile.EOF) { ... } d. while (inputFile.hasNext()) { ... }
d. while (inputFile.hasNext()) { ... }
A constructor is a method that ________.
performs initialization or setup operations