You are on page 1of 6

Programs under practice section

1) Generate 100 lowercase letters randomly and assign to an array of characters. Count the
occurrence of each letter in the array.

2. Find the mean value of the elements of alternative indices of an array.


3. Suppose, an array has n values. Write a method that finds the median of those values.

4. Write a method that takes a 2D matrix as input and calculates the transpose of the matrix.

5. Write a method that takes two 2D matrices of same dimension and calculates their summation.
// A + B
6. Write a method that takes two 2D matrices and calculates their multiplication. // AB
Quiz-2
1. Default Constructor with code example.

A. When we write a class without any constructors, java implicitly provides a public constructor with no
arguments. This constructor is known as the default constructor, which is provided by java if and only if
no other explicit constructors are present in that class.

The following is a default constructor, as no other constructor is written in the class Pizza.

2. Right-shift Array Elements

3. Multiply all values in 2d array


4.
In line 7. int d =a; → ‘a’ is an instance variable (non-static) and a static method (main method)
cannot access a non-static variable.
In line 11. c=a+b; → ‘a’ and ‘b’ are instance variables (non-static) and cannot be accessed by
static method f1().
In line 12. f2(); → f2() is an instance method and it cannot be invoked by the static method f1();
In line 18. c=a+b+e; → this line is wrong because the variable ‘e’ has not been initialized.
In line 19. f1(); → f1() has an integer return type, but no variable is declared to initialize the
return value of this method.
Some Other programs
Write a method to randomly initialize a 2D array of size NxN with the constrain that the main
diagonal locations of the array are strictly zeros.

In line 2 , static int a; should be initialised as java does not initialize final constants.
In line 6. ‘b’ is an instance variable and it cannot be referenced to a static context.
In Line 7. Static method f1() cannot invoke a non-static method f2().
In line 12. ‘c’ is a local variable and cannot have modifier. Also int c is already declared in line 12.
In line 14. ‘a’ is a final constant and cannot be accesed to change its value.

You might also like