0% found this document useful (0 votes)
5 views2 pages

Java Arrays, Exceptions, and Constructors Guide

The document provides a concise overview of Java concepts related to arrays, matrices, exception handling, constructors, and method overloading. It explains different types of arrays, how to declare and instantiate multi-dimensional arrays, and includes a matrix multiplication program. Additionally, it covers exception handling mechanisms, the significance of constructors, and the differences between constructors and methods.

Uploaded by

gmranuj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Java Arrays, Exceptions, and Constructors Guide

The document provides a concise overview of Java concepts related to arrays, matrices, exception handling, constructors, and method overloading. It explains different types of arrays, how to declare and instantiate multi-dimensional arrays, and includes a matrix multiplication program. Additionally, it covers exception handling mechanisms, the significance of constructors, and the differences between constructors and methods.

Uploaded by

gmranuj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Short Oral Answers

Section A: Arrays & Matrices 1. Explain different types of arrays in Java? - Single-
dimensional array: list of elements accessed by one index. Example: int[] a = {1,2,3}. -
Multi-dimensional array: array of arrays (e.g., 2D, 3D). Example: int[][] m = new
int[3][4]. - Jagged array: rows of different lengths (2D where each row array size
varies). - Anonymous array: created without reference when passed to method.

2. How to declare and instantiate multi-dimensional array in JAVA? - Declaration: int[][]


mat; - Instantiation: mat = new int[3][4]; - Combined: int[][] mat =
{{1,2,3,4},{5,6,7,8},{9,10,11,12}};

3. How to declare 5x4 matrix in java. Explain with example - Declaration & instantiation:
int[][] m = new int[5][4]; - Example fill: for (int i=0;i<5;i++){ for (int
j=0;j<4;j++){ m[i][j] = i*4 + j + 1; } }

4. Write program to perform matrix multiplication - Short program (assumes compatible


sizes: A rows x cols, B cols x p): See code snippet in document.

Section B: Exception Handling 1. What is exception handling? - Mechanism to catch and


manage runtime errors (exceptions) to prevent program crash and provide graceful recovery.

2. Difference between error and exception handling - Error: serious problem from JVM
(e.g., OutOfMemoryError) usually unrecoverable. - Exception: recoverable runtime problem
(e.g., IOException, NullPointerException) that can be caught and handled.

3. What is use of throw keyword, demonstrate with example program - throw: explicitly
throws an exception instance. Example: if (age < 18) throw new
IllegalArgumentException("Must be 18+");

4. What is use of throws keyword in Java - declares that a method may pass exception
responsibility to its caller. Example: void read() throws IOException { ... }

5. What is the use of finally block - finally runs after try/catch whether exception
occurs or not. Used to release resources.

Section C: Constructors & Overloading Q1. Why Method Overloading is not possible by
changing the return type only? - Overloading resolution uses method signature (name +
parameter types). Return type is not part of signature, so changing it alone is ambiguous
for caller.

Q2. What is constructor? Explain its significance. Explain default and parameterized
constructors - Constructor: special method with class name used to initialize objects. -
Default constructor: no-arg constructor provided by compiler if none declared. -
Parameterized constructor: user-defined with parameters to initialize fields.

Q3. Explain types of constructor with suitable example. - Default (no-arg) constructor:
class A { A() { } } - Parameterized constructor: class A { A(int x) { this.x=x; } } - Copy
constructor (manual in Java): class A { A(A other) { this.x = other.x; } }

Q4. Explain concept of constructor and method overloading. - Constructor overloading:


multiple constructors with different parameter lists. - Method overloading: multiple
methods with same name but different parameter lists. Both resolved at compile time by
argument types/count.

Q5. Differentiate between Constructor and Method - Name: constructor name = class name;
method has its own name. - Return type: constructor has no return type; method has a
return type (can be void). - Purpose: constructor initializes object; method performs
actions. - Invocation: constructor called at object creation (new); method called on
object reference.

Matrix Multiplication — Example Java Program (short) --------------------------------


public class MatrixMul { public static void main(String[] args) { int A[][] =
{{1,2,3},{4,5,6}}; int B[][] = {{7,8},{9,10},{11,12}}; int r1 = [Link], c1 =
A[0].length; int r2 = [Link], c2 = B[0].length; if (c1 != r2) {
[Link]("Incompatible matrices"); return; } int C[][] = new
int[r1][c2]; for(int i=0;i<r1;i++){ for(int j=0;j<c2;j++){ C[i][j]=0;
for(int k=0;k<c1;k++){ C[i][j] += A[i][k]*B[k][j]; } } }
// print result for(int i=0;i<r1;i++){ for(int j=0;j<c2;j++){
[Link](C[i][j] + " "); } [Link](); } } }

End of short oral answers.

You might also like