Chapter 6 Book Quiz Flashcards

1
Q

This is a collection of programming statements that specify the fields and methods that a particular type of object may have.

a. class
b. method
c. parameter
d. instance

A

a. class
Explanation: A class defines the structure of objects, specifying their fields (data) and methods (actions). It acts as a blueprint for creating objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A class is analogous to a(n):

a. house
b. blueprint
c. drafting table
d. architect

A

b. blueprint
Explanation: A class acts like a blueprint, detailing how an object should look and behave, just like a blueprint outlines the construction of a house.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

An object is a(n):

a. blueprint
b. primitive data type
c. variable
d. instance of a class

A

d. instance of a class
Explanation: Objects are specific instances created from a class. They occupy memory and hold data and behavior defined by the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

This is a class member that holds data:

a. method
b. instance
c. field
d. constructor

A

c. field
Explanation: Fields are variables defined inside a class that hold data for each object created from that class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

This key word causes an object to be created in memory:

a. create
b. new
c. object
d. construct

A

b. new
Explanation: The new keyword allocates memory for an object and calls its constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

This is a method that gets a value from a class’s field, but does not change it:

a. accessor
b. constructor
c. void
d. mutator

A

a. accessor
Explanation: Accessor methods, often called “getters,” retrieve the value of a field without modifying it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

This is a method that stores a value in a field or in some other way changes the value of a field:

a. accessor
b. constructor
c. void
d. mutator

A

d. mutator
Explanation: Mutator methods, often called “setters,” change the value of fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When the value of an item is dependent on other data, and that item is not updated when the other data is changed, what has the value become?

a. bitter
b. stale
c. asynchronous
d. moldy

A

b. stale
Explanation: Stale data occurs when dependent data changes but the computed value is not recalculated, leading to outdated information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

This is a method that is automatically called when an instance of a class is created:

a. accessor
b. constructor
c. void
d. mutator

A

b. constructor
Explanation: Constructors are special methods invoked during object creation to initialize the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When a local variable has the same name as a field, the local variable’s name does this to the field’s name:

a. shadows
b. complements
c. deletes
d. merges with

A

a. shadows
Explanation: Shadowing occurs when a local variable with the same name as a field hides the field within the scope of the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

This is automatically provided for a class if you do not write one yourself:

a. accessor method
b. default instance
c. default constructor
d. variable declaration

A

c. default constructor
Explanation: Java provides a default no-argument constructor if no other constructor is explicitly defined in the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Two or more methods in a class may have the same name, as long as this is different:

a. their return values
b. their access specifier
c. their parameter lists
d. their memory address

A

c. their parameter lists
Explanation: Method overloading allows multiple methods to share the same name if they have different parameter lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The process of matching a method call with the correct method is known as:

a. matching
b. binding
c. linking
d. connecting

A

b. binding
Explanation: Binding is the process of associating a method call with its corresponding method using the method signature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A class’s responsibilities are:

a. the objects created from the class
b. things the class knows
c. actions the class performs
d. both b and c

A

d. both b and c
Explanation: A class’s responsibilities include storing data (fields) and performing actions (methods).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

TRUE OR FALSE: The new operator creates an instance of a class.

A

TRUE
Explanation: The new operator is used to instantiate an object, allocating memory and invoking its constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

TRUE OR FALSE: Each instance of a class has its own set of instance fields.

A

TRUE
Explanation: Every object created from a class has its own unique copy of the instance fields, allowing objects to hold different data.

17
Q

TRUE OR FALSE: When you write a constructor for a class, it still has the default constructor that Java automatically provides.

A

FALSE
Explanation: Writing a constructor overrides the default behavior, so Java no longer provides the default constructor.

18
Q

TRUE OR FALSE: A class may not have more than one constructor.

A

FALSE
Explanation: A class can have multiple constructors as long as their parameter lists differ, which is called constructor overloading.

19
Q

TRUE OR FALSE: To find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain.

A

FALSE
Explanation: Classes are identified by finding nouns in the problem domain description, not verbs. Verbs typically indicate actions, which relate to methods.

20
Q

Find the error in the following class:
public class MyClass
{
private int x;
private double y;
public void MyClass(int a, double b)
{
x = a;
y = b;
}
}

A

public class MyClass {
private int x;
private double y;

// Correct constructor without a return type
public MyClass(int a, double b) {
    x = a;
    y = b;
} } What Happens in the Original Code? In the original code, the method MyClass(int a, double b) is treated as a regular method and not a constructor. Since no valid constructor is defined, Java provides a default no-argument constructor. However, the fields x and y would remain uninitialized unless the method MyClass is explicitly called.

Key Points:
A constructor’s job is to initialize an object when it is created.
Ensure that a constructor does not have a return type.

21
Q

Assume that the following method is a member of a class. Find
the error.
public void total(int value1, value2, value3)
{
return value1 + value2 + value3;
}

A

Corrected Code:
To fix both issues, decide if the method should return a value or just perform an action:

Option 1: If the method is meant to return the total:
Change the return type to int and declare the parameter types correctly:

java
Copy code
public int total(int value1, int value2, int value3)
{
return value1 + value2 + value3;
}
Option 2: If the method is not supposed to return a value:
Keep the void return type and remove the return statement. Instead, perform an action (e.g., print the total):

java
Copy code
public void total(int value1, int value2, int value3)
{
System.out.println(value1 + value2 + value3);
}
Explanation:
Parameter Declaration:

In Java, every parameter in a method must have its type explicitly declared.
Writing int value1, value2, value3 causes a syntax error because value2 and value3 lack their types.
Return Type:

A void method cannot use the return keyword to return a value. The return keyword can only be used in a void method to terminate execution early, without a value.
Correct Usage:
Use int as the return type if you want the method to compute and return the sum.
Use void if the method should simply perform an action without returning a value.

22
Q

The following statement attempts to create a Rectangle object.
Find the error.
Rectangle box = new Rectangle;

A

Corrected Code:
If the Rectangle class has a no-argument constructor, the corrected code is:

java
Copy code
Rectangle box = new Rectangle();
If the Rectangle class has a constructor that takes arguments (e.g., length and width), you need to provide appropriate arguments:

java
Copy code
Rectangle box = new Rectangle(10.0, 5.0); // Example with length 10.0 and width 5.0
Additional Notes:
If the Rectangle class does not define any constructors, Java will automatically provide a default no-arg constructor. However, if the class defines its own constructors, Java will not provide a default constructor, and you must use one of the defined constructors to create an object.

23
Q

. Find the error in the following class:
public class TwoValues
{
private int x, y;
public TwoValues()
{
x = 0;
}
public TwoValues()
{
x = 0;
y = 0;
}
}

A

Corrected Code:
You can either:

Use a single constructor with both assignments (x = 0 and y = 0):
java
Copy code
public class TwoValues {
private int x, y;

public TwoValues() {
    x = 0;
    y = 0;
} } Provide different constructors with different parameters (e.g., one constructor for initializing just x and another for initializing both x and y): java Copy code public class TwoValues {
private int x, y;

// Constructor that initializes only x
public TwoValues(int x) {
    this.x = x;
    this.y = 0;  // Default value for y
}

// Constructor that initializes both x and y
public TwoValues(int x, int y) {
    this.x = x;
    this.y = y;
} } Additional Notes: Constructor Overloading: In Java, constructor overloading is allowed as long as the constructors have different parameter lists. In this case, the error arises because both constructors have the same signature (no parameters). If you want multiple constructors, they must differ in the number or type of parameters.
24
Q

Find the error in the following class:
public class FindTheError
{
public int square(int number)
{
return number * number;
}
public double square(int number)
{
return number * number;
}
}

A

Corrected Code:
To resolve this issue, you must either:

Change the method signatures by adding a different parameter type or different number of parameters.
If you want to keep both methods, one possible solution is to have one method take a different type of argument or change the parameters in some other way.
Option 1: Use a different parameter type (e.g., a double for one of the methods):
java
Copy code
public class FindTheError {
public int square(int number) {
return number * number;
}

public double square(double number) {  // Change parameter type to double
    return number * number;
} } Option 2: Use the same parameter type but different numbers of parameters: java Copy code public class FindTheError {
public int square(int number) {
    return number * number;
}

public double square(int number, double factor) {  // Add a second parameter
    return number * number * factor;
} } Additional Notes: Method Overloading requires that methods differ either by the number of parameters or the types of parameters. The return type alone cannot be used to distinguish between overloaded methods. The corrected version ensures that each method has a unique signature, which Java requires for method overloading.