





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






System.out.println( 1 + 2 + "3" + 4);
// what is printed?
> 33 ... or 3345?
PREDICT THE OUTPUT + DRY RUN
Short code snippets, the verified output and a step-by-step dry run for every problem. All outputs tested in Java.
Compiled by Anirban Giri
Contents
Tap a section to jump to it. Every page has a CONTENTS link at the bottom right.
1 Operators, Casting & Expressions (Problems 1 to 6) p. 2
2 Math Library & Decisions (Problems 7 to 11) p. 4
3 Loops & Patterns (Problems 12 to 18) p. 5
4 Classes, Methods & Constructors (Problems 19 to 22) p. 6
5 Arrays (Problems 23 to 26) p. 7
6 Strings & Wrapper Classes (Problems 27 to 30) p. 8
How to dry-run a program in 4 steps
- List the variables and write their starting values. - Go line by line in the order Java runs it. Update the value of a variable every time it changes. - Note what is printed at each println or print (print stays on the same line). - Check the data types : int division, char arithmetic and String concatenation change the answer.
Output rules cheat strip
Rule Example Result
Integer division drops the fraction 7 / 2 3
% gives the remainder 7 % 2 1
Post-increment uses, then adds x = 5; y = x++; y = 5, x = 6
Pre-increment adds, then uses x = 5; y = ++x; y = 6, x = 6
Numbers add until a String appears 1 + 2 + "a" and "a" + 1 + 2 3a and a
char + int is an int 'A' + 1 66
Math.pow and sqrt return double; round returns long Math.pow(2, 3), Math.round(2.5)^ 8.0, 3
== compares references; equals() compares text new String("a") == "a" false
1 Operators, Casting & Expressions
1
Division, remainder and casting
int a = 10, b = 3; System.out.println(a / b); System.out.println(a % b); System.out.println((double) a / b);
OUTPUT
DRY RUN
Question Bank by Anirban Giri
2 Math Library & Decisions
7
Math round, ceil and floor
System.out.println(Math.round(3.5)); System.out.println(Math.round(-3.5)); System.out.println(Math.ceil(-2.3)); System.out.println(Math.floor(-2.3));
OUTPUT
DRY RUN
8
Ternary operator
int a = 7, b = 9; int m = (a > b)? a : b; String r = (m % 2 == 0)? "Even" : "Odd"; System.out.println(m + " " + r);
OUTPUT
DRY RUN
9
if-else ladder
int marks = 72; if (marks >= 90) System.out.println("A"); else if (marks >= 75) System.out.println("B"); else if (marks >= 60) System.out.println("C"); else System.out.println("D");
OUTPUT
DRY RUN
10
switch fall-through
int n = 3; switch (n) { case 1: System.out.print("One "); case 3: System.out.print("Three "); case 4: System.out.print("Four "); break; case 5: System.out.print("Five "); default: System.out.print("None"); }
OUTPUT
DRY RUN
Question Bank by Anirban Giri
11
Nested Math methods
System.out.println(Math.max(Math.abs(-9), Math.min(4, 6)) + " " + Math.cbrt(27));
OUTPUT
DRY RUN
3 Loops & Patterns
12
Sum of squares
int s = 0; for (int i = 1; i <= 5; i++) s += i * i; System.out.println(s);
OUTPUT
DRY RUN
13
Decreasing step
for (int i = 10; i > 0; i -= 3) System.out.print(i + " ");
OUTPUT
DRY RUN
14
Digit sum with while
int n = 1234, s = 0; while (n > 0) { s = s + n % 10; n = n / 10; } System.out.println(s);
OUTPUT
DRY RUN
15
do-while runs once
int i = 5; do { System.out.print(i + " "); i--; } while (i > 5);
OUTPUT
DRY RUN
Question Bank by Anirban Giri
20
Method overloading
int sum(int a, int b) { return a + b; } double sum(double a, double b) { return a + b; } // in main, with object ob: System.out.println(ob.sum(2, 3)); System.out.println(ob.sum(2.5, 3));
OUTPUT
DRY RUN
21
static variable counter
class Count { static int c = 0; Count() { c++; } } // in main: new Count(); new Count(); new Count(); System.out.println(Count.c);
OUTPUT
DRY RUN
22
Constructor overloading
class A { A() { System.out.print("X "); } A(int n) { System.out.print(n + " "); } } // in main: new A(); new A(7); new A(2 + 3);
OUTPUT
DRY RUN
5 Arrays
23
Alternate elements
int a[] = {5, 2, 8, 1, 9}; int s = 0; for (int i = 0; i < a.length; i += 2) s += a[i]; System.out.println(s);
OUTPUT
DRY RUN
Question Bank by Anirban Giri
24
Reverse traversal
int a[] = {5, 2, 8, 1}; for (int i = a.length - 1; i >= 0; i--) System.out.print(a[i] + " ");
OUTPUT
DRY RUN
25
Array reference
int a[] = {1, 2, 3}; int b[] = a; b[0] = 99; System.out.println(a[0] + " " + b[0]);
OUTPUT
DRY RUN
26
Diagonal of a 2D array
int m[][] = {{1, 2, 3}, {4, 5, 6}}; int s = 0; for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) if (i == j) s += m[i][j]; System.out.println(s);
OUTPUT
DRY RUN
6 Strings & Wrapper Classes
27
String methods together
String s = "Computer"; System.out.println(s.substring(3) + s.charAt(0) + s.indexOf('t') + s.length());
OUTPUT
DRY RUN
28
== and equals()
String a = "abc"; String b = new String("abc"); System.out.println((a == b) + " " + a.equals(b));
OUTPUT
DRY RUN
Question Bank by Anirban Giri