JAVA Answers.

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

JAVA ANSWERS.

1. JVM
JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the
runtime environment in which java byte code can be executed. It is a specification.

JVMs are available for many hardware and software platforms (so JVM is platform
dependent).

JRE
JRE stands for Java Runtime Environment. It is the implementation of JVM.

JDK
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE +
development tools.

2.

Many types:

1. Class(Method) Area

2. Heap

3. Stack

4. Program Counter Register

5. Native Method Stack

3.

Just-In-Time (JIT) compiler: It is used to improve the performance. JIT compiles


parts of the byte code that have similar functionality at the same time, and hence
reduces the amount of time needed for compilation. Here the term “compiler” refers to a
translator from the instruction set of a Java virtual machine (JVM) to the instruction set
of a specific CPU.

4.

A platform is basically the hardware or software environment in which a program runs.


There are two types of platforms software-based and hardware-based. Java provides
software-based platform.
JAVA ANSWERS.
5.

The Java platform differs from most other platforms in the sense that it's a software-
based platform that runs on top of other hardware-based platforms.It has two
components:

1. Runtime Environment

2. API(Application Programming Interface)

6.

The bytecode. Java is compiled to be a byte code which is the intermediate language
between source code and machine code. This byte code is not platform specific and
hence can be fed to any platform.

7.

The class loader is a subsystem of JVM that is used to load classes and interfaces. There
are many types of class loaders e.g. Bootstrap class loader, Extension class loader,
System class loader, Plug-in class loader etc.

8.

The default constructor provides the default values to the objects. The java compiler
creates a default constructor only if there is no constructor in the class.

9.

Yes, that is current instance (You cannot use return type yet it returns a value).

10.

No, constructor is not inherited.

11.

Program compiles and runs properly.


JAVA ANSWERS.
12.

The local variables are not initialized to any default value, neither primitives nor object
references.

13.

Object based programming languages follow all the features of OOPs except Inheritance.
Examples of object based programming languages are JavaScript, VBScript etc.

14.

The object references are all initialized to null in Java.

15.

o Constructor is just like a method that is used to initialize the state of an object. It
is invoked at the time of object creation.

16.

No, constructor can't be final.

17.

o Static variable is used to refer the common property of all objects (that is not
unique for each object) e.g. company name of employees, college name of
students etc.

o Static variable gets memory only once in class area at the time of class loading.

18.

because object is not required to call static method if It were non-static method,jvm
creates object first then call main() method that will lead to the problem of extra
memory allocation.

19.

o s used to initialize the static data member.

o It is excuted before main method at the time of class loading.

20.

Yes, one of the way is static block.


JAVA ANSWERS.
21.

Static or class method

1) A method i.e. declared as static is known as static method.

2) Object is not required to call static method.

3) Non-static (instance) members cannot be accessed in static context (static method,


static block and static nested class) directly.

4) For example: public static int cube(int n){ return n*n*n;}

Instance method
A method i.e. not declared as static is known as instance method.

Object is required to call instance methods.

Static and non-static variables both can be accessed in instance methods.

For example: public void msg (){...}.

22.

Inheritance is a mechanism in which one object acquires all the properties and behaviour
of another object of another class. It represents IS-A relationship. It is used for Code
Reusability and Method Overriding.

23.

To reduce the complexity and simplify the language, multiple inheritance is not
supported in java in case of class.

24.

Holding the reference of the other class within some other class is known as
composition.

25.

Pointer is a variable that refers to the memory address. They are not used in java
because they are unsafe (unsecured) and complex to understand.

26.

No. Because super() or this() must be the first statement.


JAVA ANSWERS.
27.

If a class have multiple methods by same name but different parameters, it is known as
Method Overloading. It increases the readability of the program.

28.

Yes, You can have many main () methods in a class by overloading the main method.

29.

No, you can't override the static method because they are the part of class not object.

30.

Method Overloading
1) Method overloading increases the readability of the program.

2)  method overloading is occurs within the class.

3)  In this case, parameter must be different.

Method Overriding
1) Method overriding provides the specific implementation of the method that is
already provided by its super class.

2) Method overriding occurs in two classes that have IS-A relationship.

3) In this case, parameter must be same.

31.

If you make any variable as final, you cannot change the value of final variable(It will be
constant).

32.

Final methods can't be overridden.

33.

Final class can't be inherited.

34.

Yes, such as, public static final void main (String [] args){}.

35.
JAVA ANSWERS.
In case of static binding type of object is determined at compile time whereas in dynamic
binding type of object is determined at runtime.

36.

Abstraction is a process of hiding the implementation details and showing only


functionality to the user.

37.

Abstraction hides the implementation details whereas encapsulation wraps code and data
into a single unit.

38.

A class that is declared as abstract is known as abstract class. It needs to be extended


and its method implemented. It cannot be instantiated.

39.

No, because abstract method needs to be overridden whereas you can't override final
method.

40.

Interface is a blueprint of a class that have static constants and abstract methods. It can
be used to achieve fully abstraction and multiple inheritances.

41.

Abstract class

1) An abstract class can have method body (non-abstract methods).

2) An abstract class can have instance variables.

3) An abstract class can have constructor.

4) An abstract class can have static methods.

5) You can extends one abstract class.

42.

No, they are implicitly public.

43.
JAVA ANSWERS.
A package is a group of similar type of classes’ interfaces and sub-packages. It
provides access protection and removes naming collision.

44.

By static import, we can access the static members of a class directly, there is no to
qualify it with the class name.

45.

The classes that extend throwable class except Runtime Exception and Error are known
as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are
checked at compile-time.

46.

throwable.

47.

It is not necessary that each try block must be followed by a catch block. It should be
followed by either a catch block OR a finally block. And whatever exceptions are likely to
be thrown should be declared in the throws clause of the method.

48.

Yes, by try block. Finally must be followed by either try or catch.

49.

Throw keyword
1) Throw is used to explicitly throw an exception.

2) Checked exceptions can not be propagated with throw only.

3) Throw is followed by an instance.

4) Throw is used within the method.

5) You cannot throw multiple exception

throws keyword
1) throws is used to declare an exception.

2) checked exception can be propagated with throws.

3) throws is followed by class.

4) throws is used with the method signature.


JAVA ANSWERS.
5) You can declare multiple exception e.g. public void method () throws IOException,
SQLException.

50.

The simple meaning of immutable is unmodifiable or unchangeable. Once string object


has been created, its value can't be changed.

51.

Because java uses the concept of string literal. Suppose there are 5 reference
variables,all referes to one object "sachin".If one reference variable changes the value of
the object, it will be affected to all the reference variables. That is why string objects are
immutable in java.

52. String is an immutable object. String Buffer is a mutable object. String Buffer is
synchronized whereas String Builder is not synchronized.

53.

A class which is declared inside another class is known as nested class. There are 4
types of nested class member inner class, local inner class, anonymous inner class and
static nested class.

54.

Any interface i.e. declared inside the interface or class, is known as nested interface. It
is static by default.

55.

Garbage collection is a process of reclaiming the runtime unused objects. It is performed


for memory management.

56.

Finalize () method is invoked just before the object is garbage collected. It is used to
perform cleanup processing.

57.

Final: final is a keyword, final can be variable, method or class. You, can't change the
value of final variable, can't override final method, can't inherit final class.
Finally: finally block is used in exception handling. Finally block is always executed.
Finalize (): finalize () method is used in garbage collection. Finalize () method is
invoked just before the object is garbage collected.The finalize () method can be used
to perform any cleanup processing.
58.
Serialization is a process of writing the state of an object into a byte stream. It is
mainly used to travel object's state on the network.
JAVA ANSWERS.

59.
Yes, by changing the runtime behaviour of a class if the class is not secured.

60.
An applet is a small java program that runs inside the browser and generates dynamic
contents.

61.

Multithreading is a process of executing multiple threads simultaneously. Its main


advantage is:

o Threads share the same address space.

o Thread is lightweight.

o Cost of communication between process is low.

62.

A thread is a lightweight subprocess.It is a separate path of execution. It is called


separate path of execution because each thread runs in a separate stack frame.

63.

The join () method waits for a thread to die. In other words, it causes the currently
running threads to stop executing until the thread it joins with completes its task.

64.

Wait ()

1) The wait () method is defined in Object class.

2) wait() method releases the lock.

sleep ()

The sleep () method is defined in Thread class.]

The sleep () method doesn't releases the lock.


JAVA ANSWERS.

65.

Yes, but it will not work as a thread rather it will work as a normal object so there will
not be context-switching between the threads.

66.

Synchronization is the capabilility of control the access of multiple threads to any


shared resource.It is used:

1. To prevent thread interference.

2. To prevent consistency problem.

67.

o Synchronized block is used to lock an object for any shared resource.

o Scope of synchronized block is smaller than the method.

68.

If you make any static method as synchronized, the lock will be on the class not on
object.

69.

The notify () is used to unblock one waiting thread whereas notify All () method is
used to unblock all the threads in waiting state.

70.

Deadlock is a situation when two threads are waiting on each other to release a
resource. Each thread waiting for a resource which is held by the other waiting thread.

71.

ArrayList

1. ArrayList is not synchronized.


JAVA ANSWERS.
2. ArrayList is not a legacy class.
3. ArrayList increases its size by 50% of the array size.

Vector

Vector is synchronized.

Vector is a legacy class.

Vector increases its size by doubling the array size.

72.

ArrayList

1) ArrayList uses a dynamic array.


2) ArrayList is not efficient for manipulation because a lot of shifting is
required.
3) ArrayList is better to store and fetch data.

LinkedList

LinkedList uses doubly linked list.

LinkedList is efficient for manipulation.

LinkedList is better to manipulate data.

73.

List can contain duplicate elements whereas Set contains only unique elements.

74.

Set contains values only whereas Map contains key and values both.
JAVA ANSWERS.
75.

Collection is an interface whereas Collections is a class. Collection interface


provides normal functionality of data structure to List, Set and Queue. But, Collections
class is to sort and synchronize collection elements.

76.

If we use generic class, we don't need typecasting. It is typesafe and checked at


compile time.

77.
The Dictionary class provides the capability to store key-value pairs.

78.

JDBC is a Java API that is used to connect and execute query to the database. JDBC
API uses jdbc drivers to connect to the database.

79.

JDBC Driver is a software component that enables java application to interact with the
database. There are 4 types of JDBC drivers:

1. JDBC-ODBC bridge driver

2. Native-API driver (partially java driver)

3. Network Protocol driver (fully java driver)

4. Thin driver (fully java driver)

80.

o Registering the driver class

o Creating connection

o Creating statement

o Executing queries

o Closing connection
JAVA ANSWERS.
81.

The java.sql package contains interfaces and classes for JDBC API.

Interfaces:

o Connection

o Statement

o Prepared Statement

o Result Set

o ResultSetMetaData

o Database Metadata

o Callable Statement etc.

Classes:

o Driver Manager

o Blob

o Clob

o Types

o SQLException etc.

82.

There are 3 JDBC statements.

1. Statement

2. PreparedStatement

3. CallableStatement

83.

The Driver Manager class manages the registered drivers. It can be used to


register and unregister drivers. It provides factory method that returns the instance of
Connection.

84.
JAVA ANSWERS.
The Connection interface maintains a session with the database. It can be used
for transaction management. It provides factory methods that returns the instance of
Statement, PreparedStatement, CallableStatement and Database Metadata.

85.

The Result Set object represents a row of a table. It can be used to change the
cursor pointer and get the information from the database.

86. The DatabaseMetaData interface returns the information of the database such as
username, driver name, driver version, number of tables, number of views etc.

87. By using PreparedStatement interface, we can store and retrieve images.

88. The Connection interface provides methods for transaction management such as


commit (), rollback () etc.
JAVA ANSWERS.

You might also like