Answer is A.
To test:
class MyString {
String msg;
MyString(String msg){
this.msg=msg;
}
}
public class Test {
public static void main(String[] args) {
System.out.println("Hello " + new StringBuilder("Java SE 8"));
System.out.println("Hello " + new MyString("Java SE 8").msg);
}
}
The correct answer is the letter A. when we put a "." after a variable we can call a method or an attribute of it. Notice that the "println" method already calls the attributes, so when we instantiate the "new MyString("Java SE 8")" and after placing the ".msg" we are directly accessing the var "msg" which is "Java SE 8" printing the same normally. I tested it if you want:
class MyString {
String msg;
MyString(String msg) {
this.msg = msg;
}
}
public class Test {
public static void main (String[] args) {
System.out.println("Hello " + new StringBuilder("Java SE 8"));
System.out.println("Hello " + new MyString("Java SE 8").msg);
}
}
Correct answer should be A. As the two classes are in the same package (p1.MyString and p1.Test), they don't need to be imported from one another.
upvoted 3 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
MPignaProTech
2 months, 3 weeks agoakbiyik
2 years, 1 month agocarloswork
2 years, 2 months agoiSnover
2 years, 3 months agoalex_au
2 years, 3 months ago