Choose the correct relationship which is present in java

  1. Choosing the Right Java Collection
  2. Map Interface in Java
  3. Java MCQ (Multiple Choice Questions)
  4. Inheritance test
  5. Constructing a JPA Query Between Unrelated Entities
  6. Java Operators: Arithmetic, Relational, Logical and more
  7. What are Java parent and child classes in Java


Download: Choose the correct relationship which is present in java
Size: 32.52 MB

Choosing the Right Java Collection

It's very useful to know the organization of the collection interfaces and classes in the Java library before trying to use them efficiently. The List, Queue interfaces extend the Collection. Maps in the Java library are not treated as regular collections, so the Map interface doesn't extend Collection. Here's the diagram for interface relationships in the Java library: Any concrete collection implementation (collection class) is derived from one of the collection interfaces. The semantics of collection classes are defined by their interfaces, as concrete collections provide specific implementations for operations that their parent interfaces define. Consequently, we need to choose the proper collection interface before selecting the suitable collection class. 3. Choose the Right Collection Interface To summarize, we use lists when the insertion order of elements matters and there are duplicate elements. Sets are used when elements are treated as a set of objects, there are no duplicates, and the insertion order doesn't matter. Queues are used when LIFO, FIFO, or removal by priority semantics is required, and finally, maps are used when the association of keys and values is needed. 4. Choose the Right Collection Implementation Below we can find the comparison tables of collection classes separated by the interfaces they implement. The comparisons are made based on common operations and their performance. Specifically, the performance of operations is estimated using Big-O ...

Map Interface in Java

Maps are perfect to use for key-value association mapping such as dictionaries. The maps are used to perform lookups by keys or when someone wants to retrieve and update elements by keys. Some common scenarios are as follows: • A map of error codes and their descriptions. • A map of zip codes and cities. • A map of managers and employees. Each manager (key) is associated with a list of employees (value) he manages. • A map of classes and students. Each class (key) is associated with a list of students (value). Creating Map Objects Since Map is an Syntax: Defining Type-safe Map Map hm = new HashMap(); // Obj is the type of the object to be stored in Map Characteristics of a Map Interface • A Map cannot contain duplicate keys and each key can map to at most one value. Some implementations allow null key and null values like the • The order of a map depends on the specific implementations. For example, • There are two interfaces for implementing Map in Java. They are Map and Methods in Java Map Interface Method Action Performed This method is used in Java Map Interface to clear and remove all of the elements or mappings from a specified Map collection. This method is used in Map Interface in Java to check whether a particular key is being mapped into the Map or not. It takes the key element as a parameter and returns True if that element is mapped in the map. This method is used in Map Interface to check whether a particular value is being mapped by a single or more than one ...

Java MCQ (Multiple Choice Questions)

Answer: (a) Bytecode is executed by the JVM. Explanation: The output of the Java compiler is bytecode, which leads to the security and portability of the Java code. It is a highly developed set of instructions that are designed to be executed by the Java runtime system known as Java Virtual Machine (JVM). The Java programs executed by the JVM that makes the code portable and secure. Because JVM prevents the code from generating its side effects. The Java code is portable, as the same byte code can run on any platform. Hence, the correct answer is option (a). 2) Which of the following is not a Java features? • Dynamic • Architecture Neutral • Use of pointers • Object-oriented Show Answer Workspace Answer: (c) Use of pointers Explanation: The Java language does not support pointers; some of the major reasons are listed below: • One of the major factors of not using pointers in Java is security concerns. Due to pointers, most of the users consider C-language very confusing and complex. This is the reason why Green Team (Java Team members) has not introduced pointers in Java. • Java provides an effective layer of abstraction to the developers by not using pointers in Java. Java is dynamic, architecture-neutral, and object-oriented programming language. Hence, the correct answer is option (c). 3) What should be the execution order, if a class has a method, static block, instance block, and constructor, as shown below? public class First_C • Instance block, method, static block...

Inheritance test

A. "X extends Y" is correct if and only if X is a class and Y is an interface. B. "X extends Y" is correct if and only if X is a interface and Y is a class. C. "X extends Y" is correct if X and Y are either both classes or both interface. D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces. Q. Which letters will be printed when the given program is run? public class MyClass Q. What will be the Output? class Animal

Constructing a JPA Query Between Unrelated Entities

Let's start by adding the necessary dependencies to our pom.xml. Then, we add a dependency for the org.hibernate.ormhibernate-core6.2.0.Final The jakarta persistence API comes as a transient dependency to the hibernate-core. And finally, we add some querydsl-jpa: com.querydslquerydsl-aptjakarta5.0.0providedcom.querydslquerydsl-jpajakarta5.0.0 Adding jakarta.xml.bindjakarta.xml.bind-api4.0.0 3. The Domain Model The domain of our example is a cocktail bar. Here we have two tables in the database: • The menu table to store the cocktails that our bar sells and their prices, and • The recipes table stores the instructions for creating a cocktail These two tables are not strictly related to each other. A cocktail can be in our menu without keeping instructions for its recipe. Additionally, we could have available recipes for cocktails that we don't sell yet. In our example, we are going to find all the cocktails on our menu that we have an available recipe. 4. The JPA Entities @Entity @Table(name = "menu") public class Cocktail The first annotation is Recipe entity. Next, we annotate the recipe field with the @NotFound(action = NotFoundAction.IGNORE) Hibernate annotation. This tells our ORM to not throw an exception when there is a recipe for a cocktail that doesn't exist in our menu table. The annotation that associates the Cocktail with its associated Recipe is @JoinColumn. By using this annotation, we define a pseudo foreign key relationship between the two entities. Since w...

Java Operators: Arithmetic, Relational, Logical and more

• • Java Introduction • Java Hello World • Java JVM, JRE and JDK • Java Variables and Literals • Java Data Types • Java Operators • Java Input and Output • Java Expressions & Blocks • Java Comment • Java Flow Control • Java if...else • Java switch Statement • Java for Loop • Java for-each Loop • Java while Loop • Java break Statement • Java continue Statement • Java Arrays • Java Arrays • Multidimensional Array • Java Copy Array • Java OOP (I) • Java Class and Objects • Java Methods • Java Method Overloading • Java Constructor • Java Strings • Java Access Modifiers • Java this keyword • Java final keyword • Java Recursion • Java instanceof Operator • Java OOP (II) • Java Inheritance • Java Method Overriding • Java super Keyword • Abstract Class & Method • Java Interfaces • Java Polymorphism • Java Encapsulation • Java OOP (III) • Nested & Inner Class • Java Static Class • Java Anonymous Class • Java Singleton • Java enum Class • Java enum Constructor • Java enum String • Java Reflection • Java Exception Handling • Java Exceptions • Java Exception Handling • Java try...catch • Java throw and throws • Java catch Multiple Exceptions • Java try-with-resources • Java Annotations • Java Annotation Types • Java Logging • Java Assertions • Java List • Java Collections Framework • Java Collection Interface • Java List Interface • Java ArrayList • Java Vector • Java Stack • Java Queue • Java Queue Interface • Java PriorityQueue • Java Deque Interface • Java LinkedList • Java ArrayDe...

What are Java parent and child classes in Java

Java supports inheritance, an OOPs concept where one class acquires the members (methods and fields) of another. You can inherit the members of one class from another, use the extends keyword as: class A extends B Output Hello this is the method of the super class Hello this is the method of the sub class