Made with ScholarlyMake flashcards like these from your own notes
Question:What is an array in Java?
Answer:An array is a fundamental data structure in Java that allows you to store a collection of elements of the same type.
Question:What are the purposes of arrays in Java?
Answer:1. Grouping related data: Arrays allow you to group related data elements of the same type into a single entity. 2. Efficient storage and retrieval: Java arrays provide efficient storage and retrieval of elements as they are stored in contiguous memory locations. 3. Iteration and processing: Arrays are commonly used in loops to iterate over the elements and perform operations on them.
Question:What is an element in an array?
Answer:Each item stored in an array is called an element.
Question:What is an index in an array?
Answer:Each location of an element in an array has a numerical index, which is used to identify the element.
Question:What is the general syntax for declaring an array in Java?
Answer:The general syntax for declaring an array is: type arrayName new type[arraySize].
Question:How do you declare an array without initializing it?
Answer:You can declare an array with a specified type but without initializing it, e.g., int array;.
Question:How do you declare and allocate memory for an array in Java?
Answer:To declare and allocate memory for an array, you use: int array = new int[5]; which declares and allocates memory for 5 integers.
Question:How can you declare and initialize an array with values in Java?
Answer:You can declare and initialize an array with values at the same time, e.g., int array = {1, 2, 3, 4, 5};.
Question:How do you access array elements in Java?
Answer:Array elements are accessed using their indices, e.g., arrayName[index]. The indices start from 0.
Question:What is the purpose of the enhanced for loop (for-each loop) in Java?
Answer:The for-each loop is a simpler way to iterate over arrays or collections, allowing you to access every element without needing the index.
Question:What is a multi-dimensional array in Java?
Answer:A multi-dimensional array in Java is like a 2D array, which can be visualized as a table with rows and columns.
Question:What is the declaration syntax for a 2D array in Java?
Answer:The declaration syntax for a 2D array is: type arrayName = new type[rowSize][columnSize].
Question:What are the benefits of using arrays in Java?
Answer:1. Efficient storage and access. 2. Data organization. 3. Iteration and processing. 4. Versatility (can store any data type). 5. Flexibility (fixed size but can use other data structures for dynamic data).
Question:What is an Exception in Java?
Answer:An Exception is an error that occurs during the execution of Java code, and Java will normally stop and generate an error message.
Question:What is the purpose of try and catch statements in Java?
Answer:The try statement allows you to define a block of code to be tested for errors while it is being executed, and the catch statement handles the error if one occurs.
Question:What is the difference between checked and unchecked exceptions in Java?
Answer:Checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime.
Question:What is a NullPointerException in Java?
Answer:A NullPointerException is thrown when an application attempts to use null in a case where an object is required.
Question:What is the purpose of the return keyword in Java functions?
Answer:The return keyword specifies the value to be sent back to the caller from a method.
Question:What are formal and actual parameters in Java?
Answer:Formal parameters are defined in the function's signature; actual parameters are the real values provided when the function is called.
Question:What is a void method in Java?
Answer:A void method is a method that does not return any value.
Question:How do you define a method in Java?
Answer:A method in Java is defined within a class and has a return type, a name, and parameters.
Question:What does it mean for a method to have parameters?
Answer:A method with parameters accepts external input when it is called, allowing it to perform tasks based on that input.
Question:What is recursion in Java?
Answer:Recursion is a programming technique where a method calls itself to solve a problem.
Question:What is out of memory error in Java?
Answer:An OutOfMemoryError is thrown when the JVM runs out of memory, for example, by creating a large number of objects without sufficient heap space.
Question:What is the significance of exception handling in Java?
Answer:Exception handling provides a standardized mechanism for handling errors and unexpected events without cluttering your code, allowing for a more robust and fault-tolerant design.
Question:What does finally do in a try-catch block?
Answer:The finally block always runs, regardless of whether an exception is thrown or caught, allowing you to execute cleanup code.
Question:What are Methods with Parameters and No Return Values?
Answer:Methods that accept parameters but do not return any value and are useful for performing specific tasks without providing a result.
Question:What are Methods with No Parameters but With Return Values?
Answer:Methods that do not require parameters but still return a value, useful for computations or fetching specific results.
Question:Give an example of a method that takes parameters and returns a value.
Answer:public static int square(int num) { return num * num; }
Question:What is the function signature for the multiply method in Java?
Answer:public static int multiply(int x, int y)
Question:What are recursive functions in Java?
Answer:Recursive functions in Java are functions that call themselves during their execution.
Question:What is the purpose of recursive functions?
Answer:They provide an elegant and powerful way to solve complex problems by breaking them down into smaller, more manageable subproblems.
Question:What is the base case in recursion?
Answer:The base case is the simplest scenario that can be solved directly without further recursion.
Question:What does the base case prevent in recursion?
Answer:It prevents the recursion from continuing indefinitely.
Question:What is an example of a base case for calculating factorial?
Answer:When n is 0, because the factorial of 0 is 1.
Question:What is the recursive case in a recursive function?
Answer:This is the part where the function calls itself, typically with a modified argument.
Question:How does recursion work in calculating factorial of 5?
Answer:The function calls factorial(4), factorial(3), factorial(2), and finally factorial(1), reaching the base case at factorial(1).
Question:What are the advantages of recursion?
Answer:1. Simplicity: simplifies code for problems with a recursive structure. 2. Code Readability: recursive solutions are often more concise and easier to understand.
Question:What are the disadvantages of recursion?
Answer:1. Stack Overflow: if recursion depth is too large, it can lead to a stack overflow. 2. Performance: may be less efficient due to the overhead of function calls and potential repeated calculations.
Question:What are array built-in functions in Java?
Answer:Utility methods provided by the java.util.Arrays class and other related classes that are used to manipulate and process arrays.
Question:What does Arrays.toString do?
Answer:Converts a 1D array into a human-readable string.
Question:What is the output of Arrays.toString for int arr = {1, 2, 3}?
Answer:1, 2, 3
Question:What does Arrays.deepToString do?
Answer:Converts a multidimensional array into a string representation.
Question:What does the method Arrays.sort do?
Answer:Sorts the elements of the array in ascending order.
Question:What is the purpose of Arrays.parallelSort?
Answer:Sorts large arrays in parallel using multiple threads for faster sorting.
Question:What does Arrays.binarySearch do?
Answer:Searches for a key in a sorted array and returns its index or a negative value if the key is not found.
Question:What does Arrays.equals do?
Answer:Checks if two one-dimensional arrays are equal in content.
Question:What does Arrays.copyOf do?
Answer:Creates a new array with the specified length and copies elements from the original array.
Question:What does Arrays.fill do?
Answer:Fills the entire array with a specified value.
Question:What is the purpose of Arrays.hashCode?
Answer:Generates a hash code for the array, useful for storing arrays as keys in hash-based collections.
Question:What does the method Arrays.stream do?
Answer:Converts an array into a stream for functional programming.
Question:What does the property array.length do?
Answer:Returns the size of the array.
Question:What are the consequences of accessing an array index out of bounds?
Answer:A runtime exception is thrown.
Question:How do you declare a 2D array in Java?
Answer:int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
Question:What exception is thrown when trying to access an invalid array index?
Answer:ArrayIndexOutOfBoundsException.
Question:What is the definition of a stack overflow?
Answer:An error that occurs when the recursion depth is too large, leading to a limit in the stack space.
Question:What does the void keyword indicate in a Java method?
Answer:The method does not return a value.
Question:What happens if a Java method does not have a return statement?
Answer:The method must be declared void.
Question:What happens if an exception is not caught in a Java program?
Answer:The program terminates immediately.
Question:What is the arithmetic exception thrown for?
Answer:It is thrown when attempting to divide by zero.
Question:What does Arrays.copyOfRange do?
Answer:Copies a specific range of elements into a new array.
Question:What is the purpose of the finally block in Java?
Answer:To execute cleanup code regardless of whether an exception occurs.
Question:What is static resource allocation in relation to arrays?
Answer:When you declare an array with a fixed size, its size cannot be changed during runtime.
Question:What are the requirements of static resource allocation (SRA) in Java?
Answer:All elements must have the same data type.
Question:What is contiguous memory location (CML) in relation to arrays?
Answer:Array elements are stored in contiguous side-by-side memory locations.
Question:What are common array errors?
Answer:Out of Bounds Error occurs when attempting to access an index outside the array's declared size.
Question:What is the purpose of arrays?
Answer:To group related data together, enable efficient storage, and ease iteration and processing of multiple elements.
Question:How does variable access in arrays differ from individual variables?
Answer:Arrays allow efficient logic implementation, such as looping, which isn't possible with individual variables.
Question:What is a NumberFormatException?
Answer:An unchecked exception that occurs for invalid string-to-number conversions.
Question:What exception is thrown when a thread is interrupted while paused?
Answer:InterruptedException.
Question:What type of loop is particularly useful for iterating over arrays in Java?
Answer:For-each loop.
Question:What does Arrays.deepHashCode do?
Answer:Generates a hash code for a multidimensional array.
Question:What is the result of executing a public static int method?
Answer:It can be called from other static methods without creating an instance of the class.
Question:How are arrays passed to methods in Java?
Answer:As arguments, allowing methods to access and manipulate the array elements directly.
Question:What happens when trying to instantiate an abstract class?
Answer:InstantiationException is thrown.
Question:What is the output of the code snippet: int arr = {2, 4, 6, 8}; for(int i = 1; i < arr.length; i++) System.out.print(arr[i]);
Answer:468
Question:Which class provides utility methods for arrays in Java?
Answer:java.util.Arrays.
Question:What is the syntax to create an array using the new keyword in Java?
Answer:int arr = new int[10]; // Creates an array with 10 elements, all initialized to 0.
Question:How do you create an array using an initializer list in Java?
Answer:int[] arr = {1, 2, 3, 4, 5}; // Creates an array and assigns the values directly.
Question:How do you access a specific element in an array in Java?
Answer:Use the index to access the element. Example: arr[2] accesses the third element (index 2) in the array.
Question:What is a for-each loop and how is it used to iterate over an array in Java?
Answer:A for-each loop simplifies iterating over an array. Syntax: for (int a : arr) { System.out.println(a); } // Prints each element in the array.
Question:What is a matrix in Java?
Answer:A matrix is a two-dimensional array consisting of rows and columns.
Question:Provide an example of a matrix in Java.
Answer:int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; // Represents a 3x3 matrix.
Question:What is a palindrome?
Answer:A palindrome is a word, phrase, number, or sequence of characters that reads the same backward as it does forward.
Question:Give examples of palindromes.
Answer:Words: "radar," "level," "madam"; Numbers: 121, 12321; Phrases: "A man, a plan, a canal, Panama" ignoring spaces and punctuation.
Question:What is the java.util.Arrays library used for?
Answer:Most built-in functions for working with arrays in Java are part of the java.util.Arrays library, providing helpful tools for common programming tasks.
Question:What does 'util' stand for in java.util.Arrays?
Answer:'Util' stands for utility, meaning it provides helpful tools for common programming tasks.
Question:What is the result of Arrays.sort() method in Java?
Answer:Arrays.sort(arr); // Sorts the array in ascending order.
Question:What is a Function in Java?
Answer:A block of code that performs a specific task and is invoked using its name. Typically associated with functional programming.
Question:What is a Method in Java?
Answer:A function that is associated with a class or an object in Java. Methods can have an access modifier (e.g., public, private) and may operate on object data.
Question:What does the DRY principle stand for?
Answer:DRY stands for "Don't Repeat Yourself." It emphasizes writing reusable functions or methods to avoid duplicating code.
Question:What is Functional Code?
Answer:A code block designed to solve a specific problem, usually defined with an access modifier in Java.
Question:What is a Function Definition in Java?
Answer:Specifies what the function does and how it operates, e.g., 'public static int add(int a, int b)'.
Question:What does a Function Call or Invocation do?
Answer:Executes the function by passing arguments, e.g., 'int result = add(3, 2);'.
Question:What is the Main method in Java?
Answer:The entry point of the program, defined as 'public static void main(String[] args) { ... }'.
Question:What are Arguments in a function?
Answer:Values passed to a function when it is called.
Question:What are Parameters in a function?
Answer:Variables declared in a function's definition to receive arguments.
Question:What is Method Overloading?
Answer:Defining multiple methods with the same name but different parameter lists allows flexibility and different functionality.
Question:What is a Recursive Function?
Answer:A function that calls itself to repeat a task.
Question:What is the Recursive Case?
Answer:The part of the function where it calls itself.
Question:What is the Base Case in recursion?
Answer:The condition that stops the recursion; without it, the recursion would be infinite.
Question:What is the difference between Iteration and Recursion in programming?
Answer:Iteration uses loops to repeat tasks, while recursion calls itself to repeat tasks.
Question:What does 'passed by value' mean in Java?
Answer:When a variable is passed, its value is copied, and changes in the method do not affect the original variable.
Question:What does 'passed by reference' mean in Java?
Answer:When an array is passed, changes in the method affect the original array because the reference memory address is passed.
Question:Provide an example of passing an array to a method.
Answer:Example: public static void modifyArray(int[] arr) { arr[0] = 6; } in main: int[] arr = {1, 2, 3, 4}; modifyArray(arr);.
Question:What will the output be after modifying an array and printing it?
Answer:System.out.println(Arrays.toString(arr)); // Output: [6, 2, 3, 4] after the modification.