What is the output of the following code?

deborah1965deborah1965 Posts: 3Registered Users ■□□□□□□□□□
Code:

public class ExamNameFinder {
public static String getExamName(String examId) {

String examName = null;

if (examId == null) {
return "ERROR";
}

switch (examId) {

case "1Z0-864": examName = "SCEA 5 Part 1";
break;

case "1Z0-804": examName = "OCP Java SE 7";
break;

case "1Z0-803": examName = "OCA Java SE 7";
break;

default: examName = "ERROR";
break;
}

return examName;
}


public static void main(String[] args) {

String examId = "1Z0-803";
String examName = ExamNameFinder.getExamName(examId);
System.out.println(examName);

}
}

Comments

  • paul78paul78 Posts: 2,860Member ■■■■■■■■■■
    Welcome to TE.

    I noticed your unanswered post so thought I would offer a reply.

    Perhaps if you provided some context including your own thoughts to the problem, there would be more replies. Most forum members will not simply provide an answer to what appears to be a simple homework problem. Myself included.

    Is there some aspect of the case statement that you don't understand?
    Did you already attempt to byte-compile the code and run it through the java intepreter?
  • JDMurrayJDMurray Certification Invigilator Surf City, USAPosts: 11,089Admin Admin
    Once you understand how the switch statement in Java SE 7 works it's a trivial question to answer.

    I hope this isn't an exhibit from a practice exam or ****. ("Moves finger towards big red button...")
  • deborah1965deborah1965 Posts: 3Registered Users ■□□□□□□□□□
    paul78 wrote: »
    Welcome to TE.

    I noticed your unanswered post so thought I would offer a reply.

    Perhaps if you provided some context including your own thoughts to the problem, there would be more replies. Most forum members will not simply provide an answer to what appears to be a simple homework problem. Myself included.

    Is there some aspect of the case statement that you don't understand?
    Did you already attempt to byte-compile the code and run it through the java intepreter?

    Thanks for your tips Mr.Paul
    JDMurray wrote: »
    Once you understand how the switch statement in Java SE 7 works it's a trivial question to answer.

    I hope this isn't an exhibit from a practice exam or ****. ("Moves finger towards big red button...")

    Thanks Admin
  • deborah1965deborah1965 Posts: 3Registered Users ■□□□□□□□□□
    Anyway thanks friends, i got the correct solution to my confusion

    here is the solution

    A switch works with the byte, short, char, and int primitive data types. It also works with enumerated, the String class (introduced in Java SE 7), and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer. The String in the switch expression is compared with the expressions associated with each case label as if the String.equals method were being used.

    So the Correct answer is OCA JAVA SE 7.

    Thanks EPractize Labs Training Lab

    My Friend Suggest me your tool.

    I got the correct answer for my question from your trial version itself. Thanks
  • paul78paul78 Posts: 2,860Member ■■■■■■■■■■
    Glad you were able to figure it out. Java's dynamic typing can sometimes be a bit confusing if you are used to loosely typed languages.

    Good thing JD didnt have to use his big red button. I think it also launches the space shuttle and sets off all the car alarms within a 2 mile radius. icon_wink.gif
  • JDMurrayJDMurray Certification Invigilator Surf City, USAPosts: 11,089Admin Admin
    paul78 wrote: »
    Good thing JD didnt have to use his big red button. I think it also launches the space shuttle and sets off all the car alarms within a 2 mile radius. icon_wink.gif
    Nope, that's Webmaster's Big Red Button. Mine mostly causes UPSes to beep menacingly and small dogs to tilt their heads.
Sign In or Register to comment.