Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
Top 100 Java Interview Questions with Answers
Q50. What's the default access specifier for variables and methods of a class?
Ans: Default access specifier for variables and method is package protected i.e variables and class is available to any other class but in the same package,not outside the package.
Q51. Give an example of use of Pointers in Java class.
Ans: There are no pointers in Java. So we can't use concept of pointers in Java.
Q52. How can we restrict inheritance for a class so that no class can be inherited from it?
Ans: If we want a class not to be extended further by any class, we can use the keyword Final with the class name.
In the following example, Stone class is Final and can't be extend
public Final Class Stone { // Class methods and Variables }
Q53. What's the access scope of Protected Access specifier?
Ans: When a method or a variable is declared with Protected access specifier, it becomes accessible in the same class,any other class of the same package as well as a sub-class.
Modifier |
Class |
Package |
Subclass |
World |
public |
Y |
Y |
Y |
Y |
protected |
Y |
Y |
Y |
N |
no modifier |
Y |
Y |
N |
N |
private |
Y |
N |
N |
N |
Q54. What's difference between Stack and Queue?
Ans: Stack and Queue both are used as placeholder for a collection of data. The primary difference between a stack and a queue is that stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle.
Q55. In java, how we can disallow serialization of variables?
Ans: If we want certain variables of a class not to be serialized, we can use the keyword transient while declaring them. For example, the variable trans_var below is a transient variable and can't be serialized:
public class transientExample { private transient trans_var; // rest of the code }
Q56. How can we use primitive data types as objects?
Ans: Primitive data types like int can be handled as objects by the use of their respective wrapper classes. For example, Integer is a wrapper class for primitive data type int. We can apply different methods to a wrapper class, just like any other object.
Q57. Which types of exceptions are caught at compile time?
Ans: Checked exceptions can be caught at the time of program compilation. Checked exceptions must be handled by using try catch block in the code in order to successfully compile the code.
Q58. Describe different states of a thread.
Ans: A thread in Java can be in either of the following states:
- Ready: When a thread is created, it's in Ready state.
- Running: A thread currently being executed is in running state.
- Waiting: A thread waiting for another thread to free certain resources is in waiting state.
- Dead: A thread which has gone dead after execution is in dead state.
Q59. Can we use a default constructor of a class even if an explicit constructor is defined?
Ans: Java provides a default no argument constructor if no explicit constructor is defined in a Java class. But if an explicit constructor has been defined, default constructor can't be invoked and developer can use only those constructors which are defined in the class.
Q60. Can we override a method by using same method name and arguments but different return types?
Ans: The basic condition of method overriding is that method name, arguments as well as return type must be exactly same as is that of the method being overridden. Hence using a different return type doesn't override a method.
Q61.What will be the output of following piece of code?
public class operatorExample { public static void main(String args[]) { int x = 4; system.out.println(x++); } }
Ans: In this case postfix ++ operator is used which first returns the value and then increments. Hence it's output will be 4.
Q61. A person says that he compiled a java class successfully without even having a main method in it? Is it possible?
Ans: main method is an entry point of Java class and is required for execution of the program however; a class gets compiled successfully even if it doesn't have a main method. It can't be run though.
Q62. Can we call a non-static method from inside a static method?
Ans: Non-Static methods are owned by objects of a class and have object level scope and in order to call the non-Static methods from a static block (like from a static main method), an object of the class needs to be created first. Then using object reference, these methods can be invoked.
Q63. What are the two environment variables that must be set in order to run any Java programs?
Ans: Java programs can be executed in a machine only once following two environment variables have been properly set:
- PATH variable
- CLASSPATH variable
Q64. Can variables be used in Java without initialization?
Ans: In Java, if a variable is used in a code without prior initialization by a valid value, program doesn't compile and gives an error as no default value is assigned to variables in Java.
Q65. Can a class in Java be inherited from more than one class?
Ans: In Java, a class can be derived from only one class and not from multiple classes. Multiple inheritances is not supported by Java.
Q66. Can a constructor have different name than a Class name in Java?
Ans: Constructor in Java must have same name as the class name and if the name is different, it doesn't act as a constructor and compiler thinks of it as a normal method.
Q67. What will be the output of Round(3.7) and Ceil(3.7)?
Ans: Round(3.7) returns 4 and Ceil(3.7) returns 4.
Q68: Can we use goto in Java to go to a particular line?
Ans: In Java, there is not goto keyword and java doesn't support this feature of going to a particular labeled line.
Q69. Can a dead thread be started again?
Ans: In java, a thread which is in dead state can't be started again. There is no way to restart a dead thread.
Q70. Is the following class declaration correct?
Ans:
public abstract final class testClass { // Class methods and variables }
Ans: The above class declaration is incorrect as an abstract class can't be declared as Final.
Q71. Is JDK required on each machine to run a Java program?
Ans: JDK is development Kit of Java and is required for development only and to run a Java program on a machine, JDK isn't required. Only JRE is required.
Q72. What's the difference between comparison done by equals method and == operator?
Ans: In Java, equals() method is used to compare the contents of two string objects and returns true if the two have same value while == operator compares the references of two string objects.
In the following example, equals() returns true as the two string objects have same values. However == operator returns false as both string objects are referencing to different objects:
public class equalsTest { public static void main(String args[]) { String str1 = new String("Hello World"); String str2 = new String("Hello World"); if (str1.equals(str2)) { // this condition is true System.out.println("str1 and str2 are equal in terms of values"); } if (str1 == str2) { //This condition is true System.out.println("Both strings are referencing same object"); } else { // This condition is NOT true System.out.println("Both strings are referencing different objects"); } } }
Q73. Is it possible to define a method in Java class but provide it's implementation in the code of another language like C?
Ans: Yes, we can do this by use of native methods. In case of native method based development, we define public static methods in our Java class without its implementation and then implementation is done in another language like C separately.
Q74. How are destructors defined in Java?
Ans: In Java, there are no destructors defined in the class as there is no need to do so. Java has its own garbage collection mechanism which does the job automatically by destroying the objects when no longer referenced.