Every ICSE Computer Applications paper hands you a few programs and asks a deceptively small question: what will this print? No marks for writing new code, no marks for explaining the theory — just the exact output. Students lose these marks in bulk, and they lose them for one reason: they read the program instead of running it on paper. This chapter is a cross-unit revision bank. It pulls together the traps from Units 1 to 8 of the Class X syllabus and shows you, program by program, how to trace them by hand. Every single program below was compiled and executed before it was printed here, so the outputs you see are machine outputs, not guesses.
- The Dry-Run Method: How to Trace a Program on Paper
- Division, Modulus and the Order of Operations
- Increment, Decrement and When the Value Changes
- Type Conversion, Casting and char Arithmetic
- switch Fall-Through, Ternary and Short-Circuit Logic
- Loop Traces and Pattern Output
- String Method Output Questions
- Arrays and Double Dimensional Arrays
- Methods, Overloading and Parameter Passing
- Math, Wrapper and Character Methods
- Errors the Examiner Shows You On Purpose
- Where These Questions Sit in the Paper
- Practice Worksheet (12 Questions with Answers)
The Dry-Run Method: How to Trace a Program on Paper
A dry run is exactly what a computer does, slowed down to the speed of your pen. You keep a small table with one column per variable, and after every statement that changes a variable you write a new row. You never keep a value in your head. The moment you try to hold three variables in your head at once, you make the mistake that costs the mark.
Three habits separate students who score full marks on these questions from students who do not. First, they write the initial values as row zero before the loop begins. Second, they write a separate column for output, and they add to it the instant a println runs, not at the end. Third, they trace the loop variable one step past the last iteration, because the value it holds when the loop exits is very often what the question actually asks for.
One row per change, one column per variable, and one column for output. If your trace table has fewer rows than the loop has iterations, you have skipped something.
public class Q5
{
public static void main(String args[])
{
int i, s = 0;
for (i = 1; i <= 5; i++)
{
s = s + i * i;
System.out.println("i = " + i + ", s = " + s);
}
System.out.println("Final i = " + i);
System.out.println("Final s = " + s);
}
}
Build the table before you write a single line of output. Notice that i is declared outside the loop, so it still exists after the loop ends.
| i | i <= 5? | s = s + i*i | printed |
| 1 | yes | 0 + 1 = 1 | i = 1, s = 1 |
| 2 | yes | 1 + 4 = 5 | i = 2, s = 5 |
| 3 | yes | 5 + 9 = 14 | i = 3, s = 14 |
| 4 | yes | 14 + 16 = 30 | i = 4, s = 30 |
| 5 | yes | 30 + 25 = 55 | i = 5, s = 55 |
| 6 | no — exit | — | — |
Output
i = 1, s = 1 i = 2, s = 5 i = 3, s = 14 i = 4, s = 30 i = 5, s = 55 Final i = 6 Final s = 55
The last two lines are where the marks hide. i is 6, not 5, because the loop only stops after the update has pushed it past the condition.
Division, Modulus and the Order of Operations
More output marks are lost on the humble slash than on any other operator in the syllabus. In Java, / changes its meaning depending on what sits on either side of it. If both operands are integers, the answer is an integer and the fractional part is thrown away — not rounded, thrown away. The moment one operand is a double, the whole division becomes real division.
int / int always gives an int. 17 / 5 is 3, never 3.4. To get 3.4 you need at least one side to be a real number: 17 / 5.0.
int a = 17, b = 5; System.out.println(a / b); System.out.println(a % b); System.out.println(-17 / 5); System.out.println(-17 % 5); System.out.println(17 / 5.0); System.out.println(a / b * b);
Output
3 2 -3 -2 3.4 15
Read the last line again. a / b * b is not 17. Java evaluates left to right for operators of equal precedence, so it computes 17 / 5 first, gets 3, and only then multiplies by 5 to give 15. The two lost units vanished at the division step and never came back.
The negatives follow one simple rule: the remainder takes the sign of the left-hand operand. That is why -17 % 5 is -2 and not 3.
Writing double avg = sum / n; where both sum and n are int. The division happens in integer arithmetic first, and only the already-truncated answer is widened to double. You will print 2.0 when the real average was 2.5. Cast one operand: (double) sum / n.
Increment, Decrement and When the Value Changes
The ++ operator has two personalities. In its postfix form, x++, the old value is handed to the surrounding expression and the variable is increased afterwards. In its prefix form, ++x, the variable is increased first and the new value is handed over. When both forms appear in one expression, the examiner is testing whether you can keep the order straight.
int x = 5;
System.out.println(x++);
System.out.println(x);
System.out.println(++x);
int y = 10;
int z = y++ + ++y;
System.out.println("z = " + z);
System.out.println("y = " + y);
Output
5 6 7 z = 22 y = 12
Take z = y++ + ++y apart slowly. Java reads left to right. y++ contributes the current value 10 and quietly makes y 11. Then ++y makes y 12 first and contributes 12. So z is 10 + 12 = 22, and y finishes at 12. Students who guess “21” have added 10 + 11; students who guess “23” have added 11 + 12.
Write the value of the variable in the margin after every single sub-expression. In y++ + ++y that means two margin notes, not one. It takes four seconds and it converts a guess into a certainty.
Type Conversion, Casting and char Arithmetic
Java quietly widens a smaller type into a larger one when it needs to — char into int, int into double. That is implicit conversion, and it happens without you asking. Going the other way, from bigger to smaller, must be requested in writing with a cast, and it always truncates rather than rounds.
char c = 'A'; System.out.println(c + 1); System.out.println((char)(c + 1)); System.out.println((int)'a'); double d = 7.9; System.out.println((int) d); System.out.println((int)(d + 0.5)); int n = 10; System.out.println(n / 4); System.out.println((double) n / 4); System.out.println((double)(n / 4));
Output
66 B 97 7 8 2 2.5 2.0
The first two lines are the classic trap. c + 1 promotes 'A' to its Unicode value 65, adds 1, and prints the int 66. Only when you cast the whole sum back to char do you see the letter B.
The last two lines are the same trap wearing different clothes. (double) n / 4 casts n first, so the division is real and gives 2.5. (double)(n / 4) lets the integer division finish first, gets 2, and then dresses it up as 2.0. Same characters, different brackets, different mark.
A cast binds tightly to whatever comes immediately after it. Where you put the bracket decides whether you are casting one operand or the finished answer. (int)(d + 0.5) is the standard way to round a positive double by hand: 7.9 becomes 8.4, and truncation leaves 8.
The Unicode values worth memorising are small in number and they come up every year: 'A' is 65, 'Z' is 90, 'a' is 97, 'z' is 122, and '0' is 48. The gap between an uppercase letter and its lowercase partner is always 32, which is why (char)('A' + 32) gives 'a'.
switch Fall-Through, Ternary and Short-Circuit Logic
A switch does not stop at the end of a matching case. It stops at a break. If the break is missing, execution falls straight through into the next case and keeps going. Examiners remove one break on purpose and wait.
int ch = 2;
switch (ch)
{
case 1: System.out.println("One");
case 2: System.out.println("Two");
case 3: System.out.println("Three");
break;
case 4: System.out.println("Four");
default: System.out.println("Default");
}
System.out.println("Done");
Output
Two Three Done
Control enters at case 2, prints Two, finds no break, falls into case 3, prints Three, and only then meets a break that sends it out of the switch. Four and Default are never reached.
int x = 8, y = 3;
System.out.println(x > y ? x - y : y - x);
int c = 0;
boolean r = (x < y) && (++c > 0);
System.out.println("r = " + r + ", c = " + c);
boolean r2 = (x > y) || (++c > 0);
System.out.println("r2 = " + r2 + ", c = " + c);
System.out.println(!(x == 8) + " " + (x != y));
Output
5 r = false, c = 0 r2 = true, c = 0 false true
Both logical operators are lazy. In (x < y) && (++c > 0) the left side is already false, so the whole expression cannot possibly be true and Java never evaluates the right side — which means ++c never runs and c stays 0. In (x > y) || (++c > 0) the left side is already true, so again the right side is skipped. The question is never really about the boolean; it is about the counter.
Assuming && and || always evaluate both sides. They do not, and any side effect hiding on the right — an increment, a method call, an assignment — may simply never happen. Trace the left operand first, decide whether the answer is already settled, and only then look right.
Loop Traces and Pattern Output
Loop questions come in two shapes. Either you are asked for the printed lines, or you are asked how many times the loop body runs. Both are answered by the same trace table, and both depend on noticing whether the loop prints with print or println — one keeps the cursor on the same line, the other moves it down.
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
Output
1 1 2 1 2 3 1 2 3 4
The inner loop is controlled by the outer counter, so row i contains exactly i numbers. The empty System.out.println() after the inner loop is the only reason the rows are separate lines at all — remove it and everything collapses into a single long line.
int n = 4;
int k = 1;
while (k <= n)
{
System.out.print(k * k + " ");
k = k + 1;
}
System.out.println();
int m = 0;
do
{
System.out.print(m + " ");
m = m + 2;
} while (m < 0);
System.out.println();
Output
1 4 9 16 0
The do-while condition m < 0 is false from the very start, yet 0 is still printed. That is the whole point of the construct: the body runs once before the test is ever consulted. If this had been an ordinary while, nothing would have been printed.
int n = 1234, rev = 0, d;
while (n > 0)
{
d = n % 10;
rev = rev * 10 + d;
n = n / 10;
System.out.println("d = " + d + ", rev = " + rev + ", n = " + n);
}
System.out.println("Answer = " + rev);
Output
d = 4, rev = 4, n = 123 d = 3, rev = 43, n = 12 d = 2, rev = 432, n = 1 d = 1, rev = 4321, n = 0 Answer = 4321
Three statements do all the work, and they always appear in this order: % 10 peels off the last digit, rev * 10 + d pushes it onto the answer, and / 10 shortens the number. Learn the trio as a unit and every digit-extraction question in the paper becomes the same question.
When a question says “how many times is the loop executed”, count the rows where the condition was true, not the rows in your table. The final row, where the condition fails, is a test that happened but a body that did not run.
String Method Output Questions
String questions are pure bookkeeping. The index of the first character is 0, length() is a method with brackets while an array’s length has none, and substring(a, b) includes position a but stops just before position b. Write the string out on paper with the index numbers underneath before you answer anything.
String s = " Principal Saab ";
String t = s.trim();
System.out.println(t.length());
System.out.println(t.indexOf('a'));
System.out.println(t.lastIndexOf('a'));
System.out.println(t.substring(4));
System.out.println(t.substring(0, 4));
System.out.println(t.replace('a', 'o'));
System.out.println(t.charAt(10));
System.out.println(t.compareTo("Principal"));
System.out.println(t.equalsIgnoreCase("PRINCIPAL SAAB"));
Output
14 7 12 cipal Saab Prin Principol Soob S 5 true
trim() removes the spaces at both ends but not the one in the middle, so t is Principal Saab with 14 characters. Index 7 is the a in Principal; index 12 is the second a of Saab. compareTo returns 5 because the first nine characters match exactly and the only difference left is length: 14 minus 9.
compareTo does not return −1, 0 or 1 as a rule. It returns the difference between the first pair of characters that differ, and if no pair differs it returns the difference in lengths. Give the actual number.
String a = "SAAB";
String b = "SAAB";
String c = new String("SAAB");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
System.out.println(a.compareTo("SAAC"));
String d = "Sa" + "ab";
System.out.println(d.equalsIgnoreCase(a));
Output
true false true -1 true
a and b are two names for one shared object in the string pool, so == reports true. Writing new String("SAAB") forces a brand-new object, so a == c is false even though the letters are identical. equals ignores all of that and compares the characters, which is why it is the method you should always use for strings. compareTo("SAAC") gives −1 because 'B' is 66 and 'C' is 67.
Answering false for a == b because “== never works on strings”. With two plain string literals it does return true, and the examiner is checking whether you know why. The honest rule is different: == compares object identity, equals compares content, and you should write equals in your own programs.
Arrays and Double Dimensional Arrays
An array question is a string question with numbers instead of letters. The index still starts at 0, the last valid index is still length - 1, and the fastest way to lose the mark is to draw the boxes without numbering them.
int a[] = {4, 8, 15, 16, 23, 42};
System.out.println(a.length);
System.out.println(a[0] + a[a.length - 1]);
int s = 0;
for (int i = 0; i < a.length; i = i + 2)
{
s = s + a[i];
}
System.out.println(s);
int m[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int d = 0;
for (int i = 0; i < 3; i++)
{
d = d + m[i][i];
}
System.out.println(d);
System.out.println(m[1][2] + m[2][1]);
Output
6 46 42 15 14
The step of i = i + 2 visits indices 0, 2 and 4 only, giving 4 + 15 + 23 = 42. In the matrix, m[i][i] walks the left diagonal: 1 + 5 + 9 = 15. And m[1][2] is 6 while m[2][1] is 8, because the first subscript is the row and the second is the column — swap them and you get a different element.
String w[] = {"mango", "apple", "kiwi"};
for (int i = 0; i < w.length - 1; i++)
{
for (int j = 0; j < w.length - 1 - i; j++)
{
if (w[j].compareTo(w[j + 1]) > 0)
{
String t = w[j];
w[j] = w[j + 1];
w[j + 1] = t;
}
}
}
for (int i = 0; i < w.length; i++)
{
System.out.print(w[i] + " ");
}
System.out.println();
System.out.println(w[0].length() + w[2].length());
Output
apple kiwi mango 10
This is bubble sort with compareTo standing in for >. After sorting, w[0] is apple (5 letters) and w[2] is mango (5 letters), so the final line prints 10 — a number, because both operands are int. Had they been strings it would have printed 55.
If a sorting question asks you to show each pass, write the whole array on a fresh line after every completed pass, not after every swap. That is what the marking scheme is looking for and it is far quicker to write.
Methods, Overloading and Parameter Passing
Two ideas produce almost every method-based output question: which overloaded version gets chosen, and whether a change made inside a method survives after it returns.
public class Q13
{
void show(int a)
{
System.out.println("int version: " + a);
}
void show(double a)
{
System.out.println("double version: " + a);
}
void show(int a, int b)
{
System.out.println("two ints: " + (a + b));
}
public static void main(String args[])
{
Q13 ob = new Q13();
ob.show(5);
ob.show(5.0);
ob.show('A');
ob.show(2, 3);
}
}
Output
int version: 5 double version: 5.0 int version: 65 two ints: 5
ob.show('A') is the interesting call. There is no show(char), so Java widens char to the nearest type it can find. int is a closer fit than double, so the int version wins and prints the Unicode value 65.
public class Q12
{
static void change(int n, int b[])
{
n = n * 2;
b[0] = b[0] * 2;
}
public static void main(String args[])
{
int n = 5;
int b[] = {5, 6};
change(n, b);
System.out.println("n = " + n);
System.out.println("b[0] = " + b[0]);
}
}
Output
n = 5 b[0] = 10
Both parameters were doubled inside the method, yet only one change is visible afterwards. A primitive such as int is passed by value: the method gets a private copy and doubling the copy leaves the original untouched. An array is an object, so what is copied is the reference — the address. Both names point at the same set of boxes, so writing into b[0] writes into the caller’s array.
Primitives in, copies changed, originals safe. Arrays and objects in, the same object shared, changes permanent. Reassigning the parameter itself — b = new int[5]; — changes nothing outside, because you have only pointed the local copy of the address somewhere else.
Math, Wrapper and Character Methods
Library-method questions are decided by return types as often as by values. If you can say what type comes back, you can say whether the printed answer carries a decimal point.
System.out.println(Math.pow(2, 3)); System.out.println(Math.sqrt(144)); System.out.println(Math.round(4.5)); System.out.println(Math.round(-4.5)); System.out.println(Math.round(4.4f)); System.out.println(Math.ceil(-2.3)); System.out.println(Math.floor(-2.3)); System.out.println(Math.abs(-7)); System.out.println(Math.max(3, Math.min(8, 5)));
Output
8.0 12.0 5 -4 4 -2.0 -3.0 7 5
Math.pow and Math.sqrt always return double, so they print 8.0 and 12.0 and never 8 or 12. Math.round(-4.5) is the line that catches everyone: rounding in Java always goes towards positive infinity on a tie, so −4.5 becomes −4, not −5. And ceil means “towards the ceiling” — for a negative number that is the smaller magnitude, −2.0.
Math.round(double) returns a long; Math.round(float) returns an int. Either way the printed value has no decimal point. Every other Math method named in the syllabus — pow, sqrt, cbrt, ceil, floor and random — returns double. Math.random() in particular hands back a double that is at least 0.0 and always below 1.0, which is why it is almost always wrapped in a cast such as (int)(Math.random() * 100). Math.abs, Math.max and Math.min return whatever type you gave them.
System.out.println(Character.isLetterOrDigit('#'));
System.out.println(Character.toUpperCase('p'));
System.out.println(Character.isWhitespace(' '));
System.out.println(Integer.parseInt("25") + 5);
System.out.println("25" + 5);
System.out.println(Double.parseDouble("2.5") * 2);
int a = Integer.valueOf("7");
System.out.println(a + 3);
Output
false P true 30 255 5.0 10
Lines four and five are the whole lesson. Integer.parseInt("25") converts the text into the number 25, so + 5 is arithmetic and gives 30. Without the conversion, "25" + 5 is string concatenation and gives 255. The + operator behaves completely differently depending on whether either side is a String.
Integer.valueOf("7") hands back an Integer object, which unboxes automatically into the int variable a. That silent conversion is what the syllabus calls unboxing; going the other way is autoboxing.
Writing 2.5 as the answer to Math.pow(5, 2)-style questions is rare, but writing 25 instead of 25.0 is extremely common. If the method returns double, the printed answer must carry .0. Half a mark, every time.
Errors the Examiner Shows You On Purpose
Some questions do not ask for output at all. They ask what is wrong, or what the program will do when it is run. There are two families, and confusing them costs marks: a compile-time error means the program never runs, while a runtime error means it starts, prints whatever it managed to print, and then stops abruptly.
public class Q19
{
public static void main(String args[])
{
int total;
for (int i = 1; i <= 3; i++)
{
total = total + i;
}
System.out.println(total);
}
}
What actually happens
Q19.java:8: error: variable total might not have been initialized
total = total + i;
^
Q19.java:10: error: variable total might not have been initialized
System.out.println(total);
^
2 errors
Nothing is printed, because nothing runs. A local variable gets no default value in Java — unlike an instance variable, which would quietly start at 0. The fix is one character: int total = 0;.
int a[] = new int[4];
for (int i = 0; i <= 4; i++)
{
a[i] = i * 10;
}
System.out.println(a[3]);
What actually happens
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
This program compiles perfectly. The trouble only appears on the fifth pass of the loop, when i is 4 and the array’s last legal index is 3. The single character at fault is the = in i <= 4; it should read i < a.length. Note that a[3] is never printed — the exception ends the program first.
If the question says “name the type of error”, answer in three parts: the family (compile-time, runtime or logical), the specific name, and the line. “Runtime error — ArrayIndexOutOfBoundsException, at the assignment inside the loop” earns the whole mark; “array error” earns part of it.
Where These Questions Sit in the Paper
For Class X Computer Applications the Council prescribes one written paper of two hours carrying 100 marks, together with an Internal Assessment of 100 marks built from a minimum of twenty laboratory assignments across the year. Note that this is different from most of your other subjects, where the written paper is 80 marks and internal assessment is 20 — Computer Applications splits the two halves evenly.
In the recent pattern the written paper divides into Section A, worth 40 marks, where every question is compulsory, and Section B, worth 60 marks, where you answer any four of the questions set. Output and dry-run questions live overwhelmingly in Section A, arriving as short one-mark and two-mark items, and they are the single most reliable source of marks in the whole paper because the answer is a fact rather than an opinion. Section B is where the full programs live, but even there a sub-question will often ask you to state the output of a fragment.
Everything in this chapter draws on the Class X syllabus as a whole: Unit 1 revises the Class IX material on data types, operators, conditionals and loops, and Units 2 to 8 add classes, user-defined methods, constructors, library classes, encapsulation, arrays and string handling. An output question is free to combine any of them in five lines, which is exactly why cross-unit revision beats unit-by-unit revision at this stage of the year.
Write the output exactly as the machine would, one item per line, with no extra words. If the program prints 8.0, write 8.0. Adding “the answer is” or converting it to 8 can cost you the mark even when your reasoning was right.
Practice Worksheet
Twelve questions, all in the style the paper actually uses. Trace each one on paper first — with a real table, not in your head — and only then open the answer. Every output below is the genuine output of the program on a real machine.
1. Give the output.
int p = 25, q = 4; System.out.println(p % q + q % p); System.out.println(p / q + p % q); System.out.println(2 + 3 + "Java" + 2 + 3);
Show Answer
5 7 5Java23
25 % 4 is 1 and 4 % 25 is 4, giving 5. Then 25 / 4 is 6 and 25 % 4 is 1, giving 7. In the third line Java works left to right: 2 + 3 are still numbers so they add to 5, but once a String joins in everything after it is concatenation, so 2 and 3 are stuck on as characters.
2. Give the output.
int a = 6;
int b = a-- - --a;
System.out.println("a = " + a);
System.out.println("b = " + b);
int c = 3;
c += c++ * 2;
System.out.println("c = " + c);
Show Answer
a = 4 b = 2 c = 9
a-- hands over 6 and drops a to 5; --a drops it to 4 and hands over 4; so b is 6 − 4 = 2 with a resting at 4. For c: c++ supplies the old value 3, so the right-hand side is 3 × 2 = 6, and c += 6 assigns 3 + 6 = 9 — the increment to 4 is overwritten by the assignment.
3. Give the output.
char x = 'g'; System.out.println((char)(x - 32)); System.out.println(x + 'a'); System.out.println((int)(9.99)); System.out.println((float)25 / 2);
Show Answer
G 200 9 12.5
'g' is 103; subtracting 32 gives 71, and casting back to char gives G. Adding two characters adds their Unicode values, 103 + 97 = 200, and the result is an int. Casting a double to int truncates, so 9.99 becomes 9 — never 10.
4. Give the output and state how many lines are printed.
char g = 'B';
switch (g)
{
case 'A': System.out.println("Excellent");
break;
case 'B': System.out.println("Very Good");
case 'C': System.out.println("Good");
default: System.out.println("Keep Working");
}
Show Answer
Very Good Good Keep Working
Three lines. Only case 'A' has a break, so entering at case 'B' falls through case 'C' and on into default. The default block is not reserved for unmatched values — it is simply the last stop on the road.
5. Give the output.
String s = "COMPUTER APPLICATIONS";
System.out.println(s.indexOf('P'));
System.out.println(s.substring(9, 12));
System.out.println(s.replace('A', '@'));
System.out.println(s.toLowerCase().charAt(4));
System.out.println(s.endsWith("ONS"));
Show Answer
3 APP COMPUTER @PPLIC@TIONS u true
indexOf reports the first P, at index 3 in COMP. substring(9, 12) takes indices 9, 10 and 11 and stops before 12. replace changes every matching character, not just the first. And toLowerCase() returns a new string, leaving s itself untouched.
6. How many times does the loop body run, and what is printed?
int i = 1, n = 0;
while (i < 40)
{
i = i * 3;
n++;
}
System.out.println("i = " + i);
System.out.println("n = " + n);
Show Answer
i = 81 n = 4
Four times. i takes the values 3, 9, 27 and 81; the test fails on the fifth check because 81 is not less than 40. The variable ends beyond the boundary, which is exactly what the question is testing.
7. Give the output.
for (int i = 5; i >= 1; i--)
{
for (int j = 5; j >= i; j--)
{
System.out.print("*");
}
System.out.println();
}
Show Answer
* ** *** **** *****
Both loops count downwards, yet the triangle grows. The inner loop runs from 5 down to i, so it executes 6 - i times: once when i is 5, five times when i is 1.
8. Give the output.
int a[] = {12, 7, 25, 3, 18};
int mx = a[0], pos = 0;
for (int i = 1; i < a.length; i++)
{
if (a[i] > mx)
{
mx = a[i];
pos = i;
}
}
System.out.println("mx = " + mx);
System.out.println("pos = " + pos);
System.out.println(a[pos - 1] + a[pos + 1]);
Show Answer
mx = 25 pos = 2 10
The largest value is 25 at index 2. The final line adds its neighbours, a[1] which is 7 and a[3] which is 3, giving 10.
9. Give the output.
int m[][] = {{2, 4, 6}, {1, 3, 5}, {7, 8, 9}};
int r = 0, c = 0;
for (int i = 0; i < 3; i++)
{
r = r + m[1][i];
c = c + m[i][2];
}
System.out.println("Row 1 sum = " + r);
System.out.println("Column 2 sum = " + c);
System.out.println(m[0][0] * m[2][2]);
Show Answer
Row 1 sum = 9 Column 2 sum = 20 18
Holding the first subscript fixed at 1 walks along a row: 1 + 3 + 5 = 9. Holding the second subscript fixed at 2 walks down a column: 6 + 5 + 9 = 20. Remember that row 1 is the second row, because counting starts at 0.
10. Give the output.
static int f(int n) { return n * n; }
static double f(double n) { return n / 2; }
static String f(String n) { return n + n; }
System.out.println(f(4));
System.out.println(f(4.0));
System.out.println(f("ab"));
System.out.println(f('b'));
Show Answer
16 2.0 abab 9604
The last call is the trap. There is no f(char), so 'b' widens to its Unicode value 98 and the int version runs, giving 98 × 98 = 9604. Java prefers the smallest widening it can get away with, which is why int wins over double.
11. Give the output.
System.out.println(Math.sqrt(Math.pow(3, 2) + Math.pow(4, 2)));
System.out.println(Math.round(7.5) + Math.round(-7.5));
System.out.println(Math.max(Math.abs(-9), Math.ceil(8.1)));
System.out.println(Character.isUpperCase('k'));
Show Answer
5.0 1 9.0 false
The first line is 3-4-5 in disguise, and it prints 5.0 rather than 5 because Math.sqrt returns a double. The second gives 8 + (−7) = 1, since ties always round towards positive infinity. In the third, Math.abs(-9) is the int 9 while Math.ceil(8.1) is the double 9.0, so the compiler binds the call to the Math.max(double, double) version, the int is promoted, and the answer prints as 9.0.
12. Name the error, state the type, and give everything the program manages to print before it stops.
String s = "Kaizen";
for (int i = 0; i <= s.length(); i++)
{
System.out.print(s.charAt(i));
}
Show Answer
Kaizen
It prints Kaizen and then throws a runtime error — a StringIndexOutOfBoundsException for index 6. The string has six characters occupying indices 0 to 5, so i <= s.length() asks for one character too many. The correction is i < s.length(). Because print was used rather than println, all six characters appear on a single line before the crash.
In the exam, an answer like this needs both halves to score full marks: write out everything the program managed to print, and then name the exception. “Kaizen, then a StringIndexOutOfBoundsException” is a complete answer; either half on its own is not.
Aage kya? Ab tak aapne har unit alag-alag padha tha; is chapter ne unhe ek saath jodkar dikhaya ki paper mein sawaal kaise aata hai. Agar kisi section par haath rukta hai, toh seedha us chapter par wapas jaaiye — Operators and Expressions, Iterative Constructs, Arrays ya String Handling — aur phir yahan lautkar wahi sawaal dobara try kijiye.
Kaizen: Aaj se roz sirf ek output question lijiye — paanch line ka, isse zyada nahi — aur use trace table banakar hal kijiye. Pehle apna answer likhiye, phir answer kholiye. Do hafte mein aapke paas saath sawaal ka tajurba ho jaayega, aur Section A ke ye chhote marks aapke liye sabse pakke marks ban jaayenge.
