(a) Create a class with one integer instance variable. Initialize the variable using:
- (a)default constructor
- (b)parameterized constructor
Show SolutionHide Solution↓
class Number {
int a;
public Number() {
a = 0;
}
public Number(int x) {
a = x;
}
}
(b) Complete the code below to create an object of Scanner class:
Scanner sc = ____ Scanner(__________);
Scanner sc = new Scanner(System.in);
(c) What is an array? Write a statement to declare an integer array of 10 elements.
An array is a structure to store a number of values of the same data type in contiguous memory locations. The following statement declares an integer array of 10 elements:
int arr[] = new int[10];
(d) Name the search or sort algorithm that:
- Makes several passes through the array, selecting the next smallest item in the array each time and placing it where it belongs in the array.
- At each stage, compares the sought key value with the key value of the middle element of the array.
- Selection Sort
- Binary Search
(e) Differentiate between public and private modifiers for members of a class.
public modifier makes the class members accessible both within and outside their class whereas private modifier makes the class members accessible only within the class in which they are declared.