8.advanced String
8.advanced String
A) charAt()
B) getChars()
C) getCharAt()
D) getAt()
Answer: A
QUESTION : 02
A) toCharArray()
B) toString()
C) toChar()
D) toArray()
Answer: A
QUESTION : 04
A.5040Sum5040 Answer: A
B. 90Sum90
C. 90Sum5040
D.Some other output
QUESTION : 05
Answer: A
Question 1
Write a Java code to check whether the input string matches with the word
“Dhoni”
Sample Sample
Input: Output:
Kohli Not matching
// Solution
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String str = "Dhoni";
String s = in.next();
if(str.equals(s)){
System.out.print("Matching");
}
else{
System.out.print("Not matching");
}
}
}
Question 2
● Write a Java program to accept two strings from user and perform the
following operations:
1) Find the length of both input strings
2) Concatenation of two strings
3) Convert the first string into uppercase
Hello 5 10
Developers Hello Developers
HELLO
// Solution
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String str1 = in.nextLine();
String str2 = in.nextLine();
System.out.println(str1.length() + " " + str2.length());
System.out.println(str1.concat(str2));
System.out.print(str1.toUpperCase());
}}
Question 3
● Take the user input two string reverse it and display the reversed string
bhopal lapohb
import java.util.Scanner;
public class MyClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
char ch[]=str.toCharArray();
String rev="";
for(int i=ch.length-1; i>=0;i--)
{
rev = rev + ch[i];
}
System.out.println(rev);
}
}
Take the user input 2 strings a and b case-insensitive anagrams. Print
“anagrams “ if they are anagrams or else print not an anagram
Constrains:
● 1<=length(a),length(b) <=50;
● Strings a and b consist of English alphabetic characters
● Ignore white spaces in a string
import java.util.*; Arrays.sort(s1);
public class MyClass Arrays.sort(s2);
{ String d1=Arrays.toString(s1);
static boolean status; String d2=Arrays.toString(s2);
public static void main(String[] args) status=d1.equals(d2);
{ }
Scanner sc=new Scanner (System.in); if(status==true)
System.out.println("enter the input string"); {
String str1=sc.nextLine(); System.out.println("it is an anagram");
System.out.println("enter another input string"); }
String str2= sc.nextLine(); else
if(str1.length()==str2.length()) {
{ System.out.println("not an anagram");
char s1[]=str1.toUpperCase().toCharArray(); }
char s2[]=str2.toUpperCase().toCharArray();
}
}
Question :05
Output:
Progrxyzammer
5Progrxyzammer
5Prtrueogrxyzammer
5Prtr41.35ueogrxyzammer
5Prtr41.41.3535ueogrxyzammer
5Phellortr41.41.3535ueogrxyzammer
Which of these class is used to create an object whose character sequence is
mutable?
a) String()
b) StringBuffer()
c) String() & StringBuffer()
d) None of the mentioned
Which of this method of class StringBuffer is used to concatenate the string
representation to the end of invoking string?
a) concat()
b) append()
c) join()
d) concatenate()
String constant pool allocates its memory in
a) Stack
b) Heap
c) Data Segment
d) Register
Which of these method of class String is used to compare two String objects for
their equality?
A. equals()
B. Equals()
C. isequal()
D. Isequal()
The …………………… compares the characters inside a sting object whereas
……………….. compares two objects references to see whether they refer to the
same instance.
A) i and ii only
B) ii and iii only
C) ii and iv only
D) i and iv only
For the following expression,
String S=new String(“abc”);
Which of the following calls are valid?
A) i and ii only
B) i, ii and iii only
C) ii, iii and iv only
D) All i, ii, iii and iv
What will be the output of the following code?
String S=new String();
System.Out.Println (“S = ” +S);
A) null
B) error
C) =S
D) S=
Question 2
I am Sam IamSam
A
// Solution
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
int str_len = str.length();
StringBuilder sb = new StringBuilder(str);
StringBuilder tmp = new StringBuilder("");
for(int i = 0; i < str_len; i++){
if(str.charAt(i) != ' '){
tmp.append(str.charAt(i));
}
}
sb.setLength(0);
for(int i = 0; i < tmp.length(); i++){
sb.append(tmp.charAt(i));
}
System.out.println(sb); } }
A
import java.util.Scanner;
public class MyClass
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String noSpaceStr = str.replace(" ", ""); // using built in method
System.out.println(noSpaceStr);
}
}
Question 4
● Take the user input and find the count of vowels, consonants, digits and
spaces in the given string
● Write a Java code to check whether the given string is palindrome or not.
● If the given string is a palindrome, then print “Yes”. Otherwise, print “No”.
spacecaps Yes
// Solution
import java.util.Scanner;
class Main{
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
int str_len = str.length();
int end = str_len - 1;
int front = 0;
boolean is_palindrome = true;
while(front < end){
if(str.charAt(front) != str.charAt(end))
{
is_palindrome = false;
break;
}
front++;
end--; }
// Solution
if(is_palindrome == true){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
THANK YOU