Q. 1 a) What is string in Java? Write a java program that demonstrates any four constructors of
string class.
Strings are defined as an array of characters. The difference between a character array and a
string is the string is terminated with a special character \0.
Internal Assessment I Sept 2019
Scheme and Solutions
Sub:
Advanced Java & J2EE
Sub
Code:
17CS553
Branch:
Date:
07-9-2019
Duration:
90 mins
Max
Marks:
50
Sem /
Sec:
V
OBE
Q. 2 a) Explain how to modify the string using following methods
i) substring( ) ii) concat( ) iii) replace( ) iv) trim( )
i) substring( )- A part of string is called substring. In other words, substring is a subset of
another string. In case of substring startIndex is inclusive and endIndex is exclusive.
public class TestSubstring{
public static void main(String args[]){
String s="SachinTendulkar";
System.out.println(s.substring(6));//Tendulkar
System.out.println(s.substring(0,6));//Sachin
}
}
Output:
Tendulkar
Sachin
ii) concat( ) - string concatenation forms a new string that is the combination of multiple strings.
class TestStringConcatenation3{
public static void main(String args[]){
String s1="Sachin ";
String s2="Tendulkar";
String s3=s1.concat(s2);
System.out.println(s3);//Sachin Tendulkar
}
}
Output: Sachin Tendulkar
iii)replace( )
The java string replace() method returns a string replacing all the old char or CharSequence to
new char or CharSequence.
public class ReplaceExample1{
public static void main(String args[]){
String s1="javatpoint is a very good website";
String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e'
System.out.println(replaceString);
}
}
Output: jevetpoint is e very good website
iv) trim( )
The java string trim() method eliminates leading and trailing spaces.
public class StringTrimExample{
public static void main(String args[]){
String s1=" hello string ";
System.out.println(s1+"javatpoint");//without trim()
System.out.println(s1.trim()+"javatpoint");//with trim()
}
}
Output:
hello string javatpoint
hello stringjavatpoint
Q.3 a) Explain the following StringBuffer methods with an example
i) insert( ) ii) append( ) iii)delete ( ) iv) reverse v) capacity
i) insert() method inserts the given string with this string at the given position.
class StringBufferExample2{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}
}
ii) append( )
The append() method concatenates the given argument with this string.
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
iii) delete ( )
The delete() method of StringBuffer class deletes the string from the specified beginIndex to
endIndex.
class StringBufferExample4{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}
}
iv) reverse( )
The reverse() method of StringBuilder class reverses the current string.
class StringBufferExample5{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
}
}
v)capacity( )
The capacity() method of StringBuffer class returns the current capacity of the buffer. The
default capacity of the buffer is 16. If the number of character increases from its current capacity,
it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will
be (16*2)+2=34.
class StringBufferExample6{
public static void main(String args[]){
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
Q. 4 a) Differentiate between equals and = = with respect to string comparison
Q. 4 b) Write a program to remove duplicate characters in a given string and display new string
without any duplicates
public static void main(String[] args) {
String stringWithDuplicates = "abcdafbd";
char[] characters = stringWithDuplicates.toCharArray();
boolean[] found = new boolean[256];
StringBuilder sb = new StringBuilder();
System.out.println("String with duplicates : " + stringWithDuplicates);
for (char c : characters) {
if (!found[c]) {
found[c] = true;
sb.append(c);
}
}
System.out.println("String after duplicates removed : " + sb.toString());
}
Output:
String with duplicates : abcdafbd
String after duplicates removed : abcdf
Q. 5 a) What are database drivers? Explain different JDBC driver types
JDBC Driver is a software component that enables java application to interact with the database.
There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
Q. 6 a) Describe various steps of JDBC with code snippets
Q. 7 a) Explain with example
i) Callable statement object ii) Prepared statement object
i)callable object
ii) Preparedstatement:
Q 7 a) Explain the following character extraction methods chatAt( ) and tocharArray( )
There are several ways by which characters can be extracted from String class object. String is
treated as an object in Java so we cant directly access the characters that comprise a string. For
doing this String class provides various predefined methods
charAt()
charAt() method is used to extract a single character at an index. It has following syntax.
Syntax
char charAt(int index)
class temp
{
public static void main(String...s)
{
String str="Hello";
char ch=str.charAt(2);
System.out.println(ch);
}
}
Output
l
toCharArray()
It is an alternative of getChars() method. toCharArray() convert all the characters in a String
object into an array of characters. It is the best and easiest way to convert string to character
array. It has following syntax.
Syntax
char [] toCharArray()
class temp
{
public static void main(String...s)
{
String str="Hello World";
char ch[]=str.toCharArray();
System.out.println(ch);
}
}
Output
Hello World
Q. 8 b) Explain the following methods with suitable example
i)startsWith( ) & endsWith( ) ii) compareTo( ) iii) indexOf( )
i) startsWith( ) & endsWith
ii) compareTo( )
We have following two ways to use compareTo() method:
int compareTo(String str)
Here the comparison is between string literals. For example string1.compareTo(string2) where
string1 and string2 are String literals.
int compareTo(Object obj)
Here the comparison is between a string and an object. For example string1.compareTo("Just a
String object") where string1 is a literal and its value is compared with the string specified in the
method argument.
public class CompareToExample {
public static void main(String args[]) {
String str1 = "String method tutorial";
String str2 = "compareTo method example";
String str3 = "String method tutorial";
int var1 = str1.compareTo( str2 );
System.out.println("str1 & str2 comparison: "+var1);
int var2 = str1.compareTo( str3 );
System.out.println("str1 & str3 comparison: "+var2);
int var3 = str2.compareTo("compareTo method example");
System.out.println("str2 & string argument comparison: "+var3);
}
}
Output:
str1 & str2 comparison: -16
str1 & str3 comparison: 0
str2 & string argument comparison: 0
iii) indexOf( )
The java string indexOf() method returns index of given character value or substring. If it is not
found, it returns -1. The index counter starts from zero.
public class IndexOfExample3 {
public static void main(String[] args) {
String s1 = "This is indexOf method";
// Passing substring and index
int index = s1.indexOf("method", 10); //Returns the index of this substring
System.out.println("index of substring "+index);
index = s1.indexOf("method", 20); // It returns -1 if substring does not found
System.out.println("index of substring "+index);
}
}
Output: index of substring 16
index of substring -1