Java Quizzes Compiled

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

1/40

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

41 Terms

1
New cards

What are the two meanings of the '+' operator discussed in the chapter and how does Java determine which meaning to use?

Arithmetic addition or string concatenation. If two numbers are being added, '+' refers to addition. If one of the values is a string, '+' refers to string concatenation.

2
New cards

What are the primitive data types?

boolean, char, byte, int, short, long, float, double

3
New cards

Which of the following are illegal identifiers in Java? a. toDec, b. to+Four, c. forgetIt, d. letItBeMe, e. 3littlePigs, f. l_o_n_g, g. whatFun, h. whyMe?

b, e, h

4
New cards

Why are escape characters needed?

To allow the use of "" in the middle of a string, if necessary.

5
New cards

Given a String, s, which is assumed to have at least one character in it, which of the following conditions would determine if the first character of the String is the same as the last character? a. (s.charAt(0) == s.charAt(s.length( ))), b. (s.charAt(1) == s.charAt(s.length( ))), c. (s.charAt(0) == s.charAt(s.length( ) - 1)), d. (s.charAt(0) == s.charAt(s.length( ) + 1)), e. (s.charAt(0) == s.charAt(last))

c

6
New cards

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

d

7
New cards

Which of the following is not a method on the String class? a. equals(), b. toUpperCase(), c. truncate(), d. substring(), e. length()

c

8
New cards

You have variable generator that is of type Random. What expression using generator would generate a random integer with the inclusive range -100 and 100?

generator.nextInt(201)-100

9
New cards

Since you cannot take the square root of a negative number, you might use which of the following instructions to find the square root of the variable x, where x might be negative? a. Math.sqrt(x*x), b. Math.sqrt((int) x), c. Math.sqrt(Math.abs(x)), d. Math.abs(Math.sqrt(x)), e. Math.sqrt(-x)

c

10
New cards

Why is encapsulation important in Java? What methods does Java use to attempt to enforce proper encapsulation?

Hides the data to keep it safe and ensure that mutations are always done safely.

11
New cards

What role and purpose does the Constructor fulfill for a class? What happens if you do not include a Constructor?

Initializes the data of the class to a known state. All variables are initialized to 0 or null.

12
New cards

Explain the difference between instance data and local data? Why are different scopes useful in developing programs?

Instance data belongs to an instance of a class, local data belongs to an individual method. Different scopes are necessary to allow the data to be used either for a single method or for the life of an instance.

13
New cards

Why should every class, that is not a driver class, include a toString() method?

Helps with debugging and ensuring the class is human-readable when printed.

14
New cards

Explain the difference between an IF statement and an IF-ELSE statement. Why might you want to use an IF-ELSE?

An IF statement only executes the block if the condition is true. An IF-ELSE has two blocks: one which executes if the condition is true and one if the condition is false.

15
New cards

If a WHILE and a FOR loop can perform the same task, why are both types of loops useful?

A FOR loop reduces some of the boilerplate for doing a common loop and makes it easier to run a loop a specified number of times.

16
New cards

What is the difference between a WHILE and a DO-WHILE loop?

A DO-WHILE loop will always execute the loop at least once. A WHILE loop may never execute.

17
New cards

What makes the conditional operator, or ternary operator, unique? Does this uniqueness make reading statements that use the operator harder or easier to understand?

The ternary is unique because it has three operands. If used well, it may be easier to understand. Otherwise, it can increase complexity and be harder to understand.

18
New cards

Why is the 'this' reference useful?

Code can use 'this' to reference instance variables that may be shadowed in the current method.

19
New cards

What does it mean when we say a method is "overloaded"?

There are multiple methods that have the same name. Java will determine which method to call based on the parameters provided at the call site.

20
New cards

What role do interfaces play in object-oriented design?

Interfaces are a contract between an implementor and a consumer. This allows a class to implement multiple interfaces and be used to satisfy the contracts to desired consumers.

21
New cards

What different class relationships does the chapter discuss?

Dependency, Aggregation, and Inheritance.

22
New cards

How would you declare a 2D array of integers? How is working with a 2D array differ from working with a 1D array?

int[][] data = new int[10][10]. Working with a 2D array is like working with an array of arrays, so the first [] navigates one array to access the array at that location, while the second [] accesses the data at that index of the inner array.

23
New cards

If I have an array called data, what does the notation data[i] signify if i is an integer? What is the valid range for i? If I declare an array with the expression data = new int[20], what would data.length return?

It references the value at position i in the array. The valid range is 0 to data.length - 1. 20.

24
New cards

How is an array of primitive datatypes different than an array of objects?

The array of primitive datatypes holds the values directly, while the array of objects holds a sequence of references to each object.

25
New cards

Does Java natively support arrays with more than two dimensions? If so, describe how you would declare and use the array. If not, describe a workaround that could be used to accomplish the task.

Yes, extra [] are used to define and access each dimension of the array

26
New cards

How many classes can a class be a direct subclass of? What is the name for this limitation. Are there any workarounds to these limitations?

One. Single Inheritance. Interfaces

27
New cards

How do the access modifiers private and protected differ? Why is protected needed when we are using inheritance?

private means only the current class can access the data or method. protected means this class and any subclass can access the data or method.

28
New cards

What keyword do we use to indicate that a class is a subclass of another class?

extends

29
New cards

Can a subclass have methods with the same signature as methods in the superclass? What about attributes? What terms do we use to encompass these techniques? Are there any downsides to this?

Yes, this is an example of overloading. Yes, this is an example of shadowing. Overriding is a useful way to expand the capabilities of a base class, but shadowing leads to confusion and should not be used.

30
New cards

Which method of achieving polymorphism did the sorting and search examples use? What is the advantage in writing those classes in that manner?

Interface polymorphism. This is useful because the sorting methods do not have to know how to compare two instances of a class, only that they can be compared and what the relative ordering is between them.

31
New cards

How can you use inheritance to implement polymorphism?

Multiple subclasses can inherit from the same base class, so a variable of the type of the base class can potentially be any of the subclasses.

32
New cards

What is polymorphism? Why is the concept of late binding important to making it work?

Polymorphism is when the instance a variable holds could potentially be one of multiple different classes. Late binding is important because the actual method called is not determined until runtime, based on which class the instance actually is.

33
New cards

How can you use interfaces to implement polymorphism?

Multiple classes can implement an interface, so a variable of the type of the interface can hold an instance of any class that implements the interface.

34
New cards

What are the different strategies that you can use in Java to handle exceptions?

Handle where it occurs with try-catch and finally, or handle it somewhere else by propagating it with throws

35
New cards

What programming construct does Java use to handle the exception?

The try-catch statement

36
New cards

What keyword do we have to add to the method signature if that method does not catch an exception it generates?

Throws

37
New cards

What makes checked exceptions different from unchecked exceptions?

Checked exceptions must be caught by a method or listed in the throws clause of any method that throws or propagates them. An unchecked method does not require these things.

38
New cards

What is recursion? What advantages does it have? Are there any things to look out for when using recursion?

Recursion is a technique where a method calls itself to achieve its purpose. An advantage to this is that it sometimes results in a shorter/simpler solution. Something to look out for is infinite recursion. To avoid this, make sure that the base case isn't recursive.

39
New cards

Explain the difference between direct and indirect recursion. State which method is the preferred practice and why.

With direct recursion, a method invokes itself. With indirect recursion, a method invokes another method, but the original method ends up being involved again. Direct recursion is preferred because it's easier to trace, making it easier to understand and debug.

40
New cards

How can recursion and loops be viewed as two ways of solving the same problem?

Recursion and loops both repeat an action until it is told to stop. All recursive statements and loops should be able to be written as the other.

41
New cards

What would you consider important when attempting to determine if the recursive approach to solving a problem is preferable to the iterative approach?

It's important to determine if the problem can be broken down into sub-problems that are similar in structure to the original. If this is true and there's a valid base case, then recursion could be preferable to the iterative approach.