Java Libraries – Scanner, Arrays & String Handling
Que 1. What is Java Library?
Answer: Java Library is the collection of classes that can be used in our program.
Que 2. What is the use of the import keyword?
Answer: import keyword in java is used to import the class from the package. The import statements must appear before any class definitions in the file.
For example:- import java.util.Scanner;
String Manipulation
Que 1. In Java, write a statement to store a value ‘M’ to a variable gender.
Answer: ‘M’ is a character constant in java. So it can store like
char gender = ‘M’;
Que 2. What is String in Java?
Answer: A set of characters written inside the double quotes is known as String. For example:
String name = “Anjeev Singh”;
Que 3. String class is present in which package?
Answer: java.lang
Que 4. Which function of the String class returns the character at the given index?
Answer: charAt
Que 5. Find output: System.out.println(“Information Technology”.charAt(3));
Answer: h
Que 6. Which operator in java is used to concatenate two strings?
Answer: +
Que 6. Which function in java is used to concatenate two strings?
Answer: concat()
For example: String str1 = “www.”;
str2 = str1.concat(“mycstutorial.in”)
System.out.println(str2);
Output: www.mycstutorial.in
Que 7. Which function in java is used to search a substring in the string?
Answer: indexOf()
Que 8. Which function returns the first occurrence of given substring in the string?
Answer: indexOf()
Que 9. Find output:
String str1 = “Hello”;
String str2 = “hello”;
System.out.println(str1.equals(str2))
System.out.println(str1.equalsIgnoreCase(str2));
Answer: False
True
Que 10. Which function in java is used to replace all occurrences of oldstring with newstring?
Answer: replace()
Que 11. Find Output:
String str1 = “anjeevsinghacademy.com”
String str2 = “This is the best website”;
str1.replace(‘e’, ‘#’);
str2.replace(“is”, “**”)
System.out.println(str1);
System.out.println(str2);
Answer: anj##vsinghacad#my.com
Th** ** the best website
Que 12. Which function in java is used to return the size/length of the string?
Answer: length()
Que 13. Which function in java is used to check whether the string is empty or not?
Answer: isEmpty()
Que 14. Find the output?
String str1 = “Hello”;
System.out.println(str1.toLowerCase());
System.out.println(str1.toUpperCase());
Answer: hello
HELLO
Que 15. Write the name of data types used to store following (a) Single Character (b) A string having length 1
Answer: (a) char (b) String
Data Input
Que 1. What do you mean by the interactive program?
Answer: In java, the program which allows the input of the data at run time, is called an interactive program.
Que 2. Which class in Java allows to input value at run time?
Answer: To take user input we use the prebuilt Scanner class.
import java.util.Scanner;
Que 3. What object will be passed to the constructor of the Scanner class?
Answer: To take user input we use the prebuilt Scanner class.