Computer Applications
Consider the following program segment and answer the questions given:
for(int k=1;k<=5;k++)
System.out.println(k);
System.out.println(k);
(a) Will the program segment get executed successfully?
(b) If not, state the type of error?
(c) How do you correct the program if it has any error?
Java Iterative Stmts
1 Like
Answer
(a) No, the program segment will not execute successfully.
(b) The error is a syntax error because the variable k is not known outside the loop. The variable k is created inside the for loop and only exists there. When the program tries to use k after the loop, it does not recognize it, so the program gives an error.
(c) To fix the error, declare k before the loop:
int k;
for (k = 1; k <= 5; k++)
System.out.println(k);
System.out.println(k);
Answered By
2 Likes
Related Questions
Write Java statements for the following:
(a) Initialise the array with the three favourite subjects.
(b) Declare an array to store the marks in 3 subjects of 40 students.A Student executes the given program segment and it results in 1.0, irrespective of the value of n. State the type of the error, write the correct statement:
void solve(int n) { double power=Math.pow(n, 2/3); System.out.println(power); }Consider the array:
int a[]={12, 35, 40, 22, 56, 9, 70};(a) In the above given array, using linear search, how many iterations are required to check for the existence of the value 56?
(b) If the array is arranged in descending order, how many iterations are required to check for the existence of 56 using linear search?
Evaluate the expression:
3 * 6 % 5 * 4 / 2 * 7 % 5