Java experienced interview question/Java Interview Questions and Answers for Freshers & Experienced

Compare the sleep() and wait() methods in Java

1. sleep() is a blocking operation that keeps a hold on the monitor / lock of the shared object for the specified number of milliseconds.
2. wait(), on the other hand, simply pauses the thread until either (a) the specified number of milliseconds have elapsed or (b) it receives a desired notification from another thread (whichever is first), without keeping a hold on the monitor/lock of the shared object.
sleep() is most commonly used for polling, or to check for certain results, at a regular interval. wait() is generally used in multithreaded applications, in conjunction with notify() / notifyAll(), to achieve synchronization and avoid race conditions.

Posted Date:- 2021-08-18 09:15:27

What is reflection and why is it useful?

The name reflection is used to describe code which is able to inspect other code in the same system (or itself) and to make modifications at runtime.

For example,
say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to

Posted Date:- 2021-08-18 09:13:16

What is JDBC?

JDBC is an abstraction layer that allows users to choose between databases. JDBC enables developers to write database applications in Java, without having to concern themselves with the underlying details of a particular database.

Posted Date:- 2021-08-18 09:11:59

What is Application Context?

On the surface, an application context is the same as a bean factory. Both load bean definitions, wire beans together, and dispense beans upon request. But it also provides:
A means for resolving text messages, including support for
1. Internationalization
2. A generic way to load file resources
3. Events to beans that are registered as listeners

Posted Date:- 2021-08-18 09:11:00

What does System.gc() and Runtime.gc() methods do?

These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.

Posted Date:- 2021-08-18 09:08:01

What is Spring?

Spring is an open source development framework for enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promote good programming practice by enabling a POJO-based (Plain Old Java Object) programming model.

Posted Date:- 2021-08-18 09:07:04

What is JVM? Why is Java called the “Platform Independent Programming Language”?

A 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. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible, because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.

Posted Date:- 2021-08-18 09:02:49

Find all substrings of String in java?

Java program to find all substrings of a String.
For example: If input is “abb” then output should be “a”, “b”,”b”, “ab”, “bb”, “abb”
We will use String class’s subString method to find all subString.

Posted Date:- 2021-08-18 08:56:54

How to check if two Strings are anagram in java?

Anagrams means if two String have same characters but in different order.
For example: Angel and Angel are anagrams
There are many ways to check if Strings are anagrams. Some of them are:
1. Using String methods
2. Using array.sort

Posted Date:- 2021-08-18 08:53:27

How to reverse a String in java? Can you write a program without using any java inbuilt methods?

There are many ways to do it, some of them are:

1. Using for loop
2. Using recursion
3. Using StringBuffer

Posted Date:- 2021-08-18 08:50:51

How objects of a class are created if no constructor is defined in the class?

Even if no explicit constructor is defined in a java class, objects get created successfully as a
default constructor is implicitly used for object creation. This constructor has no parameters.

Posted Date:- 2021-08-18 08:48:46

What's the purpose of using Break in each case of Switch Statement?

Break is used after each case (except the last one) in a switch so that code breaks after the
valid case and doesn't flow in the proceeding cases too.
If break isn't used after each case, all cases after the valid case also get executed resulting in
wrong results.

Posted Date:- 2021-08-18 08:47:35

Why Strings in Java are called as Immutable?

In java, string objects are called immutable as once value has been assigned to a string, it
can't be changed and if changed, a new object is created.
In below example, reference str refers to a string object having value "Value one".
String str="Value One";
When a new value is assigned to it, a new String object gets created and the reference is moved to
the new object.
str="New Value";

Posted Date:- 2021-08-18 08:46:26

When the constructor of a class is invoked?

The constructor of a class is invoked every time an object is created with new keyword.

For example, in the following class two objects are created using new keyword and hence,
constructor is invoked two times.

public class const_example {
const_example() {
system.out.println("Inside constructor");
}
public static void main(String args[]) {
const_example c1=new const_example();
const_example c2=new const_example();
}
}

Posted Date:- 2021-08-18 08:45:08

State some situations where exceptions may arise in java?

1) Accessing an element that does not exist in the array.
2) Invalid conversion of number to string and string to a number.
(NumberFormatException)
3) The invalid casting of class
(Class cast Exception)
4) Trying to create an object for interface or abstract class
(Instantiation Exception)

Posted Date:- 2021-08-18 08:43:02

Can an Interface extend another Interface?

Yes an Interface can inherit another Interface, for that matter an Interface can extend more than
one Interface.

Posted Date:- 2021-08-18 08:42:09

What are the restriction imposed on a static method or a static block of code?

A static method should not refer to instance variables without creating an instance and cannot use
"this" operator to refer the instance.

Posted Date:- 2021-08-18 08:41:31

Which method is used to create the daemon thread?

setDaemon method is used to create a daemon thread.

Posted Date:- 2021-08-18 08:40:48

What is daemon thread?

Daemon thread is a low priority thread, which runs intermittently in the back ground doing the
garbage collection operation for the java runtime system.

Posted Date:- 2021-08-18 08:39:27

Why String is declared final or immutable in java?

There are various reasons to make String immutable.
1. Thread Safe
2. Security
3. Class Loading
4. Cache hash value
5. String pool

Posted Date:- 2021-08-18 08:38:13

Write a Java Program to iterate ArrayList using for-loop, while-loop, and advance for-loop.

We have inserted three elements and printed the size of the ArrayList.
Then, we have used While Loop with an iterator. Whenever the iterator has (next) element, it will display that element until we reach the end of the list. So it will iterate three times.
Likewise, we have done for Advanced For Loop where we have created an object called obj for the ArrayList called list. Then printed the object.
Thereafter, we have put the condition of For Loop where the iterator i is set to 0 index, then it is incremented by 1 until the ArrayList limit or size is reached. Finally, we have printed each element using a get(index) method for each iteration of For Loop.

import java.util.*;

public class arrayList {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("20");
list.add("30");
list.add("40");
System.out.println(list.size());
System.out.println("While Loop:");
Iterator itr = list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("Advanced For Loop:");
for(Object obj : list) {
System.out.println(obj);
}
System.out.println("For Loop:");
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
}
}

Output:

3
While Loop:
20
30
40
Advanced For Loop:
20
30
40
For Loop:
20
30
40

Posted Date:- 2021-08-18 08:34:51

What access modifiers can be used for class?

We can use only two access modifiers for class public and default.
public: A class with a public modifier can be visible

1) In the same class
2) In the same package subclass
3) In the same package nonsubclass
4) In the different package subclass
5) In the different package nonsubclass.

Posted Date:- 2021-08-18 08:31:01

Explain where variables are created in memory?

When we declare variables are created in the stack. So when the variable is out of scope those variables get garbage collected.

Posted Date:- 2021-08-18 08:30:11

In how many ways we can do exception handling in java?

We can handle exceptions in either of the two ways :
1) By specifying a try-catch block where we can catch the exception.
2) Declaring a method with throws clause.

Posted Date:- 2021-08-18 08:29:14

In how many ways we can do synchronization in java?

There are two ways to do synchronization in java:
1) Synchronized methods
2) Synchronized blocks
To do synchronization we use the synchronized keyword.

Posted Date:- 2021-08-18 08:27:36

Can we use catch statement for checked exceptions?

If there is no chance of raising an exception in our code then we can’t declare catch block for handling
checked exceptions. This raises a compile-time error if we try to handle checked exceptions when there is
no possibility of causing an exception.

Posted Date:- 2021-08-18 08:26:51

What is synchronization and why is it important?

The term synchronization is the ability to control the access of multiple threads to shared resources. And it is important because, without it, it is not possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to major errors.

Posted Date:- 2021-08-18 08:25:43

How to sort a collection of custom Objects in Java?

We need to implement comparable interface to custom object class(Lets say Country) and then implement compareTo(Object o) method which will be used for sorting. It will provides default way of sorting custom objects.

Posted Date:- 2021-08-18 08:20:51

. Difference between ‘IS-A’ and ‘HAS-A’ relationship in java?

IS-A relationship HAS- A RELATIONSHIP
Is a relationship also known as inheritance Has a relationship also known as composition or
aggregation.
For IS-A relationship we use extends keyword For Has a relationship we use the new keyword

Posted Date:- 2021-08-18 08:19:59

What are thread groups?

Thread Groups are a group of threads and other thread groups. It is a way of grouping threads so that
actions can be performed on a set of threads for easy maintenance and security purposes.

Posted Date:- 2021-08-18 08:16:30

What is Nested top-level class?

If a class is declared within a class and specify the static modifier, the compiler treats the class just
like any other top-level class. Nested top-level class is an Inner class.

Posted Date:- 2021-08-18 08:15:45

Which class represents the socket that both the client and server use to communicate with each
other?

java.net.Socket class represents the socket that both the client and server use to communicate
with each other.

Posted Date:- 2021-08-18 08:15:14

What is currentThread?

It is a public static method used to obtain a reference to the current thread.

Posted Date:- 2021-08-18 07:19:05

Can we have a try block without catch block?

Each try block requires at least one catch block or finally block. A try block without a catch or finally will
result in a compiler error. We can skip either of catch or finally block but not both.

Posted Date:- 2021-08-18 07:18:29

What value does read Line() return when it has reached the end of a file?

The readLine() method returns null when it has reached the end of a file.

Posted Date:- 2021-08-18 07:17:19

Does garbage collection guarantee that a program will not run out of memory?

No, Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. Java training.

Posted Date:- 2021-08-18 07:16:40

What are the wrapped, classes?

The Wrapped classes are those classes that allow primitive types to be accessed as objects.

Posted Date:- 2021-08-18 07:16:19

What is the catch or declare rule for method declarations?

If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.

Posted Date:- 2021-08-18 07:15:09

How many objects will be created below:

String str1= new String("Peter");
String str2= new String("Peter");

Three objects will be created here, two in heap memory and one in String constant pool.

Posted Date:- 2021-08-18 07:14:24

What is marker interface in java? Can you provide some examples of marker interface?

Marker interfaces are those interfaces which do not have any method in it.

Examples of marker interfaces are : Serializable and Cloneable.

Posted Date:- 2021-08-18 07:13:31

What is Java ClassPath?

ClassPath is environment variable which java virtual machine (JVM) uses to locate all classes which is used by the program.
For example: jre/lib/rt.jar has all java classes and you also need to include jar files or class file which is being used by program.

Posted Date:- 2021-08-18 07:10:05

How can we pass argument to a function by reference instead of pass by value?

In java, we can pass argument to a function only by value and not by reference.

Posted Date:- 2021-08-18 07:08:08

What's the difference between the methods sleep and wait?

The code sleep2000; puts thread aside for exactly two seconds. The code wait2000, causes a wait of
up to two second. A thread could stop waiting earlier if it receives the notify or notifyAll call. The
method wait is defined in the class Object and the method sleep is defined in the class Thread.

Posted Date:- 2021-08-18 07:06:56

What is the difference between a Window and a Frame?

The Frame class extends Window to define a main application window that can have a menu bar.

Posted Date:- 2021-08-18 07:05:43

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits,
it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns.
UTF-16 uses 16-bit and larger bit patterns.

Posted Date:- 2021-08-18 07:05:11

What is a WAR file?

This is Web Archive File and used to store XML, java classes, and JavaServer pages. which is used
to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, static Web
pages etc.

Posted Date:- 2021-08-18 07:04:39

What is the preferred size of a component?

The preferred size of a component the minimum size of the component, enough to display its label is known as the preferred size of the component. For example, the button size should display its label with platform-specific decoration like dotted lines around the label, etc. The FlowLayout manager gives the preferred size to a component.

Posted Date:- 2021-08-18 06:59:10

What is the difference between preemptive scheduling and time slicing?

Under preemptive scheduling, the highest priority task performs until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task performs for a predefined slice of time and then re-enters the pool of ready tasks.

Posted Date:- 2021-08-18 06:57:39

What is a transient variable?

A transient variable is a variable that may not be serialized.

Posted Date:- 2021-08-18 06:56:45

Explain about anonymous inner classes in java?

The inner class defined without any class name is called an anonymous inner class. An inner class is declared and instantiated using the new keyword. The main purpose of anonymous inner classes in java is to provide
interface implementation. We use anonymous classes when we need only one instance for a class.
We can
use all members of the enclosing class and final local variables.
When we compile anonymous inner classes compiler creates two files

Posted Date:- 2021-08-18 06:56:07

Explain method overloading?

Method overloading when a Java program contains more than one methods with the same name but different properties, then it is called method overloading.

Posted Date:- 2021-08-18 06:54:18

What is the right data type to represent a price in Java?

The right data type to represent a price in Java BigDecimal, if memory is not a concern and Performance, is not critical, otherwise double with predefined precision.

Posted Date:- 2021-08-18 06:53:09

Search
R4R Team
R4R provides Java Freshers questions and answers (Java Interview Questions and Answers) .The questions on R4R.in website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,Java experienced interview question,Java Freshers & Experienced Interview Questions and Answers,Java Objetive choice questions and answers,Java Multiple choice questions and answers,Java objective, Java questions , Java answers,Java MCQs questions and answers R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for Java fresher interview questions ,Java Experienced interview questions,Java fresher interview questions and answers ,Java Experienced interview questions and answers,tricky Java queries for interview pdf,complex Java for practice with answers,Java for practice with answers You can search job and get offer latters by studing r4r.in .learn in easy ways .