ICSE · Class 9 · Computer Applications
Class 9 Input in Java Mock Test (ICSE)
6 practice questions from Input in Java, in the style of your board paper, each with a worked answer and the marks for every step. Then take a full chapter test on paper, snap the pages and get every step marked.
Topics in Input in Java
- Initialisation and parameters
- Scanner class methods
- Types of errors: syntax, runtime and logical
- Comments in a program
Input in Java practice questions with answers
- Q1Multiple choice1 markWhich of the following methods of the Scanner class is used to check whether the upcoming token in the input stream is a valid integer value?
- (a)checkInt()
- (b)nextIntCheck()
- (c)isNextInt()
- (d)hasNextInt()
Show answer and step marks
- Correct option: (d) +1
Answer: (d) - Q2Multiple choice1 markWhich of the following symbols is used to enclose multi-line comments in Java?
- (a)//
- (b)/* ... */
- (c)# ... #
- (d)<!-- ... -->
Show answer and step marks
- Correct option: (b)+1
Answer: (b) - Q3Multiple choice1 markA program is written to record the number of power points an electrician counts while wiring a house. Which Scanner method should be used to read this count as an int?
- (a)next()
- (b)nextLine()
- (c)nextInt()
- (d)nextDouble()
Show answer and step marks
- Correct option: (c) nextInt()+1
Answer: (c) - Q4Short answer2 marksState any two differences between the `next()` and `nextLine()` methods of the `Scanner` class in Java.
Show answer and step marks
- First difference, any one of: `next()` reads a single token up to the first whitespace, whereas `nextLine()` reads the whole line including spaces; `next()` leaves the line separator (newline) in the input, whereas `nextLine()` consumes it and moves to the next line; `next()` skips leading whitespace and never returns an empty string, whereas `nextLine()` can return an empty string; or any other valid difference.+1
- Second difference, a different one from the same list.+1
Answer: Any two of: `next()` reads one token up to whitespace while `nextLine()` reads the whole line including spaces; `next()` leaves the newline in the input while `nextLine()` consumes it; `next()` never returns an empty string while `nextLine()` can. - Q5Short answer3 marksA student writes a Java program that reads two integers a and b with Scanner and prints the result of a / b. During testing, three different observations are recorded. (a) The compiler refuses to run the program and points to the line System.out.println(a / b), where the statement is not ended correctly. Name the type of this error. [1] (b) The program compiles and runs, but when the value 0 is entered for b, it stops with an ArithmeticException. Name the type of this error. [1] (c) In another version, the statement is written as System.out.println(a × b). The program runs without any message, but for a = 12 and b = 4 it prints 48 when it should print 3. Name the type of this error. [1]
Show answer and step marks
- (a) The statement is missing its terminating semicolon, which the compiler detects before running: syntax error.+1
- (b) The program compiles, so the fault appears only while running with b = 0: runtime error.+1
- (c) The program runs with no error message but gives a wrong result because the wrong operator is used: logical error.+1
Answer: (a) Syntax error; (b) Runtime error; (c) Logical error - Q6Long answer4 marksConsider the following Java code snippet designed to input 5 scores, calculate their average, and compute a remainder: import java.util.Scanner; public class TestScores { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0; int count = 0; System.out.println("Enter 5 scores:"); for(int i = 1; i ≤ 5; i++) { int score = sc.nextInt(); sum = sum + score; count++; } double average = sum / 4; int remainder = 10 % 0; System.out.println("Average: " + average); System.out.println("Remainder: " + remainder) } } (a) Identify and explain three distinct errors (one syntax error, one runtime error, and one logical error) present in the above code snippet. State where each error occurs and how to correct it. [3 marks] (b) Explain the fundamental difference in how Java handles a runtime error versus a logical error during program execution. [1 mark]
Show answer and step marks
- Syntax error: Missing semicolon at the end of the statement `System.out.println("Remainder: " + remainder)` on the final line. Correction: Add a semicolon `;` at the end of the statement.+1
- Runtime error: `int remainder = 10 % 0;` causes an `ArithmeticException` during execution because modulo by zero is mathematically undefined. Correction: Change the divisor from `0` to a non-zero integer.+1
- Logical error (accept either): `double average = sum / 4;` divides the sum of 5 scores by `4` instead of `5` (`count`), and/or integer division truncates the average before it is stored in the `double`. Correction: `double average = (double) sum / count;`.+1
- Difference: A runtime error halts execution and throws an exception, crashing the program, whereas a logical error allows the program to run and complete successfully without throwing exceptions, but yields incorrect output due to flawed algorithm design.+1
Answer: (a) Syntax error: missing semicolon after `System.out.println("Remainder: " + remainder)`; add `;`. Runtime error: `10 % 0` throws an `ArithmeticException`; use a non-zero divisor. Logical error: `sum / 4` divides by 4 instead of 5 (and integer division truncates); use `(double) sum / count`. (b) A runtime error stops the program with an exception, while a logical error lets it run to completion but gives wrong output.
Take a full Input in Java test
PrepitUp writes a fresh paper on this chapter at the length you pick. Write it on paper with a timer, photograph the pages, and see the marks for every step and what to practise next.
- 15 min10 marks
- 30 min20 marks
- 1.5 hr40 marks
- 3 hr80 marks
Get your Input in Java answers marked
Sign in, pick this chapter and a length, write the test on paper and get every step marked.
Sign in to take this test