0% found this document useful (0 votes)
18 views

Java Iq

The document discusses core Java interview questions covering topics such as what is Java, why Java is called a platform, Java features, how Java is platform independent, differences between JDK and JRE, the Java Virtual Machine, garbage collection, and more. A total of 38 questions are covered in the document.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java Iq

The document discusses core Java interview questions covering topics such as what is Java, why Java is called a platform, Java features, how Java is platform independent, differences between JDK and JRE, the Java Virtual Machine, garbage collection, and more. A total of 38 questions are covered in the document.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

JavaITan’s

CORE JAVA INTERVIEW QUESTION’S


DAY - 1

---------*********---------
TEJAS PATIL
JAVA DEVELOPER

PURSUING CDAC, PUNE

1
JavaITan’s

Contents
What is Java ? ............................................................................................................................................... 4

Why Java is called as “Platform” ? ............................................................................................................... 4

What are the features of Java ? ..................................................................................................................... 4

How java is platform independent ? ............................................................................................................. 6

Is Java Pure-Object oriented Language ? ...................................................................................................... 6

What is native code? ..................................................................................................................................... 6

What is the difference bet’n JDK and JRE. ................................................................................................. 7

What is JVM ? .............................................................................................................................................. 7

What is the lifetime of Java Virtual Machine ? ............................................................................................ 8

What are the main components of JVM ? ..................................................................................................... 8

What is the job done by classloader ? ........................................................................................................... 8

Explain the hierarchy of classloaders in java. .............................................................................................. 8

What is the role played by Bytecode Verifier ? ............................................................................................ 9

What is Execution Engine inside JVM ? ...................................................................................................... 9

What are the memory areas allocated by JVM ?......................................................................................... 10

a) Class(Method) Area: .................................................................................................................... 10

b) Heap:.............................................................................................................................................. 10

c) Stack:.............................................................................................................................................. 10

d) Program Counter Register: ......................................................................................................... 10

e) Native Method Stack: ................................................................................................................... 10

What is the difference between interpreter and JIT ? ................................................................................. 10

What is the difference between Java compiler ( javac ) and JIT ? ............................................................. 11

Is JRE different for different Platforms ?.................................................................................................... 11

Difference bet’n C++ and java in terms of object creation. ........................................................................ 11

What is garbage collection ? ....................................................................................................................... 11

Can we force garbage collection programmatically ? ................................................................................. 12

1) When does an Object becomes eligible for Garbage collection in Java ?........................................... 12

2
JavaITan’s

What is an alternative for finalized method? .............................................................................................. 12

When do u get NullPointerException ?....................................................................................................... 13

What is the signature of main function in java ?......................................................................................... 13

Who invokes main() function ?................................................................................................................... 13

Can we define more than one public class in a java source code ? what is the rule of public class and
file name . ? ................................................................................................................................................. 13

Difference bet’n path and classpath. ........................................................................................................... 13

What is the rule for local member in java. .................................................................................................. 14

What is .class file known as ? .................................................................................................................... 14

How java is strong object oriented ? ........................................................................................................... 14

What is finalized() method ? ....................................................................................................................... 14

Why there is no sizeof operator in java ? .................................................................................................... 14

What kinds of programs u can develop using Java ? .................................................................................. 14

Difference bet’n C++ pointer and Java reference. ..................................................................................... 15

U have reference type as a member of class. What is the default value it gets ? ........................................ 15

What are the expressions allowed in switch block of java ? ....................................................................... 15

In a java source code there are 3 classes define e.g class A{} class B{} and class C{}. When u
compile the source code how many .class files u get and what are their names ? ...................................... 15

What is java’s old name ? ........................................................................................................................... 15

What are the technologies come under “Distributed Object System “ ? ..................................................... 16

What do u mean by Java Enabled Browser, Web Server and Application Server ? .................................. 16

What is “this” ? what is its importance ? .................................................................................................... 16

What is UnsupportedClassVersion error ? .................................................................................................. 16

What is Unicode Character Set in Java ? .................................................................................................... 16

Explain Java Execution Flow...................................................................................................................... 16

What is the difference bet’n Lazy and Eager Resolution ? ......................................................................... 18

3
JavaITan’s

What is Java ?
Answer:- Java technology is both a programming language and a platform from
Oracle Corporation.

Why Java is called as “Platform” ?


Ans:- The word "platform" in this context refers to the environment - the combination
of hardware and software - on which a program runs. For example, some programs
are designed to work specifically on Intel processors, and on the Microsoft Windows
operating system. In the case of Java, programs written in Java are designed to work
on a special program called the Java Virtual Machine (JVM) - this would be the
platform. Since JVM programs have been created for many operating systems and
processors, a compiled Java code can run on many operating systems as well. You
might say that it is "multiplatform", but you might also say that the "platform" is the
JVM.

What are the features of Java ?


Answer:- a) Simple

Simple syntax. No pointers, no multiple inheritance with the classes which causes ambiguity
error. For almost every task API (Application Programming Interface) is available;
Programmer just need to know how to use that API.

b) Object Oriented

Java is strong object oriented as it does not allow features like global data, friend function
which are against OOP principles.

c) Automatic Garbage Collection:


Automatic garbage collection is the process of looking at heap memory, identifying which
objects are in use and which are not, and deleting the unused objects. An in use object, or a
referenced object, means that some part of your program still maintains a pointer to that
object. An unused object, or unreferenced object, is no longer referenced by any part of your
program. So the memory used by an unreferenced object can be reclaimed.

4
JavaITan’s

d) Robust

Robust means reliable and no programming language can really assure reliability. Java puts a
lot of emphasis on early checking for possible errors, as Java compilers are able to detect
many problems that would first show up during execution time in other languages.
It provides the powerful exception handling and type checking mechanism as compare to
other programming languages.
e) Platform Independent

Unlike other programming languages such as C, C++ etc which are compiled into platform
specific machines. Java is guaranteed to be compile-once, run-anywhere language.

On compilation Java program is compiled into bytecode. This bytecode is platform


independent and can be run on any machine. Any machine with Java Runtime Environment
can run Java Programs.

f) Secure

When it comes to security, Java is always the first choice. With java secure features it enable
us to develop virus free, temper free system.

g) Multi Threading

Java multithreading feature makes it possible to write program that can do many tasks
simultaneously. Benefit of multithreading is that it utilizes same memory and other resources
to execute multiple threads at the same time, like While typing, grammatical errors are
checked along.

5
JavaITan’s

h) Portable

Java Byte code can be carried to any platform.

i) Architectural Neutral

No implementation dependent features. Everything related to storage is predefined, example:


size of primitive data types

j) High Performance
Java enables high performance with the use of Just-In-Time (JIT) compiler.

How java is platform independent ?


Answer:- Java program, once compiled , can be run on any platform without
recompiling.

Is Java Pure-Object oriented Language ?


Answer:- No. Java is not because it supports Primitive datatype[^] such as int, byte, long...
etc, to be used, which are not objects.

There are seven qualities to be satisfied for a programming language to be pure Object
Oriented. They are:

➢ Encapsulation/Data Hiding

➢ Inheritance
➢ Polymorphism
➢ Abstraction
➢ All predefined types are objects
➢ All operations are performed by sending messages to objects
➢ All user defined types are objects.

What is native code?


Answer:- Native code is computer programming (code) that is compiled to run with a
particular processor (such as an Intel x86-class processor) and its set of instructions.

6
JavaITan’s

What is the difference bet’n JDK and JRE.

• JRE is an acronym for Java Runtime Environment.It is used to provide


runtime environment.It contains JVM + API ( jars and dlls)

• JDK is an acronym for Java Development Kit.It physically exists.It contains


JRE + development tools like compiler,debugger etc.

What is JVM ?
Answer:- A Java virtual machine (JVM), an implementation of the Java Virtual
Machine Specification, interprets compiled Java binary code (called bytecode) for a
computer's processor (or "hardware platform") so that it can perform a Java program's
instructions.

in other words,The Java Virtual machine (JVM) is the virtual machine that run the
Java bytecodes.

7
JavaITan’s

What is the lifetime of Java Virtual Machine ?


Answer:- When a Java application starts, a runtime instance of JVM is born. When
the application completes, the instance dies.

What are the main components of JVM ?


Answer:- a) class loader b) bytecode verifier c) interpreter and
JIT compiler

What is the job done by classloader ?


Answer:- classloader loads the java class. In java , classes are loaded on demand
basis i.e. only when u use the class for the first time in ur application, class will be
loaded.

Explain the hierarchy of classloaders in java.


Answer:- There are 3 types of class loaders in java

a) BootStrap Class Loader or Premordial Class Loader

b) Extension Class Loader

c) System Class Loader

BootStrap or Premordial class loader :-

BootStrap class loader loads those classes which are essential for JVM to function
properly. BootStrap class loader is responsible for loading all core java classes for instance
java.lang.* ,java.io.* etc. BootStrap class loader finds these necessary classes from
“jdk/jre/lib/rt.jar” .

Extension class loader :-

Extensions Classloader is the immediate child of Bootstrap classloader. This


classloader loads the classes in lib\ext directory of the JRE.

System class loader :-

System-Classpath classloader is the immediate child of Extensions


classloader. It loads the classes and jars specified by the CLASSPATH environment
variable, java.class.path system property, -cp or -classpath command line settings. If
any of the jars specified in one of the above manner have a MANIFEST.MF file with
a Class-Path attribute, the jars specified by the Class-Path attribute are also loaded.
This class loader is also used to load an application’s entry point class that is the
“static void main()” method in a class.

8
JavaITan’s

What is the role played by Bytecode Verifier ?


Answer:- Bytecode Verifier performs verification of bytecodes. When a class loader presents
the bytecodes of a newly loaded Java platform class to the virtual machine, these bytecodes
are first inspected by a verifier. The verifier checks that the instructions cannot perform
actions that are obviously damaging. All classes except for system classes are verified.

What is Execution Engine inside JVM ?


Answer:-

9
JavaITan’s

What are the memory areas allocated by JVM ?


Answer:-

a) Class(Method) Area:

Class(Method) Area stores per-class structures such as the runtime constant pool, field and
method data, the code for methods.

b) Heap:

It is the runtime data area in which objects are allocated.

c) Stack:

Java Stack stores frames.It holds local variables and partial results, and plays a part in
method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when its
method invocation completes.

d) Program Counter Register:

PC (program counter) register. It contains the address of the Java virtual machine instruction
currently being executed.

e) Native Method Stack:

It contains all the native methods used in the application.

What is the difference between interpreter and JIT ?

• Answer:- Interpreter: Reads, interprets and executes the bytecode instructions one
by one. As it interprets and executes instructions one by one, it can quickly interpret
one bytecode, but slowly executes the interpreted result. This is the disadvantage of
the interpreter.
• JIT (Just-In-Time) compiler: The JIT compiler has been introduced to compensate
for the disadvantages of the interpreter. The JIT compiler compiles the entire
bytecode to change it to native code. Execution in native code is much faster than

10
JavaITan’s

interpreting instructions one by one. The compiled code can be executed quickly since
the native code is stored in the cache.

However, it takes more time for JIT compiler to compile the code than for the interpreter to
interpret the code one by one. Therefore, if the code is to be executed just once, it is better to
interpret it instead of compiling. Therefore, the JVMs that use the JIT compiler internally
check how frequently the method is executed and compile the method only when the
frequency is higher than a certain level.

What is the difference between Java compiler ( javac ) and JIT ?


Answer:- javac converts source code into the bytecode whereas JIT converts
bytecode into native code.

Is JRE different for different Platforms ?


Ans:- yes

Difference bet’n C++ and java in terms of object creation.


Ans:- in C++ object can be created on stack as well as on heap (because java has
given importance to late binding from the very beginning)

What is garbage collection ?


Ans:- Automatic garbage collection is the process of looking at heap memory,
identifying which objects are in use and which are not, and deleting the unused
objects. An in use object, or a referenced object, means that some part of your

11
JavaITan’s

program still maintains a pointer to that object. An unused object, or unreferenced


object, is no longer referenced by any part of your program. So the memory used by
an unreferenced object can be reclaimed.

Can we force garbage collection programmatically ?


Answer:- no, we can at the most request for Garbage Collection using “System.gc()”
or “Runtime.gc()”

When does an Object becomes eligible for Garbage collection in Java ?


Answer:- An Object becomes eligible for Garbage collection or GC if it’s not reachable
from any live threads or any static references in other words you can say that an object
becomes eligible for garbage collection if its all references are null. Cyclic
dependencies are not counted as reference so if Object A has reference of object B and
object B has reference of Object A and they don't have any other live reference then both
Objects A and B will be eligible for Garbage collection.
Generally an object becomes eligible for garbage collection in Java on following cases:
a) All references of that object explicitly set to null e.g. object = null
b) Object is created inside a block and reference goes out scope once control exit that
block.
c) Parent object set to null, if an object holds reference of another object and when you
set container object's reference null, child or contained object automatically becomes
eligible for garbage collection.
d) If an object has only live references via WeakHashMap it will be eligible for garbage
collection.

What is an alternative for finalized method?


Answer:- It is not recommended to rely on finalize method because of its non-
guaranteed behavior but then what is alternative of releasing resource, performing
cleanup because there is no destructor in Java? Having a method
like close() or destroy() make much sense for releasing resources held by classes. In
fact JDK library follows this. E.g various inputstream and outputstream
implementations are having “close()” method to release the resources. in fact it’s one
of the best practice to call close method from finally block in java. Only problem
with this approach is its not automatic, client has to do the cleanup and if client forgot
to do cleanup there are chances of resources getting leaked, which again suggest us
that we could probably give another chance to finalize method.

12
JavaITan’s

What is the difference between System.gc() and Runtime.getRuntime().gc()


?

Answer:- Both are same. System.gc() is effectively equivalent


to Runtime.gc(). System.gc()internally calls Runtime.gc().
The only difference is System.gc() is a class method where as Runtime.gc() is an instance
method. So, System.gc() is more convenient.
if you look at System class source code, it has got following method:
public static void gc()
{
Runtime.getRuntime().gc();
}

When do u get NullPointerException ?


Answer:- when reference variable containing null, used to invoke member, we get
NullPointerException.

What is the signature of main function in java ?


Answer:- public static void main(String args[]) or
public static void main(String …args)

Who invokes main() function ?


Answer:- JVM invokes main() function.

Can we define more than one public class in a java source code ? what is
the rule of public class and file name . ?
Answer:- no we can’t define more than one public class in one source file. Name of the
public class and the file name must match.

Difference bet’n path and classpath.


Answer:-path is used for “exe”,”dll” and “bat” files
Classpath is used for “jar” files.

13
JavaITan’s

What is the rule for local member in java.


Answer:- local member must be initialized before use.

What is .class file known as ?


Answer:- java bytecode

How java is strong object oriented ?


Answer:- java does not allow global members, friend function. Everything (variables as well
as methods) must be defined inside the class.

What is finalized() method ?


Answer:- finalize method in java is called before Garbage collector reclaim the Object. It’s
last chance for any object to perform cleanup activity i.e. releasing any
system resources held, closing connection if open etc. Main issue with finalize method in java
is its not guaranteed by JLS that it will be called by Garbage collector or exactly when it will
be called, for example an object may wait indefinitely after becoming eligible for garbage
collection and before its finalize() method gets called. Similarly even after finalize gets called
it’s not guaranteed it will be immediately collected. Because of above reasons it make no
sense to use finalize method for releasing critical resources or performing
time critical activity inside finalize. It may work in development in one JVM but may not
work in other JVM.

Why there is no sizeof operator in java ?


Ans:- because size of all the java datatypes is identical across all platforms.

What kinds of programs u can develop using Java ?

There are mainly 5 type of applications that can be created using java:

a) Standalone Application

It is also known as desktop application or window-based application. An application


that contain “main” function as an entry point.

b) Applet

It is a java program which can run on java-enabled web browser

14
JavaITan’s

c) Web Application

An application that runs on the server side and creates dynamic page, is called web
application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating
web applications in java.

d) Enterprise Application

An application that is distributed in nature, such as banking applications etc. It has the
advantage of high level security, load balancing and clustering. In java, EJB is used
for creating enterprise applications.

e) Mobile Application

An application that is created for mobile devices. Currently Android and Java ME are
used for creating mobile applications.

Difference bet’n C++ pointer and Java reference.


Ans:- similarity bet’n two is both of them store the address of heap based object.
Difference is c++ pointer can be incremented, decremented and also can be
dereferenced. None of these operations u can invoke on java reference variable.

U have reference type as a member of class. What is the default value it


gets ?
Ans:- null

What are the expressions allowed in switch block of java ?


Ans:- byte,short,int,char and String ( jdk 1.7 onwards)

In a java source code there are 3 classes define e.g class A{} class B{} and
class C{}. When u compile the source code how many .class files u get and
what are their names ?
Ans:- A.class, B.classs, C.class

What is java’s old name ?


Ans:- oak

15
JavaITan’s

What are the technologies come under “Distributed Object System “ ?


Ans:- RMI, CORBA and EJB

What do u mean by Java Enabled Browser, Web Server and Application


Server ?
Ans:- Java-Enabled means having “JRE” inside it.

What is “this” ? what is its importance ?


Ans:- this is a reference to the current object or invoking object. With the help of
“this” reference member function comes to know about the invoking object.

What is UnsupportedClassVersion error ?


Ans:- when a upper-version class file is executed in the lower-version JVM, (e.g. javac
belongs to 8 and JRE belongs to 1.7 )

java.lang.UnsupportedClassVersionError occurs.

What is Unicode Character Set in Java ?


Ans:- Since Java is designed to support Internationalization ( I18N ),it makes sense that it
would use Unicode to represent characters. Unicode defines a fully international character set
that can represent all of the characters found in all human languages. It is a unification of
dozens of character sets such as Latin, Greek, Arabic, Hebrew and many more. For this
purpose , it requires 16 bits. Thus char in java is 16 bit type. The range of char is 0 to 65535.
There are no negative chars.

Explain Java Execution Flow.


Answer:- Virtual Machine Start-Up

A Java virtual machine starts execution by invoking the method main of some specified class,
passing it a single argument, which is an array of strings if provided else an empty array.

For example:
java Test

will typically start a Java virtual machine by invoking method main of class Test .

16
JavaITan’s

Load the Class Test

Link Test: Verify, Prepare, (Optionally) Resolve


Linking involves verification, preparation and (optionally) resolution.

Verification checks that the loaded representation of Test is well-formed, with a proper
symbol table. Verification also checks that the code that implements Test obeys the semantic
requirements of the Java programming language and the Java virtual machine. If a problem is
detected during verification, then an error is thrown.

Preparation involves allocation of static storage and any data structures that are used
internally by the virtual machine, such as method tables.

Resolution is the process of checking symbolic references from Test to other classes and
interfaces, by loading the other classes and interfaces that are mentioned and checking that
the references are correct.
Resolution can be either “lazy (in case of non-static references)” or “early (in case of static
references)”.

Initialize Test: Execute Initializers


Initialization consists of execution of any class variable initializers and static initializers of
the class Test, in textual order. But before Test can be initialized, its direct superclass must be
initialized, as well as the direct superclass of its direct superclass, and so on, recursively. In
the simplest case, Test has Object as its implicit direct superclass; if class Object has not yet
been initialized, then it must be initialized before Test is initialized. Class Object has no
superclass, so the recursion terminates here.
If class Test has another class Super as its superclass, then Super must be initialized before
Test. This requires loading, verifying, and preparing Super if this has not already been done
and, depending on the implementation, may also involve resolving the symbolic references
from Super and so on, recursively.
Initialization may thus cause loading, linking, and initialization errors, including such errors
involving other types.

Invoke Test.main
Finally, after completion of the initialization for class Test (during which other consequential
loading, linking, and initializing may have occurred), the method main of Test is invoked.

The method main must be declared public, static, and void. It must accept a single argument
that is an array of strings. This method can be declared as either public static void
main(String[] args)
Or public static void main(String... args)

Loading of Classes and Interfaces which are referenced from main() function………

17
JavaITan’s

What is the difference bet’n Lazy and Eager Resolution ?


Answer:- e.g. suppose Car class has Engine as a member.
Here if Car has Engine as static member, Engine will be resolved as soon as Car is
loaded ( Eager ). On the other hand if Car has Engine as non-static member, Engine
will be resolved only after you instantiate the Car (Lazy).

THANK YOU

Do Follow on Instagram for More Information……!

18

You might also like