Telegram Group & Telegram Channel
What is the output of the following program?

public class BearOrShark {
public static void main(String[] args) {
int luck = 10;
if((luck>10 ? luck++: --luck)<10) {
System.out.print("Bear");
} if(luck<10) System.out.print("Shark");
} }


A. Bear
B. Shark
C. BearShark
D. The code will not compile because of line 4.
E. The code will not compile because of line 6.
F. The code compiles without issue but does not produce any output

Explanation:
The code compiles and runs without issue, so options D and E are correct. Remember that only one of the right-hand ternary expressions will be evaluated at runtime. Since luck is not less than 10, the second expression, --luck, will be evaluated, and since the pre-increment operator was used, the value returned will be 9, which is less than 10. So the first if-then statement will be visited and Bear will be output. Notice there is no else statement on line 6. Since luck is still less than 10, the second if-then statement will also be reached and Shark will be output; therefore, the correct answer is option C.



tg-me.com/topJavaQuizExplain/15
Create:
Last Update:

What is the output of the following program?

public class BearOrShark {
public static void main(String[] args) {
int luck = 10;
if((luck>10 ? luck++: --luck)<10) {
System.out.print("Bear");
} if(luck<10) System.out.print("Shark");
} }


A. Bear
B. Shark
C. BearShark
D. The code will not compile because of line 4.
E. The code will not compile because of line 6.
F. The code compiles without issue but does not produce any output

Explanation:
The code compiles and runs without issue, so options D and E are correct. Remember that only one of the right-hand ternary expressions will be evaluated at runtime. Since luck is not less than 10, the second expression, --luck, will be evaluated, and since the pre-increment operator was used, the value returned will be 9, which is less than 10. So the first if-then statement will be visited and Bear will be output. Notice there is no else statement on line 6. Since luck is still less than 10, the second if-then statement will also be reached and Shark will be output; therefore, the correct answer is option C.

BY Explanations “Top Java Quiz Questions”


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/topJavaQuizExplain/15

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

At a time when the Indian stock market is peaking and has rallied immensely compared to global markets, there are companies that have not performed in the last 10 years. These are definitely a minor portion of the market considering there are hundreds of stocks that have turned multibagger since 2020. What went wrong with these stocks? Reasons vary from corporate governance, sectoral weakness, company specific and so on. But the more important question is, are these stocks worth buying?

However, analysts are positive on the stock now. “We have seen a huge downside movement in the stock due to the central electricity regulatory commission’s (CERC) order that seems to be negative from 2014-15 onwards but we cannot take a linear negative view on the stock and further downside movement on the stock is unlikely. Currently stock is underpriced. Investors can bet on it for a longer horizon," said Vivek Gupta, director research at CapitalVia Global Research.

telegram from cn


Telegram Explanations “Top Java Quiz Questions”
FROM USA