Computer Applications
Give the output of the following program segment:
for(r=1;r<=3;r++)
{ for(c=1;c<r;c++)
{ System.out.println(r);
}
}
Answer
Output
2
3
3
Explanation
When
r = 1:- Inner loop runs for
cfrom 1 to less than 1 → No iterations (becausec < ris false at c=1) - So no output for r=1.
- Inner loop runs for
When
r = 2:- Inner loop runs for
c = 1(since1 < 2) - Prints 2 once.
- Inner loop runs for
When
r = 3:- Inner loop runs for
c = 1andc = 2(sincec < 3) - Prints
3twice.
- Inner loop runs for
Related Questions
Evaluate the expression:
3 * 6 % 5 * 4 / 2 * 7 % 5
Consider the following program which calculates the Norm of a matrix. Norm is square root of sum of squares of all elements.
Fill in the blanks (a) and (b) with appropriate java statements:
double norm() { int x[][]={{1,5,6},{4,2,9},{6,1,3}}; int r, c, sum=0; for(r=0;r<3;r++) { for(c=0;c<3;c++) sum=sum+_____(a)_____; } ____(b)________; }Give the output of the following program segment:
void encode() { String s= "DICe"; char ch; for(i=0;i<4;i++) { ch=s.charAt(i); if("AEIOUaeiou".indexOf(ch)>=0) System.out.println("@"); else System.out.println(ch); }Name the following:
(a) What is the main condition to perform binary search on an array?
(b) A sort method in which consecutive elements are NOT compared.