JavaRush /Java Blog /Random EN /Coffee break #58. 20 Important Interview Questions a Java...

Coffee break #58. 20 Important Interview Questions a Java Developer Should Know About. Benefits of knowing multiple programming languages

Published in the Random EN group

20 Important Interview Questions a Java Developer Should Know About

Source: Dev.toCoffee break #58.  20 Important Interview Questions a Java Developer Should Know About.  Benefits of knowing multiple programming languages ​​- 1

1. What are the two types of exceptions in Java? What is the difference between them?

Answer: There are two types of exceptions in Java: checked and unchecked exceptions.
  1. Unchecked exceptions do not need to be declared in a method or constructor keyword statement if they can be thrown when the method or constructor executes and propagate beyond the method or constructor boundary.
  2. On the other hand, checked exceptions must be declared in the constructor's method or keyword statement.

2. What is JVM? Why is Java called a "platform independent programming language"?

Ans: Java Virtual Machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file, which is executed by the JVM. The Java language is designed to create application programs that can run on any platform, without the need to rewrite or recompile for each individual platform. The Java Virtual Machine makes this possible because it is aware of the specific instruction length and other features of the underlying hardware platform.

3. What is the difference between an applet and a Java application?

Answer:
  • Applets run in a Java-enabled browser window.
  • A Java application is a standalone Java program that can run outside of the browser.
Both of them require a Java Virtual Machine (JVM). However, a Java application requires a main method with a specific signature to begin executing. Java applets do not need such a method to begin execution. Also, Java applets typically use a restrictive security policy, while Java applications typically use a more relaxed security policy.

4. What is the difference between JDK and JRE?

Answer:
  • The Java Runtime Environment (JRE) is basically a Java Virtual Machine (JVM) that runs your Java programs. It also includes browser plugins to run the applet.
  • The Java Development Kit (JDK) is a full-featured software development kit for the Java language, including the JRE, compilers, and tools (such as JavaDoc and Java Debugger) to enable the user to develop, compile, and run Java applications.

5. What is a servlet?

Ans: A servlet is a class of the Java programming language used to process client requests and create dynamic web content. Servlets are primarily used to process or store data submitted by an HTML form, provide dynamic content, and manage state information that is not present in stateless HTTP.

6. What is a JSP page?

Answer: Java Server Page (JSP) is a text document that contains two types of text:
  • static data,
  • JSP elements.
Static data can be expressed in any text format, such as HTML or XML. JSP is a technology that mixes static content with dynamically generated content.

7. What are directives? What types of directives are available in JSP?

Ans: Directives are instructions that are processed by the JSP engine when the page is compiled into a servlet. Directives are used to set page-level instructions, insert data from external files, and specify custom tag libraries. Directives are defined between <%@ and %>. The different types of directives are shown below:
  • Include directive: Used to include a file and combine the contents of the file with the current page.
  • Page Directive: It is used to define certain attributes on a JSP page such as error page and buffer.
  • Taglib: Used to declare a custom taglib that is used on the page.

8. What do the System.gc() and Runtime.gc() methods do?

Answer: These methods can be used as a hint to the JVM to trigger garbage collection. Typically, the Java Virtual Machine (JVM) runs garbage collection periodically or when free memory reaches low levels.

9. What differences are there between HashMap and Hashtable?

Answer: There are several differences between HashMap and Hashtable in Java:
  1. Hashtable is synchronized whereas HashMap is not. This makes HashMap better for non-threaded applications, since unsynchronized objects generally perform better than synchronized ones.
  2. Hashtable does not allow null keys or values. HashMap allows you to use one null key and any number of null values.
  3. One of the subclasses of HashMap is LinkedHashMap, so if you want a predictable iteration order (default insertion order), you can easily swap HashMap for LinkedHashMap. This wouldn't be so easy if you were using Hashtable.

10. What is JDBC?

Answer: JDBC is an abstraction layer that allows users to choose between databases. With JDBC, developers can write database applications in Java without worrying about the details behind a particular database.

11. What does the "static" keyword mean? Can you override a private or static method in Java?

Answer: The static keyword means that a variable member or method can be accessed without requiring an instance of the class to which it belongs . The user cannot override static methods in Java because method overriding is based on dynamic binding at runtime and static methods are permanently bound at compile time. A static method is not associated with any instance of the class, so this concept is not applicable.

12. What is the significance of a finally block when handling exceptions?

Answer: A finally block will always be executed, regardless of whether an exception is thrown. Even if there is no catch statement and an exception occurs. One last thing to mention is that the finally block is used to release resources such as I/O buffers, database connections, etc.

13. What is the difference between Exception and Error in Java?

Answer: Error is a critical condition that cannot be handled by program code. An Exception is an exceptional situation that can be handled by program code.

14. When does an object become eligible for garbage collection in Java?

Answer: A Java object is subject to garbage collection when it becomes unavailable to the program that is currently using it.

15. What is an iterator?

Answer: The Iterator interface provides a number of methods that can iterate through any collection . Every Java collection contains an Iterator method that returns an Iterator instance . Iterators can remove elements from the underlying collection during iteration.

16. What is passing by reference and passing by value?

Answer:
  • When an object is passed by value , it means that a copy of the object is passed. This way, even if changes are made to this object, it will not affect the original value.
  • When an object is passed by reference , it means that the object is not actually passed, but rather a reference to the object is passed. This way, any changes made by the external method are also reflected in all places.

17. What is a Java applet?

Answer: A Java applet is a program that can be included in an HTML page and executed in a Java-enabled client browser. Applets are used to create dynamic and interactive web applications.

18. How does HashMap work in Java?

Answer: HashMap in Java stores key-value pairs. HashMap requires a hash function, uses hashCode and equals methods to set and retrieve elements to and from the collection. When the put method is called , HashMap calculates the hash value of the key and stores the pair at the appropriate index within the collection. If the key exists, its value is updated with the new value. Some important characteristics of HashMap are its capacity, load factor and threshold size variation.

19. What are the main interfaces of the Java Collections Framework?

Ans: The Java Collections Framework is a designed set of interfaces and classes that support operations on collections of objects. The main interfaces that are found in the Java Collections Framework are:
  • A Collection represents a group of objects, known as its elements.
  • A Set is a collection that cannot contain duplicate elements.
  • A List is an ordered collection that can contain repeating elements.
  • A Map is an object that maps keys to values ​​and cannot contain duplicate keys.

20. What data types does Java support? What is Autoboxing and Unboxing?

Answer: The Java programming language supports the following eight primitive data types:
  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char
Autoboxing is an automatic conversion performed by the Java compiler between primitive types and their corresponding object wrapper classes. If the transformation goes in the opposite direction, this operation is called Unboxing . Thanks for reading and good luck on your technical interview!

Benefits of knowing multiple programming languages

Source: Dev.to Personally, I am convinced that every developer should know more than one programming language. In other words, if we are talking about programming languages, the developer must be a polyglot.Coffee break #58.  20 Important Interview Questions a Java Developer Should Know About.  Benefits of knowing multiple programming languages ​​- 2

Primary and secondary languages

To make things easier, let me clarify some of the terms used in this article. I believe that every developer should have one language that he prefers. I call this language the main one. I will call any languages ​​other than the main one auxiliary. Of course, a developer may change his primary language (more than once) throughout his career. This is fine. There are many reasons why this is explained.

Expert

In discussions about polyglot developers, the argument is often made that the developer's knowledge of a second language comes at the cost of being less proficient in their primary language. Proponents of this idea assume that learning each new language takes time, and this time you can no longer spend on acquiring knowledge of your primary language. This line of thinking seems wrong to me. I see another analogy: sports. It is known that playing one sport improves your performance in another. Of course, you can win gold medals in both events, and darts is unlikely to make you a better swimmer. On the other hand, if you are a rock climber, then yoga will help you develop flexibility and balance. Thus, practicing yoga will make you a better climber than you would be without it. I think learning each additional language will often improve your skills in the other languages ​​you know as well.

Wider range of tools

Additionally, knowing multiple programming languages ​​greatly improves your arsenal as a developer. It broadens your horizons and allows you to look at certain things from different perspectives. Let's take an example from everyday life. If you've ever used a screwdriver, you're familiar with Phillips-head screws. At first glance, all Phillips-head screws are the same and it seems that they can all be tightened with one screwdriver. Yes, it happens that it is difficult for you to tighten or unscrew some screw, but in general one screwdriver is enough for you. Coffee break #58.  20 Important Interview Questions a Java Developer Should Know About.  Benefits of knowing multiple programming languages ​​- 3But one day you walk into a tool store, look at all those beautiful screwdrivers, and discover that there are different types of Phillips screwdrivers. You see that the screwdriver you already have is a Phillips (PH) and decide to buy its twin sister, the Pozidriv (PZ). And then it suddenly turns out that for some screws one is better suited, and for others - another. They look the same, but they are not interchangeable . (If you were wondering what the letters PH and PZ on your screwdrivers mean - now you know). With more knowledge and an extra tool, you'll be able to do a better job the next time you need to tighten a Phillips head screw. The same goes for programming languages.

You don't need to pay for additional knowledge

So, I personally don't see the value in learning an additional programming language. Will you be equally excellent at both languages? Probably not, but it's unlikely that's your goal. Do you learn anything new about your primary language? I'm absolutely sure of this. Knowing a new language will reveal the differences between them. And this, in turn, will awaken your curiosity. You'll wonder why things are done differently in a new language and what they do. In conclusion, I will give an example from life. I used one command line tool written in Python. Every time I installed this tool on a new machine, there were problems, and with each update the number of problems increased. If you have Go in your toolbox, you will immediately notice its advantage in this scenario. Go allows you to compile its artifacts into a self-contained binary. You can be sure that once compiled, the binary will work on every machine you compiled it for. So I ported the Python CLI to Go. With this new version, I no longer had to worry about changing the runtime or third party dependencies. This is a great example of choosing the most appropriate tool for the task. And to be able to choose, you need to become a polyglot developer.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION