0% found this document useful (0 votes)
2 views6 pages

Java Output Answers

Uploaded by

tanki58589
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Java Output Answers

Uploaded by

tanki58589
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSC103 – Java Output Questions

Answers from All Past Papers (SP24 · FA24 · SP25 · FA25)

Terminal Exam – FALL 2025

Question #1 – Show the output / errors of the following code snippets (10 marks)

Snippet 1
int sum = 0, y = 0;
do {
for(int x=1; x<5; x++) {
sum += x;
y++;
}
} while(y<3);
[Link](sum);

Output: 30
Explanation: The do-while runs while y<3. Inner for: x goes 1,2,3,4 → sum += 10, y += 4. After 1st iteration y=4,
which is not < 3 so loop exits immediately after first run. Wait — do-while checks AFTER first run. After 1st pass:
sum=10, y=4. y<3 is false → loop stops. sum = 10. Actually re-check: inner for adds x=1+2+3+4=10, y increments 4
times → y=4 after first do pass. Condition y<3 → false. Output: 10.
Output: 10
Explanation: Do-while body runs once: inner for runs x=1..4, sum=1+2+3+4=10, y=4. y<3 is false → exits. Prints 10.

Snippet 2
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
[Link](table[i][j]+" ");
break;
}
}
// table = {{10,20,30},{40,50,60},{70,80,90}}

Output: 10 40 70
Explanation: The inner loop always breaks after j=0, so only the first element of each row is printed.

Snippet 3
String s = "Hi";
for(int i=1; i<10; i++) s+="s";
[Link](i);

Output: Compile Error – variable i cannot be resolved outside the for-loop scope.

Snippet 4
int i=0;
while(i++<5) {
[Link](i);
}

Output: 1 2 3 4 5
Explanation: Post-increment: condition checks i then increments. i=0 → prints 1; i=1 → prints 2 … i=4 → prints 5; i=5
→ 5<5 false, stops.

Snippet 5 – isNumeric("123")
public static boolean isNumeric(String token) {
try {
double num = [Link](token);
return true;
} catch(NumberFormatException ex) {
return false;
}
}

Output: true
Explanation: "123" is a valid double, so parseDouble succeeds and returns true.

Snippet 6
String str = "Java_is_fun";
for(int i=0; i if("aeiou".indexOf([Link](i)) > -1) {
str = [Link](0,i)+"*";
}
}
[Link](str);

Output: J*
Explanation: When first vowel 'a' is found at i=1, str becomes "J*" (length 2). Loop continues i=2 which is >= new
length 2, ends. Prints J*.

Snippet 7 – recursive r(int m)


public static String r(int m) {
if(m > 0) return "+" + r(m-1);
else return "";
}
public static String s(int m, int n) {
if(n > 0) return "-" + s(m, n-1) + ".";
else return r(2*m);
}
[Link](s(3,2));

Output: --.++++++
Explanation: s(3,2) = "-" + s(3,1) + "."; s(3,1) = "-" + s(3,0) + "."; s(3,0) = r(6) = "++++++". Building back: s(3,1) = "-" +
"++++++" + "." = "-++++++."; s(3,2) = "-" + "-++++++." + "." = "--++++++.."
Output: --++++++..

Snippet 8 – 2D sum array


int sum[][] = new int[4][4];
for(int k=0; k<4; k++) sum[k][0] = 1;
for(int k=0; k<4; k++) sum[0][k] = 1;
for(int m=1; m<4; m++)
for(int n=1; n<4; n++)
sum[m][n] = sum[m-1][n-1] + sum[m-1][n-1];
for(int n=1; n<4; n++)
sum[0][n] = sum[0][n-1] + sum[0][n-1];
[Link](sum[3][n] + " ");

Output: Compile Error – variable n is out of scope outside the for loop.

Snippet 9 – which catch block executes?


try { method(); }
catch(ArithmeticException ex) { print("ArithmeticException"); }
catch(RuntimeException ex) { print("RuntimeException"); }
catch(Exception e) { print("Exception"); }

static void method() throws Exception {


[Link](1/0);
}

Output: ArithmeticException
Explanation: 1/0 throws ArithmeticException. The first matching catch block executes. ArithmeticException extends
RuntimeException, but first catch matches exactly.

Snippet 10 – which catch block executes?


try {
int[] list = new int[10];
[Link]("list[10] is " + list[10]);
}
catch(ArithmeticException ex) { print("ArithmeticException"); }
catch(RuntimeException ex) { print("RuntimeException"); }
catch(ArrayIndexOutOfBoundsException) { print("ArrayIndexOutOfBoundsException"); }
catch(Exception e) { print("Exception"); }

Output: RuntimeException
Explanation: list[10] throws ArrayIndexOutOfBoundsException. ArithmeticException doesn't match.
RuntimeException DOES match (AIOOBE extends RuntimeException), and it appears before the AIOOBE catch. So
RuntimeException block executes.

Terminal Exam – SPRING 2025

Question #1 – Show the output of the following code snippets (5 marks)

Snippet i
int base=4, exp=4, mod=6;
int result = (int) [Link](base, exp) % mod;
[Link]("Result: " + result);

Output: Result: 4
Explanation: [Link](4,4) = 256. 256 % 6 = 4. (256 = 42*6 + 4)

Snippet ii
int x = 18, y = 5;
boolean check = (x % y == 3) && (x / y > 3);
[Link]("Check: " + check);

Output: Check: true


Explanation: 18%5=3 (true) && 18/5=3 which is NOT > 3 (false). → false. Wait: 18/5 in integer = 3, not > 3. So: true
&& false = false.
Output: Check: false

Snippet iii
int roll = 7;
String name = "Alice";
double gpa = 3.75;
[Link]("Roll:\t%Sd\tName:\tX%S\n#s\tGPA:\t%X.2f%n", roll, name, gpa);

Output: Compile Error – invalid format specifiers %S, X%, %X.


Explanation: Valid specifiers are %d, %s, %.2f etc. The format string in the image uses incorrect characters.

Snippet iv
float f = 300.75f;
byte b = (byte) f;
[Link]("Byte value: " + b);

Output: Byte value: 44


Explanation: float 300.75 cast to byte: 300 % 256 = 44.

Snippet v
int m = 4;
int n = 6;
int res = m++ + --n + m + n--;
[Link]("res: " + res);
[Link]("m: "+m+", n: "+n);

Output: res: 24 | m: 5, n: 4
Explanation: m++=4 (then m=5), --n=5 (n becomes 5), m=5, n--=5 (then n=4). res = 4+5+5+5 = 19. Then m=5, n=4.
Re-check: m++=4(m→5), --n: n=6→5, m is now 5, n--=5(n→4). res=4+5+5+5=19.
Output: res: 19 | m: 5, n: 4

Snippet vi
String str = " 456 ";
int num = [Link]([Link]());
[Link]("String converted to int: " + num);

Output: String converted to int: 456


Explanation: [Link]() removes leading/trailing spaces → "456". parseInt converts to 456.

Terminal Exam – FALL 2024

Question #1 – Show the output of the following code snippets (6 marks)

Snippet i
int base=3, exp=10, mod=5;
int result = (int) [Link](base, exp) % mod;
[Link]("Result: " + result);

Output: Result: 4
Explanation: [Link](3,10)=59049. 59049 % 5 = 4.

Snippet ii
int x=100, y=30, z=7;
int result = x / y * z % y + z;
[Link]("Result: " + result);

Output: Result: 28
Explanation: L-to-R: 100/30=3, 3*7=21, 21%30=21, 21+7=28.

Snippet iii
int id=101;
String name="John";
double score=89.657;
[Link]("ID: %3d | Name: %10s | Score: %.1f%n", id, name, score);

Output: ID: 101 | Name: John | Score: 89.7


Explanation: %3d = 101 (fits), %10s = right-padded 'John' in 10 chars, %.1f rounds 89.657 to 89.7.

Snippet iv
short value = 32767;
value++;
[Link]("Short value: " + value);

Output: Short value: -32768


Explanation: Short max is 32767. Incrementing overflows to -32768 (integer overflow wrap-around).

Snippet v
int u=3, v=7, w=18;
int result = ++u * v-- - (w++ + --v) + (u++ - w--)* ++v;
[Link]("Result: " + result);
[Link]("Values after expression: u="+u+", v="+v+", w="+w);

Output: Result: -22 | u=5, v=5, w=18


Explanation: Step by step: ++u=4(u=4), v--=7(v=6), w++=18(w=19), --v=5(v=5), u++=4(u=5), w--=19(w=18),
++v=6(v=6). result = 4*7 - (18+5) + (4-19)*6 = 28 - 23 + (-15)*6 = 28-23-90 = -85. This expression is complex; exact
value depends on strict Java left-to-right evaluation. Approximate: -85

Snippet vi
String str = " 456 ";
int num = [Link]([Link]());
[Link]("String converted to int: " + num);

Output: String converted to int: 456


Explanation: Same as SP25 snippet vi – trim removes spaces, parseInt gives 456.

Terminal Exam – SPRING 2024

Question-1 – Write the output of the following code snippets (4 marks)

Snippet i
char c1='D'; // ASCII 68
char c2='A'; // ASCII 65
int AVSum = c1 + c2; // 133
[Link]("Character 1: %c, ASCII Value: %d%n", c1,(int)c1);
[Link]("Character 2: %c, ASCII Value: %d%n", c2,(int)c2);
[Link]("Sum of Characters as ASCII Value: %d%n", AVSum);
Output:
Character 1: D, ASCII Value: 68
Character 2: A, ASCII Value: 65
Sum of Characters as ASCII Value: 133
Explanation: D=68, A=65, sum=133. %c prints character, %d prints integer value.

Snippet ii
double a=10.5; int b=5; int c=6;
[Link](a+b/b*c-b);

Output: 11.5
Explanation: b/b=1 (int), 1*c=6, a+6=16.5, 16.5-5=11.5.

Snippet iii
int x=10;
int y=5;
int result = x % (x-y);
[Link]("Result: " + result);

Output: Result: 0
Explanation: x-y=5, x%5 = 10%5 = 0.

Snippet iv
double a=2.0; double b=7.8;
double c = ++a * b++ + ++b;
[Link](a);
[Link](b);
[Link](c--);

Output:
3.0
9.8
30.0
Explanation: ++a → a=3.0; b++=7.8 (b becomes 8.8); ++b → b=9.8. c = 3.0*7.8 + 9.8 = 23.4+9.8 = 33.2. println(c--)
prints 33.2 then c becomes 32.2. a=3.0, b=9.8 prints correctly.
Output:
3.0
9.8
33.2

Note: Always trace operator precedence carefully in exams. Pre-increment/decrement (++x) evaluates before use;
post (x++) evaluates after.

You might also like