You are on page 1of 9

Q.1 Answer the following.

(a) 3 short questions of 1 mark each


(i) What is a Java String ?
A java String is a class in java that permits to store a collection of data that is represented by
textual data. It is implemented as an object of the class java.lang.string
(ii) Which class is used for efficient string concatenation?
StringBuilder class is used for efficient string concatenation
(iii) What are the key differences between StringBuffer and StringBuilder?
StringBuilder=>
- Non synchronized so it’s not thread safe
- Builder is faster than buffer bcs it’s not synchronized
- Builder is more efficient than buffer
StringBuffer=>
- Synchronized so thread safe i.e 2 thread cannot call the method of stringBuffer at the
same time
- Buffer is less efficient than stringbuilder
- Buffer introduced in java 1.0
(b) Objective type/MCQs/True-False/Fill in blanks (7 questions of 1 mark each)
(i) append() method is used for concatenating strings efficiently.
(ii) The StringTokenizer class is used for tokenizing strings.
(iii)Pattern class is used for pattern matching in Java.
(iv) StringBuffer provides mutable sequences of characters.
(v) Java provides the Pattern class for pattern matching.
(vi) Which method is used to compare the content of two strings in Java?
A) compare()
B) compareTo()
C) equals()
D) isEqual()
(vii) Which class is used for writing bytes to a file in Java?
A) FileInputStream
B) FileOutputStream
C) FileReader
D) FileWriter
Q.2 Answer the following. (2 or 3 mark questions)
(a) Two Questions of 2 Marks
(i) Differentiate between the StringBuffer and StringBuilder classes in Java.
StringBuilder=>
- Non synchronized so it’s not thread safe
- Builder is faster than buffer bcs it’s not synchronized
- Builder is more efficient than buffer
StringBuffer=>
- Synchronized so thread safe i.e 2 thread cannot call the method of stringBuffer at the
same time
- Buffer is less efficient than stringbuilder
- Buffer introduced in java 1.0

(ii)
(b) Two Questions of 3 Marks
(i) Differentiate between the StringBuffer and StringBuilder classes in Java.
StringBuilder=>
- Non synchronized so it’s not thread safe
- Builder is faster than buffer bcs it’s not synchronized
- Builder is more efficient than buffer
StringBuffer=>
- Synchronized so thread safe i.e 2 thread cannot call the method of stringBuffer at the
same time
- Buffer is less efficient than stringbuilder
- Buffer introduced in java 1.0

(ii) Describe the functionality of the FileOutputStream and FileInputStream classes


in Java I/O.
FileOutputStream class is used for writing data in a file as a stream of data
FileInputStream class is used for reading data in a file as a stream of data
Q.3 Attempt any TWO.
(i) Explain the concept of immutability in Java String and its significance.
Immutability in java String means than when a string is created, it’s immutable, just as a
constant, so if you try to modify a string, it won’t modify the string but will instead create
another object with the value indicated. It is significant bcs it ensure than string is thread safe
and that modification won’t occure when shared. It allows various optimization and improve
performance and reduce memory allocation overhead.
(ii) Discuss the purpose and usage of DataOutputStream and DataInputStream in
Java.
DataOutput is used to write primitive data as binary data stream and DataInputStream permit
to read primitive data as a bytes strean
(iii) How does the StringTokenizer class tokenize a string? Give an example.
The stringtokenizer is used to tokenize (or break) a string into tokens based on a delimiter
mentionned in the method. We can tokenize a string by following those steps:
1) Create an instance of the StringTokenizer class by specifying the string concerned and
the delimiter to break the string.
2) By using a while loop for exemple, you pass throught the string with the .hasmoretokens()
method to see if there’s still tokens left and by output the tokens with .nexttokens() for
example.
Here’s an example:
public static void main(String[] args){
String str=”je suis un homme”;
StringTokenizer tokenizer= new StringTokenizer(str, “ “)
while(tokenizer.hasMoreTokens()){
System.out.println(tokenizer.nextTokens());
}
}
Q.4 Answer the following.
(a) Answer the following.
Explain the role of BufferedOutputStream and BufferedInputStream in Java I/O.
StringBuffer=>
- Synchronized so thread safe i.e 2 thread cannot call the method of stringBuffer at the
same time
- Buffer is less efficient than stringbuilder
- Buffer introduced in java 1.0

(b) Answer the following.


Difference between String , StringBuffer, StringBuilder

OR
(b) Answer the following.
What is the purpose of SequenceInputStream in Java I/O? Provide an example.
SequenceInputStream is used to read multiple file at the same time because for example
FileInputStream or BufferedInputStream is only capable of reading a file on a time. The
sequenceinputstream can be for example used to concate 2 or more file into one by reading
both of the files at the same time.

======================================================================
==============================================================--------------
----------------------------------------------------------------------------------------------------------

Q 1 (A) Five one-line Questions


(Each Question 01 marks)
1. What is the primary characteristic of a Java String?
immutablity
2. How does Java handle String comparison?
equals() and compareTo()
3. What are the key differences between StringBuffer and StringBuilder?
synchronization and performance
4. Define the concept of a substring in Java String.
a substring is a portion of the original string
5. What is the significance of immutability in Java String?
the significance of immutability in java string ensure thread safety and enables
optimizations
Q 1 (B) Five Fill in the blanks
(Each Question 01 marks)
1. _StringBuffer_______ class in Java represents mutable sequences of characters.
2. Java provides the _StringBuilder________ class to manipulate strings efficiently.
3. The __StringTokenizer_________ class is used for tokenizing strings.
4. __Pattern______ is used for pattern matching in Java.
5. the _append()__________ method is used for concatenating strings efficiently.

Q 2 Attempt any four (Short Question)


(Each Question 03 marks)
1. Explain the concept of immutability in Java String and its significance.
The immutability is a property of Java string class because when a string is created, the
value hold by the variable can’t be modified after the creation, so when we try to modify the
value, it will only create another object that hold the specified value without changing the
value of the original one.
2. Differentiate between the StringBuffer and StringBuilder classes in Java.
3. How does the String tokenizer class tokenize a string in Java? Provide an example.
4. Discuss the differences between String and StringBuilder regarding performance and use
cases.
5. Explain the usage of regular expressions in Java with suitable examples.

Q 3 Attempt any two questions


(Each Question 04 marks)
1. Describe the functionality of the FileOutputStream and FileInputStream classes in Java
I/O.
2. Discuss the purpose and usage of DataOutputStream and DataInputStream in Java.

Q 4 Attempt any two questions


(Each Question 05 marks)
Q.A (compulsory)

1. Explain the role of BufferedOutputStream and BufferedInputStream in Java I/O.


2. Discuss the significance of FileReader and FileWriter classes in Java file handling.

Q.B

1. What is the purpose of SequenceInputStream in Java I/O? Provide an example.


2. Explain the difference between PrintWriter and PrintStream in Java I/O.

public static void main(string[] argos){


String mail=”email@email.mail”;
String EmailPattern=”regex mail pattern”;
Pattern pattern= Pattern.compile(EmailPattern);
Matcher matcher= pattern.matcher(mail);

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class PhoneNumberValidation {


public static void main(String[] args) {
String phoneNumber = "9876543210"; // Replace with the phone number to validate

// Regular expression pattern for Indian mobile numbers


String phonePattern = "[6-9]{1}[0-9]{9}";

Pattern pattern = Pattern.compile(phonePattern);


Matcher matcher = pattern.matcher(phoneNumber);

if (matcher.matches()) {
System.out.println("Valid phone number!");
} else {
System.out.println("Invalid phone number.");
}}
FileWriter:

● FileWriter is used to create a file and write characters into it. It’s part of
the Character Stream classes.
● Avoid using FileInputStream and FileOutputStream for textual
information, as they are Byte Stream classes.

import java.io.FileWriter;
import java.io.IOException;

public class CreateFile {


public static void main(String[] args) throws IOException {
String str = "File Handling in Java using FileWriter and FileReader";
FileWriter fw = new FileWriter("output.txt");

for (int i = 0; i < str.length(); i++) {


fw.write(str.charAt(i));
}

System.out.println("Writing successful");
fw.close();
}
}

FileReader:

● FileReader reads data in the form of characters from a ‘text’ file.


● It inherits from the InputStreamReader class.
● Constructors assume default character encoding and byte-buffer size.
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {


public static void main(String[] args) throws IOException {
FileReader reader = null;

try {
reader = new FileReader("input.txt");
int ch;

while ((ch = reader.read()) != -1) {


System.out.print((char) ch); // Print each character
}
} finally {
if (reader != null) {
reader.close();
}}}}
PrintStream:

● PrintStream is a class that allows you to write data to an output stream.


● It writes raw bytes in the machine’s native character format.
● It’s a subclass of OutputStream, which processes data byte-by-byte.
● Commonly used for writing binary data or logging.
● Useful for console output (System.out) and error streams (System.err).
● Does not throw checked exceptions (e.g., IOException) during write
operations.
● Example usage:

import java.io.PrintStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

public class PrintStreamExample {

public static void main(String[] args) throws FileNotFoundException {

PrintStream ps = new PrintStream(new FileOutputStream("output.txt"));

ps.println("Hello, PrintStream!");

ps.close(); }}

PrintWriter:

● PrintWriter is also used for writing data to an output stream.


● It processes each character at a time and uses Unicode for character
encoding.
● It’s a character stream that converts bytes to recognized encoding schemes.
● Commonly used for writing text data.
● Useful for writing to files, sockets, or other character-based streams.
● Throws checked exceptions (e.g., IOException) during write operations.
● Example usage:

import java.io.PrintWriter;

import java.io.FileNotFoundException;

import java.io.FileWriter;

public class PrintWriterExample {

public static void main(String[] args) throws FileNotFoundException {

PrintWriter pw = new PrintWriter(new FileWriter("output.txt"));

pw.println("Hello, PrintWriter!");

pw.close();

}}

1. Key Differences:
○ PrintStream writes raw bytes, while PrintWriter processes
characters.
○ PrintStream is a byte stream, and PrintWriter is a character
stream.
○ PrintStream does not throw checked exceptions, whereas
PrintWriter does.
○ Use PrintStream for binary data or console output, and
PrintWriter for text data.

Reponse gpt sujet 2:

Q 1 (A) Five one-line Questions

1. The primary characteristic of a Java String is immutability.


2. Java handles String comparison using methods like equals() or compareTo().
3. Key differences between StringBuffer and StringBuilder include
synchronization and performance.
4. In Java String, a substring is a portion of the original string.
5. The significance of immutability in Java String ensures thread safety and
enables optimizations.

Q 1 (B) Five Fill in the blanks

1. StringBuffer class in Java represents mutable sequences of characters.


2. Java provides the StringBuilder class to manipulate strings efficiently.
3. The StringTokenizer class is used for tokenizing strings.
4. Pattern is used for pattern matching in Java.
5. append() method is used for concatenating strings efficiently.

Q 2 Attempt any four (Short Question)

1. Immutability in Java String refers to its inability to change after creation,


ensuring thread safety and enabling optimizations.
2. StringBuffer is synchronized and thread-safe, while StringBuilder is not
synchronized, offering better performance in single-threaded scenarios.
3. The StringTokenizer class tokenizes a string in Java by breaking it into tokens
based on specified delimiters. Example: StringTokenizer tokenizer =
new StringTokenizer(str, " ");
4. String is immutable, whereas StringBuilder is mutable, leading to differences
in performance and use cases. StringBuilder is preferred for frequent string
manipulation, while String is used when immutability is desired.
5. Regular expressions in Java provide a powerful way to search, manipulate,
and validate strings. For example, String regex =
"\\d{3}-\\d{2}-\\d{4}"; matches a social security number pattern.

Q 3 Attempt any two questions

1. FileOutputStream and FileInputStream classes in Java I/O are used for writing
and reading bytes to and from files, respectively. FileOutputStream writes data
as a stream of bytes to a file, while FileInputStream reads data from a file as a
stream of bytes.
2. DataOutputStream and DataInputStream classes in Java are used for writing
and reading primitive data types as binary data streams. They provide
methods to write and read various primitive data types consistently across
different platforms.

Q 4 Attempt any two questions Q.A (compulsory)

1. BufferedOutputStream and BufferedInputStream in Java I/O are used for


buffering input and output streams, respectively, to improve performance.
BufferedOutputStream buffers data in memory before writing it to the
underlying output stream, while BufferedInputStream buffers data from the
input stream in memory, reducing the number of reads from the underlying
input stream.
2. FileReader and FileWriter classes in Java file handling are used to read from
and write to text files, respectively. They provide convenient methods for
reading and writing characters from/to files.

Q.B

1. SequenceInputStream in Java I/O is used to concatenate multiple


InputStreams into a single InputStream. An example:
SequenceInputStream sequenceInputStream = new
SequenceInputStream(file1, file2);
2. PrintWriter and PrintStream in Java I/O are both used for writing formatted
text to a destination. PrintWriter is character-oriented and provides additional
methods for formatting, while PrintStream is byte-oriented and primarily used
for writing to output streams such as System.out.

You might also like