Crack Your Java Interview: 50 Expert Questions and Answers (Part II)

PlacementsPrep
0

Crack Your Java Interview: 50 Expert Questions and Answers (Part I)



51. What is the method in java?

Answer It contains the executable body that can be applied to the specific object of the class.
The method includes method name, parameters or arguments and return type and a body of executable code.
Syntax : type methodName(Argument List){
ex: public float add(int a, int b, int c) methods can have multiple arguments. Separate with commas when we have multiple arguments. thrown in the method are instances of their subclass.

52. Explain about Automatic type conversion in java?

Answer Java automatic type conversion is done if the following conditions are met:
1) When two types are compatible
Ex: int, float int can be assigned directly to float variable.
2) Destination type is larger than source type. Ex: int, long.
Int can be assigned directly to long .Automatic type conversion takes place if int is assigned to long because long is larger datatype than int.
Widening Conversion comes under Automatic type conversion.

53. What is the difference between the prefix and postfix forms of the ++ operator?

Answer The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value.

54. In how many ways we can do exception handling in java?

Answer We can handle exceptions in either of the two ways :
1) By specifying a try-catch block where we can catch the exception.
2) Declaring a method with throws clause.

55. What does null mean in java?

Answer When a reference variable doesn’t point to any value it is assigned null.
Example: Employee employee;
In the above example employee object is not instantiate so it is pointed nowhere.

56. Can we define a package statement after the import statement in java?

Answer We can’t define a package statement after the import statement in java. a package statement must be the first statement in the source file. We can have commented before the package statement.

57. Define How many objects are created in the following piece of code? MyClass c1, c2, c3; c1 = new MyClass (); c3 = new MyClass ();

Answer Only 2 objects are created, c1 and c3. The reference c2 is only declared and not initialized.

58. What is JSP?

Answer JSP is a technology that returns dynamic content to the Web client using HTML, XML and JAVAelements. JSP page looks like a HTML page but is a servlet. It contains Presentation logic andbusiness logic of a web application.

59. What is the purpose of apache tomcat?

Answer Apache server is a standalone server that is used to test servlets and create JSP pages. It is free and open source that is integrated in the Apache web server. It is fast, reliable server to configure the applications but it is hard to install. It is a servlet container that includes tools to configure and manage the server to run the applications. It can also be configured by editing XML configuration files.

60. Explain where variables are created in memory?

Answer When we declare variables are created in the stack. So when the variable is out of scope those variables get garbage collected.

61. Can we use catch statement for checked exceptions?

Answer: if there is no chance of raisin an e ception in our code then we can’t declare catch bloc for handling checked exceptions. This raises a compile-time error if we try to handle checked exceptions when there is no possibility of causing an exception.

62. Explain a situation where finally block will not be executed?

Answer Finally, the block will not be executed whenever JVM shutdowns. If we use system.exit(0) in try statement finally block if present will not be executed.

63.What is UNICODE?

Answer Unicode is used for internal representation of characters and strings and it uses 16 bits to represent each other.

64. Explain about the main() method in java?

Answer The main () method is the starting point of execution for all java applications. public static void main(String[] args) {}
String args[] are an array of string objects we need to pass from command line arguments. Every Java application must have at least one main method.

65. Define How destructors are defined in Java?

Answer In Java, there are no destructors defined in the class as there is no need to do so. Java has its owngarbage collection mechanism which does the job automatically by destroying the objects when no longer referenced

66. What will be the output of Round(3.7) and Ceil(3.7)?

Answer Round(3.7) returns 4 and Ceil(3.7) returns 4.

67. What is constructor in java?

Answer A constructor is a special method used to initialize objects in the java.
we use constructors to initialize all variables in the class when an object is created. As and when an object is created it is initialized automatically with the help of constructor in java.
We have two types of constructors
Default Constructor
Parameterized Constructor

68. How can we find the actual size of an object on the heap?

Answer In Java, there is no way to find out the actual size of an object on the heap.

69. Can a variable be local and static at the same time?

Answer No a variable can’t be static as well as local at the same time. Defining a local variable as static gives compilation error.

70. Can we have static methods in an Interface?

Answer Static methods can’t be overridden in any class while any methods in an interface are by default abstract and are supposed to be implemented in the classes being implementing the interface. So it makes no sense to have static methods in an interface in Java.

71. In how many ways we can do synchronization in java?

Answer There are two ways to do synchronization in java:
1) Synchronized methods
2) Synchronized blocks
To do synchronization we use the synchronized keyword.

72. When do we use synchronized blocks and advantages of using synchronized blocks?

Answer If very few lines of code require synchronization then it is recommended to use synchronized blocks. The main advantage of synchronized blocks over synchronized methods is it reduces the waiting time of threads and improves performance of the system.

73. What is the difference between access specifiers and access modifiers in java?

Answer In C++ we have access specifiers as public, private, protected and default and access modifiers as static, final. But there is no such division of access specifiers and access modifiers in java. In Java, we have access to modifiers and nonaccess modifiers. Access Modifiers: public, private, protected, default Non Access Modifiers: abstract, final, strip.

74. Define How objects are stored in Java?

Answer In java, each object when created gets a memory space from a heap. When an object is destroyed by a garbage collector, the space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects.

75. What access modifiers can be used for class?

Answer We can use only two access modifiers for class public and default.
public: A class with a public modifier can be visible
1) In the same class
2) In the same package subclass
3) In the same package nonsubclass
4) In the different package subclass
5) In the different package nonsubclass.
default: A class with default modifier can be accessed
1) In the same class
2) In the same package subclass
3) In the same package nonsubclass
4) In the different package subclass
5) In the different package nonsubclass. ( )

76. Explain about abstract classes in java?

Answer Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such a case, we declare a class as abstract. To make a class abstract we use keyword abstract. Any class that contains one or more abstract methods is declared as abstract. If we don’t declare a class as abstract which contains abstract
methods we get a compile-time error. We get the following error. “The type must be an abstract class to define abstract methods.” Signature; abstract class.
For example, if we take a vehicle class we cannot provide implementation to it because there may be two-wheelers, four-wheelers, etc. At that moment we make vehicle class abstract. All the common features of vehicles are declared as abstract methods in vehicle class. Any class which extends the vehicle will provide its method implementation. It’s the responsibility of subclass to provide the implementation.
The important features of abstract classes are:
1) Abstract classes cannot be instantiated.
2) An abstract class contains abstract methods, concrete methods or both.
3) Any class which extends abstract class must override all methods of an abstract class.
4) An abstract class can contain either 0 or more abstract methods.
Though we cannot instantiate abstract classes we can create object references. Through superclass references, we can point to subclass.

77. Can we create a constructor in abstract class?

Answer We can create a constructor in the abstract class, it doesn’t give any compilation error. But when we cannot instantiate class there is no use in creating a constructor for abstract class.

78. String and StringBuffer both represent String objects. Can we compare String andStringBuffer in Java?

Answer Although String and StringBuffer both represent String objects, we can’t compare them with each other and if we try to compare them, we get an error.

79. In how many ways we can create threads in java?

Answer We can create threads in java by any of the two ways :
1) By extending Thread class
2) By implementing the Runnable interface.

80. Explain creating threads by implementing Runnable class?

Answer This is the first and foremost way to create threads. By implementing the runnable interface and implementing the run() method we can create a new thread. Method signature : public void run() Run is the starting point for execution for another thread within our program. Example : public class MyClass implements Runnable { @Override public void run()

81. When do we use synchronized methods in java?

Answer If multiple threads try to access a method where the method can manipulate the state of the object, in such a scenario we can declare a method as synchronized.

82. Can we cast any other type to Boolean Type with type casting?

Answer No, we can neither cast any other primitive type to Boolean data type nor can cast Boolean data typeto any other primitive data type.

83. What are synchronized methods and synchronized statements?

Answer Synchronized methods are methods that are used to control access to an object. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

84. Explain the importance of finally block in java?

Answer Finally block is used for cleaning up of resources such as closing connections, sockets, etc. if try block executes with no exceptions then finally is called after try block without executing catch block. If there is an exception thrown in try block finally block executes immediately after the catch block. If an exception is thrown, finally block will be executed even if the no catch block handles the exception.

85. Can we catch more than one exception in a single catch block?

Answer From Java 7, we can catch more than one exception with a single catch block. This type of handling reduces the code duplication.
Note: When we catch more than one exception in a single catch block, catch parameter is implicitly final.
We cannot assign any value to catch parameter.
Ex : catch(ArrayIndexOutOfBoundsException || ArithmeticException e)
In the above example, e is final we cannot assign any value or modify e in the catch statement.

86. What are abstract methods in java?

Answer An abstract method is a method which doesn’t have anybody. An abstract method is declared with keyword abstract and semicolon in place of the method body. Signature : public abstract void (); Ex : public abstract void get details(); It is the responsibility of subclass to provide implementation to an abstract method defined in the abstract class.

87. State some situations where exceptions may arise in java?

Answer 1) Accessing an element that does not exist in the array.
2) Invalid conversion of number to string and string to a number.
(NumberFormatException)
3) The invalid casting of class
(Class cast Exception)
4) Trying to create an object for interface or abstract class
(Instantiation Exception)

88. What is an exception in java?

Answer In java, an exception is an object. Exceptions are created when abnormal situations arise in our program. Exceptions can be created by JVM or by our application code. All Exception classes are defined in java.lang. In other words, we can say Exception as a run time error.

89. What is an error in Java?

Answer Error is the subclass of Throwable class in java. When errors are caused by our program we call that as Exception, but some times exceptions are caused due to some environmental issues such as running out of memory. In such cases, we can’t handle the exceptions. Exceptions which cannot be recovered are called as errors in java. Ex: Out of memory issues.

90. What are the advantages of Exception handling in java?

Answer 1) Separating normal code from exception handling code to avoid abnormal termination of the program.
2) Categorizing into different types of Exceptions so that rather than handling all exceptions with Exception root class we can handle with specific exceptions. It is recommended to handle exceptions with specific Exception instead of handling with Exception root class.
3) Call stack mechanism: If a method throws an exception and it is not handled immediately, then that exception is propagated or thrown to the caller of that method.
This propagation continues till it finds an appropriate exception handler, if it finds handler it would be handled otherwise program terminates Abruptly.

91. What’s the difference between constructors and other methods?

Answer Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

92. Is there any limitation of using Inheritance?

Answer Yes, since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation.

93. Where and how can you use a private constructor?

Answer Private constructor is used if you do not want other classes to instantiate the object and to prevent subclassing.

94. What is type casting?

Answer Type casting means treating a variable of one type as though it is another type.

95. What is the difference between the >> and >>> operators?

Answer The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.

96. What is the difference between inner class and nested class?

Answer When a class is defined within a scope of another class, then it becomes inner class. If the access modifier of the inner class is static, then it becomes nested class.

97. Can you call one constructor from another if a class has multiple constructors?

Answer Yes, use this() syntax.

98. Why do we need wrapper classes?

Answer We can pass them around as method parameters where a method expects an object. It also provides utility methods.

99. Does Java allow Default Arguments?

Answer No, Java does not allow Default Arguments.

100. Which number is denoted by leading zero in java?

Answer Octal Numbers are denoted by leading zero in java, example: 06

Post a Comment

0Comments
Post a Comment (0)