Void Methods MCQ
What is the return type of a void method in Java?
A. int
B. void
C. double
D. No return value
Which of the following correctly defines a void method in Java?
A. public void myMethod() { return 0; }
B. public int myMethod() { System.out.println("Hello"); }
C. public void myMethod() { System.out.println("Hello"); }
D. public void myMethod() { return "Hello"; }
Can a void method in Java use the return statement?
A. Yes, with a value
B. Yes, without a value
C. No, it cannot use the return statement
D. Only if it returns null
What happens if you try to return a value from a void method in Java?
A. The value is ignored
B. It will compile but throw an exception at runtime
C. It will cause a compile-time error
D. It will cause a runtime error
Which of the following can be a correct implementation of a void method in Java?
A. void myMethod() { return 1; }
B. void myMethod() { return; }
C. void myMethod() { System.out.println("Hello"); }
D. Both B and C
Which statement is true about void methods in Java?
A. They must return a value
B. They cannot have parameters
C. They cannot return any value
D. They must be declared static
Can a void method perform operations and still not return any value?
A. Yes
B. No
What is the default return value of a void method in Java?
A. 0
B. null
C. Nothing
D. -1
In a void method, how can you exit the method prematurely?
A. By using break
B. By using continue
C. By using return
D. By using exit
Which of the following method signatures is a correct void method in Java?
A. public void method() { }
B. public int method() { }
C. void public method() { }
D. public void method(int x) { return x; }
Answers:
D. No return value
C. public void myMethod() { System.out.println("Hello"); }
B. Yes, without a value
C. It will cause a compile-time error
D. Both B and C
C. They cannot return any value
A. Yes
C. Nothing
C. By using return
A. public void method() { }
No comments:
Post a Comment