Here are some questions with answers, two of which seem too close to call. And what is more chatgpt 3.5 answers are at odds with 4.0 so I am confused even more.
Q1 - Which code fragment can be inserted at line nl to enable the code to print true?
A. public int equals (Student s) {
return studentName.compareTo(s.studentName);
}
B. protected boolean equals (Object obj) {
return true;
}
C. boolean equals (Object obj) {
if (obj = null) {
return false;
}
if (!(obj instanceof Student)) {
return false;
}
D. Student obj1 = (Student) obj
return this.studentName = obj1.studentName;
public boolean equals (Object obj) throws NullPointerException {
return true;
is correct answer A or C?
But C is correct only if following corrected code is used
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Student student = (Student) obj;
return this.studentName.equals(student.studentName);
}
So which option should be considered right in above question.
Q2. Given the information:
Derby database driver is available in the classpath.
The derby database is accessible with derby_url, derby_userName, and derby_password.
and the code fragment:
public static void main(
String[] args) throws SQLException,
ClassNotFoundException {
String mySQLdriver = <<fully qualified path to the MySQL driver>>;
String derby_url = // URL of the derby driver
String derby userName = // user name of the derby driver
String derby password = // password of the derby driver
Class.forName (mySQLdriver);
Connection con = DriverManager.getConnection(derby url, derby userName, derby password);
}
Which statement is true?
A. Only Derby driver is loaded. Derby DBMS connection is established.
B. The program throws an exception at runtime.
C. The program results a compilation error
D. Both the drivers MySQL and Derby are loaded. Derby DBMS connection is established.
A and B both seem to correct but A seems better choice. Am I correct?
Q3.
. Given:
A class Employee implements Serializable { private static final String LOCATIONID private int empid;
}
=
"LOC-01":
private transient String password="password";
public Employee (int empid, String password) { this.empid = empid;
this.password = password;
public void displayEMPDetails() {
System.out.println(empid + ":" + password + ":" + LOCATIONID);
and the code fragment:
ObjectOutputStream outputStream = new ObjectOutputStream("data.dat"); outputStream.writeObject (new Employee (100, "nopassword"));
ObjectInputStream inputStream = new ObjectInputStream("data.dat"); Employee restoredEMP = (Employee) inputStream.readObject(); restoredEMP.displayEMPDetails();
What is the result?
A 100:nopassword: LOC-01
B A compilation error occurs.
C 100:null:LOC-01
is it C or should it be A? C seems to be better choice explanation - When the Employee object is deserialized, the password field, which is marked as transient, will not be restored to its original value ("nopassword") and will instead have its default value (null).
So, the output will be: 100:null:LOC-01
Q4.
Messages.properties:
greet = Welcome!
Messages fr_FR.properties:
greet Bienvenue
Given the code fragment:
public class
Test {
}
public static void main(String[] args) {
// Line nl
Locale.setDefault(locale);
ResourceBundle resource = ResourceBundle.getBundle ("Messages"); System.out.print (resource.getString("greet"));
Which two code fragments, when inserted at Line nl independently, enable the code to print Bienvenue?
A. Locale locale = Locale.FRANCE;
B. Locale locale = new Locale( Locale.FRENCH, Locale.FRANCE);
C. Locale locale = new Locale("fr_FR");
D. Locale locale = Locale.FRENCH;
E. Locale locale = new Locale("FR");
chatGPT 3.5 seems to suggest A and C (Locale locale = new Locale("fr_FR");) which seem to be correct.
but what about A and B Locale locale = new Locale( Locale.FRENCH, Locale.FRANCE); >> not sure if using Locale twice is correct specially Locale,FRANCE
any experts want to chime in?