Banner_Ads

Interview Question-Answers | Tutorial For Beginners


tech4allsa2z:This blog provides you tutorial and most common Interview questions for beginners related to the following technology like java, python.

Top 10 Core Java Interview Questions and Answers for Freshers

Core Java- Interview Questions and Answers for Freshers

  

Is it possible to have multiple main methods in the same class?  


Answer. No, you can't have multiple main () methods in the same class. If you do this the program will fail to compile. The compiler throws an error that the main her method is already defined in the class.

 

Do we need to import the java.lang package at any time? Why?


Answer. No, you don't need to import the java.lang package into your program. The JVM will automatically load it into your program. The JVM imports the java.lang package by default. This package contains many components commonly used in Java programs. Java would be useless without many of the features of the java.lang package. For the same reason, java. lang is implicitly imported by every program's Java compiler.

What is the sleep () and wait () methods in Java?

Answer. sleep () is a method used to suspend a process for a few seconds or any amount of time. However, for the wait () method, the thread will wait and will not automatically return until you call notification () or NotifyAll ().  The main difference is that:

 sleep () does not release locks or monitors while waiting, whereas wait () releases locks or monitors.

 wait () is used for communication between threads and sleep () is typically used to pause execution.

When is it appropriate to use transient keyword with a variable in Java?


Answer. If a class that implements the Serializable interface needs to deserialize a variable, using a temporary variable is appropriate. If you don't want a particular variable to be serializable, even if it resides in a class that implements the Serializable  interface, you can declare the variable with the transient keyword to achieve the same purpose.

 

How can you prevent a class from being inherited in Java?

Answer. We can prevent a class from being subclasses or inherited by another class in two ways:

1.      Making the constructor of the class as private

class demo

{

 private demo ()//private constructor

     {

     }

}

2.     Declaring the class with the final keyword.

final class demo // class with final keyword

{

}

 

 

What are Generics in Java?


Answer. Java language provides the Generics that allows us to create a single class, interface, and method in java, that can be used with different types of data (objects).   This helps us to reuse our java code. Generics does not work with primitive types like int, float, char, etc.

 

What is the need for threads in Java?


Answer. To start your program, you need to use threads in core Java. Threads are lightweight processes that help execute tasks in parallel. Threads work independently to ensure maximum CPU utilization and improve CPU performance.   Threads are used to speed up Java applications by doing more than one thing at a time. Threads are useful for achieving parallelism in Java programs. CPUs are very fast and contain multiple cores these days, so you can't take advantage of all the cores with just one thread. That means expensive hardware sits idle most of the time.

 

How can you create a thread in Java?


Answer. There are two ways to create threads in Java. One using the Runnable interface and another by extending the Thread class.

Runnable interface
Java program to create a thread by implementing the Runnable interface.

Public class MyThread implements Runnable //implementing Runnable interface

{

            Thread th;

            MyThread ()

            {

                        //constructor

            }

}

Extending Thread Class

Java program to create a thread by extending Thread class.

Public class MyThread extends Thread //extending Thread Class

{

            MyThread ()

            {

                        //constructor

            }

 

}

 

What is a ThreadFactory?


Answer. ThreadFactory is a Java interface used to create threads instead of creating threads explicitly using new Thread (). An object that creates new threads as needed. Thread factories remove the wiring of calls to new threads, allowing applications to use specific thread subclasses, priorities, etc.

 

What are the advantages of multithreading in Java?

Answer. Multithreading allows multiple threads of a program to run simultaneously. A thread is a lightweight process that can be used within a process. Therefore, multithreading allows you to make the most of your CPU through multitasking. The benefits of multithreading in Java are: 

• Sharing Resources

• Using Multiprocessor Architectures. 

• Improved performance by reducing development time.

• Simplified and optimized program coding. 

• Improved GUI responsiveness. 

• Simultaneous and parallel occurrence of tasks. 

• Resource utilization allows better utilization of cache memory. 

• Reduced maintenance costs. 

• Productive use of CPU resources.

 

What is the use of notify () method in Java?


Answer. The notify () method of the thread class wakes up a single thread. The notify () method only issues notifications to threads waiting on a particular resource or object. If this method is used and multiple threads are waiting for the notification, only one thread will receive the notification and the rest of the threads must wait for the notification as well.

 

What is the priority of a thread? How can you change the priority of a thread?

Answer. In Java each thread has priority. Thread priority is represented by a number from 1 to 10. In most cases, the thread scheduler schedules threads according to priority. This is called pre-emptive scheduling. However, there are no guarantees as the schedule she chooses depends on the specs of her JVM.

The Thread class provides three constant properties for thread priority.

 1. public static int MIN_PRIORITY (integer value 1)

2. public static int NORM_PRIORITY (integer value 5) default priority

3. public static int MAX_PRIORITY (integer value 10)

Use the setPriority () method of the Thread class to set the thread priority to be changed.

Use the getPriority () method to get the priority of the thread

 

What is the difference between the Runnable interface and the Thread class in Java?

Answer. Runnable is an interface that represents a task that can be executed using threads or executors. The Thread class creates new threads. But implementing the Runnable interface does not create a new thread. 

• If you extend the Thread class, you cannot extend other classes because Java does not support multiple inheritance. But implementing the Runnable interface creates space for classes to extend other classes. 

• Extending the Thread class creates and connects each unique Thread object. Implementing the Runnable interface allows multiple threads to share the same object.

What is Demon Thread?

Answer. Daemon threads are background service threads that run as low-priority threads and perform background operations such as garbage collection. The JVM exits when only daemon threads remain. The setDaemon () method of the Thread class is used to mark/set a particular thread as a daemon thread or a user thread. If all running threads are daemon-only threads, the Java Virtual Machine exits. This method is always called before the thread is started.


👉Important Core Java Interview questions and answers

👉Java Exception handling Interview Questions and Answers

👉Oops Interview Questions in Java for Freshers

👉Java Collection Framework Interview questions for Freshers

👉Java9 Interview Questions & Answers for beginners

👉Java8 Interview Questions & Answers for beginners 


Post a Comment

0 Comments