Core Java Questions And Answers -Part-9

Why Java take 2 byte of memory for store character ?
Java support more than 18 international languages so java take 2 byte for characters, because for 18 international language 1 byte of memory is not sufficient for storing all characters and symbols present in 18 languages. Java supports Unicode but c support ascii code. In ascii code only English language are present, so for storing all English latter and symbols 1 byte is sufficient.


What is java and javac ?
.Java tool are used for run the java program and javac tool are used for compile the java program.


Why Boolean data types take zero byte of memory ?
Boolean data type takes zero bytes of main memory space because Boolean data type of java implemented by Sun Micro System with a concept of flip - flop. A flip - flop is a general purpose register which stores one bit of information (one true and zero false).


Which access specifier are called universal access specifier ?
Public


Which access specifier is not a keyword ?
Default

Which access specifier is package level access specifier ?
Default

Type Casting
Assigning a value of one type to a variable of another type is known as Type Casting.
Example :
int x = 10;
byte y = (byte)x;
In Java, type casting is classified into two types,
Widening Casting(Implicit)
Narrowing Casting(Explicitly done)

Widening or Automatic type converion
Automatic Type casting take place when,
è the two types are compatible
è the target type is larger than the source type


Narrowing or Explicit type conversion
When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting.


What is the difference between Method and Constructor
·         Method can be any user defined name
Constructor must be class name
·         Method should have return type
Constructor should not have any return type (even void)

·         Method should be called explicitly either with object reference or class reference.
Constructor will be called automatically whenever object is created

·         Method is not provided by compiler in any case.
The java compiler provides a default constructor if we do not have any constructor.



Important points For Exception Handling:
1.    If you do not explicitly use the try catch blocks in your program, java will provide a default exception handler, which will print the exception details on the terminal, whenever exception occurs.
2.    Super class Throwable overrides toString() function, to display error message in form of string.
3.    While using multiple catch block, always make sure that exception subclasses comes before any of their super classes. Else you will get compile time error.
4.    In nested try catch, the inner try block, uses its own catch block as well as catch block of the outer try, if required.
5.    Only the object of Throwable class or its subclasses can be thrown.


NullPointerException
Trying to access a null object or performing operations on an object having a null value.

1.    package com.beingjavaguys.core; 
2.    import java.util.ArrayList; 
3.     
4.    public class ExceptionTest { 
5.     
6.    public static void main(String[] args) { 
7.    String string = null; 
8.    System.out.println(string.length()); 
9.    } 
10.   } 
11.       


Exception in thread "main" java.lang.NullPointerException
 at com.beingjavaguys.core.ExceptionTest.main(ExceptionTest.java:9)




Explain what is Maven? How does it work?
Maven is a project management tool. It provides the developer a complete build lifecycle framework. On executing Maven commands, it will look for POM file in Maven; it will run the command on the resources described in the POM.

List out what are the aspects does Maven Manages?
Maven handles following activities of a developer
• Build
• Documentation
• Reporting
• Dependencies
• SCMs
• Releases
• Distribution
• Mailing list


Mention the three build lifecycle of Maven?
• Clean: Cleans up artifacts that are created by prior builds
• Default (build): Used to create the application
• Site: For the project generates site documentation

Explain what is POM?
In Maven, POM (Project Object Model) is the fundamental unit of work. It is an XML file which holds the information about the project and configuration details used to build a project by Maven.


Explain what is Maven artifact?
Usually an artifact is a JAR file which gets arrayed to a Maven repository. One or more artifacts a maven build produces such as compiled JAR and a sources JAR.
Each artifact includes a group ID, an artifact ID and a version string.

Explain what is Maven Repository? What are their types?
A Maven repository is a location where all the project jars, library jars, plugins or any other particular project related artifacts are stored and can be easily used by Maven.
Their types are local, central and remote

Why Maven Plugins are used?
Maven plugins are used to
• Create a jar file
• Create war file
• Compile code files
• Unit testing of code
• Documenting projects
• Reporting

List out the dependency scope in Maven?
The various dependency scope used in Maven are:
• Compile: It is the default scope, and it indicates what dependency is available in the classpath of the project
• Provided: It indicates that the dependency is provided by JDK or web server or container at runtime
• Runtime: This tells that the dependency is not needed for compilation but is required during execution
• Test: It says dependency is available only for the test compilation and execution phases
• System: It indicates you have to provide the system path
• Import: This indicates that the identified or specified POM should be replaced with the dependencies in that POM’s section


Mention how profiles are specified in Maven?
Profiles are specified in Maven by using a subset of the elements existing in the POM itself.


Explain how you can exclude dependency?
By using the exclusion element, dependency can be excluded


Mention the difference between Apache Ant and Maven?
• Ant is a toolbox – Maven is a framework
• Ant does not have formal conventions like project directory structure – Maven has conventions
• Ant is procedural; you have to tell to compile, copy and compress – Maven is declarative ( information on what to make & how to build)
• Ant does not have lifecycle; you have to add sequence of tasks manually – Maven has a lifecycle
• Ant scripts are not reusable – Maven plugins are reusable


In Maven what are the two setting files called and what are their location?
In Maven, the setting files are called settings.xml, and the two setting files are located at

• Maven installation directory: $M2_Home/conf/settings.xml
• User’s home directory: ${ user.home }/ .m2 / settings.xml


List out what are the build phases in Maven?
Build phases in Maven are
• Validate
• Compile
• Test
• Package
• Install
• Deploy

List out the build, source and test source directory for POM in Maven?
• Build = Target
• Source = src/main/java
• Test = src/main/test



Where do you find the class files when you compile a Maven project?
You will find the class files ${basedir}/target/classes/.

Explain what would the “jar: jar” goal do?
jar: jar will not recompile sources; it will imply just create a JAR from the target/classes directory considering that everything else has been done


List out what are the Maven’s order of inheritance?
The maven’s order of inheritance is
• Parent Pom
• Project Pom
• Settings
• CLI parameters


For POM what are the minimum required elements?
The minimum required elements for POM are project root, modelVersion, groupID, artifactID and version


Explain how you can produce execution debug output or error messages?
To produce execution debug output you could call Maven with X parameter or e parameter



Explain how to run test classes in Maven?
To run test classes in Maven, you need surefire plugin, check and configure your settings in setting.xml and pom.xml for a property named “test.”

What is archetype?
Archetype is the maven plugin. It creates the project structure.


What would the command mvn clean do ?
This command removes the target directory with all the build data before starting the build process.


What is Central Repository?
It is repository provided by Maven community. It contains a large number of commonly used libraries. When Maven does not find any dependency in local repository, it starts searching in central repository using following URL: http://repo1.maven.org/maven2/.


What is local repository?
Maven local repository is a folder location on your machine. It gets created when you run any maven command for the first time. Maven local repository keeps your project's all dependencies (library jars, plugin jars etc).


What is Remote Repository?
Sometimes, Maven does not find a mentioned dependency in central repository as well then it stops the build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository which is developer's own custom repository containing required libraries or other project jars.


What is the sequence in which Maven searches for dependency libraries?
Following is the search pattern −
Step 1 − Search dependency in local repository, if not found, move to step 2 else if found then do the further processing.
Step 2 − Search dependency in central repository, if not found and remote repository/repositories is/are mentioned then move to step 4 else if found, then it is downloaded to local repository for future reference.
Step 3 − If a remote repository has not been mentioned, Maven simply stops the processing and throws error (Unable to find dependency).
Step 4 − Search dependency in remote repository or repositories, if found then it is downloaded to local repository for future reference otherwise Maven as expected stop processing and throws error (Unable to find dependency).


What is SNAPSHOT in Maven?
SNAPSHOT is a special version that indicates a current development copy. Unlike regular versions, Maven checks for a new SNAPSHOT version in a remote repository for every build.


What is difference between Snapshot and Version?
In case of Version, if Maven once downloaded the mentioned version say data-service:1.0, it will never try to download a newer 1.0 available in repository. To download the updated code, data-service version is be upgraded to 1.1.
In case of SNAPSHOT, Maven will automatically fetch the latest SNAPSHOT (data-service:1.0-SNAPSHOT) everytime app-ui team build their project.


What is a Mojo?
A mojo is a Maven plain Old Java Object. Each mojo is an executable goal in Maven, and a plugin is a distribution of one or more related mojos.


No comments:

Post a Comment