0% found this document useful (0 votes)
16 views4 pages

III Cse Cs3391 Oop QB Unit 4

This document covers various concepts related to I/O, generics, and string handling in Java. It explains streams, the FileInputStream and FileOutputStream classes, generic programming, string manipulation methods, and provides examples for each concept. Additionally, it includes questions for further exploration of generic inheritance, thread states, synchronization, and file operations.

Uploaded by

e.guhan310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

III Cse Cs3391 Oop QB Unit 4

This document covers various concepts related to I/O, generics, and string handling in Java. It explains streams, the FileInputStream and FileOutputStream classes, generic programming, string manipulation methods, and provides examples for each concept. Additionally, it includes questions for further exploration of generic inheritance, thread states, synchronization, and file operations.

Uploaded by

e.guhan310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT IV

I/O, GENERICS STRING HANDLING


PART A

1. What is meant by a stream?


A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device
by the java I/O [Link] input stream may abstract many different kinds of input: from a disk file,a
keyboard,or a network socket. Likewise,an output stream may refer to the console such as a disk file, or a
network connection.

2. What are the two types of streams?


There are two types of streams
Byte streams
Character streams

3. Write the use of FileInputStream class


Java FileInputStream class obtains input bytes from a file. It is used for reading byte-oriented data (streams of
raw bytes) such as image data, audio, video etc. You can also read character- stream data.

4. Write down the methods in FileInputStream Class.


The methods in FileInputStream class are int
available()
int read()
long skip(long x)
FileChannel getChannel()
FileDescriptor getFD()
protected void finalize()
void close()

5. Write the use of FileOutputStream class.


Java FileOutputStream is an output stream used for writing data to a file.
You can write byte-oriented as well as character-oriented data through FileOutputStream class. But, for
character-oriented data, it is preferred to use FileWriter than FileOutputStream.

6. Write down the methods in FileOutputStream Class.


The methods in FileOutputStream class are protected
void finalize()
void write(byte[] ary) void
write(int b) FileChannel
getChannel() FileDescriptor
getFD() void close()

7. Write the purpose of using generic programming


The term generics means parameterized types. Parameterized types are important because
they enable you to create classes, interfaces, and methods in which the type of data upon
which they operate is specified as a parameter.

8. What is meant by generic class?


A class that can refer to any type is known as generic class. Here, we are using T type parameter to
create the generic class of specific type.
CS3391_OOP
General Form:
class class-name<type-param-list>
{
//Body of the class
}

9. What is meant by generic method?


Generic method is a method with type parameters. In this Generic concept , types and methods can
be generic.
Syntax:
<type-param-list>re-type meth-name(param-list)
{
// Function Body
........
}

10. What is meant by Bounded Wildcards?


A bounded wildcard specifies either an upper bound or a lower bound for the type argument. This
enables you to restrict the types of objects upon which a method will operate. The most common
bounded wildcard is the upper bound, which is created using an extends clause in much the same
way it is used to create a bounded type.

11. How to create a single class, which automatically works with different types of data? Give
example. (Nov/Dec 2020)(Apr/May 2021)
A Wrapper class is a class whose object wraps or contains primitive data types. When we create
an object to a wrapper class, it contains a field and in this field, we can store primitive data types.
In other words, we can wrap a primitive value into a wrapper class object. They convert primitive
data types into objects. Objects are needed if we wish to modify the arguments passed into a
method.

12. How do you create an empty string in Java?


We can create an empty string using the empty string literal "" or by using the String constructor
with no arguments, like this: String emptyString = new String();

13. Explain the equals() method in the String class and how it differs from ==.
The equals() method in the String class checks if the content of two strings is equal.
The == operator checks if two string references point to the same memory location (object
identity).

14. How can you concatenate strings in Java?


We can concatenate strings using the + operator or by using the concat() method. For example:
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
// or1
String fullNameConcat = [Link](" ").concat(lastName);

15. What is the purpose of the charAt() method in the String class?
The charAt(int index) method returns the character at the specified index in the string. It allows
you to access individual characters within a string.

16. Explain how to convert a string to uppercase and lowercase in Java.


You can use the toUpperCase() and toLowerCase() methods to convert a string to uppercase or
lowercase, respectively. For example:
String text = "Hello, World!";
String uppercase = [Link]();
String lowercase = [Link]();

CS3391_OOP
17. How do you check if a string contains a specific substring in Java?
We can use the contains() method or the indexOf() method to check if a string contains a substring.
For example:
String text = "This is a sample text.";
boolean containsSubstring = [Link]("sample");
int indexOfSubstring = [Link]("sample");

18. What is the purpose of the split() method in Java, and how is it used?
The split() method is used to split a string into an array of substrings based on a specified delimiter.
For example:
String text = "apple,banana,cherry";
String[] fruits = [Link](",");
// fruits will contain ["apple", "banana", "cherry"]

19. What is the difference between String and StringBuffer when it comes to thread safety?
String is immutable and inherently thread-safe. Once created, its content cannot be modified. In
contrast, StringBuffer is mutable and designed to be thread-safe, allowing concurrent modification
by multiple threads without synchronization.

20. How do you remove leading and trailing whitespace from a string in Java?
We can use the trim() method to remove leading and trailing whitespace from a string. For example:
String text = " Hello, World! ";
String trimmedText = [Link](); // Result: "Hello, World!"

21. What is the substring() method in Java, and how is it used?


The substring(int beginIndex) method returns a new string that is a substring of the original string,
starting from the beginIndex to the end of the string. You can also use the substring(int beginIndex,
int endIndex) method to specify both the starting and ending indices.

22. How do you compare two strings in Java, considering case sensitivity?
To compare two strings while considering case sensitivity, you can use the equals() method. For
case-insensitive comparison, you can use equalsIgnoreCase().

23. What is the purpose of the replace() method in the String class?
The replace(CharSequence target, CharSequence replacement) method replaces all occurrences of
the target substring with the replacement substring within the string. It returns a new string with the
replacements made.

24. How do you convert an integer or other data types to a string in Java?
We can convert other data types to a string in Java using the [Link]() method or by
concatenating the value with an empty string. For example:
int number = 42;
String strNumber = [Link](number);
// or
String strNumberConcat = number + "";

25. How do you reverse a string in Java?


We can reverse a string in Java by converting it to a StringBuilder, using the reverse() method, and
then converting it back to a string. For example:
String original = "Hello, World!";
StringBuilder reversed = new StringBuilder(original).reverse();
String reversedString = [Link](); // Result: "!dlroW ,olleH"

26. How can you count the occurrences of a specific character in a string in Java?
We can count the occurrences of a specific character in a string using a loop or by using the
replace() method to remove all occurrences of the character and then subtracting the length of the
resulting string from the original string.
CS3391_OOP
27. How do you compare the contents of two strings without considering their case?
Answer: To compare two strings without considering case, you can convert both strings to
lowercase or uppercase using the toLowerCase() or toUpperCase() method and then use the equals()
method for comparison

PART B
1. Explain in detail about generic inheritance and generic interfaces. Discuss exploring the impact
of inheritance in generic classes with example.
2. Write about inheritance rules for generic types with example.
3. Define thread. Explain the states of thread briefly. State the reasons for synchronization in
thread. Write a simple concurrent programming to create, sleep, and delete the threads.
4. State the motivations of generic programming. Explain the generic classes and methods with
example.
5. Write a Java program that correctly implements producer consumer problem using the concept
of inter thread communication
6. How will you resolve deadlock in Interthread communication?
7. Explain about Writing to Console
8. Explain about Reading a file from Console
9. Explain in detail with program how to read and write from files

CS3391_OOP

You might also like