Computer Applications
Consider the following program segment and answer the questions given below:
int x[][] = {{2,4,5,6}, {5,7,8,1}, {34, 1, 10, 9}};
(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?
Answer
(a) The position of 34 is x[2][0]
(b) x[2][3] + x[1][2] = 9 + 8 = 17
Explanation:
(a) Position of 34
- The array
x
is a 2D array, where elements are accessed using row and column indices. - The element
34
is located in the 3rd row and 1st column. - In zero-based indexing:
- Row index:
2
- Column index:
0
Position: x[2][0]
(b) Result of x[2][3] + x[1][2]
Step 1: Locate x[2][3]
x[2][3]
refers to the element in the 3rd row and 4th column.- Value at
x[2][3]
=9
.
Step 2: Locate x[1][2]
x[1][2]
refers to the element in the 2nd row and 3rd column.- Value at
x[1][2]
=8
.
Step 3: Add the Values
x[2][3] + x[1][2]
=9 + 8
=17
.
Related Questions
A student is trying to convert the string present in x to a numerical value, so that he can find the square root of the converted value. However the code has an error. Name the error (syntax / logical / runtime). Correct the code so that it compiles and runs correctly.
String x= "25"; int y=Double.parseDouble(x); double r=Math.sqrt(y); System.out.println(r);
Consider the following program segment and answer the questions below:
class calculate { int a; double b; calculate() { a=0; b=0.0; } calculate(int x, double y) { a=x; b=y; } void sum() { System.out.println(a*b); }}
Name the type of constructors used in the above program segment?
Define a class to search for a value input by the user from the list of values given below. If it is found display the message "Search successful", otherwise display the message "Search element not found" using Binary search technique.
5.6, 11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5.