Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

In System.out.println(), What Is System, Out And Println?

Answer»

System is a predefined final CLASS, out is a PrintStream object and PRINTLN is a built-in OVERLOADED METHOD in the out object.

System is a predefined final class, out is a PrintStream object and println is a built-in overloaded method in the out object.

2.

What Is Explicit Casting?

Answer»

Explicit casting in the PROCESS in which the complier are specifically informed to about TRANSFORMING the object.
EXAMPLE
long i = 700.20;
int j = (int) i; //Explicit casting.

Explicit casting in the process in which the complier are specifically informed to about transforming the object.
Example
long i = 700.20;
int j = (int) i; //Explicit casting.

3.

What Is The Java Virtual Machine (jvm)?

Answer»

The JAVA Virtual MACHINE is SOFTWARE that can be ported ONTO various hardware-based PLATFORMS

The Java Virtual Machine is software that can be ported onto various hardware-based platforms

4.

What Are Different Types Of Access Modifiers?

Answer»

ACCESS specifiers are KEYWORDS that determine the type of access to the member of a class. These keywords are for allowing privileges to parts of a program such as functions and variables. These are:
• Public : accessible to all CLASSES
PROTECTED : accessible to the classes within the same package and any subclasses.
• Private : accessible only to the class to which they belong
• Default : accessible to the class to which they belong and to subclasses within the same package.

Access specifiers are keywords that determine the type of access to the member of a class. These keywords are for allowing privileges to parts of a program such as functions and variables. These are:
• Public : accessible to all classes
• Protected : accessible to the classes within the same package and any subclasses.
• Private : accessible only to the class to which they belong
• Default : accessible to the class to which they belong and to subclasses within the same package.

5.

Which Class Is The Superclass Of Every Class?

Answer»

Object.

Object.

6.

Name Primitive Java Types.

Answer»

The 8 PRIMITIVE types are BYTE, char, short, INT, LONG, float, double, and BOOLEAN.

The 8 primitive types are byte, char, short, int, long, float, double, and boolean.

7.

What Type Of Parameter Passing Does Java Support?

Answer»

In JAVA the ARGUMENTS (primitives and objects) are always passed by value. With objects, the object REFERENCE itself is passed by value and so both the original reference and parameter copy both REFER to the same object.

In Java the arguments (primitives and objects) are always passed by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.

8.

Explain The Encapsulation Principle.

Answer»

ENCAPSULATION is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Objects ALLOW procedures to be encapsulated with their data to reduce potential INTERFERENCE. One way to think about encapsulation is as a PROTECTIVE wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Objects allow procedures to be encapsulated with their data to reduce potential interference. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

9.

What Do You Understand By Casting In Java Language? What Are The Types Of Casting?

Answer»

The process of CONVERTING ONE DATA type to another is CALLED Casting. There are two types of casting in Java; these are implicit casting and explicit casting.

The process of converting one data type to another is called Casting. There are two types of casting in Java; these are implicit casting and explicit casting.

10.

How Can One Prove That The Array Is Not Null But Empty?

Answer»

Print array.length. It will print 0. That MEANS it is empty. But if it would have been NULL then it would have THROWN a NullPointerException on ATTEMPTING to print array.length.

Print array.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print array.length.

11.

Can An Application Have Multiple Classes Having Main Method?

Answer»

Yes. While starting the APPLICATION we mention the class name to be run. The JVM will LOOK for the main method only in the class whose name you have MENTIONED. Hence there is not conflict amongst the multiple CLASSES having main method.

Yes. While starting the application we mention the class name to be run. The JVM will look for the main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.

12.

When Is Static Variable Loaded? Is It At Compile Time Or Runtime? When Exactly A Static Block Is Loaded In Java?

Answer»

Static variable are loaded when CLASSLOADER brings the class to the JVM. It is not necessary that an object has to be created. Static VARIABLES will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static BLOCKS. Static block is not member of a class, they do not have a return statement and they cannot be called DIRECTLY. Cannot contain this or super. They are primarily USED to initialize static fields.

Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class, they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields.

13.

Can I Have Multiple Main Methods In The Same Class?

Answer»

We can have MULTIPLE overloaded main methods but there can be only ONE main method with the following signature :
public STATIC void main(String[] args) {}
No the program fails to compile. The compiler says that the main method is ALREADY defined in the class.

We can have multiple overloaded main methods but there can be only one main method with the following signature :
public static void main(String[] args) {}
No the program fails to compile. The compiler says that the main method is already defined in the class.

14.

How Can I Swap Two Variables Without Using A Third Variable?

Answer»

ADD TWO variables and assign the VALUE into First variable. SUBTRACT the Second value with the result Value. and assign to Second variable. Subtract the Result of First Variable With Result of Second Variable and Assign to First Variable. Example:
int a=5,b=10;a=a+b; b=a-b; a=a-b;

Add two variables and assign the value into First variable. Subtract the Second value with the result Value. and assign to Second variable. Subtract the Result of First Variable With Result of Second Variable and Assign to First Variable. Example:
int a=5,b=10;a=a+b; b=a-b; a=a-b;

15.

Explain Working Of Java Virtual Machine (jvm)?

Answer»

JVM is an ABSTRACT computing machine like any other real computing machine which first CONVERTS .java file into .class file by USING Compiler (.class is nothing but byte code file.) and INTERPRETER reads byte codes.

JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (.class is nothing but byte code file.) and Interpreter reads byte codes.

16.

What Is Data Encapsulation?

Answer»

Encapsulation may be used by creating ‘get’ and ’set’ methods in a class (JAVABEAN) which are used to access the FIELDS of the object. Typically the fields are made private while the get and set methods are public. Encapsulation can be used to VALIDATE the data that is to be stored, to do calculations on data that is stored in a field or fields, or for use in introspection (OFTEN the CASE when using javabeans in Struts, for instance). Wrapping of data and function into a single unit is called as data encapsulation. Encapsulation is nothing but wrapping up the data and associated methods into a single unit in such a WAY that data can be accessed with the help of associated methods. Encapsulation provides data security. It is nothing but data hiding.

Encapsulation may be used by creating ‘get’ and ’set’ methods in a class (JAVABEAN) which are used to access the fields of the object. Typically the fields are made private while the get and set methods are public. Encapsulation can be used to validate the data that is to be stored, to do calculations on data that is stored in a field or fields, or for use in introspection (often the case when using javabeans in Struts, for instance). Wrapping of data and function into a single unit is called as data encapsulation. Encapsulation is nothing but wrapping up the data and associated methods into a single unit in such a way that data can be accessed with the help of associated methods. Encapsulation provides data security. It is nothing but data hiding.

17.

What Is Reflection Api? How Are They Implemented?

Answer»

REFLECTION is the process of introspecting the features and state of a class at runtime and dynamically manipulate at run time. This is supported using Reflection API with built-in classes like Class, Method, FIELDS, Constructors etc. Example: Using Java Reflection API we can GET the class name, by using the getName method.

Reflection is the process of introspecting the features and state of a class at runtime and dynamically manipulate at run time. This is supported using Reflection API with built-in classes like Class, Method, Fields, Constructors etc. Example: Using Java Reflection API we can get the class name, by using the getName method.

18.

Does Jvm Maintain A Cache By Itself? Does The Jvm Allocate Objects In Heap? Is This The Os Heap Or The Heap Maintained By The Jvm? Why

Answer»

YES, the JVM MAINTAINS a cache by itself. It creates the OBJECTS on the HEAP, but REFERENCES to those objects are on the STACK.

Yes, the JVM maintains a cache by itself. It creates the Objects on the HEAP, but references to those objects are on the STACK.

19.

What Is Phantom Memory?

Answer»

PHANTOM MEMORY is FALSE memory. Memory that does not EXIST in REALITY.

Phantom memory is false memory. Memory that does not exist in reality.

20.

Can A Method Be Static And Synchronized?

Answer»

A static method can be synchronized. If you do so, the JVM will obtain a lock on the java.lang.
Class instance ASSOCIATED with the OBJECT. It is SIMILAR to saying:
synchronized(XYZ.class) {
}

A static method can be synchronized. If you do so, the JVM will obtain a lock on the java.lang.
Class instance associated with the object. It is similar to saying:
synchronized(XYZ.class) {
}

21.

Explain The Inheritance Principle.

Answer»

INHERITANCE is the PROCESS by which one object acquires the properties of another object. Inheritance allows well-tested PROCEDURES to be REUSED and enables changes to make once and have effect in all relevant places

Inheritance is the process by which one object acquires the properties of another object. Inheritance allows well-tested procedures to be reused and enables changes to make once and have effect in all relevant places

22.

Describe The Principles Of Oops.

Answer»

There are three main PRINCIPALS of oops which are CALLED Polymorphism, INHERITANCE and ENCAPSULATION.

There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.

23.

Why There Are No Global Variables In Java?

Answer»

GLOBAL variables are globally accessible. Java does not support globally accessible variables DUE to following reasons:
• The global variables breaks the referential transparency.
• Global variables CREATES COLLISIONS in namespace.

Global variables are globally accessible. Java does not support globally accessible variables due to following reasons:
• The global variables breaks the referential transparency.
• Global variables creates collisions in namespace.

24.

Why Oracle Type 4 Driver Is Named As Oracle Thin Driver?

Answer»

Oracle provides a Type 4 JDBC driver, referred to as the Oracle “thin” driver. This driver includes its own implementation of a TCP/IP version of Oracle’s Net8 WRITTEN entirely in JAVA, so it is platform INDEPENDENT, can be downloaded to a BROWSER at runtime, and does not require any Oracle software on the client side. This driver requires a TCP/IP listener on the server side, and the client connection string uses the TCP/IP port address, not the TNSNAMES entry for the database name.

Oracle provides a Type 4 JDBC driver, referred to as the Oracle “thin” driver. This driver includes its own implementation of a TCP/IP version of Oracle’s Net8 written entirely in Java, so it is platform independent, can be downloaded to a browser at runtime, and does not require any Oracle software on the client side. This driver requires a TCP/IP listener on the server side, and the client connection string uses the TCP/IP port address, not the TNSNAMES entry for the database name.

25.

Expain The Reason For Each Keyword Of Public Static Void Main(string Args[])?

Answer»

public- main(..) is the FIRST method called by JAVA environment when a program is executed so it has to accessible from java environment. Hence the access SPECIFIER has to be public.

static: Java environment should be able to call this method without creating an instance of the class , so this method must be DECLARED as static.

void: main does not return anything so the return type must be void

The argument String indicates the argument type which is given at the command line and ARG is an array for string given during command line.

public- main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.

static: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.

void: main does not return anything so the return type must be void

The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line.

26.

If You're Overriding The Method Equals() Of An Object, Which Other Method You Might Also Consider?

Answer»

HASHCODE()

hashCode()

27.

What Is Meant By Pass By Reference And Pass By Value In Java?

Answer»

Pass by reference means, PASSING the ADDRESS itself RATHER than passing the value. Pass by value means passing a copy of the value.

Pass by reference means, passing the address itself rather than passing the value. Pass by value means passing a copy of the value.

28.

Main Method From Another Class?

Answer»

Yes, the main method can be CALLED from a separate class. First you MUST prepare the STRING array of arguments to PASS to the method, then call the method through a static reference to the HOST class, MaxFactors in the example below.
String[] arguments = new String[] {"123"};
MaxFactors.main(arguments);

Yes, the main method can be called from a separate class. First you must prepare the string array of arguments to pass to the method, then call the method through a static reference to the host class, MaxFactors in the example below.
String[] arguments = new String[] {"123"};
MaxFactors.main(arguments);

29.

How Can The Static Main Method Use Instance Variables?

Answer»

For very SIMPLE programs it is possible to write a MAIN method that only uses static variables and methods. For more complex systems, the main method is used to CREATE an instance of itself, or another primary class, as the basis of the application. The primary application object reference uses instance methods to create and interact with other objects, do the work and return when the application terminates.
public class SimpleClass {
public VOID doSomething() {
// Instance method statements
}
public static main(FINAL String[] args) {
SimpleClass instance = new SimpleClass();
instance.doSomething();
}
}

For very simple programs it is possible to write a main method that only uses static variables and methods. For more complex systems, the main method is used to create an instance of itself, or another primary class, as the basis of the application. The primary application object reference uses instance methods to create and interact with other objects, do the work and return when the application terminates.
public class SimpleClass {
public void doSomething() {
// Instance method statements
}
public static main(final String[] args) {
SimpleClass instance = new SimpleClass();
instance.doSomething();
}
}

30.

I Get An Exception If I Remove The Static Modifier From Main?

Answer»

The static void main(String[]) method is a basic convention of the Java programming language that provides an entry POINT into the runtime system. The main method MUST be DECLARED static because no OBJECTS exist when you first invoke the Java Virtual Machine (JVM), so there are no references to instance methods. The JVM creates the initial runtime environment in which this static method can be called, if you remove the static modifier, it will throw aNoSuchMethodException.

The static void main(String[]) method is a basic convention of the Java programming language that provides an entry point into the runtime system. The main method must be declared static because no objects exist when you first invoke the Java Virtual Machine (JVM), so there are no references to instance methods. The JVM creates the initial runtime environment in which this static method can be called, if you remove the static modifier, it will throw aNoSuchMethodException.

31.

Can The Main Method Be Declared Final?

Answer»

Yes, the static VOID MAIN(STRING[]) method can be declared FINAL.

Yes, the static void main(String[]) method can be declared final.

32.

Can The Main Method Be Overloaded?

Answer»

YES, any Java method can be overloaded, provided there is no final method with the same signature ALREADY. The Java interpreter will only invoke the STANDARD entry point signature for the MAIN method, with a string array ARGUMENT, but your application can call its own main method as required.

Yes, any Java method can be overloaded, provided there is no final method with the same signature already. The Java interpreter will only invoke the standard entry point signature for the main method, with a string array argument, but your application can call its own main method as required.

33.

Why Do We Only Use The Main Method To Start A Program?

Answer»

The ENTRY point METHOD main is used to the provide a standard convention for starting Java programs. The choice of the method name is somewhat arbitrary, but is partly DESIGNED to avoid clashes with the Thread START() andRunnable run() METHODS, for example.

The entry point method main is used to the provide a standard convention for starting Java programs. The choice of the method name is somewhat arbitrary, but is partly designed to avoid clashes with the Thread start() andRunnable run() methods, for example.

34.

Why Doesn't The Main Method Throw An Error With No Arguments?

Answer»

When you INVOKE the Java Virtual Machine on a class WITHOUT any arguments, the class' main method receives aString array of zero length. Thus, the method SIGNATURE is fulfilled. Provided the main method does not make any reference to ELEMENTS in the array, or checks the array length before doing so, no EXCEPTION will occur.

When you invoke the Java Virtual Machine on a class without any arguments, the class' main method receives aString array of zero length. Thus, the method signature is fulfilled. Provided the main method does not make any reference to elements in the array, or checks the array length before doing so, no exception will occur.

35.

Why Are Command Line Arguments Passed As A String?

Answer»

Command LINE arguments are passed to the application's main method by the Java runtime system before theapplication class or any supporting OBJECTS are instantiated. It would be much more COMPLEX to define and construct arbitrary object types to pass to the main method and primitive values alone are not versatile enough to provide the range of input data that strings can. String arguments can be PARSED for primitive values and can also be used for arbitrary text input, file and URL REFERENCES.

Command line arguments are passed to the application's main method by the Java runtime system before theapplication class or any supporting objects are instantiated. It would be much more complex to define and construct arbitrary object types to pass to the main method and primitive values alone are not versatile enough to provide the range of input data that strings can. String arguments can be parsed for primitive values and can also be used for arbitrary text input, file and URL references.

36.

What Does Public Static Void Main(string[]) Mean?

Answer»

This is a special static method SIGNATURE that is USED to run Java programs from a command line INTERFACE (CLI). There is nothing special about the method itself, it is a standard Java method, but the Java interpreter is designed to call this method when a class reference is given on the command line, as below.

This is a special static method signature that is used to run Java programs from a command line interface (CLI). There is nothing special about the method itself, it is a standard Java method, but the Java interpreter is designed to call this method when a class reference is given on the command line, as below.

37.

How Can I Write A Program That Takes Command Line Input?

Answer»

Java PROGRAMS that take INPUT from the command line declare a special static METHOD called main, which takes aString ARRAY as an argument and returns void. The example program below loops through any arguments PASSED to the program on the command line and lists their values.

Java programs that take input from the command line declare a special static method called main, which takes aString array as an argument and returns void. The example program below loops through any arguments passed to the program on the command line and lists their values.

38.

Is Jvm A Compiler Or An Interpreter?

Answer»

Interpreter.

Interpreter.