Final Exam Questions
The while loop is always the best choice in situations where the exact number of iterations is known. True False
False
the term "default constructor" is applied to the first constructor written by the author of the class. True False
False
Which of the following statements opens a file named MyFile.txt and allows you to read data from it? A. PrintWriter inputFile = new PrintWriter("MyFile.txt"); B. File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); C. Scanner inputFile = new Scanner("MyFile.txt"); D. File Scanner = new File("MyFile.txt");
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
Which of the following statements will create an object from the Random class? A. myNumber = new Random(); B. Random = new randomNumbers(); C. randomNumbers() = new Random(); D. Random myNumber = new Random();
Random myNumber = new Random();
which of the following statements correctly creates a Scanner object for keyboard input: A. Scanner kbd = new Scanner(System.keyboard); B. Scanner keyboard(System.in) C. Keyboard scanner = new Keyboard(System.in); D. Scanner keyboard = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
What will be the value of ans after the following statements are executed? int x=40; int y=40; if (x = y) ans = x + 10; A. 30 B. 50 C. 80 D. The code contains an error and will not compile.
The code contains an error and will not compile.
What would be the result after the following code is executed? int[] number = {40,3,5,7,8,12,10}; int value = number[0]; for (int i=1; i<numbers.length; i++) { if (numbers[i] < value) value = numbers[i]; } A. The value variable will contain the lowest value in the numbers array. B. The value variable will contain the sum of all the values in the numbers array. C. The value variable will contain the highest value in the numbers array. D. The value variable will contain the average of all the values in the numbers array.
The value variable will contain the lowest value in the numbers array.
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 False
True
An ArrayList object automatically expands in size to accommodate the items stored in it. True False
True
Each byte is assigned a unique number known as an address. True False
True
Encapsulation refers to the combining of data and code into a single object. True False
True
The Java API provides a class named Math that contains numerous methods which are useful for performing complex mathematical operations. True False
True
The do-while loop is ideal in situations where you always want the loop to iterate at least once. True False
True
The java.lang package is automatically imported into all Java programs. True False
True
The term "no-arg constructor" is applied to any constructor that does not accept arguments True False
True
When a local variable in an instance method has the same name as an instance field, the instance field hides the local variables. True False
True
Enclosing a group of statements inside a set of braces creates: A. a block of statements. B. a conditional statement. C. an expression. D. a relational operator.
a block of statements.
A runtime error is usually the result of: A. a syntax error B. bad data C. a logical error D. a compiler error
a logical error
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 B. a private field called setHeight that is a double data type C. a public field called Height that is a double data type D. a private method with no parameters that returns a double data type
a public method with a parameter of data type double that does not return a value
For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"}; A. a reference to the String object containing "def" B. "ghi" C. "def" D. a reference to the String object containing "ghi"
a reference to the String object containing "ghi"
Java requires that the boolean expression being tested by an if statement be enclosed in A. a set of parentheses. B. a set of braces. C. a set of double quotes. D. a set of brackets.
a set of parentheses.
In all but very rare cases, loops must contain, within themselves: A. nested decision structures. B. arithmetic operators. C. nested loops. D. a way to terminate.
a way to terminate
The variable used to keep a running total in a loop is called a(n): A. sentinel. B. accumulator. C. integer. D. summation.
accumulator.
Which of the following ArrayList class methods is used to insert an item at a specific location in an ArrayList? A. store B. set C. add D. insert
add
What is the following statement an example of? import java.util.Scanner; A. a conditional import statement B. an explicit import statement C. an unconditional import statement D. a wildcard import statement
an explicit import statement
What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x=50, y=50; if (x >= y) { ans = x = 10 x -= y; } else { ans = y + 10 y += x; } Select one: A. ans = 60, x = 0, and y = 50 B. ans = 60, x = 50, and y = 100 C. ans = 45, x = 50, and y = 50 D. ans = 45, x = 50, and y = 0
ans = 60, x = 0, and y = 50
If you do not provide initialization values for a class's numeric fields, they will: A. cause a runtime error B. contain an unknown value C. cause a compiler error D. be automatically initialized to 0
be automatically initialized to 0.
A block of code is enclosed in a set of A. brackets, [ ] B. parentheses, () C. braces, { } D. double quotes, " "
braces, { }
One or more objects may be created from a(n): A. method B. field C. class D. instance
class
The ________ loop is ideal in situations where you always want the loop to iterate at least once. A. while B. for C. do-while D. posttest
do-while
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
double r = 2.9X106
Java source files end with the .class extension True False
false
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)
if (temp >= 0 && temp <= 100)
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.file.*; D. import java.io.*;
import java.io.*;
Which of the following import statements is required in order to use the ArrayList class? A. import java.util.Tools; B. import java.util.ArrayList; C. import java.util.API; D. import java.util.Containers;
import java.util.ArrayList;
If a loop does not contain, within itself, a valid way to terminate, it is called a(n) ________ loop A. while B. do-while C. for D. infinite
infinite
What does the following statement do? double[] array1 = new double[10]; A. It will allow valid subscripts in the range of 0 through 9. B. It declares array1 to be a reference to an array of double values. C. It creates an instance of an array of ten double values. D. It does all of these.
it does all of these
Class objects normally have____that perform useful operations on their data, but primitive variables do not. A. fields B. methods C. instances D. relationships
methods
Which of the following expressions will generate a random number in the range of 1 through 10? A. myNumber = randomNumbers.nextInt(10) + 1; B. myNumber = randomNumbers.nextInt(1) + 10; C. myNumber = randomNumbers.nextInt(11) - 1; D. myNumber = randomNumbers.nextInt(10);
myNumber = randomNumbers.nextInt(10) + 1;
Select all that apply. Which method of the Random class will return a random number within the range of 0.0 and 1.0? Select one or more: A. nextFloat() B. nextDouble() C. nextInt(int n) D. nextLong()
nextFloat() nextDouble()
Which Scanner class method reads a String? A. nextLine B. charAt C. getLine D. nextString
nextLine
Using the blueprint/house analogy, you can think of a class as a blueprint that describes a house and____as instances of the house built from the blueprint. A. attributes B. methods C. fields D. objects
objects
A group of related classes is called a(n): A. collection B. package C. attachment D. archive
package
A constructor: A. performs initialization or setup operations B. never receives any arguments C. returns an object of the class D. removes the object from memory
performs initialization or setup operations
A ________ loop will always be executed at least once. A. conditional B. posttest C. pretest D. user-controlled
posttest
A cross between human language and a programming language is called: A. pseudocode B. the Java language C. the Java Virtual Machine D. a compiler
pseudocode
________ operators are used to determine whether a specific relationship exists between two values. A. Logical B. Relational C. Arithmetic D. Assignment
relational
The ________ method removes an item from an ArrayList at a specific index. A. clear B. remove C. pop D. deleteAt
remove
A ________ is a value that signals when the end of a list of values has been reached. A. sentinel B. terminal C. token D. delimiter
sentinel
You can use the ________ method to replace an item at a specific location in an ArrayList. A. set B. remove C. add D. replace
set
Before entering a loop to compute a running total, the program should first A. read all the values into main memory. B. set the accumulator variable to an initial value, often zero. C. know exactly how many values there are to total. D. set all variables to zero.
set the accumulator variable to an initial value, often zero.
A(n)________is used as an index to pinpoint a specific element within an array. A. element B. range C. subscript D. boolean value
subscript
In the following Java statement, what value is stored in the variable name? String name = "John Doe"; A. "name" B. the memory address where name is located C. John Doe D. the memory address where "John Doe" is located
the memory address where "John Doe" is located
When an argument is passed by value: A. changes can be made to the argument variable B. the parameter variable cannot be changed C. the parameter variable holds a copy of the value passed to it D. the parameter variable holds the address of the argument
the parameter variable holds the address of the argument
In an if-else statement, if the boolean expression is false: A. the statement or block following the else is executed. B. all the statements or blocks are executed. C. the first statement or block is executed. D. no statements or blocks are executed.
the statement or block following the else is executed.
To indicate the data type of a variable in a UML diagram you specify: A. the variable followed by the data type B. the data type followed by the variable name C. the variable name followed by a colon and the data type D. the class name followed by the variable name followed by the data type
the variable name followed by a colon and the data type
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. the z field is available to code written outside the Sphere class. B. the x field is available to code that is written outside the Sphere class. C. The radius, x, y, and z fields are members of the Sphere class. D. The radius field is not available to code written outside the Sphere class.
the z field is available to code written outside the Sphere class.
Which of the following methods returns a string representing all of the items stored in an ArrayList object? A. show B. toString C. getList D. print
toString
A flag may have the values A. true or false. B. of any Unicode character. C. of any range of integers. D. defined or undefined.
true or false
The boolean expression in an if statement must evaluate to A. true or false. B. left or right. C. positive or negative. D. degrees or radians.
true or false
The primitive data types only allow a(n)____to hold a single value: A. class B. literal C. object D. variable
variable
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.EOF) B. while (inputFile.nextLine == " ") C. while (inputFile != null) D. while (inputFile.hasNext())
while (inputFile.hasNext())
Key words are: A. words that have a special meaning in the programming language. B. symbols or words that perform operations on one or more operands. C. words or characters representing values that are defined by the programmer. D. the data names in your program.
words that have a special meaning in the programming language.
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
x <= y
Which of the following statements will correctly convert the data type, if x is a float and y is a double? A. x = y; B. x = float y; C. x = <float>y; D. x = (float)y;
x = (float)y;
What will be the values of x and y as a result of the following code? int x=25, y=8; x += y++ A. x = 34, y = 9 B. x = 25, y = 8 C. x = 33, y = 8 D. x = 33, y = 9
x = 33, y = 9
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); A. x = 160, y= 80 B. x = 32, y =4 C. x = 9, y = 52 D. x = 37, y = 5
x = 37, y=5
What would be displayed as a result of executing the following code? int x=15, y=20, z=32; x += 12; y /= 6; z -= 14; System.out.println("x=" + x + " y=" + y + " z=" + z); A. x=27, y=3.333, z=18 B. x=37, y=-14, z=4 C. x=27, y=3, z=18 D. x=27, y=2, z=18
x=27, y=3, z=18
Which symbol indicates that a member is public in a UML diagram: A. - B. * C. + D. #
+
Which symbol indicates that a member is private a UML diagram? A. - B. # C. + D. *
-
What will be the value of discountRate after the following statements are executed? double discountRate = 0.0; int purchase = 100; if (purchase > 1000) discountRate = 0.05; else if (purchase > 750) discountRate = 0.03; else if (purchase > 500) discountRate = 0.01; A. 0.0 B. 0.05 C. 0.01 D. 0.03
0.0
What will be the value of discountRate after the following statements are executed? double discountRate; char custType = 'B'; switch (custType) { case 'A': discountRate = 0.08; break; case 'B': discountRate = 0.06; case 'C': discountRate= = 0.04; default: discountRate = 0.0; } A. 0.06 B. 0.04 C. 0.0 D. 0.08
0.0
What will be the value of discountRate after the following statements are executed? double discountRate = 0.0; int purchase = 1250; char cust = 'N'; if (purchase > 1000) if (cust == 'Y') discountRate = 0.05; else discountRate = 0.04; else if (purchase > 750) if (cust == 'Y') discountRate = 0.04; else discountRate = 0.03; else discountRate = 0.0; A. 0.03 B. 0.05 C. 0.0 D. 0.04
0.04
What will be the value of bonus after the following statements are executed? int bonus, sales = 10000; if (sales < 5000) bonus = 200; else if (sales < 7500) bonus = 500; else if (sales < 10000) bonus = 750; else if (sales < 20000) bonus = 1250; A. 1000 B. 500 C. 1250 D. 750
1000
What will be displayed after the following statements are executed? int x=65; int y=55; if (x >= y) { int ans = x + y; } System.out.println(ans); A. 10 B. 100 C. 120 D. The code contains an error and will not compile.
120
What will be the value of x after the following code is executed? int x, y = 15; x = y-- A. 0 B. 16 C. 15 D. 14
15
What will be the value of x after the following statements are executed? int x = 75; int y = 60; if (x > y) x = x-y A. 135 B. 15 C. 60 D. 75
15
What will be the value of x after the following statements are executed? int x = 10; switch(x) { case 10: x += 15; case 12: x -= 5; default: x *= 3; } A. 20 B. 5 C. 30 D. 25
20
What will be the value of x after the following code is executed? int x=10, y=20; while (y<100) { x += y; y += 20; } A. 90 B. 130 C. 210 D. 110
210
What will be displayed after the following statements are executed? int y=10; if (y == 10) { int x = 30; x += y; System.out.println(x); } A. 20 B. 30 C. 40 D. The code contains an error and will not compile.
40
What will be the value of x after the following statements are executed? int x=10; for (int y=5; y<20; y += 5); x += y; A. 25 B. 50 C. 40 D. 30
40
How many times will the following do-while loop be executed? int x=11; do { x += 20; } while (x <= 100); A. 4 B. 1 C. 3 D. 5
5
What is the result of the following expression? 10 + 5 * 3 - 20 A. 5 B. -50 C. 25 D. -5
5
What is the value of z after the following code is executed? int x=5, y=28; float z; z = (float) (y/x); A. 3.0 B. 5.6 C. 5.60 D. 5.0
5.0
What is the result of the following expression? 17 % 3 * 2 - 12 + 15 A. 7 B. 12 C. 105 D. 8
7
What is the value of z after the following statements have been executed? int x=4, y=33; double z; z=(double) (y/x); A. 4 B. 8.0 C. 0 D. 8.25
8.0
Select all that apply. Which of the following steps is normally performed by a for loop? A. terminate when the control variable reaches its maximum or minimum value B. update the control variable during each iteration C. test the control variable by comparing it to a maximum or minimum value D. initialize the control variable to a starting value
All
When an array is passed to a method: A. the method has direct access to the original array. B. it is passed just as any other object would be passed. C. a reference to the array is passed. D. All of these are true.
All of these are true
What would be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5}; A. A compiler error will occur. B. The variable x will contain the values 0 through 5. C. An array of 6 values, ranging from 0 through 5 and referenced by the variable x will be created. D. An array of 6 values, all initialized to 0 and referenced by the variable x will be created.
An array of 6 values, ranging from 0 through 5 and referenced by the variable x will be created.
A methods that gets a value from a class's field but does not change it is known as a mutator methods. True False
False
In a for loop, the control variable is always incremented. True False
False
How many times will the following do-while loop be executed? int x=11; do { x += 20; } while (x > 100); A. 0 B. 4 C. 5 D. 1
1
What will be the value of x after the following code is executed? int x=10; while (x<100) { x += 100; } A. 10 B. 100 C. 90 D. 110
100
How many times will the following for loop be executed? for (int count=10; count<=21; count++) System.out.println("Java is great!"); A. 0 B. 10 C. 11 D. 12
12
what does <String> specify in the following statement? ArrayList<String> nameList = new ArrayList<String>(); A. It specifies that String objects may not be stored in the ArrayList object. B. It specifies that the ArrayList will be converted to a String array. C. It specifies that only String objects may be stored in the ArrayList object. D. It specifies that everything stored in the ArrayList object will be converted to a String object.
It specifies that only String objects may be stored in the ArrayList object.
What would be displayed as a result of executing the following code? final int x=22, y=4; y += x; System.outprintln("x=" + x + ",y+" + y) A. x=22, y=4 B. x=22, y=88 C. x=22, y=26 D. Nothing, there is an error in the code.
Nothing, there is an error in the code
A contructor is a method that is automatically called when an object is created. True False
True
A loop that repeats a specific number of times is known as a(n): A. conditional. B. count-controlled. C. pretest. D. infinite.
count-controlled.
The key word new: A. creates an object in memory B. creates a new variable in memory C. creates a new class D. creates a new Java byte code file
creates an object in memory
A characteristic of____is that only an object's methods are able to directly access and make changes to an object's data. A. data hiding B. classes C. component reusability D. procedures
data hiding
Variables are classified according to their A. locations B. names C. data types D. values
data types
A ________ is a boolean variable that signals when some condition exists in the program. A. flag B. block C. sentinel D. case
flag
The ________ loop is ideal in situations where the exact number of iterations is known. A. posttest B. for C. while D. do-while
for
A constructor: A. has the return type of void. B. always accepts two arguments. C. always has a private access specifier. D. has the same name as the class
has the same name as the class
The ________ statement is used to create a decision structure which allows a program to have more than one path of execution. A. null B. if C. flag D. block
if
Another term for an object of a class is a(n): A. method B. instance C. member D. access specifier
instance
When an object is created, the attributes associated with the object are called: A. instance methods B. class instances C. fixed attributes D. instance fields
instance fields
Methods that operate on an object's fields are called: A. private methods B. public methods C. instance variables D. instance methods
instance methods
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.integer(); B. int number = inputFile.readInt(); C. int number = inputFile.next(); D. int number = inputFile.nextInt();
int number = inputFile.nextInt();
A method: A. may have zero or more parameters B. may not have only one parameter variable C. must have at least two parameter variables D. never has parameter variables
may have zero or more parameters
Which method is used to determine the number of items stored in an ArrayList object? A. size B. volume C. listLength D. items
size
The scope of a local variable is: A. inside the parentheses of a method header B. inside the class but not inside any method C. the entire class D. the method in which it is defined
the method in which it is defined
An object typically hides its data but allows outside code access to: A. the data files. B. the pseudocode. C. private data members. D. the methods that operate on the data.
the methods that operate on the data