Chapter 5 Book Quiz Flashcards

1
Q

This type of method does not return a value.
a. null
b. void
c. empty
d. anonymous

A

b. void
Explanation: A void method is one that does not return a value. It simply performs an action but does not provide any output.

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

This appears at the beginning of a method definition.
a. semicolon
b. parentheses
c. body
d. header

A

d. header
Explanation: The method header is the part of the method definition that appears at the beginning and includes the method’s name, return type, and parameter list (if any).

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

The body of a method is enclosed in .
a. curly braces { }
b. square brackets []
c. parentheses ()
d. quotation marks “”

A

a. curly braces { }
Explanation: The body of a method is enclosed in curly braces { }, which contain the statements that define what the method does.

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

A method header can contain .
a. method modifiers
b. the method return type
c. the method name
d. a list of parameter declarations
e. all of these
f. none of these

A

e. all of these
Explanation: The method header includes all of the following: method modifiers (like public, static), the method return type (like void or int), the method name, and optionally a list of parameter declarations.

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

A value that is passed into a method when it is called is known as
a. parameter
b. argument
c. signal
d. return value

A

b. argument
Explanation: When you pass a value into a method, that value is called an argument. The method can then use this value by referring to it through the parameter.

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

A variable that receives a value that is passed into a method is known as a(n) .
a. parameter
b. argument
c. signal
d. return value

A

a. parameter
Explanation: A parameter is a variable declared in the method header that holds the value passed into the method when it is called

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

This javadoc tag is used to document a parameter variable.
a. @parameter
b. @param
c. @paramvar
d. @arg

A

b. @param
Explanation: The @param javadoc tag is used to document the parameters of a method, explaining what each parameter represents.

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

This statement causes a method to end and sends a value back to the statement that called the method.
a. end
b. send
c. exit
d. return

A

d. return
Explanation: The return statement is used to exit the method and optionally return a value to the caller.

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

This javadoc tag is used to document a method’s return value.
a. @methodreturn
b. @ret
c. @return
d. @returnval

A

c. @return
Explanation: The @return javadoc tag is used to describe the return value of a method in the documentation.

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

TRUE OR FALSE: You terminate a method header with a semicolon.

A

False
Explanation: A method header does not end with a semicolon. The method header ends with a pair of parentheses, and the body of the method is enclosed in curly braces.

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

TRUE OR False
Explanation: A method header does not end with a semicolon. The method header ends with a pair of parentheses, and the body of the method is enclosed in curly braces.

A

True
Explanation: Java will automatically perform widening conversions (like converting an int to a double) when needed, since the values fit without data loss.

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

TRUE OR FALSE: When passing an argument to a method, Java will automatically perform a narrowing conversion (convert the argument to a lower-ranking data type), if necessary.

A

False
Explanation: Java does not automatically perform narrowing conversions (like converting a double to an int). This requires explicit casting, and the compiler will throw an error if it’s attempted without it.

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

TRUE OR FALSE: A parameter variable’s scope is the entire program that contains the method in which the parameter is declared.

A

False
Explanation: The scope of a parameter variable is limited to the method in which it is declared. It is not accessible outside the method.

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

TRUE OR FALSE: When code in a method changes the value of a parameter, it also changes the value of the argument that was passed into the parameter.

A

False
Explanation: In Java, parameters are passed by value. If a method changes the value of a parameter, it does not affect the original argument passed to the method.

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

TRUE OR FALSE: When an object, such as a String, is passed as an argument, it is actually a reference to the object that is passed.

A

True
Explanation: When objects (like a String) are passed as arguments, what is actually passed is a reference to the object, not a copy of the object itself.

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

TRUE OR FALSE: The contents of a String object cannot be changed.

A

True
Explanation: Strings in Java are immutable, meaning their contents cannot be changed once they are created. Any modification to a String results in a new String object being created.

17
Q

TRUE OR FALSE: When passing multiple arguments to a method, the order in which the arguments are passed is not important.

A

False
Explanation: The order of arguments is important. The arguments must be passed in the same order as the parameters are declared in the method header.

18
Q

TRUE OR FALSE: No two methods in the same program can have a local variable with the same name.

A

False
Explanation: Different methods can have local variables with the same name, as each method’s local variables are scoped to that method and cannot be accessed by other methods.

19
Q

TRUE OR FALSE: It is possible for one method to access a local variable that is declared in another method.

A

False
Explanation: Local variables are only accessible within the method they are declared in. They cannot be accessed by other methods.

20
Q

TRUE OR FALSE: You must have a return statement in a value-returning method.

A

True
Explanation: A value-returning method must include a return statement to provide the value back to the caller. The type of the returned value must match the method’s declared return type.

21
Q
  1. Find the error in the following method definition:
    // This method has an error!
    public static void sayHello();
    {
    System.out.println(“Hello”);
    }
A

Correct Code:
java
Copy code
public static void sayHello() {
System.out.println(“Hello”);
}
Reason:
In Java, a method header should not end with a semicolon. The semicolon (;) is used to terminate statements, not method headers.
The correct syntax for a method declaration ends with parentheses () and the method body is enclosed in curly braces {}.
Key Points:
Method Declaration: The method declaration (header) defines the return type (void), method name (sayHello), and parameters (if any). It should end with parentheses, not a semicolon.
Method Body: The body of the method, which contains the statements that execute when the method is called, must be enclosed in curly braces {}.

22
Q

Find the error in the following method definition:
// This method has an error!
public static double timesTwo(double num)
{
double result = num * 2;
}

A

Correct Code:
java
Copy code
public static double timesTwo(double num)
{
double result = num * 2;
return result; // Return the result
}
Reason:
The method timesTwo is defined to return a double value, as indicated by the public static double return type in the method signature.
However, the method does not have a return statement, so it doesn’t actually return anything. This causes a compilation error because Java expects a double value to be returned when the method is called.
Key Points:
Return Type Mismatch: When a method is declared with a return type (in this case, double), the method must return a value of that type.
Return Statement: In this method, we need to return the result variable (which is of type double), so we add a return result; statement.

23
Q

Find the error in the following method definition:
// This method has an error!
public static int half(double num)
{
double result = num / 2.0;
return result;
}

A

Correct Code:
There are two possible solutions depending on the intended behavior:

Option 1: Change the return type to double If the method is supposed to return a double, you should change the method’s return type to double:

java
Copy code
public static double half(double num)
{
double result = num / 2.0;
return result;
}
Option 2: Cast the result to int If you want to keep the return type as int, you can explicitly cast the result to int. Note that this will lose the decimal part of the division:

java
Copy code
public static int half(double num)
{
double result = num / 2.0;
return (int) result; // Cast the result to int
}
Reason:
Return Type Mismatch: The return type must match the type of the value being returned.
Type Casting: If you want to return an int but the result is a double, you need to explicitly cast it using (int).
Key Points:
Type Mismatch: Java requires that the method return type and the type of the value returned match exactly.
Type Casting: When returning a different type, such as casting a double to an int, be mindful of the potential loss of precision.