LIWA

Data processing in Java

ICSE Class 9 · Computer Applications · Chapter 6

What this chapter covers

Key points

Questions answered

Define initialisation. Give one example of static and one of dynamic initialisation.
Initialisation is assigning a starting value to a variable when it is declared or before it is first used. Static: `int days = 30;`. Dynamic: `int total = price * qty;`, where the value is calculated at run time.
State one advantage of input through parameters over initialisation.
The same compiled program can be run with different data, entered in the dialog box at the time of execution, without editing the source code.
What is a package? Name the package that contains the Scanner class and the package that is imported by default.
A package is a named group of related classes. `Scanner` belongs to `java.util`; `java.lang` is imported into every program automatically.
Differentiate between `import java.util.Scanner;` and `import java.util.*;`.
The first imports only the Scanner class; the second imports all the classes of the `java.util` package. Both let you use the short name `Scanner`.
Write the statement that creates a Scanner object named `in`, and explain the role of `System.in`.
`Scanner in = new Scanner(System.in);`. `System.in` is the standard input stream connected to the keyboard, so the object reads what the user types.
Write the Scanner statements to read: (a) a 10-digit mobile number, (b) the weight of a parcel in kilograms, (c) the name of a city with a space in it, (d) a single character.
(a) `long mob = sc.nextLong();` (b) `double w = sc.nextDouble();` (c) `String city = sc.nextLine();` (d) `char ch = sc.next().charAt(0);`.
Differentiate between `next()` and `nextLine()`.
`next()` reads a single word and stops at the first white space; `nextLine()` reads the whole line including spaces, up to the Enter key.
Why does the Scanner class need `next().charAt(0)` to read a character?
The Scanner class has no method that reads a character, so a word is read with `next()` and its first character, at position 0, is taken with `charAt(0)`.

Past board exam questions

ICSE 2027 sample paper · 1 mark

Roy wanted to buy the latest mobile phone. He approached a service provider to get a sim card with a 10-digit mobile number. What is the Scanner class method to accept the mobile number?

  1. Sc.next( )
  2. Sc.nextInt( )
  3. Sc.nextLong( )
  4. Sc.next( ).charAt(0)
Show answer
Sc.nextLong( )

ICSE 2027 sample paper · 1 mark

Fill in the blank to complete the following Java statement: Scanner SC = __________ Scanner(System.in);

  1. Scanner
  2. dot
  3. class
  4. new
Show answer
new