Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 38

Given the code fragment:

And given the requirements:

✑ If the value of the qty variable is greater than or equal to 90, discount = 0.5

✑ If the value of the qty variable is between 80 and 90, discount = 0.2

Which two code fragments can be independently placed at line n1 to meet the requirements? (Choose two.)

    Correct Answer: A, C

    To meet the requirements, if the quantity (qty) is 90 or more, the discount should be 0.5, and if the quantity is between 80 and 90, the discount should be 0.2. Option A evaluates if qty is greater than or equal to 90 to set the discount to 0.5, and separately checks if qty is between 80 and 90 to set the discount to 0.2. This meets both requirements. Option C uses a ternary operator to achieve the same logic in a single line by checking if qty is greater than or equal to 90 first, then if it is greater than 80, and finally setting the discount accordingly. Options B, D, and E have logical errors or do not meet the requirements fully.

Discussion
kkaayyyyOptions: AC

A and C are the correct options because in Option B we are using the discount variable twice, and thus only the second discount's value will be the final updated value in Option D only the case where qty >= 90 will work in Option E everytime the output printed will be 0.2 no matter if the condition is 1st or 2nd.

BelloMioOptions: AB

Ok I got why D is wrong. it is wrong in the case if qty is 85 for example. it will go in the first if condition which will make discount = 0.2 all good. Then it will also go in the second if condition where it will go into the else statement and assign discount = 0, making the code not correct

DarGrinOptions: AC

Only A and C are correct

anassasl

why D is not correct ?

montoyamontes

If qty = 80 then the result must be discount=0.2 if(qty> 80 && qty <90 ) { //80>90: true && 80<90: true discount = 0.2 // this is correct }else{ discount =0 //this is ignored } if(qty>=90){ //80>=90:false discount = 0.5 //this is ignored so go to else }else{ // discount=0 } Finally discount=0 != 0.2

Vicky_65Options: AC

thisis correct

carlosworkOptions: AC

Answer is AC. It boring to test... It is necessary to change the value of the variable 'qty' to perform the test. The code can be compiled on the command line and the value passed by "args" or simply change the value of this variable directly in the code. That's it. To test: public static void main(String[] args) { double discount = 0; int qty = Integer.parseInt(args[0]); qty=90; // change here // Answer A if (qty >= 90) { discount = 0.5; } System.out.println(discount); if (qty > 80 && qty < 90) { discount = 0.2; } System.out.println(discount); // Answer C discount = (qty >= 90) ? 0.5 : (qty > 80)? 0.2: 0; System.out.println(discount); }