1.
Consider the following program:
class Point2D {
private int x, y;
public Point2D(int x, int y) {
this.x = x;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
public static void main(String []args) {
Point2D point = new Point2D(10, 20);
System.out.println(point.toString());
}
}
Which one of the following options provides the output of this program when executed?
Correct Answer
D. [10,0]
Explanation
The correct answer is [10,0] because the Point2D object "point" is created with the coordinates (10, 20). However, the "toString()" method only returns the x coordinate and the y coordinate, separated by a comma and enclosed in square brackets. Therefore, the output will be "[10,0]".
2.
Explain Runtime Exceptions.
Correct Answer
A. It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compliation.
Explanation
Runtime exceptions are exceptions that occur during the execution of a program and could have been avoided if the programmer had taken proper precautions. Unlike checked exceptions, which need to be explicitly handled or declared, runtime exceptions are not enforced by the compiler and are ignored at the time of compilation. This means that the programmer is not required to catch or declare these exceptions, but they can still occur during runtime if certain conditions are not met.
3.
Which of the following is a collection that contains no duplicate elements?
Correct Answer
C. Set
Explanation
A Set is a collection that contains no duplicate elements. This means that each element in a Set is unique and cannot be repeated. Unlike other collections like Deque, ArrayList, and Map, which can contain duplicate elements, a Set ensures that every element is distinct. Therefore, a Set is the correct choice for a collection that contains no duplicate elements.
4.
Given this three definitions:
interface I1 {}
interface I2 {}
abstract class C {}
which one of the following will compile without errors?
Correct Answer
D. class CI12 extends C implements I1, I2 {}
Explanation
The correct answer is "class CI12 extends C implements I1, I2 {}". In Java, a class can only extend one superclass but can implement multiple interfaces. In this case, the class CI12 extends the superclass C and implements the interfaces I1 and I2, which is allowed and will compile without errors.
5.
Which of the following statements are true regarding Statement and its derived types?
Correct Answer
C. You can get an instance of PreparedStatement by calling preparedStatement() method in the Connection
interface.
Explanation
The correct answer is that you can get an instance of PreparedStatement by calling the preparedStatement() method in the Connection interface. This means that you can create a PreparedStatement object, which is a derived type of Statement, by using the preparedStatement() method provided by the Connection interface. This allows you to execute parameterized SQL queries and updates.
6.
What's the difference between TreeSet and HashSet
Correct Answer
B. Both are Set implementations, however TreeSet is a SortedSet, which means it is elements have a natural order, while HashMap has an unpredictable order
Explanation
The given answer correctly explains the difference between TreeSet and HashSet. It states that TreeSet is a SortedSet, meaning its elements have a natural order, while HashSet does not have a predictable order. This highlights the key distinction between the two implementations.
7.
What do you mean by Object?
Correct Answer
A. Object is a runtime entity and it’s state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.
8.
Consider the following code snippet:try (FileReader inputFile = new FileReader(file)) { //#1 System.out.print( (char)ch ); }}Which one of the following statements can be replaced in the place of statement #1?
Correct Answer
B. While( (ch = inputFile.read()) != -1) {
Explanation
The correct replacement for statement #1 is "while( (ch = inputFile.read()) != -1) {". This is because the FileReader class's read() method returns -1 when it reaches the end of the file. Therefore, the while loop will continue executing as long as there are more characters to read from the file.
9.
Consider the following program:import java.util.ArrayList;class RemoveTest { public static void main(String []args){ ArrayList list = new ArrayList (); list.add(new Integer(2)); list.add(1); list.add(5); list.remove(2); // REMOVE System.out.println(list); }}Which one of the following options correctly describes the behavior of this program?
Correct Answer
B. When executed, this program prints the following: [2, 1].
Explanation
The program creates an ArrayList called "list" and adds three elements to it: an Integer object with a value of 2, the integer 1, and the integer 5. The program then removes the element at index 2, which is the integer 5. Finally, the program prints the contents of the ArrayList, which is [2, 1].
10.
What's the porpouse of the DAO Pattern
Correct Answer
B. Separate the business logic from the data access logic in the application
Explanation
The purpose of the DAO (Data Access Object) pattern is to separate the business logic from the data access logic in the application. This pattern allows for a clear separation of concerns, making the code more modular and maintainable. By encapsulating the data access code in separate DAO classes, it becomes easier to make changes to the data access layer without affecting the rest of the application. This promotes code reusability and improves the overall design of the application.
11.
When we declare a field as volatile, what do we intend to do?class ClassVotile{ volatile boolean field;}
Correct Answer
B. Establish a happens before relationship regarding the reads and writes to the field
Explanation
When we declare a field as volatile, we intend to establish a happens before relationship regarding the reads and writes to the field. This means that any write to the volatile field will be visible to all subsequent reads of the field. It ensures that the changes made by one thread are visible to other threads, preventing any data inconsistencies or race conditions.
12.
Is the following statement true: A Servlet cannot respond to requests, it is meant to connect to the database and serves as a pool of connections
Correct Answer
B. False
Explanation
A Servlet is a Java class that is used to respond to client requests in a web application. It can handle requests and generate responses dynamically. While a Servlet can connect to a database and manage connections, it is not solely meant for that purpose. It can perform a wide range of tasks, such as processing form data, generating HTML content, and interacting with other components in the web application. Therefore, the statement that a Servlet cannot respond to requests and is only meant for connecting to the database is false.
13.
Which of the following option describes a Map in Java?
Correct Answer
A. An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
Explanation
A Map in Java is an object that maps keys to values. It does not allow duplicate keys, meaning each key can only map to one value. This data structure is commonly used to store and retrieve values based on a specific key.
14.
What is the difference between static and non-static variables?
Correct Answer
C. A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.
Explanation
The correct answer explains the difference between static and non-static variables. It states that a static variable is associated with the class as a whole, meaning it is shared among all instances of the class. On the other hand, non-static variables are unique to each object instance and have different values for each instance. This difference highlights the distinction between a variable that is shared among all instances and a variable that is specific to each individual instance.
15.
Consider the diagram, is the following statement true: The Controller cannot be implemented by the developer, and it must not be!
Correct Answer
B. False
Explanation
The statement "The Controller cannot be implemented by the developer, and it must not be!" is false. The Controller is a crucial component in the Model-View-Controller (MVC) architectural pattern, responsible for handling user inputs and updating the model and view accordingly. The developer is responsible for implementing the Controller, as it requires programming logic to interpret user actions and update the system accordingly. Therefore, the statement is incorrect, and the Controller can and should be implemented by the developer.
16.
Which of these packages contain classes and interfaces used for input & output operations of a program?
Correct Answer
C. Java.io
Explanation
The package java.io contains classes and interfaces that are used for input and output operations in a program. This package provides classes for reading and writing data streams, handling files and directories, and performing other input and output related tasks. Therefore, the correct answer is java.io.
17.
Is the following Statement true: InputStreams and OutputStreams are part of the java built-in mechanism for I/O operations
Correct Answer
A. True
Explanation
InputStreams and OutputStreams are indeed part of the Java built-in mechanism for I/O operations. They are used for reading and writing data, respectively, in a streaming fashion. InputStreams allow the program to read data from a source, such as a file or network connection, while OutputStreams enable the program to write data to a destination. These classes provide a convenient and standardized way to perform input and output operations in Java.
18.
ORM stands for________________
Correct Answer
B. Object Relational Mapping
Explanation
ORM stands for Object Relational Mapping. It is a programming technique that allows developers to map objects from a relational database to the objects in their application code. This helps in simplifying the data access layer and eliminates the need for writing complex SQL queries. With ORM, developers can work with objects and their relationships, rather than dealing with low-level database operations. This improves productivity, code maintainability, and reduces the chances of errors.