Chapter 5 Practice Questions Flashcards
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
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.
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
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.
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
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.
True or False: In Java, a method’s parameter variables are separate from the arguments passed into it.
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.
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) 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.
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
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.
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
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.
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
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.
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) 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.
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
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.
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
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.
True or False: Local variables in Java are automatically initialized to default values.
False
Explanation: Local variables in Java are not automatically initialized. They must be explicitly given a value before they can be used.
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) 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.
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
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.
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
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.