Browsed by
Tag: latest interview questions on core java in java

Which Java operator is right associative?

Which Java operator is right associative?

The = operator is right associative. In Core Java, none of the operators are right-associative. All Java operators are left-associative, which means that they are evaluated from left to right when they have the same precedence. Right-associative operators would be evaluated from right to left, but Java does not have such operators. In Core Java, the assignment operator (=) is right-associative. Right-associative means that when multiple assignment operators of the same precedence appear in an expression, they are evaluated from right to…

Read More Read More

How are Java source code files named

How are Java source code files named

within the file. A source code file may contain at most one public class or interface. If a public class or interface is defined A Java source code file takes the name of a public class or interface that is defined within a source code file, then the source code file must take the name of the public class or interface. If no public class or interface is defined within a source code file, then the file must take on…

Read More Read More

What is the use if “instanceof” keyword ?

What is the use if “instanceof” keyword ?

instanceof” keyword is used to check what is the type of object. F The instanceof keyword in Java is used to test whether an object is an instance of a particular class, interface, or subclass. It is a binary operator that returns a boolean value – true if the object is an instance of the specified type, and false otherwise. Here’s a basic example: java public class Example { public static void main(String[] args) { String str = “Hello, World!”;//…

Read More Read More

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

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

The read() method returns -1 when it has reached the end of a file. In Core Java, the read() method of the InputStream class returns an integer value. When it reaches the end of a file, it typically returns -1 to indicate the end of the stream. Here’s a simple example: java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class ReadFileExample { public static void main(String[] args) { try (InputStream inputStream = new FileInputStream(“example.txt”)) { int data; while ((data = inputStream.read()) != -1) {…

Read More Read More

How much subclasses you can maximum in Inheritance?

How much subclasses you can maximum in Inheritance?

In one of our old JAVA projects we had an inheritance depth of five. Believe us we never liked that code. It’s bad to have a huge inheritance depth. A maintainable inheritance depth should be maximum 5. Anything above that is horrible. There is no limit as such specified anywhere that there is some limit on the inheritance sub classing but depending on environments you will get stack over flow error.

When can an object reference be cast to an interface reference

When can an object reference be cast to an interface reference

An object reference be cast to an interface reference when the object implements the referenced interface. In Java, an object reference can be cast to an interface reference when the object’s class implements that interface. This is possible because Java supports polymorphism through interfaces. Here’s a brief explanation: When the Object Implements the Interface: If a class implements an interface, you can cast an object of that class to a reference of the interface type. java interface MyInterface { void…

Read More Read More

Why main() in java is declared as public static void main? What if the main method is declared as private?

Why main() in java is declared as public static void main? What if the main method is declared as private?

Public – main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application static – When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class. void – Java is platform independent language therefore if it will…

Read More Read More

How can a GUI component handle its own events

How can a GUI component handle its own events

A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener. In Java, GUI components can handle their own events by implementing event listener interfaces. The most common way to handle events for GUI components is by implementing listener interfaces such as ActionListener, MouseListener, KeyListener, etc. Here is a brief explanation: Implementing Event Listener Interface: To handle events on a GUI component, you need to make your class implement the…

Read More Read More

How many ways can an argument be passed to a subroutine and explain them?

How many ways can an argument be passed to a subroutine and explain them?

An argument can be passed in two ways. They are passing by value and passing by reference. Passing by value: This method copies the value of an argument into the formal parameter of the subroutine. Passing by reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter.

What is the Collection interface

What is the Collection interface

The Collection interface provides support for the implementation of a mathematical bag – an unordered collection of objects that may contain duplicates. In Java, the Collection interface is a part of the Java Collections Framework and is present in the java.util package. It is at the top of the hierarchy of collection interfaces. The Collection interface represents a group of objects, known as elements. It provides a standard set of methods to manipulate collections, such as adding, removing, and querying elements. Some…

Read More Read More