suraj k sahu


{ City } pune
< Country > india
* Profession * tl
User No # 111686
Total Questions Posted # 30
Total Answers Posted # 42

Total Answers Posted for My Questions # 47
Total Views for My Questions # 91311

Users Marked my Answers as Correct # 177
Users Marked my Answers as Wrong # 9
Questions / { suraj k sahu }
Questions Answers Category Views Company eMail

How many Java environments are there?

Phantom Technologies,

1 Core Java 2164

What is Java Classloader?

Phantom Technologies,

1 Core Java 2160

How does JAVA ClassLoader work?

IBM,

1 Core Java 3546

What is java class writing rules?

Oracle,

1 Core Java 2528

What are possible key words, we can use to declare a class?

Tech Mahindra,

2 Core Java 3023

How many types of modifiers are there?

Infosys,

1 Core Java 2571

What are access modifiers?

Infosys,

1 Core Java 2581

What are non-access modifiers?

Cognizant,

2 Core Java 2824

How does access modifiers work?

Wipro,

1 Core Java 2234

How does static modifier work?

Wipro,

1 Core Java 2425

How does abstract modifier work?

Wipro,

1 Core Java 2220

How does synchronized modifier work?

IBM,

1 Core Java 2327

How does final modifier work?

Tech Mahindra,

1 Core Java 2647

What are legal modifiers that we can use to declare an inner class?

Infosys,

1 Core Java 2590

What is difference between “==” and equals()?

IBM,

1 Core Java 2401


 [1]   2    Next



Answers / { suraj k sahu }

Question { Karrox, 28033 }

What is serialization ?


Answer

The process of converting object into byte stream is called serialization.
Advantages
1. It persists the state of an object in local disk or DB or can be transferred in network.
2. The serialized stream can be encrypted, authenticated or compressed.
3. It is used to store to some frequent used remote / third party objects as server local cache in case of web application.
4. RMI, EJB and JAXB are rely on serialization

Disadvantages
1. Large objects are not recommended to serialize.
2. Transient or static variables are not serialized.
3. If serialized object can’t be modified easily as it might impact client implementation.
4. It breaks singleton pattern (more than one object created) and encapsulation (violets access modifier rules).

Is This Answer Correct ?    0 Yes 0 No

Question { Wipro, 18772 }

how to create thread in java?


Answer

Threads are created in three ways
1. Sub-class Thread: Create sub-class of java.lang.Thread class
2. Runnable object: Implementing java.lang.Runnable interface.
3. Callable object: Implementing java.lang.Callable interface.

Is This Answer Correct ?    0 Yes 0 No


Question { BMC, 8221 }

What is thread?


Answer

Threads are lightweight process which lives inside process. These are independently running programs which have concurrent path of execution. Each thread has its own 1. Java stack 2. Program counter and 3. Native stack but have common heap space. Multiple threads with in same process share same variables and objects. They allocates objects from same heap and even they can share same instructions (execution code) at particular time. As a result above common access they can easily share information to each other.

Every program has at least one thread i.e. main thread. JVM creates main thread which calls main method to execute whole program. The main thread is non-daemon thread. Any thread created by main method is non-daemon thread by default.

JVM has daemon threads for garbage collection, object finalization and other housekeeping jobs.

Is This Answer Correct ?    0 Yes 0 No

Question { TCS, 16012 }

What is ENUM?


Answer

1. Enum is implicitly final subclass of java.lang.Enum.
2. if an enum is a member of a class, it is implicitly static.
3. The keyword “new” can never be used with an enum, even within the enum type itself.
4. For enum constants, “equals” and “==” is same thing, and can be used interchangeably.
5. Enum constants are implicitly public static final.
6. The enum constants are its instances.
7. Only private modifier can be used before it’s constructor.
8. Enum can implement interface but can’t extends any class
9. We can override only toString() method to return any custom string.
10. The static method values() return all enum constants in an order as it found inside enum class.
11. The instance method name() returns string value of enum constant.

Is This Answer Correct ?    0 Yes 0 No

Question { TCS, 5430 }

What is JVM ?


Answer

Java Virtual Machine is the runtime environment of JAVA. The JVM has 3 forms as 1. Specification 2. Concrete implementation 3. Run time execution. JVM is platform dependent. A single JVM instance is created per JAVA standalone or web application. The lifecycle of JVM instance starts with the start of application and ends with the stop of application.

Is This Answer Correct ?    1 Yes 0 No

Question { IBM, 2374 }

Why should we use java?


Answer

Java has proven its strength in past two decades with below points. This language is being used in various fields like desktop, web, enterprise, game, mobile, embedded system, cloud and analytics (Hadoop).
• Open source
• Easy to learn
• Platform independent.
• Wide range of free community, forum
• Large number of APIs and JAVA docs.
• It’s an OOP language.
• Supports multithreading.
• Automatic memory management and garbage collection
• Good memory analysis tool like JConsole and VusualVM, and MAT
• Good number of free IDE tools etc

Is This Answer Correct ?    2 Yes 0 No

Question { Phantom Technologies, 2164 }

How many Java environments are there?


Answer

There are two java environments
• JDK – Java development environment
• JRE – Java runtime environment

Is This Answer Correct ?    2 Yes 0 No

Question { Phantom Technologies, 2160 }

What is Java Classloader?


Answer

Java Classloader is part of Java runtime environment which loads class on demand (lazy loading) into JVM (Java Virtual Machine) not only from local file system but from remote system or web.

There are 3 types of Classloader.
i. Bootstrap Classloader: Loads core java API file rt.jar from folder.
ii. Extension Classloader: Loads jar files from folder.
iii. System/Application Classloader: Loads jar files from path specified in environment variable as CLASSPATH.

Is This Answer Correct ?    3 Yes 0 No

Question { IBM, 3546 }

How does JAVA ClassLoader work?


Answer

The java ClassLoader works in hierarchical manner.
There are 3 phases in class loader execution.
i. Loading phase: Finding and importing the binary data into JVM
ii. Linking phase: Linking is the process of incorporating the binary type data into the runtime state of the virtual machine. Linking is divided into three sub-steps: verification, preparation and resolution.
iii. Initializing phase: Invoking Java code that initializes class variables to their proper starting values.

1. Loading Phase: Classloader fallows 3 principles during this phase, the three principles are as below

i. Delegation Principle: The delegation of class load request executes in two ways

Namespace search: This search took place first from System Classloder (Child) to Bootstrap Classloader (Parent) for already loaded class.
Class loading request comes to System Classloader, this Classloader finds in own namespace area in constant pool in heap for the reference of requested class if found return fully qualified class name to JVM else it delegates the load request to its parent for the namespace search and finally the load request reaches to Bootstrap Classloader. The Bootstrap Classloader finds in its own namespace area in constant pool in heap for the reference of requested class, if found returns fully qualified class name to JVM.

CLASSPATH search: This search starts as Bootstrap Classloader fails to get class details in its own namespace and this classpath search recursively reaches to System Classloader.

As Bootstrap Classloader fails to get class details in its own namespace, it finds the class in its own classpath, if found, imports binary (reads .class) details and keep track in own namespace area otherewise throws ClassNotFoundException with stacktrace details to child, If exception from parent then the Extension Classloader finds in its classpath if found, imports binary data and keep track in own namespace area otherwise throws ClassNotFoundException with stack trace details to child. If exception from parent, the System Classloader tries to import binary data from CLASSPATH is set as environment variable, if fails throws ClassNotFoundException with stack trace details JVM.

After importing binary data, each Classloader trace into the binary data and pulls binary data of reference type such as superclass, field type, method return type, parameter type etc respectively and prepares internal structure in constant pool of method area in heap. During creation of internal structure, the Class File Verifier makes sure the class file (byte codes) is safe to use with the help of various checks as per Java specification and the proper class file (byte code) is loaded into JVM (method area in heap) after successful verification.

During the loading phase the symbolic references between dependent and dependency are properly organized in constant pool by JVM

Finally create instance of java.lang.Class of each Class type with act as interface between program and internal structure in heap.

ii. Visibility Principle: According to this principle, child Classloader can see all classes loaded by its parent Classloader but not vice-versa. If we try to load class explicitly, it will through ClassNotFoundException.
public class ClassLoaderTest {
public static void main(String args[]) {
try {
//printing ClassLoader of this class
System.out.println("ClassLoaderTest.getClass().getClassLoader() : "
+ ClassLoaderTest.class.getClassLoader());
//trying to explicitly load this class again using Extension class loader
Class.forName("test.ClassLoaderTest", true
, ClassLoaderTest.class.getClassLoader().getParent());
} catch (ClassNotFoundException ex) {
Logger.getLogger(ClassLoaderTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

iii. Uniqueness Principle: This principle says, same class can’t be loaded by parent and child Classloader. Class loading reference should be unique throughout all individual Classloader namespaces.

2. Linking Phase: This phase performs three main sub-phases as below
i. Verification: During this sub-phase, the symbolic references are verified by locating classes, interfaces, fields, and methods stored in the constant pool. When the virtual machine searches for a symbolically referenced entity (type, field, or method), it first make sure the entity exists. If the virtual machine finds that the entity exists, it must further check that the referencing type has permission to access the entity, given the entity has access permissions and other checks are done.

ii. Class Preparation: During this sub-phase, the binary data along with all the reference type have already imported and loaded as internal structure in constant pool (namespace area) of JVM and successfully completed all required verifications and is ready for preparation. Now JVM allocates memory for the class variables and sets them to default initial values. The class variables are not initialized to their proper initial values until the initialization phase. (No Java code is executed during the preparation step.) During preparation, the Java Virtual Machine sets the newly allocated memory for the class variables to a default value determined by the type of the variable.

iii. Resolution: Resolution is the process of locating classes, interfaces, fields, and methods referenced symbolically from a type in constant pool, and replacing those symbolic references with direct/pointer references. This phase of linking is optional until and unless each symbolic reference is first used by the program.

3. Initializing Phase: Initialization is required to ready a class or interface for its first active use, It is the process of setting class variables to their proper initial values as set by programmer.

Is This Answer Correct ?    3 Yes 0 No

Question { Oracle, 2528 }

What is java class writing rules?


Answer

• There can be only one public class per source code file and the name of the file must match the name of public class.
• Comments:
Beginning Comments: All java source file should start with this comment which contains the info as below

/*
* Classname
*
* Version info
*
* Copyright notice
*/
Package and Import Statements: These statements should not be commented.
Class/interface documentation comment (/**...*/): This comment should be before the declaration of Class/interface as below
/**
* Class description goes here.
*
* @version 1.10 04 Oct 1996
* @author Firstname Lastname
*/
Class/interface implementation comment (/*...*/), if necessary: This comment comes immediate after declaration of Class/interface.
public class/interface Blah {
/* A class/interface implementation comment can go here. */
Class variable documentation/implementation comment (/**...*/, /*...*/): This comment should be on the top of class variable declaration as per need.
/** classVar1 documentation comment */
public static int classVar1;

/* classVar2 documentation comment */
public static int classVar2;

Instance variable documentation/implementation comment (/**...*/, /*...*/): This comment should be on the top of instance variable

/** classVar1 documentation comment */
public int classVar1;

/* classVar2 documentation comment */
public int classVar2;

Method documentation comment (/**...*/): This comment should be before the declaration of method.
/**
* ...method documentation comment...
* @param someParam description
* @return if necessary
*/
Method/Block implementation comment (/*...*/): This comment should be immediate after declaration of method/block
/*
* Here is a block comment.
*/

Single line comment (/*...*/): This comment should be on top of any single statement in code level.


Trailing Comments (/*...*/):
if (a == 2) {
return TRUE; /* special case */
} else {
return isprime(a); /* works only for odd a */
}

End-Of-Line Comments (//):

if (condition) {
statements;
}// End if

fooBar.fChar = barFoo.lchar = 'c'; // AVOID

• Package statement must be the first line
• Import statements must go between the package statement and the class declaration. If there isn't a package statement then the import statement(s) must be the first line(s) in the source code file and if there are no package or import statements, the class declaration must be the first line in the source code file.
• Class/instance variable should be declared in order of public, protected and private but Class variable should be before instance variable.
• As good practice Static block(s) should come before constructers.
• Method definition should be as per Class/instance variable but related dependency methods should be defined consecutively.
• Importing as class in other class of same package is optional but mandatory in other class of different package.

Is This Answer Correct ?    1 Yes 0 No

Question { Tech Mahindra, 3023 }

What are possible key words, we can use to declare a class?


Answer

public, abstract and final

Is This Answer Correct ?    8 Yes 0 No

Question { Infosys, 2571 }

How many types of modifiers are there?


Answer

There are two types of modifiers as 1. Access-modifier and 2. Non-access-modifier

Is This Answer Correct ?    5 Yes 0 No

Question { Infosys, 2581 }

What are access modifiers?


Answer

There are 4 access modifiers
• Public
• Protected
• Default
• Private

Is This Answer Correct ?    5 Yes 0 No

Question { Cognizant, 2824 }

What are non-access modifiers?


Answer

There are below commonly used non-access modifiers
• Static
• Abstract
• Synchronized
• Final

Is This Answer Correct ?    2 Yes 0 No

Question { Wipro, 2234 }

How does access modifiers work?


Answer

Public: It is the highest visibility access modifier. It makes visible the class members (variables/methods) to any classes (child/non-child) present within same or any package.
Protected: It has lesser visibility than public access modifier. It makes visible the class members (variables/methods) to sub-classes within same or different package and it also make visible in any class of same package.
Default: It has lesser visibility than protected access modifier. It makes visible the class members (variables/methods) to any classes within same package only.
Private: It has least visibility than all access modifiers. It makes visible the class members (variables/methods) within same class only.

Is This Answer Correct ?    4 Yes 0 No

 [1]   2   3    Next