Hi Guys, In this post I'll share some interview questions and answers which i have faced or asked in interview panel. you can read my previous post related to JAVA QA JAVA QA PART 1 and JAVA QA PART 2
1). What is Java Reflection API? Why it’s so important to have?
Ans). Java Reflection API provides ability to inspect and modify the runtime behavior of java application. We can inspect a java class, interface, enum and get their methods and field details. Reflection API is an advanced topic and we should avoid it in normal programming. Reflection API usage can break the design pattern such as Singleton pattern by invoking the private constructor i.e violating the rules of access modifiers.
2). Which class is the superclass of all classes?
Ans). java.lang.Object is the root class for all the java classes and we don’t need to extend it.
}}
}}
1). What is Java Reflection API? Why it’s so important to have?
Ans). Java Reflection API provides ability to inspect and modify the runtime behavior of java application. We can inspect a java class, interface, enum and get their methods and field details. Reflection API is an advanced topic and we should avoid it in normal programming. Reflection API usage can break the design pattern such as Singleton pattern by invoking the private constructor i.e violating the rules of access modifiers.
Even though we don’t use Reflection API in normal programming, it’s very important to have. We can’t have any frameworks such as Spring, Hibernate or servers such as Tomcat, JBoss without Reflection API. They invoke the appropriate methods and instantiate classes through reflection API and use it a lot for other processing.
Ans). java.lang.Object is the root class for all the java classes and we don’t need to extend it.
3). What is difference between Heap and Stack Memory?
Ans). Major
difference between Heap and Stack memory are as follows:
1. Heap memory is used by all the parts of the application
whereas stack memory is used only by one thread of execution.
2. Whenever an object is created, it’s always stored in the Heap
space and stack memory contains the reference to it. Stack memory only contains
local primitive variables and reference variables to objects in heap space.
Memory management in stack is done in LIFO manner whereas it’s more
complex in Heap memory because it’s used globally.
4). Java Compiler is stored in JDK, JRE or JVM?
Ans). The task
of java compiler is to convert java program into bytecode, we have
javac
executable for that. So it must be stored
in JDK, we don’t need it in JRE and JVM is just the specs.
5). What will be the output of following programs?
1. static method in
class
package com.journaldev.util;
public class Test {
public static String toString(){
System.out.println("Test toString called");
return "";
}
public static void main(String args[]){
System.out.println(toString());
Ans). The code
won’t compile because we can’t have an
Object
class method
with static keyword. Note that Object class has toString()
method. You will get compile time error
as “This static method cannot hide the instance method from Object”. The reason
is that static method belongs to class and since every class base is Object, we
can’t have same method in instance as well as in class. You won’t get this
error if you change the method name from toString() to something else that is
not present in super class Object
.
1. static method
invocation
package com.journaldev.util;
public class Test {
public static String foo(){
System.out.println("Test foo called");
return "";
}
public static void main(String args[]){
Test obj = null;
System.out.println(obj.foo());
Ans). Well this is a strange situation. We
all have seen
NullPointerException
when we
invoke a method on object that is NULL. But here this program will work and
prints “Test foo called”.
1.
The reason
for this is the java compiler code optimization. When the java code is compiled
to produced byte code, it figures out that foo() is a static method and should
be called using class. So it changes the method call
obj.foo()
to Test.foo()
and hence
no NullPointerException
.
6). What is the difference between an Inner Class and a Sub-Class?
Ans). 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.
7). What are the various access specifiers for Java classes?
Ans). 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.
8). What’s the purpose of Static methods and static variables?
Ans). 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.
9). What is data encapsulation and what’s its significance?
Ans). 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.
10). What is a singleton class? Give a practical example of its usage.
Ans). 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.
11). What are Loops in Java? What are three types of loops?
Ans). 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.
12). What is an infinite Loop? How infinite loop is declared?
Ans). An infinite
loop runs without any condition and runs infinitely. An infinite loop can be
broken by defining any breaking logic in the body of the statement blocks.
Infinite loop
is declared as follows:
1
2
3
4
5
6
7
8
9
|
for (;;)
{
// Statements
to execute
// Add any
loop breaking logic
}
|
13). What is the difference between continue and break statement?
Ans). break and
continue are two important keywords used in Loops. When a break keyword is used
in a loop, loop is broken instantly while when continue keyword is used,
current iteration is broken and loop continues with next iteration.
In below
example, Loop is broken when counter reaches 4.
1
2
3
4
5
6
7
8
|
for (counter=0;counter<10;counter++)
system.out.println(counter);
if (counter==4) {
break;}
}
|
In the below
example when counter reaches 4, loop jumps to next iteration and any statements
after the continue keyword are skipped for current iteration.
1
2
3
4
5
6
7
8
9
10
11
12
|
for (counter=0;counter<10;counter++)
system.out.println(counter);
if (counter==4) {
continue;
}
system.out.println("This will not get printed when counter is 4");
}
|
14). What is the difference between double and float variables in Java?
Ans). 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.
15). What is Final Keyword in Java? Give an example.
Ans). 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.
Cheers :)
No comments:
Post a Comment