Chapter 5 Practice Questions Flashcards

1
Q

Which of the following is a benefit of using methods in Java?

A) Makes the program longer and more complex
B) Allows code reuse and simplifies the program
C) Reduces the need for testing
D) Increases the number of errors in the program

A

B) Allows code reuse and simplifies the program
Explanation: Methods help simplify programs by breaking down complex problems into smaller tasks (divide and conquer) and by reusing code whenever needed.

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

What is a void method in Java?

A) A method that returns a value
B) A method that performs a task and returns a value
C) A method that does not return a value
D) A method with no parameters

A

C) A method that does not return a value
Explanation: A void method performs a task but does not return any value. It is used when no result is expected from the method.

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

What is the purpose of the static method modifier in Java?

A) It makes the method public
B) It binds the method to a specific object
C) It allows the method to be called on the class itself rather than an instance
D) It makes the method private

A

C) It allows the method to be called on the class itself rather than an instance
Explanation: The static modifier means the method belongs to the class itself, and can be called without creating an instance of the class.

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

True or False: In Java, a method’s parameter variables are separate from the arguments passed into it.

A

True
Explanation: The parameter variables inside a method are separate from the arguments passed when calling the method. The arguments are copied into the parameters.

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

What will this code do?

java
Copy code
public static void greet(String name) {
System.out.println(“Hello, “ + name);
}

public static void main(String[] args) {
greet(“John”);
}
A) Print “Hello, John”
B) Print “Hello, null”
C) Cause a compilation error
D) Cause a runtime error

A

A) Print “Hello, John”
Explanation: The method greet takes the name parameter and prints “Hello, “ followed by the provided argument. In this case, “John” is passed as the argument, so “Hello, John” will be printed.

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

Question 1:
Which of the following is a benefit of using methods in Java?

A) Makes the program longer and more complex
B) Allows code reuse and simplifies the program
C) Reduces the need for testing
D) Increases the number of errors in the program

A

Answer: B) Allows code reuse and simplifies the program
Explanation: Methods help simplify programs by breaking down complex problems into smaller tasks (divide and conquer) and by reusing code whenever needed.

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

What happens when a reference type object is passed as an argument to a method in Java?

A) A copy of the object’s data is passed to the method
B) A reference to the object is passed, and changes affect the original object
C) A reference to the object is passed, but changes do not affect the original object
D) The object is cloned before being passed

A

B) A reference to the object is passed, and changes affect the original object
Explanation: When an object is passed as an argument, it is passed by reference. This means that if the object is modified inside the method, the changes will reflect outside the method as well.

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

What will this code do?

java
Copy code
public static void main(String[] args) {
String str = “Hello”;
str = “World”;
System.out.println(str);
}
A) Print “Hello”
B) Print “World”
C) Compile-time error
D) Runtime error

A

B) Print “World”
Explanation: Strings in Java are immutable. When str = “World”; is executed, it does not change the original “Hello” string, but instead creates a new string object “World” and assigns it to str.

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

What is functional decomposition?

A) Breaking down large problems into smaller tasks
B) Creating complex code for efficiency
C) Combining all tasks into one function
D) Ignoring function calls to simplify a program

A

A) Breaking down large problems into smaller tasks
Explanation: Functional decomposition is the process of breaking down a large, complex problem into smaller, more manageable pieces, often represented as methods.

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

Which of the following statements about method documentation is true?

A) Method comments should appear inside the method body
B) The @param tag is used to describe the return value
C) Method documentation comments should explain the method’s purpose
D) Java does not require method documentation

A

C) Method documentation comments should explain the method’s purpose
Explanation: Java requires documentation comments (/** … */) that describe the method’s purpose and parameters. The @param tag is used to describe each parameter.

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

What is the effect of using the throws clause in a method header in Java?

A) It indicates the method will handle the exception
B) It marks the method as private
C) It tells the compiler the method might throw an exception and must be handled or passed on
D) It automatically handles exceptions for the method

A

C) It tells the compiler the method might throw an exception and must be handled or passed on
Explanation: The throws clause is used to declare that a method might throw certain exceptions, and the calling method must either handle them or declare them as well.

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

True or False: Local variables in Java are automatically initialized to default values.

A

False
Explanation: Local variables in Java are not automatically initialized. They must be explicitly given a value before they can be used.

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

What will happen if the following code is executed?

java
Copy code
public static boolean isValid(int number) {
if (number >= 1 && number <= 100)
return true;
else
return false;
}

public static void main(String[] args) {
int value = 50;
if (isValid(value)) {
System.out.println(“The value is within range”);
} else {
System.out.println(“The value is out of range”);
}
}
A) It will print “The value is within range”
B) It will print “The value is out of range”
C) Compile-time error
D) Runtime error

A

A) It will print “The value is within range”
Explanation: The isValid method checks if the number is between 1 and 100. Since value is 50, it will return true, and the message “The value is within range” will be printed.

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

Which of the following statements about local variables in Java is true?

A) Local variables are initialized automatically with default values
B) Local variables exist throughout the program’s execution
C) Local variables exist only within the method and are destroyed when the method ends
D) Local variables are shared between methods

A

C) Local variables exist only within the method and are destroyed when the method ends
Explanation: Local variables are created when the method starts and are destroyed when the method ends. They are not accessible outside the method.

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

What will happen if a method calls another method with a throws IOException clause but doesn’t declare it in the calling method?

A) The code will run without any issues
B) A compile-time error will occur
C) The method will automatically handle the exception
D) The program will crash at runtime

A

B) A compile-time error will occur
Explanation: If a method calls another method that throws an exception, the calling method must either handle the exception or declare it using the throws clause.

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