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

JAVA Questions

An inner class is nested within another class and has access to the outer class' variables and methods. A subclass inherits from a superclass and can access its public and protected methods and fields. There are four access specifiers - public, protected, default, and private - that define the scope of classes, methods, and fields. Static methods and variables are shared among all objects of a class rather than having separate copies for each object. Encapsulation combines properties and methods into a single unit, following a modular approach and serving data hiding. A singleton class can have only one instance, which is useful when limiting objects, such as for a single database connection. The three types of loops in Java are for, while, and do-
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

JAVA Questions

An inner class is nested within another class and has access to the outer class' variables and methods. A subclass inherits from a superclass and can access its public and protected methods and fields. There are four access specifiers - public, protected, default, and private - that define the scope of classes, methods, and fields. Static methods and variables are shared among all objects of a class rather than having separate copies for each object. Encapsulation combines properties and methods into a single unit, following a modular approach and serving data hiding. A singleton class can have only one instance, which is useful when limiting objects, such as for a single database connection. The three types of loops in Java are for, while, and do-
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA Questions

1) What is the difference between an Inner Class and a Sub-Class?


An Inner class is a class which is nested within another class. An Inner
class has access rights for the class which is nesting it and it can access
all variables and methods defined in the outer class.

A sub-class is a class which inherits from another class called super class.
Sub-class can access all public and protected methods and fields of its
super class.

2) What are the various access specifiers for Java classes?


In Java, access specifiers are the keywords used before a class name
which defines the access scope. The types of access specifiers for classes
are:

1) Public: Class,Method,Field is accessible from anywhere.

2) Protected: Method,Field can be accessed from the same class to which


they belong or from the sub-classes, and from the class of same package,
but not from outside.

3) Default: Method,Field,class can be accessed only from the same


package and not from outside of it’s native package.

4) Private: Method,Field can be accessed from the same class to which


they belong.

3) What’s the purpose of Static methods and static variables?


When there is a requirement to share a method or a variable between
multiple objects of a class instead of creating separate copies for each
object, we use static keyword to make a method or variable shared for all
objects.

4) What is data encapsulation and what’s its significance?


Encapsulation is a concept in Object Oriented Programming for
combining properties and methods in a single unit.

Encapsulation helps programmers to follow a modular approach for


software development as each object has its own set of methods and
variables and serves its functions independent of other objects.
Encapsulation also serves data hiding purpose.

5) What is a singleton class? Give a practical example of its usage.


A singleton class in java can have only one instance and hence all its
methods and variables belong to just one instance. Singleton class
concept is useful for the situations when there is a need to limit the
number of objects for a class.

The best example of singleton usage scenario is when there is a limit of


having only one connection to a database due to some driver limitations
or because of any licensing issues.

6) What are Loops in Java? What are three types of loops?


Looping is used in programming to execute a statement or a block of
statement repeatedly. There are three types of loops in Java:

1) For Loops

For loops are used in java to execute statements repeatedly for a given
number of times. For loops are used when number of times to execute the
statements is known to programmer.

2) While Loops

While loop is used when certain statements need to be executed


repeatedly until a condition is fulfilled. In while loops, condition is
checked first before execution of statements.

3) Do While Loops

Do While Loop is same as While loop with only difference that condition
is checked after execution of block of statements. Hence in case of do
while loop, statements are executed at least once.

7) What is the difference between double and float variables in Java?


In java, float takes 4 bytes in memory while Double takes 8 bytes in
memory. Float is single precision floating point decimal number while
Double is double precision decimal number.
8) What is Final Keyword in Java? Give an example.
In java, a constant is declared using the keyword Final. Value can be
assigned only once and after assignment, value of a constant can’t be
changed.

In below example, a constant with the name const_val is declared and


assigned avalue:

Private Final int const_val=100

When a method is declared as final, it can NOT be overridden by the


subclasses. This method are faster than any other method, because they
are resolved at complied time.

When a class is declares as final, it cannot be subclassed. Example String,


Integer and other wrapper classes.

9) What are Java Packages? What’s the significance of packages?


In Java, package is a collection of classes and interfaces which are
bundled together as they are related to each other. Use of packages helps
developers to modularize the code and group the code for proper re-use.
Once code has been packaged in Packages, it can be imported in other
classes and used.

10) Can we declare a class as Abstract without having any abstract


method?
Yes we can create an abstract class by using abstract keyword before
class name even if it doesn’t have any abstract method. However, if a
class has even one abstract method, it must be declared as abstract
otherwise it will give an error.

11) What’s the difference between an Abstract Class and Interface in


Java?
The primary difference between an abstract class and interface is that an
interface can only possess declaration of public static methods with no
concrete implementation while an abstract class can have members with
any access specifiers (public, private etc) with or without concrete
implementation.
Another key difference in the use of abstract classes and interfaces is that
a class which implements an interface must implement all the methods of
the interface while a class which inherits from an abstract class doesn’t
require implementation of all the methods of its super class.

A class can implement multiple interfaces but it can extend only one
abstract class.

12) What are the performance implications of Interfaces over


abstract classes?
Interfaces are slower in performance as compared to abstract classes as
extra indirections are required for interfaces. Another key factor for
developers to take into consideration is that any class can extend only one
abstract class while a class can implement many interfaces.

Use of interfaces also puts an extra burden on the developers as any time
an interface is implemented in a class; developer is forced to implement
each and every method of interface.

13) Does Importing a package imports its sub-packages as well in


Java?
In java, when a package is imported, its sub-packages aren’t imported and
developer needs to import them separately if required.

For example, if a developer imports a package university.*, all classes in


the package named university are loaded but no classes from the sub-
package are loaded. To load the classes from its sub-package (say
department), developer has to import it explicitly as follows:

Import university.department.*

14) Is it compulsory for a Try Block to be followed by a Catch Block


in Java for Exception handling?
Try block needs to be followed by either Catch block or Finally block or
both. Any exception thrown from try block needs to be either caught in
the catch block or else any specific tasks to be performed before code
abortion are put in the Finally block.

15) How is Java different from C++?


C++ is only a compiled language, whereas Java is compiled as well as an
interpreted language.
Java programs are machine-independent whereas a c++ program can run
only in the machine in which it is compiled.
C++ allows users to use pointers in the program. Whereas java doesn’t
allow it. Java internally uses pointers.
C++ supports the concept of Multiple inheritances whereas Java doesn't
support this. And it is due to avoiding the complexity of name ambiguity
that causes the diamond problem

You might also like