in Education by
I have been using java for awhile now, there is a question bothering me. Whenever I use the return statement for a method in if(), while() or for() loop, I get an error " Missing return statement". I have already given a return statement but it is inside a statement or loop. Then why am I getting an error. Here, is a code as an example. public int example() { if(condition) { return value; } } Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You are getting the error because a function must always return a value. Imagine what will happen if you execute example() and it does not go into “ if ” condition, what will your function return? Compiler needs to know what to return in every situation, hence it gives you the error. You can correct this by either putting an “ else “ condition with a return statement or by putting return statement towards the end of the function. public int example() { int result = -1; if(condition) { result = x; } return result; }

Related questions

0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Which mode allows us to run program interactively while watching source code and variables during execution? (a) ... Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic class to return and accept any type of object? (a) K ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic class to return and accept a number? (a) K (b) N ... Interfaces & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept any type of object? (a) ... chapter Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept a number? (a) K (b) N ... in portion Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Actually I'm a newbie to the parsing stuff with Python Beautifulsoup4. I was scraping this website. I need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    While shuffling a pack of 52 playing cards, 2 are accidentally dropped. Find the probability that the missing cards to be ... D. 27/51 Select the correct answer from above options...
asked Nov 20, 2021 in Education by JackTerrance
0 votes
0 votes
    What is the default value used by sqoop when it encounters a missing value while importing form CSV file. A - NULL B - null C - space character D - No values...
asked Jan 13, 2021 in Technology by JackTerrance
0 votes
    What does Collections.emptySet() return? (a) Immutable Set (b) Mutable Set (c) The type of Set ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these return type of hasNext() method of an iterator? (a) Integer (b) Double (c) Boolean (d ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these methods return description of an exception? (a) getException() (b) getMessage() (c) ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
...