InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What are shallow copy and deep copy in java? |
|
Answer» To copy the object's data, we have several methods like deep copy and shallow copy. Example - class Rectangle{INT length = 5; int BREADTH = 3;}Object for this Rectangle class - Rectangle obj1 = new Rectangle();
Now by doing this what will happen is the new reference is created with the name obj2 and that will point to the same memory location.
Both these objects will point to the memory location as stated below - Now, if we change the values in shallow copy then they affect the other reference as well. Let's see with the help of an example - class Rectangle{int length = 5; int breadth = 3;}public class Main{public static void main(String[] args) { Rectangle obj1 = new Rectangle(); //Shallow Copy Rectangle obj2 = obj1; System.out.println(" Before Changing the value of object 1, the object2 will be - "); System.out.println(" Object2 Length = "+obj2.length+", Object2 Breadth = "+obj2.breadth); //Changing the values for object1. obj1.length = 10; obj1.breadth = 20; System.out.println("\N After Changing the value of object 1, the object2 will be - "); System.out.println(" Object2 Length = "+obj2.length+", Object2 Breadth = "+obj2.breadth); }}Output - Before Changing the value of object 1, the object2 will be - Object2 Length = 5, Object2 Breadth = 3After Changing the value of object 1, the object2 will be - Object2 Length = 10, Object2 Breadth = 20We can see that in the above code, if we change the values of object1, then the object2 values also get changed. It is because of the reference. Now, if we change the code to deep copy, then there will be no effect on object2 if it is of type deep copy. Consider some SNIPPETS to be added in the above code. class Rectangle{ int length = 5; int breadth = 3;}public class Main{public static void main(String[] args) { Rectangle obj1 = new Rectangle(); //Shallow Copy Rectangle obj2 = new Rectangle(); obj2.length = obj1.length; obj2.breadth = obj1.breadth; System.out.println(" Before Changing the value of object 1, the object2 will be - "); System.out.println(" Object2 Length = "+obj2.length+", Object2 Breadth = "+obj2.breadth); //Changing the values for object1. obj1.length = 10; obj1.breadth = 20; System.out.println("\n After Changing the value of object 1, the object2 will be - "); System.out.println(" Object2 Length = "+obj2.length+", Object2 Breadth = "+obj2.breadth); }}The above snippet will not affect the object2 values. It has its separate values. The output will be Before Changing the value of object 1, the object2 will be - Object2 Length = 5, Object2 Breadth = 3After Changing the value of object 1, the object2 will be - Object2 Length = 5, Object2 Breadth = 3Now we see that we need to write the number of codes for this deep copy. So to reduce this, In java, there is a method called clone(). The clone() will do this deep copy internally and return a new object. And to do this we need to write only 1 line of code. That is - Rectangle obj2 = obj1.clone(); |
|
| 2. |
What part of memory - Stack or Heap - is cleaned in garbage collection process? |
|
Answer» HEAP. |
|
| 3. |
What is a ClassLoader? |
Answer»
|
|
| 4. |
What is the main objective of garbage collection? |
|
Answer» The main objective of this process is to free up the memory space occupied by the unnecessary and UNREACHABLE OBJECTS during the Java program EXECUTION by deleting those unreachable objects.
|
|
| 5. |
Difference between static methods, static variables, and static classes in java. |
Answer»
|
|
| 6. |
Can the static methods be overridden? |
Answer»
|
|
| 7. |
Why is the main method static in Java? |
|
Answer» The main method is always STATIC because static MEMBERS are those methods that belong to the classes, not to an individual object. So if the main method will not be static then for every object, It is available. And that is not acceptable by JVM. JVM calls the main method based on the class name itself. Not by creating the object. Because there MUST be only 1 main method in the java program as the execution starts from the main method. So for this reason the main method is static. |
|
| 8. |
Can the static methods be overloaded? |
|
Answer» YES! There can be two or more STATIC methods in a CLASS with the same name but DIFFERING INPUT parameters. |
|
| 9. |
When can you use super keyword? |
Answer»
|
|
| 10. |
Identify the output of the java program and state the reason. |
|
Answer» 1. PUBLIC class InterviewBit2. {3. public STATIC void main(String[] args) {4. FINAL int i;5. i = 20;6. int j = i+20;7. i = j+30;8. System.out.println(i + " " + j);9. }10. } The above code will GENERATE a compile-time error at Line 7 saying - [error: variable i might already have been initialized]. It is because variable ‘i’ is the final variable. And final variables are allowed to be initialized only once, and that was already done on line no 5. |
|
| 11. |
Is it possible that the ‘finally’ block will not be executed? If yes then list the case. |
|
Answer» YES. It is possible that the ‘finally’ BLOCK will not be executed. The CASES are-
|
|
| 12. |
Do final, finally and finalize keywords have the same function? |
|
Answer» All three keywords have their own utility while programming. Final: If any restriction is required for classes, variables, or methods, the final KEYWORD comes in handy. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. The variable value becomes fixed after incorporating the final keyword. Example: final int a=100;a = 0; // errorThe second statement will throw an error. Finally: It is the block present in a program where all the codes WRITTEN inside it get executed irrespective of handling of exceptions. Example: try {int variable = 5;}catch (Exception exception) {System.out.println("Exception occurred");}finally {System.out.println("Execution of finally block");}Finalize: Prior to the GARBAGE collection of an object, the finalize method is called so that the clean-up activity is implemented. Example: public static void main(String[] args) {String example = new String("InterviewBit");example = NULL;System.gc(); // Garbage COLLECTOR called}public void finalize() {// Finalize called} |
|
| 13. |
Explain the use of final keyword in variable, method and class. |
|
Answer» In JAVA, the final keyword is used as defining something as constant /final and represents the non-access MODIFIER.
|
|
| 14. |
A single try block and multiple catch blocks can co-exist in a Java Program. Explain. |
|
Answer» Yes, multiple catch BLOCKS can EXIST but specific approaches should come prior to the general approach because only the first catch block satisfying the catch CONDITION is executed. The given code illustrates the same: public CLASS MultipleCatch {public static void main(String args[]) { try { int n = 1000, x = 0; int ARR[] = new int[n]; for (int i = 0; i <= n; i++) { arr[i] = i / x; } } catch (ArrayIndexOutOfBoundsException exception) { System.out.println("1st block = ArrayIndexOutOfBoundsException"); } catch (ArithmeticException exception) { System.out.println("2nd block = ArithmeticException"); } catch (Exception exception) { System.out.println("3rd block = Exception"); }}}Here, the second catch block will be executed because of division by 0 (i / x). In case x was greater than 0 then the first catch block will execute because for loop runs till i = n and array index are till n-1. |
|
| 15. |
Comment on method overloading and overriding by citing relevant examples. |
|
Answer» In JAVA, method overloading is made POSSIBLE by INTRODUCING different methods in the same class consisting of the same name. Still, all the functions differ in the number or type of parameters. It takes place inside a class and enhances program readability. The only difference in the return type of the method does not promote method overloading. The following example will furnish you with a clear PICTURE of it. class OverloadingHelp { public INT findarea (int l, int b) { int var1; var1 = l * b; return var1; } public int findarea (int l, int b, int h) { int var2; var2 = l * b * h; return var2; }}Both the functions have the same name but differ in the number of arguments. The first method calculates the area of the rectangle, whereas the second method calculates the area of a cuboid. Method overriding is the concept in which two methods having the same method signature are present in two different classes in which an inheritance relationship is present. A particular method implementation (already present in the base class) is possible for the derived class by using method overriding. Both class methods have the name walk and the same parameters, distance, and time. If the derived class method is called, then the base class method walk gets overridden by that of the derived class. |
|
| 16. |
Can the main method be Overloaded? |
|
Answer» Yes, It is possible to overload the main method. We can CREATE as many overloaded main methods we want. However, JVM has a predefined calling method that JVM will only call the main method with the definition of - PUBLIC static void main(string[] ARGS)Consider the below code snippets: class Main { public static void main(String args[]) { System.out.println(" Main Method"); } public static void main(INT[] args){ System.out.println("Overloaded Integer ARRAY Main Method"); } public static void main(char[] args){ System.out.println("Overloaded Character array Main Method"); } public static int main(double[] args){ System.out.println("Overloaded Double array Main Method"); } public static void main(float args){ System.out.println("Overloaded float Main Method"); }} |
|
| 17. |
Define Copy constructor in java. |
|
Answer» Copy CONSTRUCTOR is the constructor USED when we want to initialize the value to the NEW object from the OLD object of the same class. class InterviewBit{ String department; String service; InterviewBit(InterviewBit ib){ this.departments = ib.departments; this.services = ib.services; }}Here we are initializing the new object value from the old object value in the constructor. ALTHOUGH, this can also be achieved with the help of object cloning. |
|
| 18. |
Briefly explain the concept of constructor overloading |
|
Answer» Constructor overloading is the process of creating multiple constructors in the class consisting of the same name with a difference in the constructor parameters. Depending UPON the number of parameters and their corresponding types, distinguishing of the different types of constructors is done by the compiler. class Hospital {INT variable1, VARIABLE2;double VARIABLE3;public Hospital(int DOCTORS, int nurses) { variable1 = doctors; variable2 = nurses;}public Hospital(int doctors) { variable1 = doctors;}public Hospital(double salaries) { variable3 = salaries}}Three constructors are defined here but they differ on the basis of parameter type and their numbers. |
|
| 19. |
How is an infinite loop declared in Java? |
|
Answer» Infinite loops are those loops that run INFINITELY WITHOUT any breaking conditions. Some EXAMPLES of CONSCIOUSLY declaring infinite loop is:
|
|
| 20. |
Can you tell the difference between equals() method and equality operator (==) in Java? |
||||||
Answer»
Note:
|
|||||||
| 21. |
Tell us something about JIT compiler. |
Answer»
|
|
| 22. |
What do you mean by data encapsulation? |
Answer»
|
|
| 23. |
What are the default values assigned to variables and instances in java? |
Answer»
|
|
| 24. |
What do you understand by an instance variable and a local variable? |
|
Answer» Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an OBJECT and remain BOUND to it at any cost. All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected. Example: class Athlete {PUBLIC String athleteName;public double athleteSpeed;public int athleteAge;}LOCAL variables are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is RESTRICTED to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable. Example: public void athlete() {String athleteName;double athleteSpeed;int athleteAge;} |
|
| 25. |
Pointers are used in C/ C++. Why does Java not make use of pointers? |
|
Answer» POINTERS are quite complicated and unsafe to use by beginner programmers. Java focuses on code simplicity, and the USAGE of pointers can make it challenging. POINTER utilization can also CAUSE potential errors. Moreover, security is also compromised if pointers are used because the users can directly ACCESS memory with the help of pointers. Thus, a certain level of abstraction is furnished by not including pointers in Java. Moreover, the usage of pointers can make the procedure of garbage collection quite slow and erroneous. Java makes use of references as these cannot be manipulated, unlike pointers. |
|
| 26. |
How is Java different from C++? |
Answer»
|
|
| 27. |
Can java be said to be the complete object-oriented programming language? |
|
Answer» It is not wrong if we claim that java is the complete object-oriented PROGRAMMING language. Because Everything in Java is under the classes. And we can access that by creating the objects. But also if we say that java is not a COMPLETELY object-oriented programming language because it has the support of primitive data types like int, float, char, BOOLEAN, DOUBLE, etc. Now for the question: Is java a completely object-oriented programming language? We can say that - Java is not a pure object-oriented programming language, because it has direct access to primitive data types. And these primitive data types don't directly belong to the Integer classes. |
|
| 28. |
Difference between Heap and Stack Memory in java. And how java utilizes this. |
|
Answer» Stack memory is the portion of memory that was assigned to EVERY individual program. And it was fixed. On the other hand, HEAP memory is the portion that was not allocated to the java program but it will be AVAILABLE for use by the java program when it is required, mostly during the runtime of the program. Java Utilizes this memory as -
Example- Consider the below java program: class Main { public void printArray(int[] array){ for(int i : array) System.out.println(i); } public static void main(String args[]) { int[] array = NEW int[10]; printArray(array); }}For this java program. The stack and heap memory occupied by java is - Main and PrintArray is the method that will be available in the stack AREA and as well as the variables declared that will also be in the stack area. And the Object (Integer Array of size 10) we have created, will be available in the Heap area because that space will be allocated to the program during runtime. |
|
| 29. |
Why is Java not a pure object oriented language? |
|
Answer» Java SUPPORTS primitive data TYPES - byte, BOOLEAN, char, short, int, float, long, and double and hence it is not a PURE OBJECT oriented language. |
|
| 30. |
Why is Java a platform independent language? |
|
Answer» Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.
Improve your chances of cracking the PYTHON interview Majority Element EasyAsked in Best TIME to Buy and Sell Stocks EasyAsked in Merge TWO sorted lists EasyAsked in View All Practice Questions |
|