Write the question and answer only:
The .equals() method compares the Object's values by default.

 True or False?

 

 

  True 
  False 
 
Question 2
15 / 15 pts
What is Junit, and Why would we use Junit testing in our code? (Select All that apply)
  The process of checking the functionality of an application to ensure it runs as per the speciﬁed requirements 
  A unit testing framework for Java programming language 
  A method used to ensure that another part of code works as expected 
  The Junit is only good for UI/UX/web designers 
 
Question 3
15 / 15 pts
What are Parameterized Tests used for in JUnit?
  To run a test once without any arguments. 
  To run a test multiple times with diﬀerent arguments. 
  To run a test once with a ﬁxed set of arguments. 
  To run a test multiple times without any arguments. 
 
IncorrectQuestion 4
0 / 15 pts
Monolithic structures do not support the testing of individual behaviors.

True or False?

  True 
  False 
 
Question 5
15 / 15 pts
How can we test a method declared as protected?
  By using a Reﬂection of the class that contains the tested methods. 
  Methods declared as protected cannot be tested 
  By copying the method content into the test case methods 
  By placing tests in the same package as the class that contains the tested method 
 
Question 6
15 / 15 pts
What is the most likely output/result of the following JUnit tests? (please assume that all necessary imports are correct)

class MyClass {

  public int add(int a, int b) {
    return a + b;
  }
}
 

public class MyTests {

    private final MyClass myClass = new MyClass();

    @BeforeAll
    public void beforeAll() {
        System.out.println("beforeAll/beforeClass
        Finished ");
    }

    @Test
    public void additionTest() {
        assertEquals(2, myClass.add(1, 1));
        System.out.println("additionTest
          Finished ");
    }
}
  beforeAll/beforeClass Finished 
  org.junit.platform.commons.JUnitException: @BeforeAll method 'public void example.MyTests.beforeAll()' must be static 
  beforeAll/beforeClass Finished additionTest Finished 
  additionTest Finished 
 
Question 7
15 / 15 pts
What is the most likely output/result of the following JUnit test? (please assume that all necessary imports are correct)

class MyClass {

  public int add(int a, int b) {
    return a + b;
  }
}
 

public class CalculatorTests {

  private final MyClass calculator = new MyClass();

  @Test
  void additionTest() {
    System.out.println("Test started");
    assertEquals(57, calculator.add(1, 1));
    System.out.println("Test Finished");
  }
}
  Test started but the test will FAIL. (With an AssertionFailedError in the Failure Trace) 
  This code would not compile 
  Test started Test Finished 
 
Question 8
15 / 15 pts
In the following code, the query.setParameter("empId", id) statement substitutes the "id" argument into the query (HQL).  (True / False)  

public Employee findByEmployeeId(Integer id) {
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();

String hql = "FROM Employee e where e.employeeNumber = :empId";

TypedQuery<Employee> query = session.createQuery(hql, Employee.class);
query.setParameter("empId", id);

Employee result = query.getSingleResult();

t.commit();
factory.close();
session.close();

return result;
}

  False 
  True 
 
Question 9
15 / 15 pts
What is the most likely output of the following program? 

"Note: Assuming there is a record with the specified id."

public Customer findByCustomerId( Integer id ) {
     SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    String hql = "FROM Customer c where c.id = :custId";
    TypedQuery<Customer> query = session.createQuery(hql, Customer.class);
    query.setParameter("custId", id);
    Customer result = query.getSingleResult();

    t.commit();
   factory.close();
   session.close();
    return result;
}

  Return single record from the CUSTOMER Table 
  Throw Error 
  None of the mentioned 
  Return all record from CUSTOMER table 
 
Question 10
15 / 15 pts
What is the most likely output of the following program? Please assumes all imports are correct and you have a valid hibernate.cfg.xml or persistance.xml file.

 

MainClass.java

public class MainClass {
  public static void main(String[] args) {
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    Person p1 = new Person();
    p1.setName("Elbert");
    session.save(p1);

    Person p2 = new Person();
    p2.setName("fink");
     session.save(p2);
    t.commit();

    TypedQuery q = session.createQuery("select P from Person P");
    List < Person > data = q.getResultList();
    for (Person e: data) {
      System.out.println(e.getPersonId() + ":" + e.getName());
    }
  }
}


Person.java

@Entity
@Table
public class Person {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int PersonId;
  private String name;

  // Declare Address class for relationship
  @OneToOne(cascade = CascadeType.ALL)
  private Address myaddress;

  public Person(int personId, String name) {
    PersonId = personId;
    this.name = name;
  }

  public Person() {}
  public int getPersonId() {
    return PersonId;
  }

  public void setPersonId(int personId) {
    PersonId = personId;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}
  The Person objects will not be saved into the database but the code will execute without Runtime error 
  Output --> 1:Elbert 2:fink 
  1:Elbert 
  Throw runtime error 
 
Question 11
15 / 15 pts
Which of the following steps are required to properly use JDBC to issue SQL commands in your application? (Select All that apply)
  Predeﬁned any SQL commands to be issued to the database as static ﬁnal Strings 
  Use a Connection to create Statement or Prepared Statement 
  Create a connection to the database 
  Establish and maintain a stable internet connection 
  Be already logged into the database through the Graphical user interface 
  Be already logged into the database through the command-line interface 
  Include the JDBC driver in the application's classpath 
  Provide a URL to the existing database schema to be accessed by your application 
 
Question 12
15 / 15 pts
What would happen if the connections used in JDBC applications were never closed? (Select All that apply)

 

  It would lead to memory leaks 
  The database server would not be able to provide connections for new requests after it reaches its maximum number of simultaneous connections. 
  The data in the database will be corrupted 
  The connection may remain active even after the application exits 
  None of the mentioned - connections will be closed automatically by the JVM 
 
Question 13
15 / 15 pts
Which of the following methods is used to update the entity in the database?

 

  insert() 
  merge() 
  put() 
  add() 
 
Question 14
15 / 15 pts
What is a SessionFactory() in hibernate?

 

 

 

  Hibernate is a  Java object that represents data that can be persisted to the database, where its class represents a table stored in a database, and every instance of that class represents a row in the table. 
  SessionFactory is an interface. It is usually created during application start-up and kept for later use. It uses the hibernate config file to connect with the database and create hibernate sessions. 
  SessionFactory is a logical grouping of user-deﬁned persistable classes with related settings, that are deﬁned in a .xml ﬁle, which has to be located in the META- INF directory in the classpath. 
  SessionFactory object can be created without configuration information in the hibernate.cfg.xml file 
 
IncorrectQuestion 15
0 / 15 pts
The following statement is True or False?

A Session object is used to perform Create, Read, Update, Delete (CRUD) operations for instances of mapped entity classes.

  False 
  True 
 
Question 16
15 / 15 pts
What are some benefits of using hibernate over JDBC?  (Select All that apply)
  Reduced boilerplate code 
  Automatic Object-Relational Mapping 
  Does not require database connection configuration for database query 
  Hibernate provides better migration to other databases 
 
Question 17
15 / 15 pts
In Hibernate, which annotation specifies the primary key on a variable?
  @Table 
  @JoinColumn 
  @PrimaryId 
  @Id 
 
Question 18
15 / 15 pts
_________   annotation is used to map an instance field of the class to a column in the database table, and it is must be placed before the field or the getter method of the field. By default, Hibernate can implicitly infer the mapping based on the field name and field type of the class. But if the field name and the corresponding column name are different, we have to use this annotation explicitly.
  @Id 
  @Column 
  @JoinColumn 
  @Table 
 
IncorrectQuestion 19
0 / 15 pts
What is the difference between the executeQuery(String SQL) and the executeUpdate(String SQL) of the Statement interface?
  The executeUpdate(String SQL) method is most often is used to execute DML statements (INSERT, UPDATE and DELETE) and it returns a ResultSet object containing data requested by executed SQL statement. The executeQuery(String SQL) method is used to retrieve data from the database by executing a DQL(SELECT ) statement and it returns an int value representing the count of the rows aﬀected by its execution. 
  The executeUpdate(String SQL) method is used to execute  (INSERT, UPDATE, and DELETE) statements, and it returns an int value representing the count of the rows aﬀected by its execution. The executeQuery(String SQL) method retrieves data from the database by executing a "SELECT" statement, and it returns a ResultSet object containing data requested by executed SQL statement. 
  The executeQuery(String SQL) method is most often is used to execute DML statements (INSERT, UPDATE and DELETE) and it returns an int value representing the count of the rows aﬀected by its execution. The executeUpdate(String SQL) method is used to retrieve data from the database by executing a DQL(SELECT ) statement and it returns a ResultSet object containing data requested by executed SQL statement. 
  The executeQuery(String SQL) and executeUpdate(String SQL) methods are equivalent and serve the same function on the Statement and the PreparedStatement interfaces respectively. 
 
Question 20
15 / 15 pts
The ________ annotation is used on the Entity classes (model classes) and tells that the classes are entity bean. The _________ annotation is used to define the name of the table used. (Select all that apply.)
  @Table 
  @Entity 
  @Model 
  @Entities