JAVA INTERVIEW QUESTION ANSWER

Java_img

 

1) What is java?

Ans: Java is a general-purpose, class-based, object-oriented programming language designed for having lesser implementation dependencies. 

2) Why is Java a platform independent language?

Ans: Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.

The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it.

3) What is api in java?

Ans: Java application programming interface (API) is a list of all classes that are part of the Java development kit (JDK). It includes all Java packages, classes, and interfaces, along with their methods, fields, and constructors. These pre-written classes provide a tremendous amount of functionality to a programmer.

4) How to initialize array in java?

Ans: "int[] arr = new int[5];     // integer array of size 5 you can also change data type

String[] cars = {""Volvo"", ""BMW"", ""Ford"", ""Mazda""};"

5) How to take input from user in java?

Ans: "import java.util.Scanner;

  Scanner console = new Scanner(System.in);

  int num = console.nextInt();

  console.nextLine() // to take in the enter after the nextInt()

  String str = console.nextLine();"

6) What is static in java?

Ans: In Java, a static member is a member of a class that isn’t associated with an instance of a class. Instead, the member belongs to the class itself. As a result, you can access the static member without first creating a class instance.

7) What is package in java?

Ans: A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories:

Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)

8) How to sort an array in java?

Ans: "import java. util. Arrays;

Arrays. sort(array);"

9) What is an abstract class in java?

Ans: A class that is declared using the “abstract” keyword is known as abstract class. It can have abstract methods(methods without body) as well as concrete methods (regular methods with body). A normal class(non-abstract class) cannot have abstract methods.

10) What are the various access specifiers for Java classes?

Ans: In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:

1. Public : Class,Method,Field is accessible from anywhere.

2. Protected:Method,Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package,but not from outside.

3. Default: Method,Field,class can be accessed only from the same package and not from outside of it’s native package.

4. Private: Method,Field can be accessed from the same class to which they belong.

 

11) What’s the purpose of Static methods and static variables?

Ans: When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects. 

 

12) What is data encapsulation and what’s its significance?

Ans: Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.


13) What is a singleton class? Give a practical example of its usage.

Ans: A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for a class.

 14) How to enable java in chrome?

Ans: 

·         In the Java Control Panel, click the Security tab

·         Select the option Enable Java content in the browser

·         Click Apply and then OK to confirm the changes

·         Restart the browser to enable the changes

15) What is string in java?

Ans: String is a sequence of characters, for e.g. “Hello” is a string of 5 characters. In java, string is an immutable object which means it is constant and cannot be changed once it has been created.

16) What is exception in java?

Ans: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

17) Can you tell the difference between equals() method and equality operator (==) in Java?


() 

==

This is a method defined in the Object class. 

It is a binary operator in Java.

This method is used for checking the equality of contents between two objects as per the specified business logic.

This operator is used for comparing addresses (or references), i.e checks if both the objects are pointing to the same memory location.


18) What’s the base class in Java from which all classes are derived?

Ans: java.lang.object

19) Can main() method in Java can return any data?

Ans: In java, main() method can’t return any data and hence, it’s always declared with a void return type.

20) What are Java Packages? What’s the significance of packages?

Ans: In Java, package is a collection of classes and interfaces which are bundled together as they are related to each other. Use of packages helps developers to modularize the code and group the code for proper re-use. Once code has been packaged in Packages, it can be imported in other classes and used.

21) Can we declare a class as Abstract without having any abstract method?

Ans: Yes we can create an abstract class by using abstract keyword before class name even if it doesn’t have any abstract method. However, if a class has even one abstract method, it must be declared as abstract otherwise it will give an error.

22) What are the differences between JVM, JRE and JDK in Java?

Criteria

JDK 

JRE

JVM

Abbreviation

Java Development Kit

Java Runtime Environment

Java Virtual Machine

Definition

JDK is a complete software development kit for developing Java applications. It comprises JRE, JavaDoc, compiler, debuggers, etc.

JRE is a software package providing Java class libraries, JVM and all the required components to run the Java applications.

JVM is a platform-dependent, abstract machine comprising of 3 specifications - document describing the JVM implementation requirements, computer program meeting the JVM requirements and instance object for executing the Java byte code and provide the runtime environment for execution.

Main Purpose

JDK is mainly used for code development and execution.

JRE is mainly used for environment creation to execute the code.

JVM provides specifications for all the implementations to JRE.

Tools provided

JDK provides tools like compiler, debuggers, etc for code development

JRE provides libraries and classes required by JVM to run the program.

JVM does not include any tools, but instead, it provides the specification for implementation.

Summary

JDK = (JRE) + Development tools

JRE = (JVM) + Libraries to execute the application

JVM = Runtime environment to execute Java byte code.

 

23) Can we declare the main method of our class as private?

Ans: In java, main method must be public static in order to run any application correctly. If main method is declared as private, developer won’t get any compilation error however, it will not get executed and will give a runtime error.

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

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

25) How an object is serialized in java?

Ans: In java, to convert an object into byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing a Serializable interface get serialized and their state is saved in a byte stream.

26) When we should use serialization?

Ans: Serialization is used when data needs to be transmitted over the network. Using serialization, the object’s state is saved and converted into a byte stream. The byte stream is transferred over the network and the object is re-created at the destination.

27) Who invented java?

Ans: Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995 as a core component of Sun Microsystems’ Java platform.

28) How to execute a java program?

Ans: Open a command prompt window and go to the directory where you saved the java program (HelloWorld. java). …
Type ‘javac HelloWorld. java’ and press enter to compile your code.
Now, type ‘ HelloWorld ‘ to run your program.
You will be able to see the result printed on the window.

30) What is enum in java?

Ans: An enum is a special “class” that represents a group of constants (unchangeable variables, like final variables). To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma.

31) How to uninstall java?

·         Click Start

·         Select Settings

·         Select System

·         Select Apps & features

·         Select the program to uninstall and then click its Uninstall button

·         Respond to the prompts to complete the uninstall

 



Post a Comment

0 Comments