Thursday, 18 January 2024

JavaFusion

1. What is method overriding?

  1. When the subclass has the same method declared in the parent class.
  2.  The Subclass method has a specific implementation rather than the parent class method
  3. The method signature is the same in both classes (method name and signature)
  4. The method body is different in both classes
  5. Method Overriding is called runtime polymorphism
  6. Sub class use the superclass method and changes the method body while keeping the method signature the same
  7. method signature (name, access modifier, parameter, return type)
  8. ‌Creating and implementing a method with the same name in a subclass as the parent class is called method overriding.
  9. The methods’ name should be the same
  10. Arguments passed should be the same(number, type, sequence)
  11. Methods should belong to different classes
//Super class
public int getNumbers(int a){
return a+6;
}
//Extended sub class

@Override
public int getNumbers(int a){
return a+1000;
}

2. Can we override the static method?

No. 

3. Can the main method be overridden?

No. since it is static.

4. What is the method of overloading?

  1. If a class has multiple methods with the same name but different in the parameter list it is called method overloading.
  2. Method overloading can be achieved by changing the number of arguments, Changing data type
  3. Method overloading cannot be achieved by changing the return type only.
  4. ‌Both methods are involved when the code is based on polymorphism.
  5. The method overloading is static polymorphism and the method overriding is dynamic polymorphism
  6. ‌Method overloading is a part of polymorphism where a class has multiple methods with the same name but different arguments. 
  7. Methods should have the same name.
  8. Arguments passed should be different (number, type, sequence)
  9. Methods should belong to the same class
    public static float addNumbers(float a , float b){
return a+b;
}

public static int addNumbers(int a , int b){
return a+b;
}

5. What happens if you try to change only the return type while having the same name and parameter list in methods in the same class?

It shows a compilation error.

6. Can the main method overload?

  1. Yes. 
  2. By having different argument sets.
  3. ‌A main method can be written several times in a code. 
  4. The catch is, you have to call the overloading main() method from the original default main() method of the program.

7. Can we overload the static method?

Yes. 

8. What is an interface?

  1. Interface is a blueprint for a Java class
  2. It has static constants and abstract methods
  3. It cannot be instantiated 
  4. We use the Interface keyword to declare an interface
  5. All the fields are public static and the final
  6. Even though we do not use abstract keywords all the methods are abstract 
  7. public methods need to have a method body
  8. Interfaces are implemented not extended
  9. any class implement the abstract methods in the interface it should implement all the methods or else declare itself as an abstract class
  10. ‌An interface is a collection of abstract methods in Java.
  11.  A class can implement the interface by using the ‘implement’ keyword, which will result in inheriting all the abstract methods present in the interface.
  12.  The interface provides complete(100%) data hiding
//Interface
public interface Vehicle {
String NAME = "Usher";
static void getName(){
System.out.println(NAME);
}
void getAge();
void getHome();
}

//Sub class implements Interface
public class Car implements Vehicle{
@Override
public void getAge() {
System.out.println("4");}

@Override
public void getHome() {
System.out.println("See");}
}

9. What is multiple inheritance?

  1. A class implements multiple interfaces
  2. An interface extends multiple interfaces

10. Is that possible to achieve multiple inheritance through Java class?

No.

11. Is that possible to overload the final methods?

Yes

12. Is that possible to override the final methods?

No.

13. Is that possible to extend the final class?

No

14. What is abstraction?

  1. A class which is declared with an Abstract keyword is known as an abstract class. 
  2. It can have abstract and non-abstract methods.
  3. Abstraction means hiding the method implementation and showing only the functionality to the user.
  4. An abstract class cannot be instantiated
  5. It can have constructors and static methods
  6. Abstract keyword needs to be used in abstract methods
  7. It can have private methods
  8. Abstract methods don’t have method body
  9. It can have a main method
  10. If there is only one abstract method that class should be abstract
  11. There can be abstract classes without abstract methods
  12. If another class extends the abstract class it should implement all the abstract methods or call itself an abstract
  13. We use extends keyword in the subclass which inherit methods of abstract class
  14. ‌Abstraction is a method to hide data in Java.
  15.  The purpose is to create an abstract method that is used to hide the implementation or the working of the program and show the only necessary information.
  16. Its real-life example would be a car.
  17.  Only the steering wheel and indicators are visible but the internal working is hidden
  18. This is because of the abstract methods which are empty/without a body.
  19.  They act as a base for subclasses.
  20.  They have to be extended and built upon.
  21. ‌Helps in creating a secured program as only required data is visible.
  22. Reduces the complexity of the program.
  23. Easier implementation of the software.
  24. Groups the related classes as siblings.
public abstract class Benz {

private String name;
public static void speed(){
System.out.println("345");
}

abstract void color();
abstract void tyres();
}

15. What is polymorphism?

  1. Polymorphism has many forms.
  2. It has runtime polymorphism
  3. Compile time polymorphism
  4. ‌Polymorphism means taking many forms or shapes. 
  5. In Java, polymorphism occurs when multiple child classes inherit the methods of a parent class.
  6.  For example, a super class named vehicles with method wheels(); will have sub-classes such as Car, Scooter, etc. 
  7. Each of these will have its own implementation of the method wheels.
  8. Helps in code reusability as the classes once written can be implemented again and again.
  9. Single variables can store multiple data types.
  10. Reduces coupling of classes, unlike inheritance.

https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/

16. What is compile time polymorphism?

If we overload a static method it is compile time polymorphism

17. What is runtime polymorphism?

Runtime polymorphism is achieved by method overriding

Car a = new Mazarati();
Car b = new Porsche();
a.speed()
b.speed()

In the above example, it gives various values as output.

18. Can runtime polymorphism be achieved by data members?

No. Only methods are overridden. Not the data members.

19. What does it mean by access modifiers?

It specifies the accessibility of classes, methods, constructors, and variables

20. What are the access modifiers?

  1. private: Can access only within the class
  2. public: Can access from outside class
  3. default: Can be accessed from within the same package
  4. protected: Can access within the same package and outside child class only

21. What are the non-access modifiers?

  1. final
  2. static
  3. abstract

22. What does final refer to?

  1. final method cannot be overridden
  2. final class cannot be extended
  3. the final variable value cannot be changed once assigned
  4. ‌The final keyword added to any entity will declare it permanent. 
  5. No changes can be made to the value once it is declared final. 
  6. They can neither be overridden nor inherited. 
  7. The final keyword is a non-access modifier.
  8. ‌The main() method can be declared final.
  9.  Most methods are declared final so that they do not get overridden.

23. What does static stand for?

  1. static means it belongs to the class itself. Not a specific instance
  2. static methods can be called directly by method name
  3. static method uses static variables not instance variables
  4. ‌The static keyword associated with any method, variable, or nested class means it belongs to the class and not any instance of the class
  5. ‌A static method is a method for which there is no requirement to make an object.
  6.  They can be invoked without creating an object. 
  7. Therefore they belong to the class and not to the instances of the class.

24. Explain HashMap features

Key-Value pair

  1. Each element in a HashMap is a key-value pair.
  2. The key is used to retrieve the associated value

Unordered

  1. HashMap and does not guarantee any specific order of the elements (Unordered)
  2. If you need to order, you may consider using, which maintains the order of elements based on the insertion order.
  3. ‌Hashmap is an implementation of a map interface.
  4.  The data is stored in pairs in the form of a key, value.
  5.  The key acts as an index to another object(value). 
  6. The objects stored can be retrieved in the shortest time (O(1)) if the key is known

Allow Null values

  1. HashMap allows one null key and multiple null values.
  2. HashMap provides constant-time performance for basic operations such as get and put (assuming a good hash function).

Not Synchronized

  1. HashMap is not synchronized, meaning it is not thread-safe.
  2. HashMap automatically resizes itself when the number of elements exceeds a certain threshold.

25. Explain HashTable features

Synchronized

  1. Hashtable is synchronized, which means it is thread-safe.
  2.  This ensures that multiple threads can access and modify it concurrently without causing data inconsistencies.
  3.  However, this synchronization can introduce some performance overhead.

No Null values

  1. Both keys and values in a Hashtable cannot be null
  2. Attempting to insert a null key or value will result in a NullPointerException.

Key-Value pairs

  1. The key and value pairs are stored in the Hashtable
  2. You can use the put(key, value) method to insert a key-value pair, and the get(key) method to retrieve the value associated with a given key.

26. What does super() do?

  1. It will call the constructor of the superclass
  2. ‌The super keyword points to the instance of the super class or the parent class. 
  3. It is majorly used to eliminate the confusion between the super and subclasses with same-name methods.

27. In the subclass which the constructor calls first

  1. Super class constructor call before subclass constructor
  2. If we add a superclass constructor call at a second it will show a compile-time error
  3. Calling the superclass constructor should be the first statement
  4. Super class doesn’t have a default constructor subclass explicitly calls super class other constructor

28. Does subclass inherit superclass constructor?

  1. No. 
  2. Constructors are not technically members of the class
  3. So they are no inherited

29. What is inherited from the superclass?

  1. public members 
  2. protected members

30. Does private members are inherited?

No.

31. Are final methods inherited?

Yes. But cannot override

32. Can we change the access modifier during method overriding?

  1. Yes
  2. It can be the same or more stronger one
  3. Cannot be changed into a weaker one
  4. Protected -> public (Possible)
  5. Protected -> private (Not possible)

33. Type VS Instance

  1. An object can have a superclass as a type and a subclass as an instance
Animal pet = new Dog();

//Super class Animal
//Sublcass Dog

34. Polymorphic objects can access the methods of their type?

Yes. To access the method of their instance casting is required

35. If the method is overridden which method consider during runtime?

If the method is overridden by the subclass polymorphic object will execute the override method in runtime

36. What is the access modifier of the methods in the interface?

public

37. Is that possible to have private methods in the interface?

No.

38. Information about Collections?

  1. ‌Collections are data structures
  2. ‌Objects within the collection are known as elements
  3. ‌The collection is an interface
  4. ‌The map is not exactly a collection
  5. ‌But the map can use operation in collection

39. Code for creating HashSet?

Set<String> setOfNames = new HashSet<>();

40. Print whatever the item inside a collection

 list.forEach(System.out::println);

//output
{1=malay, 2=subway}
{1=japan, 2=singa}

41. Code for creating HashMap?

Map<String, Integer> items = new HashMap<>();

42. Methods of Set and List Interface

  1. add:
  2. clear
  3. contains
  4. isEmpty
  5. iterator
  6. remove
  7. size
  8. get
  9. addAll

43. Write code to catch exceptions

public static void createFile(){

File file = new File("src/test/text.docx);

try{

file.createNewFile();

}

catch(IOException e){

e.printStackTrace();
}
}

44. Where do we use the Throws keyword?

End of the method signature with the exception name

public static void createFile() throws Exception

45. How to throw new exceptions inside the method body?

public static void findError(){

if(a==1){
throw new ArithmeticException();
}

46. Usage of multiple exceptions in a method

 throws IOException, EnumConstantNotPresentException

//try catch block
catch (IOException | ArithmeticException e)

47. What is a Set?

  1. Unordered
  2. Unique values

48. What is a list?

  1. Ordered
  2. Elements can be accessed by position

49. What is Queue?

  1. Ordered elements for processing
  2. First in First out method
  3. If remove the item from the queue it will remove the first item in the queue

50. What is a Map?

Unordered unique key-value pairs

51. What is Java?

  1. It is simple and easy to learn with a simple syntax.
  2. It is an object-based language. This means it is easier to implement, and can easily be extended.
  3. The Java platform has null interactions with the OS when a program is run. This makes it very secure as the program is developed without any virus and is tamper-free.
  4. It is architectural-neutral; this means a compiled code can be run on various processors in the presence of a Java runtime system.
  5. Programs written in Java can perform various tasks simultaneously. This multi-threaded feature is very helpful in creating interactive applications.

52. What is JDK?

  1. ‌JDK means Java Development Kit
  2. ‌The JDK provides an environment to build, run, and execute programs
  3. ‌All the development tools required to develop a program and the JRE come inside the JDK

53. What is Javac?

  1. ‌The compiler used in Java is called Javac
  2. ‌The compiler itself comes inside the JDK
  3. ‌The purpose of it is to convert the Java program code line by line into bytecodes for the JVM to understand

54. What is JRE?

  1. ‌JRE is Java Runtime Environment.
  2. ‌The JRE is the platform that provides the environment for the Java programs to run
  3. ‌It is the top layer that is used by us, the end-users
  4. ‌It consists of the JVM + library classes + supporting tools and is part of the JDK
  5. ‌The programs are run on JRE as it loads the classes, verifies the access to the memory, and retrieves system resources

55. What is JVM?

  1. ‌The JVM (Java Virtual Machine) is the interpreter and is the core of both JDK and JRE
  2. ‌The JVM verifies and executes the program line by line once the program is converted into Bytecode
  3. ‌It consists of a class loader. Memory area, execution engine, JNI, and Native method libraries

56. Why Java is platform independent?

  1. ‌as the code compiled in Java can run on all OS irrespective of on which it was created
  2. ‌The code created in Java is compiled into an intermediate language called the Bytecode, which is understood by JVM and converted into native machine-specific language based on which OS it is being run on

57. What does it mean by JIT?

  1. Just In Time
  2. ‌It converts the Bytecode into the native machine language
  3. Optimize and run the code in the least possible time. i.e. boosts the performance by creating less load on the computer’s compiler.Thus the inclusion of JIT enhances the performance of the application

58. What is OOP?

  1. Object oriented programming
  2. Concept is instantiating a class by creating an object
  3. Object contains data and methods

59. What are the four pillars of OOPs?

  1. Abstraction
  2. Polymorphysm
  3. Inheritance
  4. Encapsulation

60. ‌What is an Object in Java?

‌ 1. An Object is an instance of the class, created to store data and methods.

2. An object has its identity, state/attribute, and behavior

61. What is class ?

  1. ‌A class is a collection of objects, that acts as a blueprint or template. 
  2. It is not a real-world identity and therefore, also does not occupy space.
  3. A class named animal can contain objects named dog, cat, horse, etc. with methods like eat, run, sleep, etc. 

62. ‌Can we have an empty filename in Java?

  1. Yes
  2. ‌Java file can be kept empty. 
  3. Just save it by ‘.java’ and compile by entering the command Javac ‘.java

63. ‌What are constructors in Java?

‌1. A constructor is a method that is invoked when a class is instantiated and memory is allocated to the instance. 

2. When the new keyword is used to create an object, the constructor is called. 

3. The constructor should have the same name as the class being instantiated

64. ‌What are the different types of constructors?

Default constructors : These constructors are created by the compiler itself when no constructor is defined in the program. These constructors have no parameters, so the instances are initialized with default values.
Parameterized constructor -These constructors are used to pass certain values/arguments to the objects created.

65. ‌Can a constructor be overloaded?

  1. ‌Yes
  2. a constructor can be overloaded. 
  3. This works the similar way the overloading methods work, the number of arguments or the data type of the parameters are changed to overload.

66. ‌What is the difference between constructors and methods?

  1. ‌Constructor is invoked when a class is instantiated, to initialize the state of the instance.
  2. Constructors are type of special method and don’t have a return type.
  3. The compiler provides a default constructor if there is none mentioned in the program.
  4. The name of the constructor must be of the same name as the class instantiated.
  5.  ‌The behavior of the object is reflected by the method.
  6. A method has a return type.
  7. No default method is created by the compiler if none is mentioned in the program
  8. A method can or cannot be the same as the name of the class

67. ‌What is a wrapper class in Java?

  1. To include primitive data types like int, boolean, char, etc. 
  2. In the family of objects(as Java is object-oriented) wrapper class is used. 
  3. For every primitive data type, we have a wrapper class.
  4. For example — Integer for int, Float — float, Character for char, etc.

68. What is package?

  1. ‌A package in Java is a type of file directory or folder that contains all the related classes, sub-classes, and interfaces.
  2.  There are 2 types of packages in Java, built-in and user-defined.
  3.  One such built-in package is ‘java.lang’ imports the lang package which has all the fundamental classes required to create a basic program.

69.‌What are global, local, and instance variables??

  1. ‌A global variable is declared at the start of the program, within the class, and is accessible by all the parts of the program.
  2. A local variable is created/declared inside a method, and cannot be accessed outside that.
  3. An instance variable is associated with an object and is declared within a class but outside the method.
  4.  All the objects of that class can create their own copy of that variable with their own value associated with it

70. ‌What is an instance method?

  1. ‌ An instance method requires an object of the class to call or invoke it.
  2.  Therefore, they belong to the object of the class and not the class itself.

71.‌ What is a string in Java?

  1. ‌A string is a sequence of characters and comes under non-primitive data types. 
  2. A string is a class in Java and extends the object class of Java. 
  3. It is used to manipulate strings in the program, through various methods included in it. 
  4. Syntax-String s= “name”; or String s = new String();

72. ‌What is a string constant/literal pool?

  1. ‌string constant pool is a special memory location that stores string objects
  2. it is a place in the heap memory which stores string literal values.

73. ‌What are the different classes to create strings? Differentiate between them

String — Strings created through string class are immutable i.e. they cannot be altered once created.
String Builder — Strings created through this are mutable(changeable) and preferable when used from a single thread.
String Buffer — Strings created through this are mutable, i.e. the values can be changed, and also it is thread-safe. This means it can be accessed by multiple threads and yet will remain safe.

74. ‌Why are strings immutable in Java?

  1. ‌Immutable means something that cannot be modified.
  2.  So, when we say that String is immutable in Java.
  3.  It means String objects cannot be modified once created. 
  4. In case we try to change the value of the String then a new object will get created.
  5. Now, let’s see the benefit of making String immutable.
  6.  Strings are immutable in Java because it uses the concept of string literal or string constant pool.
  7. If a new String object is created with the value — “ArtOfTesting” then the same will be placed in a Java heap memory called String pool. 
  8. Now, whenever new String literals are created with the value — “ArtOfTesting” then instead of creating multiple objects, each object will point to the same value (i.e. “ArtOfTesting”) in the String pool.
  9. This saves a lot of memory. 
  10. If the String value in Java is allowed to be modified then the concept of the String pool will not hold. 
  11. This is because, changing the value of one object can update the value of other objects also, which may not be desired

75. ‌Why is string final in Java?

‌ A String is made final to maintain its property of immutability thus saving it from any changes.

76.‌Difference between == and .equals() method in Java

  1. ‌== operator is used to compare two strings when string variables are pointing to the same memory location(address reference). 
  2. The .equals() is used to check the object values(content comparison).

77. ‌What is inheritance?

‌ 1. Inheritance is one of the concepts of OOPS where one class acquires/inherits the properties of the parent class.

2. This ensures the reusability of methods and fields created in the parent class into the new classes created in the program

3. The parent and the child classes become tightly coupled.
4. Needs proper and careful implementation due to multiple closely associated classes.
5. Increases execution time due to jumping between the parent and child classes.
Modifications have to be done to both parent and the child’s class.

78.‌What is encapsulation?

  1. ‌ Encapsulation is a way in Java to wrap variables and methods acting on those variables in a single block.
  2.  In this, variables declared in the current class are inaccessible by the other classes and can only be accessed by the (getter and setter) methods of the current class.
  3.  Consider a capsule, the medicine inside it is hidden from the patient and appears to be plastic from the outside

79.‌What is the main difference between encapsulation and abstraction?

  1. ‌ The major difference between encapsulation and abstraction is that abstraction is used to hide unwanted or unnecessary information. 
  2. This can be done by using abstract classes and interfaces.
  3. The encapsulation is used to hide data as a single block with all the variables and methods inside it.
  4.  The data can be accessed through getter and setter methods.

80.‌What are the different types of inheritances in Java?

Single-level inheritance : Just one child class inheriting the properties of a parent class.
Multi-level inheritance — Multiple child classes inheriting properties of a multiple parents class. i.e. Class A extends Class B and Class C extends Class B.
Hierarchical inheritance — When many child classes have a single parent class.
Hybrid inheritance — As the name suggests it is a mix of two or more types of inheritance.

81.‌What are the base class and derived class?

  1. ‌This is a concept of inheritance in OOPs. 
  2. a base class is also called the parent class, from which the other classes derive.
  3.  A derived class or the child class inherits properties or functions from the base class

82. ‌Why does Java not support multiple inheritances?

  1. ‌ Implementing multiple inheritances creates complexities like the diamond problem.
  2.  The diamond problem occurs when ambiguity occurs between two classes which will be overridden. 
  3. For example, class A has subclasses class B and class C. B&C are superclasses to class D. 
  4. A method in A gets overridden by both B&C. 
  5. Then from which class will D inherit this method? Such complexity is called a diamond problem.

83. ‌Can an abstract method exist without an abstract class?

  1. ‌No. 
  2. If an abstract method is declared, an abstract class must exist. 
  3. However, the vice versa is not true

84. ‌What are some advantages of OOPS?

‌1. Code maintenance — It helps in maintaining and modifying the code in the simplest way.
2. It helps in creating a clean structured program or code.
3. Reusability — You can inherit the same methods in different classes by using inheritance.
4. It is helpful for security purposes as it helps in data hiding.

85. ‌ What is this keyword?

‌1. The ‘this’ keyword is used to point to the current instance of a method or a constructor. 

2. The keyword is mostly used to eliminate the confusion of multiple methods of the same name existing in the program.

3. ‌Can be used as a reference variable to point to the instance of the current class.
4. Can be used to invoke a constructor of the current class.
5. It can be used to invoke the current class method.
6. Can be passed as an argument in any method call.

86. ‌What is the actual superclass in Java?

‌ The object class is the superclass of all the classes existing in Java.

87. ‌Can we declare an interface as final?

  1. ‌The purpose of the interface is to provide methods that can be implemented. 
  2. The final keyword abstains a method from being inherited or implemented. 
  3. Therefore, an interface can never be final.

88. ‌What is static binding and dynamic binding?

  1. ‌Identifying classes and objects during compile time is called static binding.
  2.  Methods like private, static, etc. are identified earlier because they cannot be modified or overridden.
  3. Whereas, late binding also known as dynamic binding is when the type of the object is identified during run time.
  4.  Method overriding is a perfect example of dynamic binding

89.‌ What is exception handling?

  1. Exception handling in Java helps in recognizing errors in the program.
  2.  This ensures that the flow of the program is maintained even after an error is detected.

90. ‌What are the types of exceptions in Java?

  1. ‌There are two types of exceptions checked and unchecked.
  2. Checked exceptions are handled during the compile time.
  3.  These include SQL exceptions, IO exceptions, etc. 
  4. The unchecked exceptions are those which cannot be checked or handled during compile-time and therefore throw an error during run time. 
  5. These include ArrayIndexOutOfBoundsException, NullPointerException

91.‌Explain Java exception classes hierarchy

  1. ‌The Java exception hierarchy starts from the throwable class which is a superclass. 
  2. It is further divided into ‘exceptions’ and ‘errors’ classes. 
  3. Errors are detected by the JVM. 
  4. Some common errors are — OutOfMemoryError, unknown error, etc.
  5. An Error “indicates serious problems that a reasonable application should not try to catch.
  6. Whereas exceptions are further bifurcated into checked and unchecked exceptions.

92.‌What is the ‘finally’ block?

  1. ‌The ‘finally’ keyword is used with the statement of the program that has to run even if an exception is thrown or not, i.e. important codes.
  2.  It is used with the try and catch blocks. 
  3. There is always one final block in the end.

93.‌Difference between throw and throws keyword

  1. ‌When a program is unable to produce the required output, the ‘throw’ keyword is used.
  2.  It helps us to create an exception and interrupt the flow of the program.
  3. The ‘Throws’ keyword is used to signal a probable exception in a program that may occur when a method called is executed.

94. ‌Can you catch multiple exceptions?

‌ Yes, multiple exceptions can be caught in a program.

95.‌ What is the difference between an exception and an error?

  1. ‌Exceptions can be handled with the try-catch blocks but errors cause disruption in the flow of the program and cannot be fixed by itself.
  2.  Errors occur during the run time of the program. 
  3. Most of the unchecked exceptions are errors only

96.‌What do you mean by OutOfMemoryError?

‌When the JVM runs out of heap memory it throws an error called OutOfMemoryError.

97. ‌Can you write a custom exception in Java?

‌Yes, we can write custom exceptions by creating a whole new class that ends with the name ‘Exception’

98. ‌What are some advantages of exception handling in Java?

  1. Separates the error handling codes from the main program code.
  2. Grouping the errors thus helping in solving them more quickly.
  3. Maintains the normal flow of the program

99.‌How does exception handling work in Java?

  1. An object of the error is created when an error is detected in the program. 
  2. It is called an exception object and contains all the information of the error.
  3. Call stack methods are called to handle the exception.
  4. Particular code in the call stack is searched to find a way to handle the exception.
  5. That exception handler is chosen so as to catch the exception.

100.‌What are the keywords used in exception handling?

Try — Exception is handled by writing the code inside the try block that might throw an exception.
Catch — The exception handling code is written in the catch block.

Throw — It is used by the user to create an exception if the code does not run in the desired way.

Throws — When we are aware of the checked exceptions and let the caller program know about those, the throws keyword is used before that exception.

Finally — This block always gets executed even if an exception is thrown. It is used with the try-catch blocks.

101.‌What is a stack trace?

‌A stack trace is used to list all the methods and names of the classes that have been called or used till the time exception occurred. The stack trace helps in debugging the code

102.‌ Can a child class that is overridden throw an exception?

  1. ‌If the parent class does not throw any exception it is unlikely that the child class will throw one. 
  2. But an unchecked exception can be thrown by it during run time regardless of whether an exception is thrown by the base class or not

103.‌What is a nested class?

  1. ‌A class created inside another class or interface is a nested class.
  2.  The method of nested classes is used to group similar classes together so that the code looks neat and is maintainable

104.‌What are some advantages of nested classes?

  1. Helps to maintain the program neat and readable
  2. Code optimization
  3. Creates a special relation among the nested classes which gives access to the outer class members, including the private class.

105.‌What are the different ways in which strings can be compared?

‌1. .equals()

 2. Using == operator

3. s.charAt() 

4. compareTo() 

5 .equalsIgnoreCase() 

6. compareToIgnoreCase() 

106.‌What are identifiers in Java?

  1. ‌Identifiers in Java are names given to a class, method, package, variable, etc.
  2.  to ensure they can be identified easily.
  3.  However, you cannot name them in any random way.
  4.  There are some rules to create identifiers
  5. Spaces cannot be used
  6. Special symbols cannot be used except underscore and $ sign.
  7. Reserved keywords of Java cannot be used.
  8. Integer values can only be used after 1st character.

107.‌What is synchronization in Java

  1. ‌When multiple threads(instructions) try to access the same resource, errors are bound to happen.
  2.  Using synchronized blocks in Java, you can control the access to these multiple threads.
  3.  This is called synchronization. 
  4. These synchronized blocks can be identified by the synchronized keyword

108.‌Does the order of specifiers matter while creating a method?

  1. ‌ No
  2.  the order does not matter till every specifier is mentioned.
  3.  Public static void is the same as a static public void

109. ‌Do local variables have a default value?

  1. ‌No
  2. Default variables do not have a value until initialized.
  3.  The same goes for primitives and object references

110. ‌ What are the restrictions to the static methods?

  1.  A static method cannot call a non-static method directly nor use a non-static data member.
  2.  This and super keywords cannot be used inside a static method

111. ‌Can a program be run without the main method?

  1. ‌Yes
  2.  It is possible by using a static block

112.‌What is the difference between Array and ArrayList?

Array

‌1. It is a dynamic object and holds similar values.

2. It is static in size, meaning the size cannot be manipulated once created.

3. Can store both objects and primitives.

4. Multidimensional

‌ArrayList-

  1. It is a class of Java collections framework and comes under Java.util package.
  2.  It is dynamic in size.
  3.  Therefore, can be resized according to the need
  4. Cannot store primitives.
  5. It is always of a single dimension

113.‌What is a list in Java?

  1. ‌The list is an interface in Java in which objects can be stored in an ordered way(indexed) and duplicate and null values can also be stored.
  2. ArrayList, Linked List, vector, and stack are implementation classes of the List

114.‌What is the Collection interface in Java?

  1. ‌It is a framework that acts as a base to store and manipulate groups of objects.
  2.  Classes like ArrayList, LinkedList, Vector, and interfaces like queue, list, and set come under it.
  3. Collection represent group of objects
  4. Known as elements
  5. Some collection allows duplicate elements and some doesn't allow
  6. Some are ordered and unordered
  7. It provide implementation for sub interfaces like set and list

No comments:

Post a Comment

Reverse Words in a Sentence