UNIT 4 - QB (2) Oops
UNIT 4 - QB (2) Oops
S.NO PART -A
1. Define Java I/O basics.
Java I/O (Input and Output) is used to process the input and produce the output.
Java uses the concept of stream to make I/O operation fast. These streams support all the types of
objects, data-types, characters, files etc to fully execute the I/O operations. The java.io package
contains all the classes required for input and output operations
PrintWriter supports the print( ) and println( ) methods for all types including Object. Thus, you can
use these methods in the same way as they have been used with System.out. If an argument is not a
simple type, the PrintWriter methods call the object's toString( ) method and then print the result. To
write to the console by using a PrintWriter, specify System.out for the output stream and flush the
stream after each newline.
For example, this line of code creates a PrintWriter that is connected to console output:
PrintWriter pw = new PrintWriter(System.out, true);
8. What are the 3 ways to read input from console in Java?
1. Using Buffered Reader Class
2. Using Scanner Class
3. Using Console Class
9. Define Java Console Class.
The Java Console class is be used to get input from console. It provides methods to read texts and
passwords. If you read password using Console class, it will not be displayed to the user.
The java.io.Console class is attached with system console internally.
1. String text=System.console().readLine();
2. System.out.println("Text is: "+text);
10. What is File Handling in Java?
File handling in Java implies reading from and writing data to a file. The File class from the
java.io package, allows us to work with different formats of files. In order to use the File class, you
need to create an object of the class and specify the filename or directory name.
11. What are the File Operations in Java?
1) Create a File
2) Get File Information
3) Write To a File
4) Read from a File
12. What is Generic Programming?
Generic programming is a style of computer programming in which algorithms are written in terms of
“to-be-specified-later” types that are then instantiated when needed for specific types provided as
parameters. Generic programming refers to writing code that will work for many types of data.
13. What is non-generic and its limitations?
In java, there is an ability to create generalized classes, interfaces and methods by operating through
Object class.
Limitation of non-generic:
1) Explicit casts must be employed to retrieve the stored data.
2) Type mismatch errors cannot be found until run time.
14. What is the need for Generic Programming?
Need for Generic:
1. It saves the programmers burden of creating separate methods for handling data belonging to
different data types.
2. It allows the code reusability.
3. Compact code can be created.
15. What are the Advantages of Java Generics?
1) Code Reuse
2) Type-safety
3) Elimination of casts
4) Stronger type checks at compile time
5) Enabling programmers to implement generic algorithms.
What are Generic Methods?
17. A Generic Method is a method with type parameter. We can write a single generic method declaration
that can be called with arguments of different types. Based on the types of the arguments passed to
the generic method, the compiler handles each method call appropriately.
UNBOUNDED WILDCARD: -
Unbounded wildcard can be used where generic method to be working with all types. In this case the
wildcard “?” simply matches any valid objects.
22. Define Strings.
String is a sequence of characters. But in Java, a string is an object that represents a sequence of
characters. The java.lang.String class is used to create string object.
23. How to create String object?
There are two ways to create a String object:
1. By string literal
Example: String s=“Welcome”;
2. By new keyword
Example: String s=new String(“Welcome”);
24. Define Java String Pool.
Java String pool refers to collection of Strings which are stored in heap memory. In this, whenever a
new object is created,
1) String pool first checks whether the object is already present in the pool or not.
2) If it is present, then same reference is returned to the variable
3) else new object will be created in the String pool and the respective reference
will be returned.
25. What is String Comparison in Java?
We can compare two given strings on the basis of content and reference.
It is used in authentication
(by equals() method),
sorting (by compareTo() method),
reference matching (by == operator) etc.
26. What are the ways to compare string objects?
There are three ways to compare String objects:
1. By equals() method
2. By = = operator
3. By compareTo() method
27. Differentiate String class and StringBuffer class
29 Write a Java code to check if the command line argument is file or not
class Test
{public static void main(String[] args)
{File obj=new File(args[0]);
If(obj.isFile())
{
System.out.println("This is a file name" + obj.getPath());
}
}
}