Java I FINAL

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/67

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

68 Terms

1
New cards

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

ArrayElementOutOfBoundsException

 

ArrayOutOfBoundsException

 

ArrayIndexOutOfBoundsException

 

ArrayException

ArrayIndexOutOfBoundsException

2
New cards

What do the following statements do?

 double[] array;
 array = new double[14];

Create a double array containing 14 elements.

 

Create a double array containing 15 elements.

 

Create a double array containing 13 elements.

 

Declare but do not create a double array.

 

Create a double array containing 14 elements

3
New cards

Which of the following statements is false?

 int[] g;
 g = new int[23];

g is a reference to an array of integers.

 

The first statement declares an array reference.

 

The second statement creates the array.

 

The value of g[3] is -1.

 

The value of g[3] is -1

4
New cards

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;
       }

 

0, 2, 4, 6, 12

5
New cards

Which of the following statements about arrays are true?

A. An array is a group of variables containing values that all have the same type.
B. Elements are located by index.
C. The length of an array c is determined by the expression .length();.
D. The zeroth element of array c is specified by c[0].

 

A, B, D

6
New cards

In array items, which expression below accesses the value at row 3 and column 4?

 

items[2[3]]

 

items[2, 3]

 

items[2][3]

 

items[2].[3]

 

items[2][3]

7
New cards

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

8
New cards

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

 

None of them.

 

arrayName++[i].

 

++arrayName[i].

 

arrayName[i++].

 

++arrayName[i]

9
New cards

Which set of statements creates an array of Books?

 

new Book() books[];
books = new Book[numberElements];

 

Book[] books];
books = new Book()[numberElements];

 

Book[] books;
books = new Book[numberElements];

 

All of the above.

Book[] books;
books = new Book[numberElements];

10
New cards

When you pass an array or an individual array element of a reference type to a method, the called method receives ________.

a copy of the element’s reference, a copy of the element’s value

 

a copy of the element’s reference, a copy of the element’s reference

 

a copy of the element’s value, a copy of the element’s value

 

a copy of the element’s value, a copy of the element’s reference

a copy of the element’s reference, a copy of the element’s value

11
New cards

When you pass an individual element of a primitive type, the called method receives ________.

a copy of the element’s reference, a copy of the element’s value

 

a copy of the element’s reference, a copy of the element’s reference

 

a copy of the element’s value, a copy of the element’s value

 

a copy of the element’s value, a copy of the element’s reference

a copy of the element’s value

12
New cards

The preferred way to traverse a two-dimensional array is to use         .

three nested for statements.

 

two nested for statements.

 

a do while statement.

 

a for statement.

 

a do while statement

13
New cards

Consider integer array values, which contains 5 elements.

Which statements successfully swap the contents of the array at index 3 and index 4?

 

values[4] = values[3];
values[3] = values[4];

 

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

 

values[3] = values[4];
values[4] = values[3];

 

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

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

14
New cards

int[][] items = {{2, 4}, {6, 8}, {10, 12}};

what is the value returned by items[1][0]?

6

15
New cards

Assume array items contains the integer values 0, 2, 4, 6 and 8.

Which of the following uses the enhanced for loop to display each value in array items?

 

for (int i = 0 : items.length) {
   System.out.prinf("%d%n", items[i]);
}

 

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

for (int i = 0; i < items.length; i++) {
    System.out.prinf("%d%n", items[i]);
}

for (int i : items) {
   System.out.prinf("%d%n", items[i]);
}

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

16
New cards

Class Arrays provides method __________ for comparing arrays to determine whether they have the same contents.

equal

compares

 

equals

 

compare

equals

17
New cards

Class ________ represents a dynamically resizable array-like data structure.

Array

 

ArrayList

 

None of them

 

Arrays

 

ArrayList

18
New cards

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);

 } 


The output of this program will be ________.

65

19
New cards

Which of the following will not produce a compiler error?

 

All of them will produce compiler errors.

 

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

 

Changing the value of a final variable after it is initialized.

 

Using a final variable before it is initialized.

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

20
New cards

Which statement correctly passes the array items to method takeArray?

Array items contains 10 elements.

 

Arrays cannot be passed to methods—each item must be sent to the method separately

 

takeArray(items[])

 

takeArray(items)

 

takeArray(items[9])

takeArray(items)

21
New cards

Which of the following statements about creating arrays and initializing their elements is false?

 

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

 

When an array is created with operator new, the number of elements must be placed in square brackets following the type of element being stored.

 

for loop is commonly used to set the values of the elements of an array.

 

The new keyword should be used to create an array.

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

22
New cards

Which of the following tasks cannot be performed using an enhanced for loop?

 

Incrementing the value stored in each element of the array.

 

Calculating the product of all the values in an array.

 

Comparing the elements in an array to a specific value.

 

Displaying all even element values in an array.

 

Incrementing the value stored in each element of the array

23
New cards

Which set of statements totals the items in each row of two-dimensional array items, and displays each row’s total?

 

int total = 0;
for (int row = 0; row < items.length; row++) {
     for (int column = 0; column < items[row].length; column++) {
        total += items[row][column];
     }
     System.out.printf("Row %d's total is %d%n", row, total);

}

 _____________________________________________________

for (int row = 0; row < items.length; row++) {
     int total = 0;
     for (int column = 0; column < items[column].length; column++) {
        total += items[row][column];
     }
    System.out.printf("Row %d's total is %d%n", row, total);

}

 _____________________________________________________

int total = 0;
for (int row = 0; row < items.length; row++) {
     for (int column = 0; column < items[column].length; column++) {
        total += items[row][column];
     }
     System.out.printf("Row %d's total is %d%n", row, total);

}

 _____________________________________________________

for (int row = 0; row < items.length; row++) {
     int total = 0;
     for (int column = 0; column < items[row].length; column++) {
        total += items[row][column];

       System.out.printf("Row %d's total is %d%n", row, total);

    } 
}

for (int row = 0; row < items.length; row++) {
     int total = 0;
     for (int column = 0; column < items[row].length; column++) {
        total += items[row][column];

       System.out.printf("Row %d's total is %d%n", row, total);

    } 
}

24
New cards

A(n) ________ indicates a problem that occurs while a program executes.

 

omitted import

 

exception

 

syntax error

 

missing semicolon

exception

25
New cards

Which command below runs TestProgram, and passes in the values files.txt and 3?

 

java TestProgram "files.txt", "3"

 

java TestProgram files.txt, 3

 

java TestProgram files.txt 3

 

None of them

java TestProgram files.txt 3

26
New cards

Arrays are ________.

variable-length entities

 

used to draw a sequence of lines, or "rays"

 

fixed-length entities

 

data structures that contain up to 10 related data items

fixed-length entities

27
New cards

Any field declared with keyword ________ is constant.

final

28
New cards

An array with m rows and n columns is not ________.

  A. an m-by-n array.

  B. an n-by-m array.

  C. a two-dimensional array.

  D. a dual-transcripted array.

B and D

29
New cards

Exception handling helps you create ________  programs.

fault-tolerant

30
New cards

A constructor cannot ________.

specify return types or return values

 

have the same name as the class

 

be overloaded

 

initialize variables to their defaults

specify return types or return values

31
New cards

Which statement is false?

 

The compiler always creates a default constructor for a class.

 

None of them.

 

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.

 

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.

 

The compiler always creates a default constructor for a class

32
New cards

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

mutator, accessor

33
New cards

Composition is sometimes referred to as a(n) ________.

many-to-one relationship

 

is-a relationship

 

one-to-many relationship

 

has-a relationship

has-a relationship

34
New cards

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

static

35
New cards

Which of the following statements is true?

 

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

 

None of them is true.

 

The private members of a class are directly accessible to the clients of a class.

 

Information hiding is achieved by restricting access to class members via keyword public.

Methods and instance variables can both be either public or private

36
New cards

Which of the following statements is true?

 

None of them is true.

 

Information hiding is achieved by restricting access to class members via keyword public.

 

The private members of a class are directly accessible to the clients of a class.

 

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

Methods and instance variables can both be either public or private

37
New cards

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

values

38
New cards

Which of the following class members should usually be private?

 

Variables (or fields).

 

Methods.

 

Constructors.

 

All of them.

Variables (or fields)

39
New cards

Constructors ________.

A. when overloaded, are selected by number, types and order of types of parameters.

B. when overloaded, can have identical argument lists.

C. initialize instance variables.

Both A and C

40
New cards

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

private.

 

All of them.

 

protected.

 

public.

private

41
New cards

Static class variables ________.

are final

 

are private

 

are public

 

are shared by all objects of a class

are shared by all objects of a class

42
New cards

An advantage of inheritance is that ________.

 

all instance variables can be uniformly accessed by subclasses and superclasses

 

None of them.

 

objects of a subclass can be treated like objects of their superclass

 

all methods can be inherited

objects of a subclass can be treated like objects of their superclass

43
New cards

Using the protected keyword also gives a member ________.

block scope

 

package access

 

private access

 

public access

package access

44
New cards

Inheritance is also known as the ________.

is-a relationship

45
New cards

Consider the classes below, declared in the same file:

class A {
 int a;
 public A() {

a = 7;

 }
}

class B extends A {
 int b;
 public B() {

b = 8;

 } 
}

Which of the statements below is false?

 

A reference of type A can be treated as a reference of type B.

 

After the constructor for class B executes, the variable a will have the value 7.

 

After the constructor for class B executes, the variable b will have the value 8.

 

Both variables a and b are instance variables.

A reference of type A can be treated as a reference of type B

46
New cards

Overriding a method differs from overloading a method because ________.

A. overloaded methods have the same signature.

B. overridden methods have the same signature.

Only B

 

Both A and B

 

Neither A Nor B

 

Only A

Only B

47
New cards

Every class in Java, except ________, extends an existing class.

object

48
New cards

To avoid duplicating code, use ________, rather than ________.

a class that does not extend Object, a class that explicitly extends Object.

 

inheritance, the “copy-and-past” approach.

 

a class that explicitly extends Object, a class that does not extend Object.

 

the “copy-and-paste” approach, inheritance

 

inheritance, the “copy-and-past” approach

49
New cards

Which statement is true when a superclass has protected instance variables?

 

A subclass object can assign an invalid value to the superclass's instance variables, thus leaving an object in an inconsistent state.

 

Subclass methods are more likely to be written so that they depend on the superclass's data implementation.

 

We may need to modify all the subclasses of the superclass if the superclass implementation changes.

 

All of them.

All of them

50
New cards

Which of the following statements is false?

 

A superclass object is a subclass object.

 

The class following the extends keyword in a class declaration is the direct superclass of the class being declared.

 

Java uses interfaces to provide the benefits of multiple inheritance.

 

A subclass is often larger than its superclass.

A superclass object is a subclass object

51
New cards

Which superclass members are inherited by all subclasses of that superclass?

 

private instance variables and methods.

 

protected instance variables and methods

 

protected constructors

 

private constructors

protected instance variables and method

52
New cards

Polymorphism allows for specifics to be dealt with during ________.

execution

53
New cards

Declaring a method final means ________.

 

it will prepare the object for garbage collection

 

it cannot be accessed from outside its class

 

it cannot be overloaded

 

it cannot be overridden

it cannot be overridden

54
New cards

Methods are declared final except for the following reasons:

 

 

final methods are static.

 

final methods and classes prevent further inheritance.

 

final methods can improve performance.

 

final methods let the compiler insert the method's code directly, reducing execution time.

final methods are static

55
New cards

The UML distinguishes an interface from other classes by placing the word "interface" in ________ above the interface name.

italics

 

bold

 

carets

 

guillemets

guillemets

56
New cards

Non-abstract classes are called ________.

instance classes

 

real classes

 

implementable classes

 

concrete classes

concrete classes

57
New cards

Which of the following is not possible?

 

A class that inherits from one class, and implements an interface.

 

A class that inherits from two classes.

 

A class that implements two interfaces.

 

All of them are possible.

A class that inherits from two classes

58
New cards

Which of the following could be used to declare abstract method method1() in abstract class Class1 (method1 returns an int and takes no arguments)?

 

public abstract int method1();

 

public int method1();

 

public int nonfinal method1();

 

public int abstract method1();

public abstract int method1();

59
New cards

Which of the following statements about abstract superclasses is true?

 

abstract superclasses may contain data.

 

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 values as abstract.

abstract superclasses may contain data

60
New cards

All of the following methods are implicitly final except:

 

a private method.

 

a method in an abstract class.

 

static method.

 

a method declared in a final class.

a method in an abstract class

61
New cards

Which of the following statements is false?

 

Java allows a class to implement multiple interfaces in addition to extending one class.

 

Classes declared with interface inheritance are tightly coupled.

 

An interface also may extend one or more other interfaces.

 

Classes declared with implementation inheritance are tightly coupled.

Classes declared with interface inheritance are tightly coupled

62
New cards

A(n) ________ class cannot be instantiated.

abstract

63
New cards

Interfaces can have ________ methods.

1

 

2

 

0

 

any number of

any number of

64
New cards

Consider classes A, B and C, where A is an abstract superclass, B is a concrete class that inherits from A and C is a concrete class that inherits from B. Class A declares abstract method originalMethod, implemented in class B. Which of the following statements is true of class C?

 

 

Method originalMethod must be overridden in class C, or a compilation error will occur.

 

If method originalMethod is not overridden in class C but is called by an object of class C, an error occurs..

 

None of them.

 

Method originalMethod cannot be overridden in class C—once it has been implemented in concrete class B, it is implicitly final.

None of them

65
New cards

Which keyword is used to specify that a class will define the methods of an interface?

 

uses

 

defines

 

extends

 

implements

implements

66
New cards

When a superclass variable refers to a subclass object and a method is called on that object, the proper implementation is determined at execution time. What is the process of determining the correct method to call?

 

non-binding.

 

on-time binding.

 

late binding.

 

early binding.

late binding

67
New cards
term image

286

68
New cards

An argument type followed by a(n) ________ in a method's parameter list indicates that the method receives a variable number of arguments of that particular type.

ellipsis(...)

varargs keyword

square brackets ([])

All of them are acceptable to indicate a variable number of arguments.

A