149k views
1 vote
After the execution of the following code, what will be the value of num if the input values are 0 3? (Assume that console is a Scanner object initialized to the standard input device.)

int num = console.nextInt();
if (num > 0)
num = num + 13;
else
if (num >= 3)
num = num + 15;


a) 0
b) 3
c) 13
d) 15

User Burns
by
5.7k points

1 Answer

2 votes

Answer:

Option a: 0

Step-by-step explanation:

Scanner is a Java class used to read a standard input from user. However, the Scanner object nextInt() method can only read an integer per time. If we input 0 3, the first integer will be taken which is 0.

Since the num = 0, the if block will be ignored. Instead, the else block of codes will run. In the else block, the line num = num + 15 will be skipped as well because the if condition is not met. At the end, the num will remain 0.

User Tushar Joshi
by
4.9k points