Examples of basic algorithms (binary search and quicksort):
import java.util.Scanner; public class BinarySearch { //BinarySearch public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int N = sc.nextInt();//Number of numbers int M = sc.nextInt();//Number to find int[] arr = new int[N]; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); } int result = solution(N,M,arr); if (result <0 ) { System.out.println("Error"); }else { System.out.println("Exist"); } } sc.close(); } private static int solution(int n, int m, int[] arr) { int low = 0; int high = arr.length-1; QuickSort(arr,low,high); while (low <= high) { int mid = low + ( high - low)/2; if (m<arr[mid]) high = mid-1; else if (m>arr[mid]) low = mid+1; else return mid; } return -1; } private static void QuickSort(int[] arr, int indexStart, int indexEnd) { // TODO Auto-generated method stub int pivotIndex = (indexStart+indexEnd)/2; //swap swap(arr,pivotIndex,indexEnd); int k = partition(arr,indexStart-1, indexEnd,arr[indexEnd]); swap(arr, k, indexEnd); if ((k - indexStart)>1) QuickSort(arr, indexStart, k-1); if ((indexEnd-k)>1) QuickSort(arr, k+1, indexEnd); } private static int partition(int[] arr, int left, int right, int pivot) { do { while (arr[++left] < pivot) ; while ((right!=0)&&arr[--right]>pivot) ; swap(arr, left, right); } while (left < right); swap(arr, left, right); return left; } private static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
Question 1: What is Java's bytecode?
Answer: A low-level representation of a program that runs on the Java Virtual Machine.
Question 2: What is the return value of Math.abs(-2147483648)?
Answer: -2147483648, which is a typical example of integer overflow.
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub int result = Math.abs(-2147483648); System.out.print(result); } }
Question 3: How can I initialize a double variable to infinity?
Answer: You can use Java's built-in constants: Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY.
Question 4: Can a value of type double and a value of type int be compared with each other?
Answer: It is not possible to do without type conversion, but Java generally does the required type conversion automatically.
Question 5: What happens if a variable is not initialized before using it?
Answer: Java throws a compile exception if there are any execution paths in the code that could lead to the use of uninitialized variables.
Question 6: What are the values of the Java expressions 1/0 and 1.0/0.0?
Answer: The first expression will generate a divide-by-zero exception (it will terminate the whisker because the value is undefined) (Exception in thread "main" java.lang.ArithmeticException: / by zero); The value of the two expressions is Infinity.
Question 7: Can I use < and > to compare String variables?
Answer: No, only primitive data types define these operators.
Question 8: What is the result of division and remainder of negative numbers?
Answer: The quotient of the expression a/b is rounded to 0; the remainder of a%b is defined as (a/b)*b+a%b is equal to a.
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(-14/3); System.out.println(14/-3); System.out.println(-14%3); System.out.println(14%-3); } }
Question 9: Why use (a&&b) instead of (a&b)?
Answer: The operators &, |, and ^ represent the bitwise logical operations AND, OR, and XOR of integers, respectively. The && and || operators are only valid in stand-alone Boolean expressions due to the principle of short-circuit evaluation: expressions are evaluated from left to right, and evaluation stops once the value of the entire expression is known.
Question 10: Is there a problem with ambiguity in nested if statements?
Answer: Yes, in Java, if <expr1> if<expr2><stmntA> else <stmntB> is equivalent to if<expr1>{if<expr2><stmntA>else<stmntB>}. The best way to avoid the "unowned" else trap is to explicitly state all curly braces.
Question 11: What is the difference between a for loop and its while form?
Answer: The code at the head of the for loop and the body of the for loop are in the same code segment. In a typical for loop, the incrementing variable is generally unavailable after the loop finishes; but in its equivalent while loop, the incrementing variable is still available after the loop finishes. This distinction is often the main reason to use a while rather than a for loop.
Question 12: What is the difference between int a[] and int[] a to declare an array?
Answer: In Java, both are legal and equivalent. The former is the way of declaring arrays in C language, and the latter is the way advocated by Java, because the variable type int[] can more clearly indicate that this is an integer array.
Question 13: Why is the starting index of the array 0 instead of 1?
Answer: Originating in machine language, setting the starting index to 1 either wastes space in the first element of the array, or takes extra time to set the index to 1.
Question 14: If a[] is an array, then System.out.println(a) prints a hexadecimal integer?
Answer: This method prints the address of this array.
Question 15: Can a program re-read the value from standard input?
Answer: No, only one chance
Question 16: What happens when the program tries to read after the standard input is empty?
Answer: Got an error. The isEmpty() function can be used to help check if there is any more input available.
Question 17: In Java, can a static method take another static method as a parameter?
Answer: No.