Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

Questions and answers about Core Java Interviews for Freshers as well as Experienced

Q1) What’s the difference between an inner class and a sub-class?

Ans. An Inner class is one that is nested in another class. An Inner Class has access rights for the class nesting it, and can access all variables as well as methods defined in the outer.

A sub-class refers to a class inheriting from a higher class. All fields and methods of its superclass are available to sub-class.

Q2) What are the access specifiers that Java classes use?

Ans. Java’s access specifiers can be described as the keywords before a class that defines its access scope. Access specifiers can be used to define the access scope of classes in one of these ways:

1. Public : Class and Method are accessible from anywhere.

2. Protected.Method.Field is only accessible from the same classes to which they belong, or from sub-classes.

3. Default: Method,Field,class cannot be accessed outside of the native package.

4. Private: Method and Field can be accessed via the same class that they belong.

Q3) What are the purposes of static methods and static variables

Ans. We use static keywords when we need to share methods or variables between multiple objects within a class. This is instead of creating separate copies of each object.

Q4) What’s data encapsulation? And what’s its significance for you?

Ans. Encapsulation can be described as a concept in Object Oriented Programming that combines properties and methods in a single unit.

Encapsulation allows programmers to develop software in a modular way. Each object can have its own set variables and methods, and serve its purposes independently of the others. Data hiding can be achieved by using encapsulation.

Q5) Give an example of its application.

A singleton Java class can only have one example, and all of its methods and variable belong to the same instance. For situations where a class has to have a limited number of objects, singleton classes are useful.

Singleton usage scenarios are those where you can only connect to one database connection due to licensing or driver restrictions.

6) How large are the memory areas that JVM has allocated?

Many types

Class(Methods Area): Class Area stores perclass structure such as runtime constant pool, method data and code for ways.

Heap refers to the runtime memory area that is used to allocate the memory to the objects

Stack Java Stack saves frames. It holds partial results as well as local variables. This stack plays an important role in method return and invocation. Each thread is assigned a private JVM-stack, which is created at the exact same time as the thread. Every method called creates a new frame. Once a method is invoked, the frame is deleted.

Program Counter Register: The PC (program count) register includes the address of the Java Virtual Machine Instruction currently being executed.

Native Method Stack : It contains all the native method used in the app.

More details.

7) What’s a JIT compiler, exactly?

JIT (Just-In-Time) compiler: It’s used to improve performance. JIT compiles components of the bytecode which have the same functionality at the moment. This reduces the time it takes to compile. “Comiler” is a translator, from the instruction sequence of a Java Virtual Machine (JVM), to the instruction sequence of a specific processor.

8) What is the platform exactly?

A platform refers to the hardware or software environment that a piece of software runs on. There are two types platforms: software-based or hardware-based. Java is the software platform.

9) What is the main difference between Java and other platforms?

The Java platform is different from other platforms in the following ways:

Java is the software platform. Other platforms can be the software platforms or hardware platforms.

Java is executed on top other hardware platforms. Other platforms only can have the hardware components.

10) Why Java’s ‘write one and run everywhere’ nature?

The bytecode. Java compiler converts Java program into the classfile (Byte Code), which acts as an intermediate language between source code, machine code. This bytecode isn’t platform-specific and can be executed by any computer.

11) What exactly is a Classloader?

Classloader, a subsystem in JVM, is used to load class files. The classloader is used to load the java program before we start it. Java includes three classloaders.

Bootstrap ClassLoader : This classloader is superclass of Extension classloader. It loads the “rt.jar” file, which contains all Java Standard Edition class files, such as Java.lang package, Java.net package, Java.util Package Classes, Java.io Package Classes, Java.sql Package Classes, and Java.net package.

Extension ClassLoader : It loads jar files from $JAVA_HOME/jre/lib/ext.

System/ApplicationClassLoader: It is the child classloader for Extension classloader. It loads the classes files from the specified classpath. The default setting for the classpath to the current directory is “-cp” or “-classpath”. You can change the path using the “-cp”, or “–classpath” switches. It is also known by the name Application classloader.

12) What are Loops in Java What are the three types?

Ans. Looping is used to repeatedly execute a program or a block. Java has three types of loops:

1) For Loops

Java has for loops that allow you to execute the same statements over and over again for a certain number of times. When the programmers know how many times they will need to execute the statements, loops are used.

2) While Loops

A while loop is when certain statements are repeated until a condition has been satisfied. While loops require that a condition be met before statements can be executed.

3) Do While Loops

While loop is identical to Do While Loop, with the only difference being that condition is checked following execution of block statements. Do while loop statements must be executed at least once.

13) What is an infinite Loop? Q7: What is an infinite Loop?

Ans. An infinite loop is a loop that runs without conditions and can be run infinitely. A statement block can break an infinite loop.

As follows is the declaration of infinite loop:

(;;)
{
// Statements to execute

// Add any loop breaking logic
}

14) What is an object-oriented paradigm?

It’s a programming paradigm that relies on objects having data and methods in the class they belong to. The object-oriented paradigm seeks to combine the benefits of modularity with reusability. To design programs and applications, objects are instances of classes that interact with each other. The object-oriented paradigm has the following characteristics.

Program design follows the bottom-up approach.

With methods to operate on the object’s data, you can focus on data.

This concept includes abstraction and encapsulation, which hide the complexity from the user and only show functionality.

Real-time implementation of abstraction, inheritance, and so on.

Examples of the object-oriented paradigm include Simula and Smalltalk, C++, Python, C#, and many others.

15) What is an object?

The object is a real-time entity that has a state and a behavior. In Java, Object refers to an instance of a class that has the instance variables representing the state and methods representing the object’s behavior. You can create an object for a class by using the keyword new.

16) What’s the difference between object-oriented programming languages and object-based programming languages?

The following are some basic differences between object-oriented and object-based languages.

Object-oriented languages adhere to all OOP concepts, whereas object-based languages don’t.

Object-oriented languages don’t have inbuilt objects whereas Object based languages have them. JavaScript, for instance, has a window object.

C#, JavaScript, Smalltalk and others are examples of object-oriented software. JavaScript and VBScript are examples of object-based languages, respectively.

17) What is the initial value for an object reference that is defined as an instance variable?

Java initializes all object references to null.

Interview Questions for Core Java – OOPs Concepts – Constructor

18) Who is the constructor?

The constructor is a special method used to initialize an object’s state. When the class is created, it is called. The object is allocated memory. The default constructor of the class gets called every time an object is created by using the new keyword. The class name must match the name of the constructor. The constructor cannot have an explicit return type.

19) What is the difference in Java between double and floating variables?

Ans: Java’s float and Double take 4 bytes of memory, respectively. Float refers to a single precision floating point decimal number, while Double refers to a double precision decimal number.

20) What is the Final Keyword in Java? Let’s take an example.

Ans: Java uses the keyword Final to declare a constant. A constant’s value can only be assigned once.

Below is an example of a constant named const_val being declared and given a value:

Private Final int const_val=100

A method that is declared final cannot be overridden or modified by subclasses.

21) What is a ternary operator? Let’s take an example.

Ans: Ternary operator, also known as conditional operator, is used to determine which value to assign to a variable on the basis of a Boolean evaluation. It is denoted as

The following example shows that if rank is 1, status will be assigned the value “Done” or “Pending”.

public class conditionTest {Public class conditionTest|Public Class ConditionTest|public class conditionTest

public static void main(String args[]) {public static void main (String args[]).|public static main(String args[]),|public staticvoid main (String args[]).

String status

int rank = 3.

Status = (rank == 1) Status = (rank == 1)?

System.out.println(status);

}

}

22) How do you generate random numbers using Java?

Ans: Math.random() can be used to generate random numbers within the range of 0.1 to 1.0

Use Random class in package Java.util

23) What is abstraction in Java?

Objects are the building blocks for Object-Oriented Programming. An object can contain some properties or methods. Access modifiers can be used to hide these properties and methods from the outside world. Access modifiers can be used to restrict access to only the required functions or properties of other programs. This is how abstraction works in OOPS.

24) What is Encapsulation?

Encapsulation is used to hide implementation details from users. A data member that is considered private can only be accessed by members of the same class. Private data members (variables) can only be accessed by the same class as their private counterparts.

If we set up public getter/setter methods to update (for instance void setName(String name)) and read (for example string getName()), the private data can be accessed by the outside class via public methods.

25) What is collection in Java?

A collection is a container that groups multiple items into one unit. A collection could be a jar filled with chocolates or a list of names.

Every programming language uses collections. Java came with a few Collection classes – Vector and Stack, Hashtable, Array.

26) What is api in Java?

The Java application programming interface (API), is a list all Java classes that are included in the Java development kit (JDK). It contains all Java classes, packages, interfaces, methods, fields and constructors. These pre-written Java classes offer a lot of functionality for programmers.

27) Is there any value in a constructor’s return?

Answer: Yes, the constructor implicitly returns current instance of the class. (You cannot use an explicit return type when using the constructor. Learn More.

28) Is the constructor inherited?

The constructor does not inherit.

29) Is it possible to make a final constructor?

The constructor cannot be final.

30) What is us of a “void” return type?

Ans: Java’s main() method cannot return data so it is always declared with a “void” return type.

31) What are Java Packages and how do they work? What is the significance of packages

Ans: Package is a Java class that combines interfaces and classes. Packages allow developers to organize their code and make it easier for others to re-use. After code is packaged in Packages it can be imported into other classes and used.

32) Can we declare a class abstract without an abstract method?

 Ans. Yes, we can declare an abstract class without any abstract method.

33) What is the difference in an Abstract class and Interface?

Ans. The main distinction between an interface class and an abstract is that an interface can only declare static methods, without any concrete implementation. While an abstract can have members with all access specifiers including anonymous or private,

One key difference between interfaces, abstract classes and interfaces is that interfaces require interface implementers to implement all interface methods. Interface implementers must implement only the interface implementers’ methods. Interface implementer classes must implement the interface’s methods. A class inheriting from an abstract type does not need to implement the methods of its superclass.

Although a class can implement multiple interfaces, it can only extend one abstract class.

34) What performance implications does Interfaces have over abstract Classes?

Ans: Interfaces perform slower than abstract classes because they require additional indirections. Developers should also remember that a class cannot extend an abstract class, but can implement multiple interfaces.

Interfaces can also present additional challenges for developers. Every interface implemented in a class must be met.

35) Does Importing a package also allow you to import its sub-packages in Java?

Ans: Java permits you to import packages but not sub-packages. If developers need to import them, they will have to do so separately.

For example, a developer might import the package university.

Import university.department. *

36) What is static in Java?

Static members in Java are member that is not associated with an instance.

37) What is package in Java?

Java packages can be used to group related classes.

Built-in Packages are packages that use the Java API.

User-defined Packages (create and manage your own packages).

18. How do you sort an array using Java?

“import Java. util.

Arrays. Sort(array);”

38) What is an abstract Java class?

An abstract class is one that has been defined using the keyword “abstract”. This class can also include abstract methods (methods with bodies), and concrete methods (regular methods with bodies).

39) What is Method?

A method is a block or code that only runs when called. A method can accept data (known as parameters). Methods can be used to perform specific actions and are also called functions.

40) What is the static technique?

An object is more likely to have a static method than a class.

You don’t need to create an object to call static methods.

Static methods can access and modify the value of a static variable.

For more information, click here

41) What restrictions are there for Java static methods?

The static methods are subject to two main limitations.

The static method cannot use non-static data members or call the nonstatic method directly.

This and super are not static, so they cannot be used in static situations.

42) Why is the main method still static?

The object is not necessary to call the static methods. JVM will need to create the object before calling main(). This will allow JVM to allocate more memory. See more.

43) Is it possible to override static methods?

We can’t change static methods.

44) What is a static block?

The static block is used for initializing the static data member. It is executed at the time classloading occurs, before the main method.

class A2{Class A2|Class A2|class A2

staticSystem.out.println(“static block is invoked”);

public static void main(String args[]){public static void main (String args []){public static main(String []){public static empty main(String []){

System.out.println(“Hello main”);

}

}

It’s available for you to test now

Output: A static block is invoked

Hello main

For more information, click here

45) Can we run a program with main() not working?

Ans) It wasn’t possible before JDK1.7, when static blocks were used. It is no longer possible with JDK 1.7. Read More

46) What happens if the static modifier in the signature of the main methods is removed?

Program compiles. But, it throws an error at runtime “NoSuchMethodError.”

47) What’s the difference between static (classic) and instance methods?

Ans: Java main method must be declared as public static to allow any application to run correctly. Developer won’t receive any compilation errors if main method is declared private. However, it will not be executed and will cause a runtime error.

48) Is Java allows you to pass arguments?

Ans: Java allows you to pass arguments to functions only by value, and not by reference.

49) How is an object serialized in Java?

Ans: Java implements an interface called Serializable to serialize objects. All objects in a class that implements serializable interface are serialized, and their state is stored in byte stream.

50) What is Serialization in Java?

Ans: Serialization can be used to transmit data over the network. Serialization saves object’s state and converts it into byte stream. The byte stream is then transferred over the network, and the object is recreated at its destination.

51) Is it mandatory for a Java Try Block to be followed in Java by a Catch Block for exception handling?

Ans: The Try block must be followed by Catch block, Finally block or both. Catch block must be used to catch any exceptions from try block. Alternatively, the Finally block can be used to perform specific tasks before code abortion is performed.

52) Is it possible to skip the Last block of exception if an exception occurs in the exception bloc?

Ans: Control passes to catch block when an exception is raised by Try block. If it does not exist, then finally block will be executed. Final block is executed whenever an exception occurs. The only way to stop the execution of statements in Finally blocks is to abort the code by writing the following line at the end.

System.exit(0)

53) What is an exception in Java?

A program exception is an event that occurs during execution and disrupts the program’s normal flow.

An error in a method results in the creation of an object by the method and its transfer to the runtime. An exception object is an object that contains information about the error. It includes its type and the current state of the program at the time it occurred. Throwing an exception is when you create an exception object and give it to the runtime.

The runtime system attempts find a way to handle an exception thrown by a method. The list of possible “somethings” that could handle the exception is an ordered list of methods called to reach the method where the error occurred. This is called the call stack.

54) Is Java supports inheritance?

Java only supports multiple inheritance via interfaces. A class can implement multiple interfaces, but only one class can be extended. Multiple inheritance is unsupported as it can lead to the deadly diamond problem.

Singleton design patterns are used to limit the instantiation and ensure that only one instance of a class is available in the JVM. A singleton class, in other words, is a class that can only have one object (an instance) per JVM instance.

55) What is an arry?

An array is a container object which holds a fixed amount of values of one type. When an array is created, it is determined its length. It is then fixed. In the main method for the “Hello World!” app, you have already seen an example array. This section will discuss arrays in more detail.

An illustration of an array is 10 boxes, numbered 0-9; an index of zero indicates that the first element of the array.

An array of 10 elements.

An element is an item within an array. Each element can be accessed using its numerical index. The previous illustration shows that

56) What is Java garbage?

Java garbage collection happens automatically. It is not necessary for the programmer to mark objects that should be deleted. JVM is responsible for garbage collection implementation. Every JVM has the ability to implement garbage collection in any way it likes. The only thing that must be met is the JVM specification. There are many JVMs out there, but Oracle’s HotSpot is the most popular. It has a solid and mature collection of garbage collection options.

57) Is it possible to make abstract methods static in Java?

If we make abstract methods static in Java, it will be part of the class and can be directly called, which is completely unnecessary. It is illegal to call an undefined method.

58) Is it possible to declare static variables and methods within an abstract class?

Yes, you can declare static methods and variables in an abstract method. We know that it is not necessary to declare the object in order to access the static environment. Therefore, we can use the name of an abstract class to access the static contextual information declared within the abstract classes. Take the following example.

Abstract class Test

{||

static int i = 102;

static void TestMethod()

{||

System.out.println(“hi !! I am good !! “;

}

}

Public class TestClass expands Test

{||

public static void main (String argus[]).

{||

Test.TestMethod()

System.out.println(“i = “+Test.i);

}

}

Output

hi !! I am good ! I am good!

i = 102

Core Java – OOPs concepts: Interview Questions about Inheritance

59) What’s this keyword in Java?

This keyword refers to the current object. This keyword can be used in Java for many purposes. This keyword can be used to refer the current class properties, such as instance methods and variable, constructors, and so on. It can be passed to constructors or methods as an argument. It can be also returned by the method as the current instance of a class.

Java this keyword

For more information, click here

60) What are the main purposes of this keyword?

These keywords can be used in the following ways:

This can be used as a reference to the current instance variable of a class.

This can be used to invoke the current class method (implicitly).

This() can be used for invoking the current class constructor.

This argument can be passed in the method call.

This argument can be passed in the constructor call.

This can be used to return current class instances from the method.

61) Is it possible for a class to have multiple constructors?

Ans: A class can have multiple constructors that each have different parameters. The arguments used to create objects will determine which constructor is used.

62) Can static methods be overridden by a class?

Ans: Static methods cannot be overridden. Ans: Static methods belong in a class, not individual objects. They are not overridden when the code is run. public class superclass {Superclass of the public class|Public class superclass|Superclass public class

public void displayResult() {

system.out.println(“Printing from superclass”);

}

}

public class subclass extends superclass {Superclass is extended to public class subclass|Public class subclass extends to superclass|Subclasses of public class extend to superclass

public void displayResult() {

system.out.println(“Displaying from subClass”);

super.displayResult();

}

public static void main(String args[]) {public static void main (String args[]).|public static main(String args[]),|public staticvoid main(String args[])

subclass obj = New Subclass()

obj.displayResult()

}

}

Output will be:

Displaying from subClass

Superclass printing

63) Is String a datatype in Java?

Ans: String in Java is not a primitive type of data. A string created in Java is an object of Java.Lang.String. All built-in methods for String class can now be used after the string object is created.

64) What is Java programming?

Java is a powerful programming language that can be used for all purposes. Java is used to create desktop and mobile apps, big data processing and embedded systems. Oracle, the company behind Java, claims Java is used on over 3 billion devices around the world. This makes Java one the most widely used programming languages.

65) What is Java’s HashMap?

Java’s HashMap uses hashing principles. It’s a data structure that allows us to store objects and retrieve them in constant time O(1), provided we have the key. HashMap uses hash functions to link key and valued in hashing.

66) Who is the inventor of Java?

Java was initially developed at Sun Microsystems by James Gosling. Oracle acquired Sun Microsystems in the past. Java was released in 1995 as a core component to Sun Microsystems’ Java platform.

67) How to execute a Java program?

Open a command prompt and navigate to the directory that you saved the Java program (HelloWorld). Java Java

Enter javac HelloWorld. To compile your code, type java and hit the enter key.

To run your program, simply type ” HelloWorld”

The result will be printed on the window.

What are the benefits of passing this to a method rather than the current class object?

This refers to the current object of the class. It must therefore be identical to the current object. There are two main benefits to passing this into a function instead of the current object.

This is a final variable. This cannot be assigned to a new value, but the current class object can be modified and may not be final.

This can be used in a synchronized block.

68) What is Inheritance?

Inheritance refers to a process by which an object gains all the properties and behaviors of another object of a different class. It is used to implement Method Overriding and Code Reusability. In Java inheritance is the ability to create new classes from existing classes. You can reuse methods and fields from the parent class when you inherit from it. You can also add methods and fields to your current class. The IS-A relationship is also known by the name parent-child relationship.

In Java, there are five types inheritance.

Single-level inheritance

Multi-level inheritance

Multiple Inheritance

Hierarchical Inheritance

Hybrid Inheritance

Java via class does not support multiple inheritance.

For more information, click here

69) How is Java using Inheritance?

The following list outlines the many benefits of inheritance in Java.

Inheritance provides code reusability. The derived class doesn’t need to define the base class method, unless it is required to implement the method in a specific way.

Without inheritance, runtime polymorphism is impossible.

Simulating inheritance of classes using real-time objects makes OOPs much more realistic.

Inheritance provides data hiding. Private data can be used by the base class to hide some data from derived classes.

Without inheritance, method overriding is not possible. Method overriding allows us to give a specific implementation for a basic method that is contained in the base class.

70) Which superclass is for all classes?

The object class is the top-level superclass of all Java classes.

71) What is the difference between an array or a vector?

Ans: An array group data of the same primitive type, and is static in Nature. Vectors, on the other hand are dynamic and can hold different types of data.

72) Why is Multi-threading?

Ans: Multi-threading is a programming concept that allows multiple tasks to be run concurrently within one program. Threads can share the same process stack, and run in parallel. This helps to improve the performance of any program.

73) What is runnable interface?

Ans: Java uses the runnable interface to implement multi-threaded applications. Java.Lang.Runnable interface implements a class that supports multi threading.

74) What are two ways to implement multi-threading Java?

Ans: Java can be used to develop multi-threaded applications using one of the two following methods:

1. Java.Lang.Runnable Interface. This interface is implemented by classes to allow multi-threading. This interface implements a Run() method.

2. You can write a Java.Lang.Thread-like class.

75) What is String or StringBuffer?

Ans: StringBuffers can be dynamically changed, unlike String which cannot be changed. This makes it a great choice when data is changing too often. String will be used in this situation, but for each data change a new String object is created. This will add overhead.

76) What is bytecode?

Bytecode refers to the compiled format of Java programs. After a Java program is converted to bytecode it can be transferred over a network and run by Java Virtual Machine (JVM). Bytecode files usually have a.class extension.

Published
Categorized as Java

Leave a comment

Your email address will not be published.