Java Interview Questions

Can you declare a class as ‘private’?

No, if we declare a class as private, then it is not available to java compiler and hence a compile time error occurs. But, inner class can be declared as private.

What are static methods?

Static methods are methods which do not act upon the instance variables of a class. Static methods are declared as ‘static’.

Reason: JVM executes the static methods and then only it creates the objects.

What are static blocks?

A static block is a block of statements declared as ‘static’,some thing like this:

static{

statements;

}

JVM executes a static block on a highest priority basis. This means JVM first goes to static block even before it looks for the main() method in the program.

Example:

Class test{

static{

system.out.println(“static block”);

}

public static void main(string args[])

{

system.out.println(“static method”);

}

}

Output: static block

static  method

Is it possible to compile and run a java program without writing main() method?

Yes, it is possible by using a static block in the java program.

Object creation in java

We know that the class code along with method code is stored in “method area” of the JVM. When an object is created, the memory is allocated on ‘heap‘. After creation of an object, JVM produces a unique identifier number for the object from the memory address of the object. This reference number is called hashcode number. 

To know the hashcode number of an object, we can use hashcode() method of object class as shown here..

Employee el=new Employee();

System.out.println(e1.hashcode());

Java Heap

The Java Heap Space is the memory “container” of you runtime Java program which provides to your Java program the proper memory spaces it needs (Java Heap, Native Heap) and managed by the JVM itself.

Your Java program life cycle typically looks like this:

– Java program coding (via Eclipse IDE etc.) e.g. HelloWorld.java
– Java program compilation (Java compiler or third party build tools such as Apache Ant, Apache Maven..) e.g. HelloWord.class
– Java program start-up and runtime execution e.g. via your HelloWorld.main() method

The Java Heap space is mainly applicable and important for the third step: runtime execution. For the HotSpot VM, the Java Heap Space is split in 3 silos:

– Java Heap for short & long lived objects (YoungGen & OldGen spaces)
– PermGen space
– Native Heap

Now let’s dissect your HelloWorld.class program so you can better understand.

– At start-up, your JVM will load and cache some of your static program and JDK libraries to the Native Heap, including native libraries, Mapped Files such as your program Jar file(s), Threads such as the main start-up Thread of your program etc.
– Your JVM will then store the “static” data of your HelloWorld.class Java program to the PermGen space (Class metadata, descriptors..)
– Once your program is started, the JVM will then manage and dynamically allocate the memory of your Java program to the Java Heap (YoungGen & OldGen). This is why it is so important that you understand how much memory your Java program needs to you can properly fine-tuned the capacity of your Java Heap controlled via –Xms & -Xmx JVM parameters. Profiling, Heap Dump analysis allow you to determine your Java program memory footprint
– Finally, the JVM has to also dynamically release the memory from the Java Heap Space that your program no longer need; this is called the garbage collection process. This process can be easily monitored via the JVM verbose GC or a monitoring tool of your choice such as JConsole

JVM Architecture with a diagram

Hello pal,

Most of the java beginners donno what is JVM and how the architecture of it will be i.e., JVM Inside..Lets explore what it is..

What is JVM??

In simple words,

  • Java Virtual Machine is not a real hardware machine but a software layer which resembles an hardware platform.
  • JVM converts java byte code into machine language and executes it
  • The byte code can be executed on any platform where jvm exists.

jvm

JVM Inside(JVM Architecture)

JVM-arc1

1. Class loader sub system: JVM’s class loader sub system performs 3 tasks
a. It loads .class file into memory.
b. It verifies byte code instructions.
c. It allots memory required for the program.

2. Run time data area: This is the memory resource used by JVM and it is divided into 5 parts
a. Method area: Method area stores class code and method code.
b. Heap: Objects are created on heap.
c. Java stacks: Java stacks are the places where the Java methods are executed. A Java stack contains frames. On each frame, a separate method is executed.
d. Program counter registers: The program counter registers store memory address of the instruction to be executed by the micro processor.
e. Native method stacks: The native method stacks are places where native methods (for example, C language programs) are executed. Native method is a function, which is written in another language other than Java.

3. Native method interface: Native method interface is a program that connects native methods libraries (C header files) with JVM for executing native methods.

4. Native method library: holds the native libraries information.

5. Execution engine: Execution engine contains interpreter and JIT compiler, which covert byte code into machine code. JVM uses optimization technique to decide which part to be interpreted and which part to be used with JIT compiler. The HotSpot represent the block of code executed by JIT compiler.

Google Authenticator

Hello Pals,

I hope you are well aware of 2 step verification for your accounts..if not?? pls clickhere to know what it is..

Today we will see what is google authenticator and how to use it..

What is exactly this google authenticator??

ga2

Google Authenticator implements TOTP security tokens from RFC6238 in mobile apps made by Google, sometimes branded “two-step authentication”. The Authenticator provides a six digit one-time password users must provide in addition to their username and password to log into Google services. With two-step authentication enabled, you’ll be prompted to enter a six-digit number after you provide your username and password. Unlike a PIN number for an ATM, this six-digit number changes with every login.

How to use this??

Doing the two-step

1. Make sure two-step authentication is enabled for your Google account

Before you start using the app, make sure that two-step authentication is enabled and configured for your account.

2. Install the app

Download and install the app on your Android device

3. Connect Google Authenticator to your Google Account

 

Just goto your newly installed google authenticator app. Now goto options->set up account.

Now you can see two different procedures..

1. Manually add an account

2. Available google accounts

Just check your gmail account in available google accounts. click on your account..this will show a pop up screen like beginning setup..Then you will be alerted with account already enrolled. Now click on scan barcode.

Note: Make sure you have barcode scanner app in your mobile.

Now just open your browser in desktop and login to your account..it will show a barcode in your desktop browser..with your mobile barcode scanner..just scan that barcode..thats it.your job is done..your account is highly secured with google authenticator 😀

How to transfer files very easily–internet as medium

Hey folks,

I hope few well known about pipe concept, the easiest way to transfer files in internet. Anyways iam sharing this post for people who are unaware of this 😉

If you want to send a doc/image/video etc from mobile(browser enabled) to desktop (or) browser enabled mobile to browser enabled mobile (or) desktop to desktop. Anything is mere like elementary act ;).

A website offers this functionality just like peer-to-peer connection(not theoritically :P). Just logon to Pipebytes and upload your file and copy the url or code and click send file. Now just share that code/url with your friend who need to paste that code in receive section and then hit pickup file. Thats it..now your coveted file will get download over your friend machine/mobile/anything.

Follow the screenshots for better understanding..

29-01-2014 08-22-54

 

Fairly simple right?? 🙂