Java 2 Midterm

0.0(0)
studied byStudied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/238

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:37 AM on 10/4/23
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

239 Terms

1
New cards

What operator is used to perform string concatenation in Java?

+

2
New cards

If a String variable called greeting contains the characters "hello", what does the expression greeting.toUpperCase() accomplish?

Returns a new string containing the characters "HELLO"

3
New cards

If a String variable called pioneer contains the characters "Grace Murray Hopper", what is the result of the expression pioneer.length()?

19

4
New cards

Given the following declaration, which expression would produce the substring "ledge"?

String words = "Knowledge is power.";

words.substring(4,9);

5
New cards

What output would be produced when the following code is executed?

String state = "Mississippi";
System.out.println(state.replace("is", "was"));

Mwasswassippi

6
New cards

If a String variable called address contains the characters "123 Main Street", what is the result of the expression address.indexOf('3')?

2

7
New cards

Which of the following is the preferred way to declare an array.

double[] prices = new double[10];

8
New cards

What is the output of the following code?

int sum = 0;
int[] values = {7, 6, 5, 4, 3, 2, 1};
for (int i = 1; i < values.length; i++)
    sum += values[i];
System.out.println("Sum: " + sum);

Sum: 21

9
New cards

What is the output of the following code?

int sum = 0;
int[] values = {1, 2, 3, 4, 5};
for (int i = 0; i <= 5; i++)
     sum = sum + values[i];
System.out.println("Sum: " + sum);

No output. A bounds error occurs.

10
New cards

What is the output of the following code?

int[] list = new int[5];
System.out.println(list[3]);

0

11
New cards

Arrays can hold primitive types, but not objects.

False

12
New cards

If you try to access an element outside of the valid index range for an array, an exception will be thrown.

True

13
New cards

If an array can hold N values, the valid indexes for that array range from 0 to N.

False

14
New cards

What output is produced by the following code?

for (int i = 1; i <= 5; i++)
{
    for (int j = 1; j <= 5; j++)
        System.out.print("*");
    System.out.println();
}

*****

*****

*****

*****

*****

15
New cards

What does the following for loop do?

for (int num = 3; num <= 30; num += 3)
    System.out.println(num);

Print the multiples of 3 between 3 and 30 (3,6,9, etc.)

16
New cards

6

What value is printed by the following code?

int count = 0;

for (int i = 0; i < 5; i++)
    count += 3;

System.out.println(count);

15

17
New cards

Going from left to right, what are the three sections of a for loop header?

initialization, condition, increment

18
New cards

for loop could always be written as an equivalent while loop.

True

19
New cards

The increment section of the for loop header always adds 1 to the loop control variable.

False

20
New cards

The body of a for loop is always executed at least once.

False

21
New cards

The control variable must be declared in the for loop header.

False

22
New cards

Swapping two elements in an array requires three assignment statements.

True

23
New cards

To change the algorithm for finding a minimum value into one that finds a maximum value, the condition of the for loop would change.

False

24
New cards

A linear search examines every element in the array.

False

25
New cards

Finding a minimum value requires that every element in the array be considered.

True

26
New cards

A for-each loop can be used to compute the sum of all values in an array.

True

27
New cards

A for-each loop can be used to fill the values in an array.

False

28
New cards

Trying to run a program in Eclipse without first saving it will cause it to crash.

False

29
New cards

Eclipse displays code using syntax highlighting.

True

30
New cards

The output of a Java program run in Eclipse appears in the package explorer view.

False

31
New cards

Compiling a program that contains syntax errors will cause Eclipse to fix the errors.

False

32
New cards

Eclipse is best described as which of the following?

Integrated Development Environment (IDE)

33
New cards

Which of the following is an appropriate analogy?

Class: concept of a dog, Object: my dog Fido

34
New cards

Which of the following is an example of the state of a Car object?

The car’s color

35
New cards

Each object has its own instance data.

True

36
New cards

The values of an object's instance data represent the object's identity.

false

37
New cards

A class represents a group of similar objects.

True

38
New cards

String method can be called through a string literal.

True

39
New cards

String object cannot be created using the new operator.

False

40
New cards

Attempting to follow a null reference will cause an exception to be thrown.

True

41
New cards

An object must be created when its object reference variable is declared.

False

42
New cards

A constructor of a class has the same name as the class.

True

43
New cards

The Java keyword new is a method that can be called to create objects.

False

44
New cards

The toString method returns a textual description of an object.

True

45
New cards

The methods of a class that can be called from other classes make up its public interface.

True

46
New cards

All Java classes have a main method.

False

47
New cards

Encapsulation ensures that an object controls its own data.

True

48
New cards

Instance data is shared by all objects of a class.

False

49
New cards

An object created from a class is called an instance of that class.

True

50
New cards

A constructor is a programming construct that contains data and methods.

False

51
New cards

A variable can be declared inside the body of a method.

True

52
New cards

If the type of the expression in a return statement does not match the return type of the method, the compiler will issue an error message.

True

53
New cards

The return type of a method must either be a Java primitive type or void.

False

54
New cards

It is usually not a good idea to explicitly return a true or false literal value in a method that returns a boolean value.

True

55
New cards

A method that has a return type of void does not have to have a return statement.

True

56
New cards

When a method is called, the type and value of each argument are included in parentheses.

False

57
New cards

A method declaration includes parentheses after the method name, even if the method takes no parameters.

True

58
New cards

What is the output of the following code?

Die die = new Die(20);

die.roll();
die.roll();

System.out.println(die.getValue());

A random number between 1 and 20.

59
New cards

What is the output of the following code?

Die die = new Die(6);
System.out.println("Die value: " + die);

Die value: 1

60
New cards

The parameter to the constructor of the Die class determines how many sides are on the die.

True

61
New cards

Each time the roll method is called, a random value is picked as the new face value.

True

62
New cards

The maximum number of sides on a Die object is 20.

False

63
New cards

The Die class is part of the Java API.

False

64
New cards

The package of any class that is declared without an explicit package statement.

default package

65
New cards

A group of related classes.

package

66
New cards

The first line of code in a source file that declares which package the class is in.

package statement

67
New cards

A way to refer to a class that includes its complete package name.

fully qualified name

68
New cards

The set of directories that are searched to find a class.

classpath

69
New cards

A feature of packages that enables two classes to have the same name.

name space

70
New cards

The String class is part of the java.util package.

False

71
New cards

Two classes can have the same name if they are in different packages.

True

72
New cards

In a program, a class must always be referred to using its fully qualified name.

False

73
New cards

An import statement tells the compiler which class you're referring to in a program.

True

74
New cards

An import statement should be written inside a class but before any methods.

False

75
New cards

The classes of the java.util package are automatically imported into every Java program.

False

76
New cards

An entire package can be imported using one import statement.

True

77
New cards

A static import lets you refer to a static method without using its class name.

True

78
New cards

Block tags are enclosed in { } braces and appear anywhere in a Javadoc comment.

False

79
New cards

Javadoc comments start with // for single-line comments and /** for multiple-line comments.

False (/** and */)

80
New cards

The @return tag describes the parameters passed to a method or constructor.

False

81
New cards

The Java API is generated from Javadoc comments.

True

82
New cards

The classic format for a getter method is to use the word "access" in front of the variable name.

False

83
New cards

A method can only be an accessor or a mutator but not both.

False

84
New cards

Encapsulation makes is difficult for hackers to access your data.

False

85
New cards

Classes should be designed to have well-defined and limited interactions with other classes.

True

86
New cards

Constants can be declared with public visibility without violating encapsulation.

True

87
New cards

An encapsulated object keeps other objects from changing the values of its instance data.

True

88
New cards

Assuming table is a two-dimensional array of integers, what does the following code do?

int sum = 0;
for (int i = 0; i < table.length; i++)
    for (int j = 0; j < table[i].length; j++)
        sum = sum + table[i][j];

Computes the sum of all values in the table.

89
New cards

Which line of code demonstrates a valid and error-free way to access the following array?

String[][] chessboard = new String[8][8];

String chessPiece = chessboard[5][2];

90
New cards

How many elements can be stored in a two-dimensional array with dimensions 5 and 10?

50

91
New cards

It is possible to create an array with more than two dimensions.

True

92
New cards

A two-dimensional array is really a one-dimensional array of one-dimensional arrays.

True

93
New cards

The maximum indexes of a two-dimensional array with 6 rows and 10 columns are 5 and 9, respectively.

True

94
New cards

A two-dimensional array is suitable for storing tabular data

True

95
New cards

A one-dimensional array has only one element type, but a multidimensional array can hold several types of elements.

False

96
New cards

Which method signature could NOT represent an overloaded version of the following method?

public int max(int x, int y)
{
    return (x > y) ? x : y;
}

max(int, int)

97
New cards

Method overloading is helpful in which of the following situations?

When two methods perform a similar task on different types of data.

98
New cards

The println method in the Java API is overloaded.

True

99
New cards

Constructors cannot be overloaded.

False

100
New cards

The return type of a method is not part of the method signature.

True