You are on page 1of 7

1. What are static blocks and static initializers in JAVA ?

Static Block in JAVA :- Java supports a special block, called static block (also called static clause) which can be used for
static initializations of a class. The code inside static block is executed only once: the first time the class is loaded into
memory.  Static blocks are executed before constructors. 

Eg . class Test {

    static int i;
    int j;
      
    // start of static block 
    static {
        i = 10;
        System.out.println("static block called ");
    }
    // end of static block 
}
  
class Main {
    public static void main(String args[]) {
  
        // Although we don't have an object of Test, static block is 
        // called because i is being accessed in following statement.
        System.out.println(Test.i); 
    }
}
Output:
static block called
10

Static initializers in JAVA : Java provides a feature called a static initializer that’s designed specifically to let you initialize
static fields. A static initializer block resembles a method with no name, no arguments, and no return type. There is no need
to refer to it from outside the class definition. The code in a static initializer block is executed by the virtual machine when
the class is loaded.

Eg. static
{
    //CODE
}

2. How to call one constructor from other constructor?

Via this() we can call one contructor of same class from other constructor. o chain to a particular superclass constructor
instead of one in the same class, use super instead of this. Note that you can only chain to one constructor, and it has to be
the first statement in your constructor body

3. What is method overriding in JAVA?

In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a
specific implementation of a method that is already provided by one of its super-classes or parent classes.

When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a
method in its super-class, then the method in the subclass is said to override the method in the super-class. Method
overriding is one of the way by which java achieve Run Time Polymorphism

4. What is super keyword in JAVA?


The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass
constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses
that have methods with the same name.

5. Diffrance between method overloading and method overriding in JAVA?

The most basic difference is that overloading is being done in the same class while for overriding base and child classes are
required. Overriding is all about giving a specific implementation to the inherited method of parent class.

Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.

Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.

private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one
private/final methods of same name but a child class cannot override the private/final methods of their base class.

Return type of method does not matter in case of method overloading, it can be same or different. However in case of
method overriding the overriding method can have more specific return type

Argument list should be different while doing method overloading. Argument list should be same in method Overriding.

6. Diffrance between abstract class and interface?


 Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8,
it can have default and static methods also.
 Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final
variables.
 Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static
and final variables.
 Implementation: Abstract class can provide the implementation of interface. Interface can’t provide the
implementation of abstract class.
 An abstract class can extend only one class or one abstract class at a time….An  interface can extend any number
of interfaces at a time
 In abstract class keyword “abstract” is mandatory to declare a method as an abstract
 An abstract class can have static, final or static final variable with any access specifier….interface can only have
public static final (constant) variable

7. Why JAVA is platform independent ?

JAVA is Concurrent i.e. it can execute many statement at once instead of executing them sequentially. Java is class based and
object oriented programming language. It is a platform independent language i.e. the compiled code can be run on any java
supporting platform. It runs on the logic of “Write once, run anywhere”.

8. What is method overloading in JAVA ?

Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument
lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor
having different argument lists. Method overloading is an example of Static Polymorphism. 

9. Diff betn C++ and JAVA?

C++ uses compiler only. C++ is compiled and run using the compiler which converts source code into machine code so, C+
+ is platform dependent. WHEREAS JAVA uses compiler and interpreter both. Java source code is converted into bytecode
at compilation time. The interpreter executes this bytecode at runtime and produces output. Java is interpreted that is why it
is platform independent.

10. What is JIT compiler ?


JIT compilers interact with the Java Virtual Machine (JVM) at run time and compile suitable bytecode sequences into native
machine code.

11. What is bytecode in JAVA?

Bytecode is program code that has been compiled from source code into low-level code designed for a software interpreter.
It may be executed by a virtual machine (such as a JVM) or further compiled into machine code, which is recognized by
the processor.

12. Difference between this() and super() in JAVA ?


 super() as well as this() both are used to make constructor calls.
 super() is used to call Base class’s constructor(i.e, Parent’s class) while this() is used to call current class’s
constructor.

13. What is a class ?

A class is a user defined blueprint or prototype from which objects are created.  It represents the set of properties or methods that
are common to all objects of one type. In general, class declarations can include these components, in order:

 Modifiers : A class can be public or has default access.


 Class name: The name should begin with a initial letter (capitalized by convention).
 Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can
only extend (subclass) one parent.
 Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
 Body: The class body surrounded by braces, { }.

14. What is an Object ?

An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc.

It is a basic unit of Object Oriented Programming and represents the real life entities.  A typical Java program creates many
objects, which as you know, interact by invoking methods. An object consists of :
 State : It is represented by attributes of an object. It also reflects the properties of an object.
 Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
 Identity : It gives a unique name to an object and enables one object to interact with other objects.
Example of an object : dog
15. What is method in JAVA ?
A Java method is a collection of statements that are grouped together to perform an operation.
16. What is encapsulation ?
Encapsulation in Java is a mechanism to wrap up variables(data) and methods(code) together as a single unit. It is the
process of hiding information details and protecting data and behavior of the object. It is one of the four important OOP
concepts. The encapsulate class is easy to test, so it is also better for unit.
17. Explain about main() method in Java
Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args).

18. Why main() method is public static and void in Java?


 Java specifies several access modifiers e.g. private, protected and public. Any method or variable which is
declared public in Java can be accessible from outside of that class. Since the main method is public in Java, JVM
can easily access and execute it.
 Java main() method is always static, so that compiler can call it without the creation of an object or before the
creation of an object of the class
 Since the main method in Java is not supposed to return any value, it's made void which simply means main is not
returning anything.

19. What is constructor in Java ?


 It is a special type of method which is used to initialize the object.
 In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created.
At the time of calling constructor, memory for the object is allocated in the memory.

20. What is difference between length and length() method in Java ?


 The length variable is applicable to array but not for string objects whereas the length() method is applicable for
string objects but not for arrays.
 array.length : length is a final variable applicable for arrays. With the help of length variable, we can obtain the
size of the array.
 string.length() : length() method is a final variable which is applicable for string objects. length() method returns
the number of characters presents in the string.

21. What is ASCII code?


22. What is Unicode?
23. Diff betn character constant and string constant in Java ?
24. What are constants and how to create constants in Java?
25. Difference between ‘>>’ operator and ‘>>>’ in Java?
26. Explain Java coding standards for classes OR Java coding conventions for classes?
27. Explain coding standards for interface in Java ?
28. Explain Java coding standards for methods ?
29. Explain Java coding standards for variables?
30. Explain Java coding standards for constants ?
31. Difference between overriding and overloading in Java ?
32. What is IS-A relationship in Java ?
33. What is HAS-A relationship in Java ?
34. IS-A Vs HAS-A
35. Explain about instanceof operator in Java ?
36. What does null means in Java ?
37. Can we have multiple classes in single file ?
38. What are packages in Java ?
39. What are all access modifiers that are allowed for top class?
40. Can we have more than one package statement in single file ?
41. Can we define package statement after import statement in Java ?
42. What are identifiers in Java ?
43. What are access modifiers in Java ?
44. Access specifiers Vs Access modifiers
45. What access modifiers can be used for class?
46. Explain access modifiers that can be used for methods ?
47. Explain access modifiers that can be used for variables ?
48. What is final access modifier in Java ?
49. Explain about abstract class in Java ?
50. Can we create constructor in abstract class ?
51. What are abstract method in Java ?
52. What is an exception in Java ?
53. State some situations where exception can rise in Java?
54. What is Exception handling in Java ?
55. What is an error in Java ?
56. Advantages of exeption handling in Java ?
57. List out five keywords related to exception Handling?
58. In how many ways we can do exception handling in Java ?
59. Explain try catch keyword in Java ?
60. Can we have try block without without catch block ?
61. Can we have multiple catch block for one try block ?
62. Explain importance of finally block in Java ?
63. Can we have any code in between try and catch block ?
64. Can we have any code in between try and finally block ?
65. Can we catch more than one exception in single catch block ?
66. What are checked Exception ?
67. What are unchecked exceptions in Java ?
68. Checked Exception vs Unchecked Exception in Java ?
69. What is default Exception handling in Java ?
70. Explain throw keyword in Java ?
71. Can we write any code after throw Statement ?
72. Explain importance of throws keyword in Java ?
73. Explain importance of finally over return statement ?
74. Explain situation where finally block will not executed ?
75. Can we used catch statement for checked exception ?
76. What are user defined exceptions ?
77. Can we rethrow same exception from catch handler ?
78. Can we nested try statements in Java ?
79. Explain importance of throwable class and its methods ?
80. Explain when ClassNotFoundException will be raised ?
81. Wxplain when NoClassDefFoundError will be raised ?
82. What is process ?

83. What is thread in Java ?

84. Diff betn process and thread ?

85. What is multitasking ?

86. What are different types of multitasking ?

87. What are benefits of multithreaded programming ?

88. List Java API that supports thread ?

89. Explain about main thread in Java ?

90. In how many ways we can create threads in Java ?

91. Explain creating Threads by implementing runnable class ?

92. Explain creating Threads by extending Thread class ?

93. Which is the best approach for creating Threads ?

94. Explain the importance of thread scheduler in Java ?

95. Explain the life cycle of Thread ?

96. Can we restart dead thread in Java ?

97. Can one thread block another thread in Java ?

98. Can we restart the thread which is already started in Java ?

99. What happens if we don’t override the run method ?

100. Can we overload run() method in Java ?

101. What is lock in java and its purpose?

102.

You might also like