Final Exam Review: CHAPTER 1 1] A Java program is best

advertisement
Final Exam Review:
CHAPTER 1
1]
A Java program is best classified as
a) hardware
b) software
c) storage
d) processor
e) input
Answer: b. Explanation: Programs are classified as software to differentiate them from the mechanisms of
the computer (hardware). Storage and the processor are two forms of hardware while input is the information
that the program processes
2]
6 bits can be used to represent ___ distinct items or values
a) 6
b) 20
c) 24
d) 32
e) 64
Answer: e. Explanation: With n bits, we can represent 2n different values. 26 = 64.
3]
When executing a program, the processor reads each program instruction from
a) secondary memory (storage)
b) the Internet
c) registers stored in the processor
d) main memory
e) could be any of these
Answer: d. Explanation: The program is first loaded from secondary memory into main memory before it is
executed so that the processor is not slowed down when reading each instruction. This idea of executing
programs stored in memory is called the Stored Program Computer and was pioneered by Von Neumann in the
1940s.
4]
Which memory capacity is the largest?
a) 1,500,000,000,000 bytes
b) 100 gigabytes
c) 3,500,000 kilobytes
d) 10 terabyte
e) 12,000,000 megabytes
Answer: e. Explanation: We convert each of these capacities to bytes (rounding off) to compare them. The
value in a remains the same, 1 ½ trillion bytes. The value in b is 100 billion bytes. The value in c is 3 ½
billion bytes. The value in d is 10 trillion bytes. The answer in e is 12 trillion bytes.
5]
Volatility is a property of
a) RAM
b) ROM
c) disk
d) software
e) computer networks
Answer: a. Explanation: Volatility means that the contents of memory are lost if the electrical power is shut
off. This is true of RAM, but not ROM or disk. Software and computer networks are not forms of memory.
6]
The ability to directly obtain a stored item by referencing its address is known as
a) random access
b) sequential access
c) read-only access
d) fetch access
e) volatility
Answer: a. Random access is meant to convey that accessing any item is equally easy, and that any item is
retrievable based solely on its address. Random access is the form of access used by both RAM and ROM
memory. Disk access, called direct access, is a similar idea and direct and random access are sometimes
referred to synonymously. Sequential access is used by tape.
7]
In order for a computer to be accessible over a computer network, the computer needs its own
a) MODEM
b) Communication line
c) Network address
d) Packet
e) Router
Answer: c. Explanation: In order to differentiate between the computers on a network, each is given its own,
unique, network address. In this way, a message intended for one computer can be recognized by that
computer through the message’s destination address. A MODEM is a device that is used to allow a computer
to communicate to another computer over the telephone line. The communication line is the network media
itself. A packet is a piece of information being sent from over a network. A router is a hardware device used
to take a message from one network and move it to another based on the message’s destination address.
8]
A URL (Universal Resource Locator) specifies the address of
a) A computer on any network
b) A computer on the Internet
c) A local area network (LAN) on the Internet
d) A document or other type of file on the Internet
e) A java program on the Internet
Answer: d. URLs are used to locate documents (or other types of files such as an image or sound file)
anywhere on the Internet. The URL contains the address of the LAN or WAN and the specific computer from
which the file is to be retrieved, but specifies the file’s address, not just the computer’s address.
9]
It is important to dissect a problem into manageable pieces before trying to solve the problem because
a) most problems are too complex to be solved as a single, large activity
b) most problems are solved by multiple people and it is easy to assign each piece to a separate person
c) it is easier to integrate small pieces of a program into one program than it is to integrate one big chunk
of code into one program
d) our first solution may not solve the problem correctly
e) all of the above
Answer: a. Any interesting problem will be too complex to solve easily as a single activity. By decomposing
the problem, we can build small solutions to each piece and then integrate the pieces. Answer d is true, but is
not the reason why we will break a problem down into pieces.
10]
Once we have implemented the solution, we are not done with the problem because
a) the solution may not be the best (most efficient)
b) the solution may have errors and need testing and fixing before we are done
c) the solution may, at a later date, need revising to handle new specifications
d) the solution may, at a later date, need revising because of new programming language features
e) all of the above
Answer: e. Explanation: A program should not be considered as a finished product until we are reasonably
assured that it is efficient and error-free. Further, it is common that programs require modification in the future
because of a change to specifications or a change to the language or computer running the program.
CHAPTER 2:
Use the following class definition to answer questions 11-14.
public class Questions1_4
{
public static void main(String[ ] args)
{
System.out.print("Here");
System.out.println("There " + "Everywhere");
System.out.println("But not" + "in Texas");
}
}
11]
The program will print the word "Here" and then print
a) "There Everywhere" on the line after "Here"
b) "There" on the line after "Here" and "Everywhere" on the line after "There"
c) "There Everywhere" on the same line as "Here"
d) "ThereEverywhere" on the same line as "Here"
e) "ThereEverywhere" on the line after "Here"
Answer: c. Explanation: System.out.print will output the word "Here" but will leave the cursor at that point rather
than starting a new line. The next statement will output "There Everywhere" immediately after the word "Here".
Since there is a blank space within the quote marks for "There", there is a blank space inserted between "There" and
"Everywhere".
12]
The final println command will output
a) "But not in Texas"
b) "But notin Texas"
c) "But not" on one line and "in Texas" on the next line
d) "But not+in Texas"
e) "But not + in Texas"
Answer: b. Explanation: The “+” performs String concatenation, so that "But not" and "in Texas" are
concatenated together. Notice that there is no blank space after "not" or before "in" so that when they are
concatenated, they are placed together without a blank space.
13]
How many lines of output are provided by this program?
a) 1
b) 2
c) 3
d) 4
e) 5
Answer: b. Explanation: There will be one line of output for the first two statements combined because the print
statement does not return the cursor to start a new line. And since the second statement is a println, it returns the
cursor and the last println outputs its message on a separate line.
14]
A reasonable comment for this program might be
a) // a program that demonstrates the differences between print, println and how + works
b) // a program that outputs a message about Texas
c) // a program that demonstrates nothing at all
d) // a program that outputs the message “Here There Everywhere But not in Texas”
e) // a program that has three output statements in it
Answer: a. Explanation: Remember that comments should not state the obvious (ruling out d and e) but instead
should explain what the program is doing or why. This program demonstrates print and println and +.
15]
Consider the following statement:
System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night");
This statement will output ___ lines of text
a) 1
b) 2
c) 3
d) 4
e) 5
Answer: b. Explanation: The \t escape sequence inserts a tab, but leaves the cursor on the same line. The \n
escape sequence causes a new line to be produced so that “4 dinner” is output on the next line. The escape
sequence \r causes the carriage to return (that is, the cursor to be moved back to the left margin) but because it does
not start a new line, “2night” is output over “4 dinn” resulting in a second line that looks like “2nighter”.
16]
If you want to output the text "hi there", including the quote marks, which of the following could do that?
a) System.out.println("hi there");
b) System.out.println(""hi there"");
c) System.out.println("\"hi there");
d) System.out.println("\"hi there\"");
e) none, it is not possible to output a quote mark because it is used to mark the beginning and ending of
the String to be output.
Answer: d. Explanation: \" is an escape sequence used to place a quote mark in a String, so it is used here to
output the quote marks with the rest of the String.
17]
Of the following types, which one cannot store a numeric value?
a) int
b) double
c) char
d) all of these can store numeric values
e) none of these can store numeric values
Answer: c. Explanation: int is used to store whole numbers (integers) and double is used to store a real or floating
point value (value with a decimal point). A char stores a single character including letters, punctuation marks and
digits. However, storing the numeric digit ‘5’ is not the same as storing the number 5.
18]
What value will z have if we execute the following assignment statement?
double z = 5 / 10;
a) z will equal 0.0
b) z will equal 0.5
c) z will equal 5.0
d) z will equal 0.05
e) none of the above, a run-time error arises because z is a double and 5 / 10 is an int
Answer: a. Explanation: 5 and 10 are both int values, so 5 / 10 is an integer division. The result is 0. Even
though z is a double and can store the real answer, 0.5, it only gets 0 because of the integer division. In order to get
0.5, we would have to first cast 5 or 10 as a double.
19]
What value will z have if we execute the following assignment statement?
int z = 50 / 10.00;
a) 5
b) 5.0
c) 50
d) 10
e) none of the above, a run-time error arises because z is an int and 50 / 10.00 is not
Answer: e. Explanation: Because 10.00 is not an int, the division produces a double precision value which cannot
be stored in the int z. For this to work, the result of the division must be cast as an int before being stored in z, or
the value 10.00 would have to first be cast as an int before the division takes place.
20]
A cast is required in which of the following situations?
a) using charAt to take an element of a String and store it in a char
b) storing an int in a double
c) storing a double in a double
d) storing a double in an int
e) all of the above require casts
Answer: d. Explanation: For a, charAt returns a char, so there is no problem. In b, the situation is a widening
operation taking a narrower type and storing the value in a wider type. Only in d is there a situation where a wider
type is being stored in a narrower type, so a cast is required.
CHAPTER 3:
21]
During program development, software requirements specify
a) how the program will accomplish the task
b) what the task is that the program must perform
c) how to divide the task into subtasks
d) how to test the program when it is done
e) all of the above
Answer: b. Explanation: The specification phase is to understand the problem at hand so that the programmer can
determine what needs to be done to solve the problem. The other efforts listed above are part of the design phase
(a, c) and testing phase (d).
22]
In which phase of program development would you expect the programmer(s) to determine the classes and
objects needed?
a) Software requirements
b) Software design
c) Software implementation
d) Software testing
e) Could occur in any of the above
Answer: b. Explanation: Determining which classes and objects to use or create is part of the design.
23]
Which of the following would not be considered an algorithm?
a) a recipe
b) a computer program
c) pseudocode
d) a shopping list
e) travel directions
Answer: d. Explanation: An algorithm is a step-by-step description of how to solve a problem, written at some
level of specificity. The recipe and probably the pseudocode are written at a “high level” whereas a program and
perhaps travel directions are written in more detail. A shopping list is not a step-by-step description, so it would
not qualify as an algorithm.
24]
If a programmer follows the four phases of program development as intended, which of the four phases should
require the least amount of creativity?
a) Software requirements
b) Software design
c) Software implementation
d) Software testing
e) None of the above, all four levels would require equal creativity
Answer: c. Explanation: Once the implementation phase has been reached, the algorithm should have already
been specified, so the only effort involved in the implementation phase is of translating from the design (which is
probably in an English-like pseudocode) to the programming language, and entering the code through an editor.
The requirements and design phases require understanding the problem and coming up with a solution respectively,
requiring creativity, and the testing phase will require diagnostic abilities usually forcing the programmer(s) to be
creative in how the errors are found and fixed.
25]
The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional
statement is known as
a) boolean execution
b) conditional statements
c) try and catch
d) sequentiality
e) flow of control
Answer: e. Explanation: The “flow of control” describes the order of execution of instructions. It defaults to
being linear (or sequential) but is altered by using control statements like conditionals and loops.
26]
Of the following if statements, which one correctly executes three instructions if the condition is true?
a) if (x < 0)
a = b * 2;
y = x;
z = a – y;
b) {
if (x < 0)
a = b * 2;
y = x;
z = a – y;
}
c) if { (x < 0)
a = b * 2;
y = x;
z=a–y;
}
d) if (x < 0)
{
a = b * 2;
y = x;
z = a – y;
}
e) b, c and d are all correct, but not a
Answer: d. Explanation: In order to have three instructions execute in the if clause when the condition is true, the
three statements must be placed in a block following the condition. This is the syntax used in d. In a, there is no
block. In b, the block is placed around the entire if statement such that the if clause is only a = b * 2; and the other
two statements are not part of the if statement, but follow it. The syntax in c is illegal resulting in a syntax error.
Don’t forget that the structure of your code (how it lines up) is immaterial to the compiler.
27]
Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but
leave x alone if x is 0?
a) if (x > 0) x++;
else x--;
b) if (x > 0) x++;
else if (x < 0) x--;
c) if (x > 0) x++;
if (x < 0) x--;
else x = 0;
d) if (x == 0) x = 0;
else x++;
x--;
e) x++;
x--;
Answer: b. Explanation: if x is positive, x++ is performed else if x is negative x-- is performed and otherwise,
nothing happens, or x is unaffected. In a, c, d and e, the logic is incorrect. In a, x-- is done if x is not positive, thus
if x is 0, x becomes –1 which is the wrong answer. In c, if x is positive, then x++ is performed. In either case, the
next statement is executed and if x is not negative, the else clause is performed setting x to 0. So if x is positive, it
becomes 0 after this set of code. In d, x++ and x-- are both performed if x is not 0. And in e, this code does not
attempt to determine if x is positive or negative, it just adds one and then subtracts 1 from x leaving x the same.
Given the nested if-else structure below, answer questions 28-30.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
28]
If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
a) 0
b) 2
c) 3
d) 4
e) 5
Answer: c. Explanation: Since (a > 0) is true, the next condition is checked. Since (b < 0) is false, the else clause
for this condition is executed. Since (a > 5) is false the else clause for this condition is executed, which is x = x + 3.
Therefore, 3 is added to x, so it is now 3.
29]
If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
a) 0
b) 2
c) 3
d) 4
e) 5
Answer: b. Explanation: Since (a > 0) is not true, the else clause for this condition is executed, which is x = x +
2, so x is now 2.
30]
If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
a) 0
b) 2
c) 3
d) 4
e) 5
Answer: e. Explanation: Since (a > 0) is true, the if clause is executed, which is another if statement. Its condition
(b < 0) is true, so its if clause is executed, which is x = x + 5, so x is now 5.
CHAPTER 4:
31]
The behavior of an object is defined by the object’s
a) instance data
b) constructor
c) visibility modifiers
d) methods
e) all of the above
Answer: d. Explanation: The methods dictate how the object reacts when it is passed messages. Each message is
implemented as a method, and the method is the code that executes when the message is passed. The constructor is one
of these methods but all of the methods combine dictate the behavior. The visibility modifiers do impact the object’s
performance indirectly.
32]
The relationship between a class and an object is best described as
a) classes are instances of objects
b) objects are instances of classes
c) objects and classes are the same thing
d) classes are programs while objects are variables
e) objects are the instance data of classes
Answer: b. Classes are definitions of program entities that represent classes of things/entities in the world. Class
definitions include instance data and methods. To use a class, it is instantiated. These instances are known as objects.
So, objects are instances of classes. Program code directly interacts with objects, not classes.
33]
To define a class that will represent a car, which of the following definitions is most appropriate?
a) private class car
b) public class car
c) public class Car
d) public class CAR
e) private class Car
Answer: c. Explanation: Classes should be defined to be public so that they can be accessed by other classes. And
following Java naming convention, class names should start with a capital letter and be lower case except for the
beginning of each new word, so Car is more appropriate than car or CAR.
34]
Which of the following reserved words in Java is used to create an instance of a class?
a) class
b) public
c) public or private, either could be used
d) import
e) new
Answer: e. Explanation: The reserved word “new” is used to instantiate an object, that is, to create an instance of a
class. The statement new is followed by the name of the class. This calls the class’ constructor. Example: Car x = new
Car( ); will create a new instance of a Car and set the variable x to it.
35]
In order to preserve encapsulation of an object, we would do all of the following except for which one?
a) Make the instance data private
b) Define the methods in the class to access and manipulate the instance data
c) Make the methods of the class public
d) Make the class final
e) All of the above preserve encapsulation
Answer: d. Explanation: Encapsulation means that the class contains both the data and the methods needed to
manipulate the data. In order to preserve encapsulation properly, the instance data should not be directly accessible
from outside of the classes, so the instance data are made private and methods are defined to access and manipulate the
instance data. Further, the methods to access and manipulate the instance data are made public so that other classes can
use the object. The reserved word “final” is usedto control inheritance and has nothing to do with encapsulation.
36]
If a method does not have a return statement, then
a) it will produce a syntax error when compiled
b) it must be a void method
c) it can not be called from outside the class that defined the method
d) it must be defined to be a public method
e) it must be an int, double, or String method
Answer: b. Explanation: All methods are implied to return something and therefore there must be a return statement.
However, if the programmer wishes to write a method that does not return anything, and therefore does not need a
return statement, then it must be a void method (a method whose header has “void” as its return type).
37]
Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls
m4, m3 calls m5. If m4 has just terminated, what method will resume execution?
a) m1
b) m2
c) m3
d) m5
e) main
Answer: b. Explanation: Once a method terminates, control resumes with the method that called that method. In this
case, m2 calls m4, so that when m4 terminates, m2 is resumed.
38]
A variable whose scope is restricted to the method where it was declared is known as a(n)
a) parameter
b) global variable
c) local variable
d) public instance data
e) private instance data
Answer: c. Explanation: Local variables are those that are “local” to the method in which they have been declared,
that is, they are accessible only inside that method. Global variables are those that are accessible from anywhere, while
parameters are the variables passed into a method. Instance data can be thought of as global variables for an entire
object.
39]
A class’ constructor usually defines
a) how an object is initialized
b) how an object is interfaced
c) the number of instance data in the class
d) the number of methods in the class
e) if the instance data are accessible outside of the object directly
Answer: a. Explanation: The constructor should be used to “construct” the object, that is, to set up the initial values of
the instance data. This is not essential, but is typically done. The interface of an object is dictated by the visibility
modifiers used on the instance data and methods.
40]
Having multiple class methods of the same name where each method has a different number of or type of
parameters is known as
a) encapsulation
b) information hiding
c) tokenizing
d) importing
e) method overloading
Answer: e. Explanation: When methods share the same name, they are said to be overloaded. The number and type of
parameters passed in the message provides the information by which the proper method is called.
CHAPTER 5:
For questions 41-44, assume x and y are String variables with x = "Hello" and y = null.
41]
The result of (x = = y) is
a) true
b) false
c) a syntax error
d) a run-time error
e) x being set to the value null
Answer: b. Explanation: x is a String instantiated to the value "Hello" and y is a String that has not yet been
instantiated, so they are not the same String. (x = = y) is a condition, testing to see if x and y are the same String (that
is, x and y reference the same item in memory), which they don't, so the result is false.
42]
The result of x.length( ) + y.length( ) is
a) 0
b) 5
c) 6
d) 10
e) a thrown exception
Answer: e. Explanation: The statement y.length( ) results in a thrown NullPointException because it is not possible to
pass a message to an object that is not currently instantiated (equal to null).
43]
If the operation y = "Hello"; is performed, then the result of (x = = y) is
a) true
b) false
c) x and y becoming aliases
d) x being set to the value null
e) a run-time error
Answer: b. Explanation: While x and y now store the same value, they are not the same String, that is, x and y
reference different objects in memory, so the result of the condition (x = = y) is false.
44]
If the operation y = x; is performed, then the result of (x = = y) is
a) true
b) false
c) x being set to the value null while y retains the value "Hello"
d) y being set to the value null while x retains the value "Hello"
e) x being set to y, which it is already since y = x; was already performed
Answer: a. Explanation: When y = x; was performed, the two variables are now aliases, that is, they reference the
same thing in memory. So, (x = = y) is now true.
For questions 45-48, consider a class that stores 2 int values. These values can be assigned int values with the messages
set1(x) and set2(x) where x is an int, and these values can be accessed through get1( ) and get2( ). Assume that y and z
are two objects of this class. The following instructions are executed:
y.set1(5);
y.set2(6);
z.set1(3);
z.set2(y.get1( ));
y = z;
45]
The statement z.get2( ); will
a) return 5
TB 48 Lewis/Loftus/Cocking: Chapter 5 Test Bank
b) return 6
c) return 3
d) return 0
e) cause a run-time error
Answer: a. Explanation: The statement y.get1( ) returns the value 5, and therefore the statement z.set2(y.get1( )) sets
the second value of z to be 5, so that z.get2( ) returns 5.
46]
The statement y.get2( ); will
a) return 5
b) return 6
c) return 3
d) return 0
e) cause a run-time error
Answer: a. Explanation: The statement y = z; causes y and z to be aliases where y.get2( ); returns the same value as
z.get2( );. Since z.set2(y.get1( )); was performed previously, z and y's second value is 5.
47]
If the instruction z.set2(y.get1( )); is executed, which of the following is true?
a) (y = = z) is still true
b) (y.get1( ) = = z.get1( )) but (y.get2( ) != z.get2( ))
c) (y.get1( ) = = z.get1( )) and (y.get2( ) = = z.get2( )) but (y != z)
d) (y.get1( ) = = z.get2( )) and (y.get2( ) = = z.get1( )) but (y != z)
e) the statement causes a run-time error
Answer: a. Explanation: Since y=z; was performed previously, y and z are aliases, so that a change to one results in a
change to the other. The statement z.set2(y.get1( )); is essentially the same as z.set2(z.get1( )); or y.set2(y.get1( )); and
in any case, it sets the second value to equal the first for the object which is referenced by both y and z.
48]
If the instructions z.set2(5); and y.set1(10); are performed, which of the following is true?
a) (y = = z) is still true
b) (y.get1( ) = = z.get1( )) but (y.get2( ) != z.get2( ))
c) (y.get1( ) = = z.get1( )) and (y.get2( ) = = z.get2( )) but (y != z)
d) (y.get1( ) = = z.get2( )) and (y.get2( ) = = z.get1( )) but (y != z)
e) this statement causes a run-time error
Answer: a. Explanation: Since y=z; was peformed previously, y and z are aliases meaning that they refer to the same
object. The statement z.set2(5); causes a change to the object's second value while y.set1(10); causes a change to the
object's first value but neither change the fact that y and z are aliases.
49]
Consider the following swap method. If String x = "Hello" and String y = "Goodbye", then swap(x, y); results
in which of the following?
public void swap(String a, String b)
{
String temp;
temp = a;
a = b;
b = temp;
}
a) x is now "Goodbye" and y is now "Hello"
b) x is now "Goodbye" and y is still "Goodbye", but (x != y)
c) x is still "Hello" and y is now "Hello", but (x != y)
d) x and y are now aliases
e) x and y remain unchanged
Answer: e. Explanation: When x and y are passed to swap, a and x become aliases and b and y become aliases. The
statement temp = a sets temp to be an alias of a and x. The statement a = b sets a to be an alias of b and y, but does not
alter x or temp. Finally, b = temp sets b to be an alias of temp and y, but does not alter y. Therefore, x and y remain the
same.
50]
Which of the following methods is a static method? The class in which the method is defined is given in
parentheses following the method name.
Lewis/Loftus/Cocking: Chapter 5 Test Bank TB 49
a) equals (String)
b) toUpperCase (String)
c) sqrt (Math)
d) format (DecimalFormat)
e) paint (Applet)
Answer: c. Explanation: The Math class defines all of its methods to be static. Invoking Math methods is done by
using Math rather than a variable of type Math. The other methods above are not static
CHAPTER 6:
For questions 51-54, assume values is an int array that is currently filled to capacity, with the following values:
9
4
12
2
6
8
18
51]
What is returned by values[3]?
a) 9
b) 12
c) 2
d) 6
e) 3
Answer: c. Explanation: Java array indices start at 0, so values[3] is really the fourth array element, which is 2.
52]
What is the value of values.length?
a) 0
b) 5
c) 6
d) 7
e) 18
Answer: d. Explanation: The length operator for an array returns the size of the array. The above picture shows that
that values stores 7 elements and since it is full, the size of values is 7.
53]
Which of the following loops would adequately add 1 to each element stored in values?
a) for(j=1;j<values.length;j++) values[j]++;
b) for(j=0;j<values.length;j++) values[j]++;
c) for(j=0;j<=values.length;j++) values[j]++;
d) for(j=0;j<values.length–1;j++) values[j]++;
e) for(j=1;j<values.length–1;j++) values[j]++;
Answer: b. Explanation: The first array element is values[0], so the for-loop must start at 0, not 1. There are
values.length elements in the array where the last element is at values.length–1, so the for loop must stop before
reaching values.length. This is the case in b. In d, the for loop stops 1 before values.length since “<” is being used to
test the condition instead of <=.
54]
The statement System.out.println(values[7]); will
a) output 7
b) output 18
c) output nothing
d) cause an ArrayOutOfBoundsException to be thrown
e) cause a syntax error
Answer: d. Explanation: The array has 7 values, but these are indexed values[0] to values[6]. Since values[7] is
beyond the bounds of the array, values[7] causes an ArrayOutOfBoundsException to be thrown.
55]
Which of the following is a legal way to declare and instantiate an array of 10 Strings?
a) String s = new String(10);
b) String[10] s = new String;
c) String[ ] s = new String[10];
d) String s = new String[10];
e) String[ ] s = new String;
9 4 12 2 6 8 18
Answer: c. Explanation: Declaring an array is done by type[ ] variable. Instantiating the array is done by variable =
new type[dimension] where dimension is the size of the array.
56]
In Java, arrays are
a) primitive data types
b) objects
c) interfaces
d) primitive data types if the type stored in the array is a primitive data type and objects if the type stored
in the array is an object
e) Strings
Answer: b. Explanation: In Java, arrays are implemented as objects. The variable is a reference variable to the block
of memory that stores the entire array. However, arrays are accessed using the notation name[index] rather than by
message passing.
57]
The “off-by-one” error associated with arrays arises because
a) the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index
too far
b) the last array index is at length + 1 and loops may only iterate to length, missing one
c) the last array element ends at length – 1 and loops may go one too far
d) programmers write a loop that goes from 0 to length – 1 whereas the array actually goes from 1 to
length
e) none of the above, the “off-by-one” error has nothing to do with arrays
Answer: a. Explanation: The array is initialized as = new type[x] where x is the size of the array. However, the array
has legal indices of 0 to x – 1 and so, programmers are often off-by-one because programmers will write code to try to
access indices 1 to x.
58]
What does the following code do? Assume list is an array of int values, temp is some previously initialized int
value, and c is an int initialized to 0.
for(j=0;j<list.length.j++)
if(list[j] < temp) c++;
a) It finds the smallest value and stores it in temp
b) It finds the largest value and stores it in temp
c) It counts the number of elements equal to the smallest value in list
d) It counts the number of elements in list that are less than temp
e) It sorts the values in list to be in ascending order
Answer: d. Explanation: The statement if(list[j]<temp) c++; compares each element in list to temp and adds one to c
only if the element is less than temp, so it counts the number of elements in list less than temp, storing this result in c.
An int array stores the following values. Use the array to answer questions 9 – 12.
59]
Which of the following lists of numbers would accurately show the array after the first pass through the Selection
Sort algorithm?
a) 9, 4, 12, 2, 6, 8, 18
b) 4, 9, 12, 2, 6, 8, 18
c) 2, 4, 12, 9, 6, 8, 18
d) 2, 4, 6, 8, 9, 12, 18
e) 2, 4, 9, 12, 6, 8, 18
Answer: c. Explanation: On each successive pass of Selection Sort, the smallest of the unsorted values is found and
swapped with the current array index (where the current index starts at 0 and goes until the second to last position in the
array). On the first pass, the smallest element, 2, is swapped with index 0, so 2 and 9 swap places.
9 4 12 2 6 8 18
60]
Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort
algorithm?
a) 9, 4, 12, 2, 6, 8, 18
b) 2, 4, 9, 6, 12, 8, 18
c) 2, 4, 12, 9, 6, 8, 18
d) 2, 4, 6, 8, 9, 12, 18
e) 2, 4, 12, 6, 8, 9, 18
Answer: c. Explanation: After one pass, the array would be 2, 4, 12, 9, 6, 8, 18. The second pass would look to swap
the item in array index 1 (4) with the smallest value after 2 (4). So, 4 would swap with 4 and the array would stay the
same as it was after the first pass.
Download