diff --git a/README.md b/README.md index 0df8330..380fef3 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,22 @@ # Java Basics -*Click if you like the project. Pull Request are highly appreciated.* +> *Click ★ if you like the project. Your contributions are heartily ♡ welcome.*
## Related Topics -* *[Multithreading Basics](multithreading-questions.md)* -* *[Collections Basics](collections-questions.md)* -* *[JDBC Basics](JDBC-questions.md)* +* *[Multithreading](multithreading-questions.md)* +* *[Collections](collections-questions.md)* +* *[Java Database Connectivity (JDBC)](JDBC-questions.md)* * *[Java Programs](java-programs.md)* * *[Java String Methods](java-string-methods.md)* -* *[JSP Basics](jsp-questions.md)* -* *[Servlets Basics](servlets-questions.md)* +* *[Jakarta Server Pages (JSP)](jsp-questions.md)* +* *[Servlets](servlets-questions.md)* * *[Java Multiple Choice Questions](java-multiple-choice-questions-answers.md)* -* *[Spring Basics](https://github.com/learning-zone/spring-basics)* -* *[Hibernate Basics](https://github.com/learning-zone/hibernate-basics)* * *[Java Design Pattern](https://github.com/learning-zone/java-design-patterns)* +* *[Hibernate](https://github.com/learning-zone/hibernate-basics)* +* *[Spring Framework Basics](https://github.com/learning-zone/spring-basics)*
diff --git a/core-java/arrays/package-info.java b/core-java/arrays/package-info.java index 42241cb..2c0f888 100644 --- a/core-java/arrays/package-info.java +++ b/core-java/arrays/package-info.java @@ -1,8 +1,6 @@ -/** - * - */ -/** - * @author U6044324 - * Nov 5, 2018 - */ -package arrays; \ No newline at end of file + +/* + @author U6044324 + Nov 5, 2018 +*/ +package arrays; diff --git a/core-java/basics/BinarySearch.java b/core-java/basics/BinarySearch.java index d30ba02..c8d6694 100644 --- a/core-java/basics/BinarySearch.java +++ b/core-java/basics/BinarySearch.java @@ -1,19 +1,47 @@ -package basics; -import java.util.Arrays; - -public class BinarySearch { - - public static void main(String[] args) { - - int arr[] = {10, 20, 15, 22, 35}; - Arrays.sort(arr); - - int key = 35; - int res = Arrays.binarySearch(arr, key); - if(res >= 0){ - System.out.println(key + " found at index = "+ res); - } else { - System.out.println(key + " not found"); - } +// Java implementation of iterative Binary Search + +import java.io.*; + +class BinarySearch { + + // Returns index of x if it is present in arr[]. + int binarySearch(int arr[], int x) + { + int l = 0, r = arr.length - 1; + while (l <= r) { + int m = l + (r - l) / 2; + + // Check if x is present at mid + if (arr[m] == x) + return m; + + // If x greater, ignore left half + if (arr[m] < x) + l = m + 1; + + // If x is smaller, ignore right half + else + r = m - 1; + } + + // If we reach here, then element was + // not present + return -1; + } + + // Driver code + public static void main(String args[]) + { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = ob.binarySearch(arr, x); + if (result == -1) + System.out.println( + "Element is not present in array"); + else + System.out.println("Element is present at " + + "index " + result); } } diff --git a/core-java/basics/BoundedTypesGenerics02.java b/core-java/basics/BoundedTypesGenerics02.java index 7657f86..a1ad4e4 100644 --- a/core-java/basics/BoundedTypesGenerics02.java +++ b/core-java/basics/BoundedTypesGenerics02.java @@ -1,29 +1,28 @@ -package basics; - interface SampleInterface { - public void displayClass(); + void displayClass(); } + class BoundTest { - private T objRef; - public BoundTest(T obj) { - this.objRef = obj; - } - - public void doRunTest(){ - this.objRef.displayClass(); - } -} + private T objRef; + public BoundTest(T obj) { + this.objRef = obj; + } + + public void doRunTest() { + objRef.displayClass(); + } +} class SampleClass implements SampleInterface { - public void displayClass() { - System.out.println("Inside supper Class A"); - } + public void displayClass() { + System.out.println("Inside super Class A"); + } } -class BoundedTypesGenerics02 { - public static void main(String a[]) { - BoundTest b = new BoundTest<>(new SampleClass()); - b.doRunTest(); - } +public class BoundedTypesGenerics02 { + public static void main(String[] args) { + BoundTest b = new BoundTest<>(new SampleClass()); + b.doRunTest(); + } } diff --git a/core-java/basics/EnumConstructorExample.java b/core-java/basics/EnumConstructorExample.java index b3e0cfe..6600380 100644 --- a/core-java/basics/EnumConstructorExample.java +++ b/core-java/basics/EnumConstructorExample.java @@ -1,24 +1,21 @@ -package basics; - enum TrafficSignal { RED("STOP"), GREEN("GO"), ORANGE("SLOW DOWN"); - - private String action; - public String getAction() { - return this.action; - } - private TrafficSignal(String action) { + + private final String action; + + TrafficSignal(String action) { this.action = action; } + + public String getAction() { + return action; + } } -public class EnumConstructorExample { +public class EnumConstructorExample { public static void main(String[] args) { - - TrafficSignal[] signals = TrafficSignal.values(); - - for(TrafficSignal signal: signals){ - System.out.println("name: "+ signal.name() + " action: " + signal.getAction()); + for (TrafficSignal signal : TrafficSignal.values()) { + System.out.println("name: " + signal.name() + " action: " + signal.getAction()); } } } diff --git a/java-multiple-choice-questions-answers.md b/java-multiple-choice-questions-answers.md index 9bb173c..33e86f6 100644 --- a/java-multiple-choice-questions-answers.md +++ b/java-multiple-choice-questions-answers.md @@ -41,6 +41,8 @@ D. int num1 = 0, num2 = 0; ``` A. double num1, int num2 = 0; ``` +**Explanation**: A. Option A does not compile because Java does not allow declaring different types as part of the same declaration. + ## Q. What is the output of following program? ```java public class Test { @@ -214,15 +216,6 @@ Output a : 2 J : 2 ``` -## Q. Which of the following declarations does not compile? -A. double num1, int num2 = 0; -B. int num1, num2; -C. int num1, num2 = 0; -D. int num1 = 0, num2 = 0; -``` -A. double num1, int num2 = 0; -``` -**Explanation**: A. Option A does not compile because Java does not allow declaring different types as part of the same declaration. ## Q. What is the output of the following? ```java diff --git a/java-programs/BubbleSort.java b/java-programs/BubbleSort.java index 2f1ee8f..15b1d88 100644 --- a/java-programs/BubbleSort.java +++ b/java-programs/BubbleSort.java @@ -7,16 +7,22 @@ public class BubbleSort { public void bubbleSort(int[] arr) { + boolean swap; int n = arr.length; int tmp = 0; for (int i = 0; i < n ; i++) { - for (int j = 1; j < n-1; j++) { + swap = false; + for (int j = 1; j < n-i; j++) { if (arr[j-1] > arr[j]) { - tmp = arr[j - 1]; - arr[j-1] = arr[j]; - arr[j] = tmp; + tmp = arr[j]; + arr[j] = arr[j-1]; + arr[j-1] = tmp; + swap = true; } } + if (!swap){ + break; + } } }