Docsity

Computer (JAVA) Output Question bank for class 10 ICSE, Exercises of Computer science

Toggle sidebar
bg1
System.out.println(
1 + 2 + "3" + 4);
// what is printed?
> 33 ... or 3345 ?
ICSE · CLASS 10 · COMPUTER APPLICATIONS
JAVA
30 OUTPUT PROBLEMS
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.
30 Problems Verified Output Dry Run Full Syllabus
Compiled by Anirban Giri
Tap the next page to jump to any section.
// cover the output, predict it, then check
Question Bank by Anirban Giri
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Computer (JAVA) Output Question bank for class 10 ICSE and more Exercises Computer science in PDF only on Docsity!

System.out.println( 1 + 2 + "3" + 4);

// what is printed?

> 33 ... or 3345?

ICSE · CLASS 10 · COMPUTER APPLICATIONS

JAVA

30 OUTPUT PROBLEMS

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.

30 Problems Verified Output Dry Run Full Syllabus

Compiled by Anirban Giri

Tap the next page to jump to any section.

// cover the output, predict it, then check

Question Bank 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

★ QUICK TIP

Cover the OUTPUT box with your hand, work through the

code yourself, then check. Mark a problem wrong if you

only guessed.

⚠ EXAM ALERT

Spaces matter: print(i + " ") leaves a space after each

value, while println() starts a new line.

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

a / b is integer division = 3; a % b is the remainder = 1; the cast makes

(double) a / b = 10.0 / 3 = 3.3333333333333335.

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

round(3.5) = 4; round(-3.5) = -3 (the .5 is rounded towards the larger value);

ceil(-2.3) = -2.0; floor(-2.3) = -3.0.

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

9 Odd

DRY RUN

a > b is false, so m = b = 9. Then 9 % 2 = 1, which is not 0, so the second

ternary gives "Odd".

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

C

DRY RUN

72 fails marks >= 90 and marks >= 75 but passes marks >= 60, so "C" is

printed and the rest is skipped.

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

Three Four

DRY RUN

n = 3 matches case 3. There is no break there, so control falls into case 4,

and the break after it stops the switch.

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

Inner first: abs(-9) = 9 and min(4, 6) = 4. Then max(9, 4) = 9. cbrt(27) = 3.0 (a

double).

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

s = 1 + 4 + 9 + 16 + 25 = 55.

13

Decreasing step

for (int i = 10; i > 0; i -= 3) System.out.print(i + " ");

OUTPUT

DRY RUN

i takes the values 10, 7, 4 and 1. Then i becomes -2, the condition fails and

the loop ends.

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

The digits are removed from the right: 4, 3, 2, 1. Their sum is 10.

15

do-while runs once

int i = 5; do { System.out.print(i + " "); i--; } while (i > 5);

OUTPUT

DRY RUN

The body runs once before the test and prints 5. Then i = 4 and 4 > 5 is false,

so the loop stops.

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

sum(2, 3) matches (int, int) and gives 5. sum(2.5, 3) matches (double, double)

with 3 widened to 3.0, giving 5.5.

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

c is a static variable, so one copy is shared by all objects. Each constructor

call does c++, three times in all.

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

X 7 5

DRY RUN

new A() runs the no-argument constructor (X). new A(7) and new A(2 + 3) run

the int constructor and print 7 and 5.

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

i takes 0, 2 and 4, so the elements 5, 8 and 9 are added: 22.

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

The loop starts at the last index (3) and moves down to 0, printing the

elements backwards.

25

Array reference

int a[] = {1, 2, 3}; int b[] = a; b[0] = 99; System.out.println(a[0] + " " + b[0]);

OUTPUT

DRY RUN

b = a copies the reference, not the array. Both names point to the same array,

so changing b[0] also changes a[0].

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

Only the cells where i == j are added: m[0][0] = 1 and m[1][1] = 5, so s = 6.

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

puterC

DRY RUN

substring(3) = "puter"; charAt(0) = 'C'; indexOf('t') = 5; length() = 8. Joined

together: puterC58.

28

== and equals()

String a = "abc"; String b = new String("abc"); System.out.println((a == b) + " " + a.equals(b));

OUTPUT

false true

DRY RUN

== compares references: a and b are different objects, so false. equals()

compares the text, which is the same, so true.

Question Bank by Anirban Giri