98-388 Exam QuestionsBrowse all questions from this exam

98-388 Exam - Question 28


HOTSPOT -

You are interviewing for a job as a Java developer. You need to demonstrate your understanding of switch statements.

For each of the following code segments, select Yes if the code segment can be changed to a switch statement with up to three case statements. Otherwise, select No.

NOTE: Each correct selection is worth one point.

Hot Area:

Exam 98-388 Question 28
Show Answer
Correct Answer:
Exam 98-388 Question 28

References:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Discussion

5 comments
Sign in to comment
mtmoore
Aug 24, 2020

No, Yes, No. You can only test for equality in switch cases and the instructions said "up to 3" cases. The middle example is doable with cases for A and B and a default case. You would have to make a case for every possible year over 25 for the first example and all the possible fractional GPAs between 4.0 and 3.0 for the last example.

Abhitera
Feb 10, 2021

Correct

atro1233
Sep 18, 2020

No, Yes, No

rodrigoandrade
Jun 21, 2021

Yes, Yes, Yes. Here is the example: public double discountByAge(int age) { double discount = -1.00; switch((age >= 25) ? 0 : (age >= 21 && age < 25) ? 1 : 2) { case 0: discount = 0.50; break; case 1: discount = 0.25; break; case 2: default: discount=0.0; break; } return discount; }

mrunmayi
Sep 15, 2020

switch doesn't work with double and string as well(except wrappers) in middle example switch holds string and in 3rd example switch holds double

alejandro355416
Apr 28, 2021

int age; switch(age){ case(age >= 25): d = .5; break; case(age >= 21): d=.25; break; default: d=0 } YES char grade; switch(grade){ case(grade == A): message = "exceeds"; break; case(grade == B): message = "standard"; break; default: message = "improve"; YES -Does not work with doubles NO

Mehal
Jun 3, 2021

First one is wrong give us compiletime error

Girul
Oct 15, 2021

Try it! double gpa = 3.2; int priority = 0; switch (gpa==4.0?1:gpa>=3.5?2:gpa>=2.5?3:0) { case 1: priority = 1; break; case 2: priority = 2; break; case 3: priority = 3; break; } System.out.println(priority);