Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Explain with a code sample how you will read a value


Download 1.52 Mb.
Pdf ko'rish
bet13/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   9   10   11   12   13   14   15   16   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Explain with a code sample how you will read a value
entered by a user.
Answer:
Java provides a class called java.util.Scanner. This can be
used to read an input from a user. The following code
demonstrates this:
System.out.println(“Enter a Number:”);
Scanner scanner = new Scanner (System.in);
int num = scanner.nextInt();
System.out.println(“The entered number is “:num);
scanner.close();
This code creates a Scanner corresponding to System.in
which is the standard input. It then asks the user to input a
number and reads this number into the variable num via the
scanner.nextInt method. So, this code prints the following
output:
Enter a Number:
10


The entered number is 10
Just like nextInt, the Scanner class has several other
methods that can be used to read data of different types like
String, float, long, boolean, etc.



021.
a.
b.
c.
d.
e.
f.
022.
CHAPTER
03
Data Types, Variables and Arrays
What are the possible ways of declaring an array of short
data type?
Answer:
You can declare an array of short data type in any of the following
ways:
short a1[];
short [] a2;
short b1[] [];
short [] [] b2;
short [] c1 = {2,3,4};
short c2[] = {2,3,4};
Approaches a and b declare a single dimensional short array.
Approaches c and d declare a two–dimensional short array.
Approaches e and f declare a single dimensional short array and
initialize it. In all the approaches, the square brackets can be
placed either after the variable name or after the short keyword.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   9   10   11   12   13   14   15   16   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling