Core Java - Interview Questions and Answers for 'Scanner' - 6 question(s) found - Order By Newest Q1. What is Scanner class used for ? when was it introduced in Java ? Core Java
Ans. Scanner class introduced in Java 1.5 for reading Data Stream from the imput device. Previously we used to write code to read a input using DataInputStream. After reading the stream , we can convert into respective data type using in.next() as String ,in.nextInt() as integer, in.nextDouble() as Double etc Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java5   scanner   file io   file input   data input stream  file handling basic Q2. Write code explaining use of Scanner class ?
Ans. Scanner sc = new Scanner(System.in);
int i = sc.nextInt(); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   scanner   io   console io   scanner   System.in Q3. Difference between Scanner and BufferedReader ? Which one is faster and Why ? Core Java
Ans. Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream.
BufferedReader read files efficiently by using a buffer to avoid physical disk operations.
Buffer size of Scanner is usually smaller than the Buffered Writer.
BufferedReader is faster that Scanner as it just reads the Stream and doesn't Parse the tokens to read the stream into primitive data types. Sample Code for Scanner Sample Code for BufferedReader Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   file io   input output   scanner   bufferedreader  file handling Q4. Write a program / method to input few numbers and then print the sum.
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?&category=code&searchOption&keyword=946 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding  scanner basic Q5. What is the difference between hasNextInt and nextInt method of scanner class ? Core Java
Ans. The hasNextInt() is used to check if there are any more elements left and the nextInt() is used to access that element. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  input output  Scanner Q6. Why do we need to close the Scanner ? Core Java
Ans. For releasing input stream reference (System.in), you should close scanner. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  scanner  input output  io