What will the value of c be as a result of executing the following code? Int a =
ID: 3820095 • Letter: W
Question
What will the value of c be as a result of executing the following code?
Int a = 4, b = 22;
Double c;
c = (double) ( b / a ) – b % a;
3.0
3.5
What will the value of k be as a result of executing the following code?
int i = 15;
int j = 15;
Double k = 36.0;
k = i + j % 2 + k / (i – 5);
3.6
19.6
What is the result of the following expression?
21 / 2 + 22 % 5 * 3
16
22
23
What would the following code output?
double checking = 50.0;
int m = 2; do{
m++;
checking = checking - 3 * m;
} while (checking >= 30;
System.out.println(m);
A.4
B.6
C.7
What does the following code output?
int price = 10, base = 15;
if (price > base)
System.out.println("Buy");
else if (price < 12)
System.out.println("Sell");
else
System.out.println("Unknown");
Buy
Sell
Explanation / Answer
1. What will the value of c be as a result of executing the following code?
int a = 4, b = 22;
double c;
c = (double) (b / a) – b % a;
3.0
3.5
Answer: 3.0
Explanation: First b / a is evaluated, 22 / 4 is 5 as both b and a are integers, now 5 is converted to double which is 5.0
Now b % a is evaluated, which is 22 % 4, the remainder is 2
Now c will be 5.0 – 2 which is 3.0
2. What will the value of k be as a result of executing the following code?
int i = 15;
int j = 15;
double k = 36.0;
k = i + j % 2 + k / (i – 5);
3.6
19.6
Answer: 19.6
Explanation: First (i-5) is evaluated, which is 15-5 = 10
Now k / (i-5) is evaluated, 36.0 / 10 = 3.6
Now j % 2 is evaluated which is 15 % 2 = 1 (remainder after 15 divided by 2)
Now finally i + j % 2 + k / (i – 5) = 15 + 1 + 3.6 = 19.6
3. What is the result of the following expression?
21 / 2 + 22 % 5 * 3
16
22
23
Answer: 16
Explanation: Expression evaluations follow BODMAS rule
As per BODMAS rule 21 / 2 is first evaluated which is 10
Next 22 % 5 is evaluated which is 2
Now 22 % 5 * 3 is evaluated which is 2 * 3 = 6
Finally the complete expression is evaluated as 10 + 6 = 16
4. What would the following code output?
double checking = 50.0;
int m = 2; do{
m++;
checking = checking - 3 * m;
} while (checking >= 30);
System.out.println(m);
A.4
B.6
C.7
Answer: A. 4
Explanation: In the given we have checking = 50.0 and m = 2
Next we have a do while loop which iterates until checking > =30
Iteration 1: m is incremented by 1, now m will be 3
Next checking = checking – 3* m = 50.0 – 9
checking = 41
Now the condition checking > =30 is checked, this is true and the loop continues
Iteration 2: m is incremented by 1, now m will be 4
Next checking = checking – 3* m = 41 – 12
checking = 29
Now the condition checking > =30 is checked, this is false and the loop terminates
Finally we are printing m, as the value of m is now 4, 4 is printed
5. What does the following code output?
int price = 10, base = 15;
if (price > base)
System.out.println("Buy");
else if (price < 12)
System.out.println("Sell");
else
System.out.println("Unknown");
Buy
Sell
Answer: Sell
Explanation: We have price = 10 and base = 15
Next we have if condition here price > base is checked, which is 10 > 15 which is false, hence it goes to the else part, here again we have one more if condition if(price < 12) this is true
And the statements in the else part are executed and prints Sell the rest of the statements are discarded.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.