What is the output of the following code?
deborah1965
Registered Users Posts: 3 ■□□□□□□□□□
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);
}
}
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
-
paul78 Member Posts: 3,016 ■■■■■■■■■■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? -
JDMurray Admin Posts: 13,099 AdminOnce 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...") -
deborah1965 Registered Users Posts: 3 ■□□□□□□□□□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.PaulOnce 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 -
deborah1965 Registered Users Posts: 3 ■□□□□□□□□□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 -
paul78 Member Posts: 3,016 ■■■■■■■■■■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. -
JDMurray Admin Posts: 13,099 AdminGood 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.