final java by Diana
how many times is the body of the loop below executed? int counter = 1; while(counter > 20) { // body of loop counter = counter - 1; } // end while
0
which statement created a random value from the sequence 2, 5, 8, 11, 14. Suppose randomNumbers is a Random object - 2 + 3 * randomNumbers.nextInt(5); - 2 + 5 * randomNumbers.nextInt(3); - 3 + 2 * randomNumbers.nextInt(5); - 5 + 3 * randomNumbers.nextInt(2);
2 + 3 * randomNumbers.nextInt(5);
consider the array: s[ 0 ] = 7 s[ 1 ] = 0 s[ 2 ] = -12 s[ 3 ] = 9 s[ 4 ] = 10 s[ 5 ] = 3 s[ 6 ] = 6 The value of s[ s[ 6 ] - s[ 5 ] ] is:
9
attempting to access an array element out of the bounds of an array causes a(n)
ArrayIndexOutOfBoundsException
In this question, assume a class, Book, has been defined; which set of statements creates an array of Book objects? - Book[ ] books; books = new Book[number elements]; - Book[ ] books]; books = new Book()[number elements]; - new Book() books[ ]; books = new Book[number elements]; - all are correct
Book[ ] books; books = new Book[number elements];
collections method sort that accepts a List as an argument sorts the elements of a List, which must implement the ____ interface
Comparable
if an ObjectInputStream is used to read information from the server, an ____ is generated when the client attempts to read a value from a stream on which end-of-stream is detected
EOFException
which method call converts the value in variable stringVariable to an integer? - Integer.parseInt(stringVariable) - Convert.toInt(stringVariable) - Convert.parseInt(stringVariable) - Integer.toInt(stringVariable)
Integer.parseInt(stringVariable)
a(n) ____ allows a program to walk through the collection and remove elements from the collection
Iterator
which class provides prebuilt dialog boxes that enable programs to display windows containing messages (such windows are called messaged dialogs) ?
JOptionPane
stacks are also known as ____ data structures
LIFO (last in, first out)
a(n) ____ is thrown when a String that is not in proper URL format is passed to a URL constructor
MalformedURLException
every class in Java, except ____, extends an existing class - Object - String - Integer - Class
Object
a(n) ____ enables a program to read data from the user
Scanner
____ sockets and the ____ protocol are more desirable for the vast majority of Java programmers
Stream, TCP
which of the following does not contain a syntax error? - System.out.println(Hello world!); - System.out.println("Hello world!"); - System.out.println('Hello world!"); - System.out.printlin("Hello world!");
System.out.println("Hello world!");
a(n) ____ is thrown when a server address indicated by a client cannot be resolved
UnknownHostException
which statement is false
a List cannot contain duplicate elements
which of the following statements about the continue statement is true? - a continue statement is used to exit a repetition structure early and continue execution after the loop - the continue statement is used to continue after a switch statement - the continue statement does not alter the flow of control - a continue statement proceeds with the next iteration of the immediately enclosing while, for, do...while statement
a continue statement proceeds with the next iteration of the immediately enclosing while, for, do...while statement
all of the following methods are implicitly final except - a method in an abstract class - a static method - a method declared in a final class - a private method
a method in an abstract class
which statement is false?
a non-generic class cannot be derived from a generic class
which statement best describes the relationship between superclass and subclass types?
a subclass reference can be assigned to a superclass variable, but a superclass variable reference cannot be assigned to a subclass variable
a(n) ____ class cannot be instantiated - abstract - concrete - final - polymorphic
abstract
which of the following statements about abstract superclasses is true? - abstract superclasses must declare all methods as abstract - abstract superclasses may not contain implementations of methods - abstract superclasses must declare all data members not given as values abstract - abstract superclasses may contain data
abstract superclasses may contain data
once the ServerSocket is created, the server can listen indefinitely (or block) for an attempt by a client to connect. this is accomplished with a call to the server ServerSocket method ____
accept
when should a program explicitly use the 'this' reference?
accessing a field that is shadowed by a local variable
having a 'this' reference allows: - an object to reference itself - a method to refer explicitly to the instance variables and other methods of the object on which the method was called - a method to refer implicitly to the instance variables and other methods of the object on which the method was called - all are correct
all are correct
a package is: - a group of related classes and interfaces - a directory structure used to organize classes and interfaces - a mechanism for software reuse - all of the above
all of the above
for the code segment below: switch(q) { case 1: System.out.println( "apple" ); break; case 2: System.out.println( "orange" ); break; case 3: System.out.println( "banana" ); break; case 4: System.out.println( "pear" ); case 5: System.out.println( "grapes" ); default: System.out.println( "kiwi" ); } which of the following values for q will result in kiwi being included in the output? - anything greater than or equal to 4 - 3 - 1 - 2
anything greater than or equal to 4
static class variables: - are public - are shared by all objects of a class - are private - are final
are shared by all objects of a class
instance variables declared final do not or cannot: - cause syntax errors if used as a left-hand value - be modified - be initialized - none are correct
be modified
all import declarations must be placed:
before the class declaration
Consider the abstract superclass below: public abstract class Foo { private int a; public int b; public Foo( int aVal, int bVal ) { a = aVal; b = bVal; } // end Foo constructor public abstract int calculate(); } // end class Foo Any concrete subclass that extends class Foo: - must implement a method called calculate - will not be able to access the instance variable a - both are correct - neither are correct
both are correct
in a class containing methods with the same name. the methods are distinguished by: - types of arguments - return type - number of arguments - both types of arguments and return type are correct - both number and types of arguments are correct
both number and types of arguments are correct
To exit out of a loop completely, and resume the flow of control at the next line in the method, use ____ - continue - return - break - all are correct
break
which statement is false? normally, an applet ____
can read files on any server that can be reached over the network
the import declaration import *; - imports the classes in package java.lang - imports all classes in the library - causes a compilation error - imports the default classes in the library
causes a compilation error
all exception classes inherit, either directly or indirectly, from:
class Throwable
which of the following statements is true? - none - interpreted programs run faster than compiled programs - compilers translate high-level language programs into machine language programs - interpreter programs typically use machine language as input
compilers translate high-level language programs into machine language programs
non-abstract classes are called - real classes - concrete classes - instance classes - implementable classes
concrete classes
what do the following statements do? double[ ] array; array = new double[14]; - create a double array containing 13 elements - creating a double array containing 15 elements - declare but do not create a double array - create a double array containing 14 elements
create a double array containing 14 elements
a programmer must do the following before using an array:
declare then create the array
what is the difference between a float and a double?
double variables store numbers with larger magnitude and finer detail
when the compiler translates a generic method into Java bytecodes, it uses ____ to replace the type parameters with actual types
erasure
polymorphism allows for specifics to be dealt with during - programming - execution - compilation - debugging
execution
T/F: UDP is a connection-oriented protocol
false
attributes of a class are also known as
fields
arrays are:
fixed-length entities
which of the following will count from 10 to 1 correctly? - for(int j = 1; j <= 10; j++) - for(int j = 10; j > 1; j--) - for(int j = 10; j <= 1; j++) - for(int j = 10; j >= 1; j--)
for(int j = 10; j >= 1; j--)
____ enable programmers to specify, with a single method declaration, a set of related methods
generic methods
when no access modifier is specified for a method or variable, the method or variable: - is private - is static - is public - has package access
has package access
an overloaded method is one that: - has the same name and parameters, but a different return type as another method - has the same name as another method, but has different parameters (by number, types, or order of the types) - has the same name and parameters as a method defined in another class - has a different name than another method, but the same parameters
has the same name as another method, but has different parameters (by number, types, or order of the types)
consider the code segment below. if(gender == 1) { if(age >= 65) { ++seniorFemales; }} this segment is equivalent to which of the following? - if(gender == 1 OR age >= 65) { ++seniorFemales;} - if(gender == 1 && age >= 65) { ++seniorFemales;} - if(gender == 1 || age >= 65) { ++seniorFemales;} - if(gender == 1 AND age >= 65) { ++seniorFemales;}
if(gender == 1 && age >= 65) { ++seniorFemales;}
which keyword is used to specify that a class will define the methods of an interface? - extends - uses - implements - defines
implements
failure to prefix the superclass method name with the keyword super and a dot (.) separator when referencing the superclass' method causes ____ - a syntax error - a runtime error - infinite recursion - a compile-time error
infinite recursion
which of the following statements is false? - information in the memory unit is persistent; it is retained when the computer's power is turned off - playing a video is an example of output - a multi-core processor implements several processors on a single integrated-circuit chip - speaking to your computer is a form of input
information in the memory unit is persistent; it is retained when the computer's power is turned off
the control variable of a counter-controlled loop should be declared as ____ to prevent errors
int
an uncaught exception:
is an exception that occurs for which there are no matching clauses
declaring a method final means - it cannot be overridden - it cannot be overloaded - it will prepare the object for garbage collection - it cannot be accesses from outside its class
it cannot be overridden
which of the following is not true of ExecutorService?
it is an object that can be run in a separate thread
a class within a package must be declared public if
it will be used by classes that are not in the same package
which command executes the Java class file Welcome.class?
java Welcome
the classes and interfaces which comprise the collections framework are members of package ____
java.util
which command compiles the Java source code file Welcome.java?
javac Welcome.java
the main method executes in the ____ thread of execution
main
a new thread begins its life cycle by transitioning to the ____ state
new
consider the following two Java code segments: Segment 1: int i = 0; while ( i < 20 ) { i++; System.out.println(i); } Segment 2: for(int i = 0; i <= 20; i++) { System.out.println(i); } is the output the same?
no
what is output by the following Java code segment? : int temp = 180; if(temp > 90) { System.out.println("too hot") // cool down temp = temp - (temp > 150 ? 100:20); } else { if(temp < 70) { System.out.println("too cold") // warm up temp = temp + (temp < 50 ? 30:20); }} if(temp == 80) { System.out.println("just right");}
none of the three messages
what is the default value of a reference?
null
____ models software in terms similar to those that people use to describe real-world objects
object-oriented design
which of the following is false? - method finalize does not take parameters and has return type void - the garbage collector reclaims unused memory - memory leaks using java are rare because of automatic garbage collection - objects are marked for garbage collection by method finalize
objects are marked for garbage collection by method finalize
an advantage of inheritance is that: - objects of a subclass can be treated like objects of their superclass - all instance variables can be uniformly accessed by subclasses and superclasses - all methods can be inherited - none are correct
objects of a subclass can be treated like objects of their superclass
overriding a method differs from overloading a method because: - overridden methods have the same signature - overloaded methods have the same signature - neither is correct - both are correct
overridden methods have the same signature
using the protected keyword gives a member: - private access - public access - block scope - package access
package access
one generic stack class could be the basis for creating many Stack classes, e.g., Stack <Double>, Stack <Integer>, and Stack <Employee>. These classes are known as ____
parameterized classes
a well-designed method: - repeats code found in other methods - performs a single, well-defined task - contains thousands of lines of code - performs multiple unrelated tasks
performs a single, well-defined task
the collections framework algorithms are ____, i.e., each of these algorithms can operate on objects that offer given interfaces without concern to the underlying implementations
polymorphic
superclass methods with this level of access cannot be called from subclasses
private
polymorphism enables you to - absorb attributes and behavior from previous classes - hide information from the user - program in the general - program in the specific
program in the general
which superclass members are inherited by all subclasses of that superclass? - protected instance variables and methods - private constructors - private instance variables and methods - protected constructors
protected instance variables and methods
every Java application is composed of at least one:
public class declaration
when a generic class is instantiated without specifying a type argument, it is said to have a ____
raw type
which of the following is not a Java primitive type? - double - byte - real - char
real
Consider the program below: public class Test { public static void main(String[] args) { int[] a; a = new int[ 10 ]; for(int i = 0; i < a.length; i++) { a[ i ] = i + 2; } int result = 0; for(int i = 0; i < a.length; i++) { result += a[i]; } System.out.printf("Result is: %d\n", result); } // end main } // end class Test The output of this program will be: - result is: 62 - result is: 65 - result is: 67 - result is: 64
result is: 65
to declare a method as static, place the keyword static before ____ in the method's declaration - method name - argument list - method modifier - return type
return type
the preferred means of creating multithreaded Java applications is by implementing the ____ interface. an object of a class that implements this interface represents a task to perform.
runnable
the default implementation of method clone of Object performs a ____ - shallow copy - full copy - deep copy - empty copy
shallow copy
using a URL as an argument to the ____ method of interface AppletContext causes the browser in which an applet is executing to display the URL
showDocument
declaring main as ____ allows the JVM to invoke main without creating an instance of the class - static - final - void - public
static
which of the following keywords allows a subclass to access a superclass method even when the subclass has overridden the superclass method? - base - this - public - super
super
when a ____ method or block is running on an object, the object is locked so no other suck method can run on that object at the same time
synchronized
which statement correctly passes the array items to method takeArray? Array items contain ten elements. - takeArray(items) - takeArray(items[ ]) - takeArray(items[9]) - arrays cannot be passed to methods; each item must be sent separately
takeArray(items)
which statement is not true? - the Java API consists of packages - the class javax.swing.JApplet is part of the Java API - the JAVA API consists of import declarations - the Java API helps programmers avoid "reinventing the wheel"
the Java API consists of import declarations
which of the following statements about a do...while repetition statement is true? - the body of a do...while loop is executed only once - none - the body of a do...while loop is always executed at least once - the body of a do...while loop is executed only if the terminating condition is true
the body of a do...while loop is always executed at least once
which of the following statements about the break statement is false? - the break statement, when executed in a while, for, or do...while, skips the remaining statements in he loop body and proceeds with the next iteration of the loop - common uses of the break statement are to escape early from a loop or to skip the remainder of a switch - a break statement can only break out of an immediately enclosing while, for, do...while, or switch statement - the break statement is used to exit a repetition structure early and continue execution after the loop
the break statement, when executed in a while, for, or do...while, skips the remaining statements in he loop body and proceeds with the next iteration of the loop
suppose variable gender is MALE and age equals 60; how is the expression: (gender == FEMALE) && (age >= 65) evaluated? - both conditions are evaluated, from left to right - the condition (gender == FEMALE) is evaluated first and the evaluation stops immediately - both conditions are evaluated, from right to left - the condition (age >= 65) is evaluated first and the evaluation stops immediately
the condition (gender == FEMALE) is evaluated first and the evaluation stops immediately
in the catch block below, what is arithmeticException? catch(ArithmeticException arithmeticException) { System.err.printf(arithmeticException); } // end catch
the name of catch block's exception parameter
when a subclass constructor calls its superclass constructor, what happens if the superclass' constructor does not assign a value to an instance variable? - the program compiles and runs because the instance variables are initialized to their default values - a compile-time error occurs - a syntax error occurs - a run-time error occurs
the program compiles and runs because the instance variables are initialized to their default values
which of the following statements about try blocks is true?
the try block should contain statements that may throw an exception
____ is a graphical language that allows people who design software systems to use an industry standard notation to represent them
the unified modeling language
what is output by the following Java code segment? : if(temp > 90) { System.out.println("too hot") }; if(temp < 70) { System.out.println("too cold") }; if(temp == 80) { System.out.println("just right") };
too hot
what is output by the following Java code segment? : int temp = 180; while ( temp != 80 ) { if(temp > 90) { System.out.print( "too hot!"); // cool down temp = temp - ( temp > 150 ? 100:20); } else { if ( temp < 70 ) { System.out.print("too cold!"); // warm up temp = temp + (temp < 50 ? 30:20);}}} if(temp == 80) { System.out.println("just right!");
too hot! just right!
to catch an exception, the code that might throw the exception must be enclosed in a
try block
end-of-line comments that should be ignored by the compiler are denoted using:
two forward slashes (//)
The preferred way to traverse a two-dimensional array is to use - three nested for statements - a for statement - two nested for statements - a do while statement
two nested for statements
a JEditorPane generates HyperlinkEvents only if it is ____
uneditable
which of the following should usually be private? - methods - constructors - variables or fields - all are correct
variables or fields
suppose method1 is declared as: void method1(int a, float b) which of the following methods correctly overloads method1? - void method2(float a, int b) - void method2(int a, float b) - void method1(int b, float a) - void method1(float 1, int b)
void method1(float 1, int b)
which statement is false?
with datagram sockets a process establishes a connection to another process
consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue(x,y); which of the following statements is false? - x and y are arguments - x and y are parameters - copies of x and y are passed to the method calculateValue - a method is called with its name and parentheses
x and y are parameters