Java Midterm 2 (Chapters 6,7,8)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which of the following should usually be private? a. a. Methods. b. b. Constructors. c. c. Variables (or fields). d. d. All of the above

Variables (or fields).

Which statement correctly passes the array items to method takeArray? Array items contains 10 elements.

b. takeArray( items ).

Which of the following methods is not in the Math class?

parseInt

The _________ of a class are also called the public services or the public interface that the class provides to its clients

public methods.

A final field should also be declared ________ if it is initialized in its declaration.

static.

To declare a method as static, place the keyword static before ________ in the method's declaration.

the return type.

The import declaration import java.util.*; is known as a ________

type-import-on-demand declaration.

Which expression adds 1 to the element of array arrayName at index i?

++arrayName[ i ].

Consider array items, which contains the values 0, 2, 4, 6 and 8. If method changeArray is called with the method call changeArray( items, items[ 2 ] ), what values are stored in items after the method has finished executing? public static void changeArray( int[] passedArray, int value ) { passedArray[ value ] = 12; value = 5; } // end method changeArray

- 0, 2, 4, 6, 12.

Q3: Which statement below could be used to simulate the outputs of rolling a six-sided die? Suppose randomNumbers is a Random object.

-1 + randomNumbers.nextInt( 6 );

Q1: Which statement creates a random value from the sequence 2, 5, 8, 11 and 14. Suppose randomNumbers is a Random object.

-2 + 3 * randomNumbers.nextInt( 5 );

8.11 Q2: Which of the following is false? a. a. A static method must be used to access private static instance variables. b. b. A static method has no this reference. c. c. A static method can be accessed even when no objects of its class have been instantiated. d. d. A static method can call instance methods directly.

-A static method can call instance methods directly.

Which of the following methods are overloaded with respect to one another? public int max ( int a, int b ) { ... } public double max ( double a, double b ) { ... } public int max ( int a, int b, int c ) { ... } public double max ( double a, double b, double c ) { ... } a. a. A and B are overloaded; C and D are overloaded. b. b. A and C are overloaded; B and D are overloaded. c. c. A, B and C are overloaded. d. d. All these four methods are overloaded.

-All these four methods are overloaded.

Which of the following statements about arrays are true?

-An array is a group of variables containing values that all have the same type. -Elements are located by index or subscript. -The zeroth element of array c is specified by c[ 0 ].

Which statement is false? a. a. An enum declaration is a comma-separated list of enum constants and may optionally include other components of traditional classes, such as constructors, fields and methods. b. b. Any attempt to create an object of an enum type with operator new results in a compilation error. c. c. An enum constructor cannot be overloaded. d. d. An enum constructor can specify any number of parameters.

-An enum constructor cannot be overloaded.

Instance variables declared final do not or cannot:

-Be modified.

Which of the following will not produce a compiler error? a. Changing the value of a constant after it is declared. b. Changing the value at a given index of an array after it is created. c. Using a final variable before it is initialized. d. All of the above will produce compiler errors.

-Changing the value at a given index of an array after it is created.

Which statement is false? a. a. The actual data representation used within the class is of no concern to the class's clients. b. b. Clients generally care about what the class does but not how the class does it. c. c. Clients are usually involved in a class's implementation. d. d. Hiding the implementation reduces the possibility that clients will become dependent on class-implementation details.

-Clients are usually involved in a class's implementation

Which of the following can be an argument to a method?

-Constants -Variables -Expressions

Arrays are:

-Fixed-length entities

When no access modifier is specified for a method or variable, the method or variable:

-Has package access.

Which statement is false? a. a. If a method does not return a value, the return-value-type in the method declaration can be omitted. b. b. Placing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error. c. c. Redeclaring a method parameter as a local variable in the method's body is a compilation error. d. d. Forgetting to return a value from a method that should return a value is a compilation error.

-If a method does not return a value, the return-value-type in the method declaration can be omitted.

Which of the following tasks cannot be performed using an enhanced for loop? a. Calculating the product of all the values in an array. b. Displaying all even element values in an array. c. Comparing the elements in an array to a specific value. d. Incrementing the value stored in each element of the array.

-Incrementing the value stored in each element of the array.

Which of the following statements describes block scope?

-It begins at the identifier's declaration and ends at the terminating right brace (}).

Stacks are known as ________ data structures.

-LIFO.

Programs designed for maintainability are constructed from small simple pieces or modules. Modules in Java are called:

-Methods -Classes

Which of the following statements is true? a. a. Methods and instance variables can both be either public or private. b. b. Information hiding is achieved by restricting access to class members via keyword public. c. c. The private members of a class are directly accessible to the client of a class. d. d. None of the above is true.

-Methods and instance variables can both be either public or private.

Invalid possibilities for array indices include:

-Negative integers.

Which of the following is false? a. a. Method finalize does not take parameters and has return type void. b. b. Memory leaks using Java are rare because of automatic garbage collection. c. c. Objects are marked for garbage collection by method finalize. d. d. The garbage collector reclaims unused memory.

-Objects are marked for garbage collection by method finalize. (Objects are marked for garbage collection when there are no more references to the object).

Which statement is not true. a. a. The Java API consists of packages. b. b. The Java API helps programmers avoid "reinventing the wheel." c. c. The Java API consists of import declarations. d. d. The class javax.swing.JApplet is part of the Java API.

-The Java API consists of import declarations. (The Java API is built from packages.)

Which of these statements best defines scope? a. a. Scope refers to the classes that have access to a variable. b. b. Scope determines whether a variable's value can be altered. c. c. Scoping allows the programmer to use a class without using its fully qualified name. d. d. Scope is the portion of a program that can refer to an entity by its simple name.

-scope is the portion of a program that can refer to an entity by its simple name

The classpath consists of a list of directories or archive files, each separated by a ________ on Windows or a ________ on UNIX/Linux/Max OS X.

-semicolon (;) -colon (:).

Q1: You can set a Random object's seed at any time during program execution by calling the object's ________ methods.

-setSeed.

A constructor cannot: a. a. be overloaded. b. b. initialize variables to their defaults. c. c. specify return types or return values. d. d. have the same name as the class.

-specify return types or return values.

If more method calls occur than can have their activation records stored on the program execution stack, an error known as a ________ occurs.

-stack overflow.

Declaring main as ________ allows the JVM to invoke main without creating an instance of the class.

-static.

Information is passed to a method in:

-the arguments to the method.

When an argument is passed by reference:

-the called method can access the argument's value in the caller directly and modify that data.

When an object is concatenated with a String:

-the object's toString method is implicitly called to obtain the String representation of the object.

Variables should be declared as fields only if:

-they are required for use in more than one method or their values must be saved between calls to the class's methods.

Math static method random generates a random double value in the range from 0.0

-up to but not including 1.0

Which method returns an array of the enum's constants?

-values.

Consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue( x, y );

-x and y are paramters.

Which flag in a format specifier indicates that values with fewer digits than the field width should begin with a leading 0?

0

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: a. Result is: 62. b. Result is: 64. c. Result is: 65. d. Result is: 67.

2 3 4 5 6 7 9 10 11 -Result 65

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

Consider the class below: public class Test { public static void main( String[] args ) { int[] a = { 99, 22, 11, 3, 11, 55, 44, 88, 2, -3 }; int result = 0; for ( int i = 0; i < a.length; i++ ) { if ( a[ i ] > 30 ) result += a[ i ]; } // end for System.out.printf( "Result is: %d\n", result ); } // end main } // end class Test The output of this Java program will be:

99 55 44 88 Result is: 286

What do the following statements do? double[] array; array = new double[ 14 ]

;-Create a double array containing 14 elements.

8.5 Q4: What happens when this is used in a constructor's body to call another constructor of the same class if that call is not the first statement in the constructor?

A compilation error occurs.

Attempting to access an array element out of the bounds of an array, causes a(n) . c.

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[ numberElements ];

Which is a correct static method call of Math class method sqrt?

Math.sqrt( 900 );.

Having a this reference allows:

-method to refer explicitly to the instance variables and other methods of the object on which the method was called -a method to refer to implicitly to the instance variables and other methods of the object on which the method was called -an object to reference itself

Q2: The identifiers in an enumeration ________. a. a. must be unique. b. b. may be duplicated. c. c. must be lowercase letters and cannot contain numbers. d. d. must be uppercase letters and cannot contain numbers.

-must be unique.

Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods.

-mutator -accessor.

Constant variables also are called

-named constants

The parameter list in the method header and the arguments in the method call must agree in:

-number -order -type

In a class containing methods with the same name, the methods are distinguished by:

-number of arguments -Types of arguments

A well-designed method:

-performs a single, well-defined task.

When implementing a method, use the class's set and get methods to access the class's ________ data.

-private.

Which statement below could be used to simulate the outputs of tossing a quarter to get heads or tails? Suppose randomNumbers is a Random object.

-randomNumbers.nextInt( 2 );

Method calls cannot be distinguished by ________.

-return type.

Which of the following statements is false? a. An exception indicates a problem that occurs while a program executes. b. Exception handling enables you to create fault-tolerant programs that can resolve (or handle) exceptions—in many cases, this allows a program to continue executing as if no problems were encountered. c. The catch block contains the code that might throw an exception, and the try block contains the code that handles the exception if one occurs. d. Inside the catch block, you can use the parameter's identifier to interact with a caught exception object.

-The catch block contains the code that might throw an exception, and the try block contains the code that handles the exception if one occurs.

Which statement is false? a. a. The compiler always creates a default constructor for a class. b. b. If a class's constructors all require arguments and a program attempts to call a no-argument constructor to initialize an object of the class, a compilation error occurs. c. c. A constructor can be called with no arguments only if the class does not have any constructors or if the class has a public no-argument constructor. d. d. None of the above.

-The compiler always creates a default constructor for a class.

Which of the following statements about creating arrays and initializing their elements is false? a. The new keyword should be used to create an array. b. When an array is created, the number of elements must be placed in square brackets following the type of element being stored. c. The elements of an array of integers have a value of null before they are initialized. d. A for loop is commonly used to set the values of the elements of an array.

-The elements of an array of integers have a value of null before they are initialized.

Using public set methods provides data integrity if:

-The instance variables are private -The methods perform validity checking

Consider the code segment below. Which of the following statements is false? int[] g; g = new int[ 23 ]; a. The first statement declares an array reference. b. The second statement creates the array. c. g is a reference to an array of integers. d. The value of g[ 3 ] is -1.

-The value of g[ 3 ] is -1.

A package is:

-a directory structure used to organize classes and interfaces -a mechanism for software reuse -a group of related classes and interfaces

A Java class can have which of the following methods? A. A. void foo( int a ) B. B. void foo( int a, int b ) C. C. void foo( double a ) D. D. void foo( double a, double b ) E. E. void foo( int b )

-all of the methods

Static class variables:

-are shared by all objects of a class.

A static method can ________

-call only other static methods of the same class directly -manipulate only static fields in the same class directly -be called using the class name and a dot(.)

The import declaration import *; ________.

-causes a compilation error.

Identifiers in Java have ________ and ________ scopes?

-class -block

-The java.text package contains classes for manipulating all of the following items except a. a. classes b. b. numbers c. c. strings d. d. characters

-classes

By default, the classpath consists only of the ________. However, the classpath can be modified by providing the ________ option to the javac compiler.

-current directory -classpath.

A programmer must do the following before using an array:

-declare then create the array.

Which of the following primitive types is never promoted to another type?

-double -boolean

Which of the following promotions of primitive types is not allowed to occur?

-double to float.

Method log takes the logarithm of its argument with respect to what base?

-e

Q1: An enumeration is a special class that's introduced by the keyword ________ and a type name.

-enum.

Filled rectangles and filled circles are drawn using Graphics method ________ and ________.

-fillRect -fillOval.

enum types are implicitly ________ and enum constants are implicitly ________

-final -static

Any field declared with keyword ________ is constant

-final.

Assume array items contains the integer values 0, 2, 4, 6 and 8. Which of the following set of statements uses the enhanced for loop to display each value in array items?

-for ( int i : items ) System.out.prinf( "%d\n", i );

The static method ________ of class String returns a formatted String.=

-format.

An overloaded method is one that:

-has the same name as another method, but different parameters (by number, types or order of the types).

Which syntax imports all static members of class Math?

-import static java.lang.Math.*.

Constructors:

-initialize instance variables -when overloaded, are selected by number, types, and order of types of parameters

Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4? a. values[ 3 ] = values[ 4 ]; values[ 4 ] = values[ 3 ]; b. values[ 4 ] = values[ 3 ]; values[ 3 ] = values[ 4 ]; c. int temp = values[ 3 ]; values[ 3 ] = values[ 4 ]; values[ 4 ] = temp; d. int temp = values[ 3 ]; values[ 3 ] = values[ 4 ]; values[ 4 ] = values[ 3 ];

-int temp = values[ 3 ]; values[ 3 ] = values[ 4 ]; values[ 4 ] = temp;

Q3: Which of the following initializer lists would correctly set the elements of array n?

-int[] n = { 1, 2, 3, 4, 5 };

Which of the following is not a package in the Java API? a. a. java.component. b. b. java.awt. c. c. javax.swing.event. d. d. java.lang.

-java.component.

When compiling a class in a package, the javac command-line option ________ causes the javac compiler to create appropriate directories based on the class's package declaration.

ANS: -d.

Q4: Which operator can be used in string concatenation? a. a. *. b. b. +=. c. c. ++. d. d. =+.

ANS: b. +=.

Suppose method1 is declared as void method1 ( int a, float b ) Which of the following methods correctly overloads method1?

ANS: c. void method1 ( float a, int b ).

When should a program explicitly use the this reference? a. a. Accessing a private variable. b. b. Accessing a public variable. c. c. Accessing a local variable. d. d. Accessing a field that is shadowed by a local variable.

Accessing a field that is shadowed by a local variable.

Java uses class ________ to represent colors using their RGB values.

Color.

A class within a package must be declared public if

It will be used by classes that are not in the same package. Classes outside the package cannot use a class if the class is not declared public.

Composition is sometimes referred to as a(n) ________

has-a relationship.

Overloaded methods always have the same _________.

method name.

A programmer-defined constructor that has no arguments is called a(n) ________.

no-argument constructor.


Kaugnay na mga set ng pag-aaral

GOETHE-ZERTIFIKAT A1: Start Deutsch A1, Sprechen Teil 2 (Fragen formulieren).

View Set

Supply and Demand Recitation Quiz - Ritter

View Set

301 Final Post and Pre Lecture Questions

View Set

Final- One- Non-System Salvo Chapters

View Set

Pediatric Medicine: Exam 1 lec 3

View Set

Einführung in die Medienwissenschaft(Knut Hickethier)

View Set