SlideShare a Scribd company logo
1 of 6
Download to read offline
Lesson:
Input
Cracking the Coding Interview in JAVA - Foundation
Pre-Requisites:
List of Concepts Involved:
Java Basic syntax
Variables
Data types
Taking input from user


Before getting into the actual input programs, lets first look into the brief description of the commonly used
jargons.



Class : a class is a template definition of the methods and variables in a particular kind of object. 



Object: Object is an instance of a class. It is actually a real world entity. 

Lets see the example below for better understanding.
Here, you can see that class ‘Car’ is just a blueprint. However, the objects actually are the real world entities
which have their own features.



Package: A package in Java is used to group related classes. Think of it as a folder in a file directory. We use
packages to avoid name conflicts, and to write a better maintainable code.


Let us begin !
Cracking the Coding Interview in JAVA - Foundation
Topic: How to get input from users in Java?
The easiest way to read input (primitive) in a Java program is through scanner class. 


Java Scanner Class

Java Scanner class allows the user to take input from the console. It belongs to the java.util package. It is used
to read the input of primitive types like int, double, long, short, float, and byte. 


The Java Scanner class then breaks the input into tokens using a delimiter (whitespace by default). It provides
many ways to read and parse various primitive values. 


In order to use scanner you have to write this import statement at the top – 

import java.util.Scanner; 



Example code for taking integer input in java

import java.util.Scanner; 

public class Main{

public static void main(String[] args){

int num;

System.out.println(“Enter a number”);

Scanner sc = new Scanner(System.in); 

num=sc.nextInt(); 

System.out.println(num);

num=num+5;

System.out.println(num);

}

}


Example code:



import java.util.Scanner;

class AddNumbers { 

public static void main(String args[]) { 

int num1,num2,sum;

System.out.println("Enter two numbers to calculate their sum: ");



Scanner sc = new Scanner(System.in); 

num1=sc.nextInt(); 

num2 = sc.nextInt(); 

sum= num1 + num2; 

System.out.println(sum); 

} 

}


Sample Input: 10 5 

Output: 15

Here, sc.nextInt() scans and returns the next token as int. A token is part of an entered line that is separated
from other tokens by space, tab or newline. So when input line is: “10 5” then sc.nextInt() returns the first token
i.e. “10” as int and sc.nextInt() again returns the next token i.e. “5” as int.
//Code for adding two integers entered by the user
// Create a Scanner
Cracking the Coding Interview in JAVA - Foundation
Example code for calculation of simple interest: 

import java.util.Scanner;

class Main { 

public static void main(String[] args) { 

Scanner sc = new Scanner(System.in);



System.out.print("Enter the principal amount: ");

double principal = sc.nextDouble();

System.out.print("Enter the interest rate: ");

double rate = sc.nextDouble();

System.out.print("Enter the time: ");

double time = sc.nextDouble();

double interest = (principal * time * rate) / 100;

System.out.println("Principal: " + principal);

System.out.println("Interest Rate: " + rate);

System.out.println("Time Duration: " + time);

System.out.println("Simple Interest: " + interest);

}

}


Output:

Enter the principal: 500

Enter the rate : 8

Enter the time: 2

Principal :500.00

Interest Rate: 8.0

Time Duration: 2.0

Simple Interest : 80.0




import java.util.Scanner;

public class Main

{ 

public static void main(String[] args)

{


Scanner sc = new Scanner(System.in);



char ch = sc.next().charAt(0);



System.out.println("char = "+ch);

}

}


Input :

h

Output :

char = h
// create an object of Scanner class

// take input from users
// Java program to read character using Scanner class
// Declare the object and initialize with

// predefined standard input object
// Character input
// Print the read value
Cracking the Coding Interview in JAVA - Foundation
Java Scanner Methods to Take Input

The Scanner class provides various methods that allow us to read inputs of different types. Lets see a few of
them in the table below :
Try and explore all of these scanner methods in the same way as we did in the previous examples. This will
make you an expert with scanner method applications.
Cracking the Coding Interview in JAVA - Foundation
MCQs
Upcoming Class Teasers
1.What is the output of the following code if input is :

20 physics wallah



Scanner s = new Scanner(System.in);

int p = s.nextInt();

String q = s.next();

System.out.print(p);

System.out.println(q);


Options
20 physics wallah
20physics
20physicswallah
20 physics 


Ans: b) 20physics

Explanation:

"s.nextInt()" scans and returns the next token as int. A token is part of an entered line that is separated from
other tokens by space, tab or newline. So when input line is - “20 physics wallah” then s.nextInt() returns the first
token as int i.e. “20” and s.next() returns the next token "physics”. 

While printing, in the first statement p is printed and then q. There is no space or nextline between both prints.
Hence output is : 20physics.



2.What is the output of the following code if input is : hello world 21


Scanner sc = new Scanner(System.in);

String p = sc.next();

int q=sc.nextInt();

System.out.print(p + " " + q);


Options:
hello world 21
InputMismatchException
hello 21
hello


Ans b) InputMismatchException

Explanation:

"sc.next()" scans and returns the next token as String. A token is part of an entered line that is separated from
other tokens by space, tab or newline. So when input line is - “ hello world 21” then sc.next() returns the first
token as String i.e. “hello” and sc.nextInt() tries to convert the next token i.e. “world” into an int, which gives
InputMismatchException.
Operators
Precedence and their associativity

More Related Content

Similar to 3.Lesson Plan - Input.pdf.pdf

Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptxKimVeeL
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfoptokunal1
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iiiNiraj Bharambe
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerAiman Hud
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
ppt on scanner class
ppt on scanner classppt on scanner class
ppt on scanner classdeepsxn
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfanjanacottonmills
 
JAVA Question : Programming Assignment
JAVA Question : Programming AssignmentJAVA Question : Programming Assignment
JAVA Question : Programming AssignmentCoding Assignment Help
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...Nithin Kumar,VVCE, Mysuru
 
CODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfCODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfanurag1231
 
Anjalisoorej imca133 assignment
Anjalisoorej imca133 assignmentAnjalisoorej imca133 assignment
Anjalisoorej imca133 assignmentAnjaliSoorej
 
information Security.docx
information Security.docxinformation Security.docx
information Security.docxSouravKarak1
 

Similar to 3.Lesson Plan - Input.pdf.pdf (20)

Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
 
LAB1.docx
LAB1.docxLAB1.docx
LAB1.docx
 
Introduction to Java Programming Part 2
Introduction to Java Programming Part 2Introduction to Java Programming Part 2
Introduction to Java Programming Part 2
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Java programs
Java programsJava programs
Java programs
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iii
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Java practical
Java practicalJava practical
Java practical
 
ppt on scanner class
ppt on scanner classppt on scanner class
ppt on scanner class
 
Java final lab
Java final labJava final lab
Java final lab
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
 
JAVA Question : Programming Assignment
JAVA Question : Programming AssignmentJAVA Question : Programming Assignment
JAVA Question : Programming Assignment
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
 
CODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfCODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdf
 
java-programming.pdf
java-programming.pdfjava-programming.pdf
java-programming.pdf
 
Anjalisoorej imca133 assignment
Anjalisoorej imca133 assignmentAnjalisoorej imca133 assignment
Anjalisoorej imca133 assignment
 
information Security.docx
information Security.docxinformation Security.docx
information Security.docx
 

Recently uploaded

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 

Recently uploaded (20)

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 

3.Lesson Plan - Input.pdf.pdf

  • 2. Cracking the Coding Interview in JAVA - Foundation Pre-Requisites: List of Concepts Involved: Java Basic syntax Variables Data types Taking input from user Before getting into the actual input programs, lets first look into the brief description of the commonly used jargons. Class : a class is a template definition of the methods and variables in a particular kind of object. Object: Object is an instance of a class. It is actually a real world entity. Lets see the example below for better understanding. Here, you can see that class ‘Car’ is just a blueprint. However, the objects actually are the real world entities which have their own features. Package: A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Let us begin !
  • 3. Cracking the Coding Interview in JAVA - Foundation Topic: How to get input from users in Java? The easiest way to read input (primitive) in a Java program is through scanner class. Java Scanner Class Java Scanner class allows the user to take input from the console. It belongs to the java.util package. It is used to read the input of primitive types like int, double, long, short, float, and byte. The Java Scanner class then breaks the input into tokens using a delimiter (whitespace by default). It provides many ways to read and parse various primitive values. In order to use scanner you have to write this import statement at the top – import java.util.Scanner; Example code for taking integer input in java import java.util.Scanner; public class Main{ public static void main(String[] args){ int num; System.out.println(“Enter a number”); Scanner sc = new Scanner(System.in); num=sc.nextInt(); System.out.println(num); num=num+5; System.out.println(num); } } Example code: import java.util.Scanner; class AddNumbers { public static void main(String args[]) { int num1,num2,sum; System.out.println("Enter two numbers to calculate their sum: "); Scanner sc = new Scanner(System.in); num1=sc.nextInt(); num2 = sc.nextInt(); sum= num1 + num2; System.out.println(sum); } } Sample Input: 10 5 Output: 15 Here, sc.nextInt() scans and returns the next token as int. A token is part of an entered line that is separated from other tokens by space, tab or newline. So when input line is: “10 5” then sc.nextInt() returns the first token i.e. “10” as int and sc.nextInt() again returns the next token i.e. “5” as int. //Code for adding two integers entered by the user // Create a Scanner
  • 4. Cracking the Coding Interview in JAVA - Foundation Example code for calculation of simple interest: import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the principal amount: "); double principal = sc.nextDouble(); System.out.print("Enter the interest rate: "); double rate = sc.nextDouble(); System.out.print("Enter the time: "); double time = sc.nextDouble(); double interest = (principal * time * rate) / 100; System.out.println("Principal: " + principal); System.out.println("Interest Rate: " + rate); System.out.println("Time Duration: " + time); System.out.println("Simple Interest: " + interest); } } Output: Enter the principal: 500 Enter the rate : 8 Enter the time: 2 Principal :500.00 Interest Rate: 8.0 Time Duration: 2.0 Simple Interest : 80.0 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char ch = sc.next().charAt(0); System.out.println("char = "+ch); } } Input : h Output : char = h // create an object of Scanner class // take input from users // Java program to read character using Scanner class // Declare the object and initialize with // predefined standard input object // Character input // Print the read value
  • 5. Cracking the Coding Interview in JAVA - Foundation Java Scanner Methods to Take Input The Scanner class provides various methods that allow us to read inputs of different types. Lets see a few of them in the table below : Try and explore all of these scanner methods in the same way as we did in the previous examples. This will make you an expert with scanner method applications.
  • 6. Cracking the Coding Interview in JAVA - Foundation MCQs Upcoming Class Teasers 1.What is the output of the following code if input is : 20 physics wallah Scanner s = new Scanner(System.in); int p = s.nextInt(); String q = s.next(); System.out.print(p); System.out.println(q); Options 20 physics wallah 20physics 20physicswallah 20 physics Ans: b) 20physics Explanation: "s.nextInt()" scans and returns the next token as int. A token is part of an entered line that is separated from other tokens by space, tab or newline. So when input line is - “20 physics wallah” then s.nextInt() returns the first token as int i.e. “20” and s.next() returns the next token "physics”. While printing, in the first statement p is printed and then q. There is no space or nextline between both prints. Hence output is : 20physics. 2.What is the output of the following code if input is : hello world 21 Scanner sc = new Scanner(System.in); String p = sc.next(); int q=sc.nextInt(); System.out.print(p + " " + q); Options: hello world 21 InputMismatchException hello 21 hello Ans b) InputMismatchException Explanation: "sc.next()" scans and returns the next token as String. A token is part of an entered line that is separated from other tokens by space, tab or newline. So when input line is - “ hello world 21” then sc.next() returns the first token as String i.e. “hello” and sc.nextInt() tries to convert the next token i.e. “world” into an int, which gives InputMismatchException. Operators Precedence and their associativity