0% found this document useful (0 votes)
49 views

8.advanced String

The document contains 5 multiple choice questions about Java strings that test various string methods like charAt(), toCharArray(), and concatenation using the + operator or concat() method. It also includes 5 code snippets with sample inputs and outputs to test string operations like checking for anagrams, deleting spaces from a string, and counting vowels, consonants, digits and spaces in a given string.

Uploaded by

Manoj Manu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

8.advanced String

The document contains 5 multiple choice questions about Java strings that test various string methods like charAt(), toCharArray(), and concatenation using the + operator or concat() method. It also includes 5 code snippets with sample inputs and outputs to test string operations like checking for anagrams, deleting spaces from a string, and counting vowels, consonants, digits and spaces in a given string.

Uploaded by

Manoj Manu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Java

String MCQ’s and


Programs
QUESTION : 01

To extract a single character from a string, you can refer directly to


an individual character via the …………………… method.

A) charAt()
B) getChars()
C) getCharAt()
D) getAt()

Answer: A
QUESTION : 02

You can use …………………… method, if you want to convert all


the characters in a string object into a character array.

A) toCharArray()
B) toString()
C) toChar()
D) toArray()
Answer: A
QUESTION : 04

public class Master {


public static void main(String[] args) {
String number1 = 50 + "";
String number2 = 40 + "";
String sum = "Sum";
String first = number1.concat(number2);
String second = first.concat(sum);
second = second.concat(number1);
second = second.concat(number2);
System.out.print(second);
}

A.5040Sum5040 Answer: A
B. 90Sum90
C. 90Sum5040
D.Some other output
QUESTION : 05

A.S1 =123456, S2=579


B. S1 =123456, S2 =123456
C. S1=579, S2=579
D. none of this

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

Sample Input: Sample Output:

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

Sample Input: Sample Output:

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

// Predict the output


public class Main{
public static void main(String[] args) {
StringBuffer s = new StringBuffer("Programmer");
s.insert(5, "xyz");
System.out.println(s);
s.insert(0, 5);
System.out.println(s);
s.insert(3, true);
System.out.println(s);
s.insert(5, 41.35d);
System.out.println(s);
s.insert(8, 41.35f);
System.out.println(s);
char arr[] = {'h','e','l','l','o'};
s.insert(2, arr);
System.out.println(s);
}
}
For above question correct explanation is given below.

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) = = operator , equals( ) method


B) equals( ) method , = = operator
C) equals( ) method, = = = operator
D) = = operator, compare( ) method
If S1 and S2 are two strings, which of the following statements or expressions
are correct.

i)String S3=S1+S2; ii) String S3=S1-S2;


iii) S1<=S2; iv) S1.equals(S2);

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?

i) S.trim() ii) S.replace(‘a’, ‘A’)


iii) S.substring(3) iv) S.toUpperCase()

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

Write a Java code to delete spaces in the given string

Sample Input: Sample Output:

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

public class MyClass


{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String s= sc.nextLine();
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==' ')
continue;
System.out.print(s.charAt(i));
}
}
}
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

Sample Input: Sample Output:

“Have a nice day444."


Digits:3
Consonants:6
Vowels: 6
Spaces: 3
import java.util.*;
else if((ch >= 'a'&& ch <= 'z’))
public class MyClass {
{ ++consonants;
public static void main(String args[]) }
{ else if( ch >= '0' && ch <= '9')
Scanner sc=new Scanner(System.in); {
++digits;
String line=sc.nextLine();
}
int vowels = 0, consonants = 0, digits = 0, spaces = 0; else if (ch ==' ')
line = line.toLowerCase(); {
for(int i = 0; i < line.length(); ++i) ++spaces;
{ }
char ch = line.charAt(i); }
System.out.println("Vowels: " + vowels);
if(ch == 'a' || ch == 'e' || ch == 'i'|| ch == 'o' || ch == 'u')
System.out.println("Consonants: " + consonants);
{ System.out.println("Digits: " + digits);
++vowels; System.out.println("White spaces: " + spaces);
} }}
Question 3

● 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”.

Sample Input: Sample Output:

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

You might also like