/
Java Arrays and Exception Handling
Save to my account
Sign up
Java Arrays and Exception Handling
Java Arrays and Exception Handling
Study
1
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.
2
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.
3
Question
What is an element in an array?
Answer
Each item stored in an array is called an element.
4
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.
5
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].
6
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;.
7
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.
8
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};.
9
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.
10
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.
11
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.
12
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].
13
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).
14
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.
15
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.
16
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.
17
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.
18
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.
19
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.
20
Question
What is a void method in Java?
Answer
A void method is a method that does not return any value.
21
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.
22
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.
23
Question
What is recursion in Java?
Answer
Recursion is a programming technique where a method calls itself to solve a problem.
24
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.
25
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.
26
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.
27
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.
28
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.
29
Question
Give an example of a method that takes parameters and returns a value.
Answer
public static int square(int num) { return num * num; }
30
Question
What is the function signature for the multiply method in Java?
Answer
public static int multiply(int x, int y)