• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

Cracking Java String class questions

  • Categories Java, Strings in Java, String
String class questions
  1. Give the output of the following:
    1. System.out.println (“Simply”. charAt(1));
    2. String na = “Simply Coding”;
      System.out.println( na . length( ) );
    3. System.out.println( “SiMPly”.indexOf(“M”));
    4. System.out.println(“SiMPly”.equalsIgnoreCase(“simply”));

Ans.

    1. i
    2. 13
    3. 2
    4. True
  1. What is the output of the following program?  
public class check
{
public static void change(String nm)
{
nm = “Simply”;
}
public static void main()
{
String name = “Coding”;
System.out.println(name);
change(name);
System.out.println(name);
}
}

Ans

Coding
Coding

  1. What is the output of the following statements when executed?
public class Mathtest
{
public static void change(char [] nm)
{
nm[1] = 'S';
}
public static void main (String args[])
{
char name[] = {'a', 'b'};
System.out.println(name[1]);
change(name);
System.out.println(name[1]);
}
}

Ans

b
S

  1. What is the output of the following statements when executed?
char ch[ ]= {‘I’ 'N', ‘T', ‘E', 'L', ‘P', ‘E’, ‘N', ‘T’, ‘I’, ‘U', 'M'};
String obj= new String(ch, 3, 4);
System.out.println("The result is = "+ obj );

Ans.:

The result is = ELPE

  1. State the output of this program Segment?
String str1 = ”Simply”;
String str2= “Coding”;
System.out.println(str1.substring(0,2). concat(str2.substring(1)));
System.out.println(( “WH”+(str1.substring(2).toUpperCase() )));

Ans.

The 1st output is= Sioding
( because str1. Substring(0,2)gives “Si” & str2. Substring(1) gives “oding”)

The 2nd output is= WHMPLY
( because str1. Substring(2). toUpperCase() gives “MPLY”, so ”WH”+” MPLY”= “WHMPLY”)

  1. If String n1 = “89”; and String n2=”100”; Give the output of the following:
    1. String st= n1+n2; System.out.println (st) ;
    2. System.out.println( n1.charAt( 1 ));
    3. System.out.println( n2 + n1);
    4. System.out.println(n1+ “ ”+n2);

Ans.

    1. 89100
    2. 9
    3. 10089
    4. 89 100
  1. Give the output of following program.
class MainString{
public static void main(String args [ ])
{
StringBuffer s = new Stringbuffer(“Simply”);
if( (s.length()>5) && (s.append(“Coding”). equals(“X”)) ); //empty statement
System.out.println(s);
}
}

Ans.

SimplyCoding

  1. What is the output of this code
String st = " SimplyCoding";
int len= st.length();
for(int j= len-1; j>= 0 ; j – )
System.out.print( st.charAt(j) );

Ans. gnidoCylpmiS

  1. Assume a string “Delhi” is assigned in a variable st and “Singapore” is assigned in variable na. Write a small code to interchange values of st and na.

Ans.

String temp= st;
st= na;
па= temp;
  1. If String x = “SimplyCoding”;
     String y= “Programs”;

What do the following functions return for:

    1. System.out.printin( x. substring( 1, 7));
    2. System.out.println (x. indexOf( x.charAt( 6)));
    3. System.out.println (y +x. substring( 5));
    4. System.out.println( x.equals( y));

Ans.

    1. implyCo ( extracts substring from st to 6th index, 1 means 1st index and 6 means 6th index as array starts with 0)
    2. 6
    3. ProgramsyCoding
    4. False
  1. Write valid java statements to perform the following operations on strings.
    1. Extract 10 characters from the 1st character to 10th character in string address to a new string str.
    2. To print the position of the first occurrence of the letter ‘R’ in the string city

Ans.

    1. String str = address.substring( 0, 10 );
    2. System.out.println( “The position =” + city. indexOf (‘R’));
  1. What will be the output of the following program, when Method invoke() is called:
public class StringArray
{
void change(String arr[])
{
for(int i=0;i<arr.length;i++)
{
arr[i]=arr[i].substring(0,1).toUpperCase()+arr[i].substring(1);
}
}
void invoke()
{
String ar[]={“simply”,“coding”,“programs”};
for(int i=0;i<ar.length;i++)
System.out.println(ar[i]);
change(ar);
for(int i=0;i<ar.length;i++)
System.out.println(ar[i]);
}
}

Ans.

simply
coding
programs
Simply
Coding
Programs

  1. Give the output of the following program fragment:
String s=new String("Hello! this is simply coding");
String t;
t=s.replace("this is","Welcome to");
System.out.println(t);

Ans.

Hello! Welcome to simply coding

  1. Give the output of the following program fragment:
String s="india",s1="SimPLY",s2=s;
System.out.println(s.equals(s1));
System.out.println(s.equalsIgnoreCase(s1));
System.out.println(s2==s);
System.out.println(s.toUpperCase()==s1.toUpperCase());
System.out.println(s.startsWith("IM".toLowerCase()));
System.out.println(s1.endsWith("iA".toUpperCase()));

Ans.

false
false
true
false
false
false

  1. What do the following functions return for:
String x ="Simply";
String y ="Coding";
System.out.println(x + y);
System.out.println(x.length());
System.out.println(x.charAt(3));
System.out.println(x.equals(y));

Ans.

SimplyCoding
6
p
false

  1. What is the output of the following:
(i)    System.out.println ("output1 :" + 6 + 4);
       System.out.println ("output2 :"+(6+4));

(ii) String S1 = "Hi";
     String S2 = "Hi";
     String S3 = "Simply";
     String S4 = "HI";
     System.out.println(S1 + "equals" + S2 + "→" + S1.equals(S2));
     System.out.println(S1 + "equals" + S3 + "→" + S1.equals(S3));
     System.out.println(S1 + "equals" + S4 + "→" + S1.equals(S4));
     System.out.println(S1 + "equalsIgnoreCase" +S4 + "→" + S1.equalsIgnoreCase(S4));

Ans.

(i) output1 :64
     output2 :10

(ii)  HiequalsHi→true

      HiequalsSimply→false

      HiequalsHI→false

     HiequalsIgnoreCaseHI→true

  1. If, String x = “Simply”;
    String y = “Coding”;
    1. System.out.println(x.substring(1,4));
    2. System.out.println(x.indexOf(x.charAt(4)));
    3. System.out.println(y+x.substring(5));
    4. System.out.println(x.equals(y));

Ans.

    1. imp
    2. 4
    3. Codingy
    4. False
  1. Write a statement each to perform the following task on a string:

(i) Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type

Ans.

(i) System.out.println(s.lastIndexOf(“ ”));
(ii) double d=Double.parseDouble(x);

  1. Write a statement each to perform the following task on a string:

(i) Extract the second last character of a word stored in the variable wd.
(ii) Check if the second character of a string str is in uppercase.

Ans.

(i) char sl=wd.charAt(wd.length()-2);
(ii) if(Character.isUpperCase(str.charAt(1)))

  1. Give the output of the following string functions:
    1. “SimplyCoding”.replace(‘i’,’A’)
    2. “Eating”.compareTo(“Coding”)

Ans.

    1. SAmplyCodAng
    2. 2
  1. Consider the following String array and give the output:
String arr[]={"Java","PHP","Python",".Net","HTML"};
System.out.println(arr[0].length()>arr[3].length());
System.out.print(arr[2].substring(0,3));

Ans.

false
Pyt

  1. String x[]={“Simply”,”Coding”,”Online”,”Tutor”,”Courses”};
    Give the output of the following statements:

(i) System.out.println(x[1]);
(ii) System.out.println(x[3].length());

Ans.

(i) Coding
(ii) 5

  1. Write the output for the following:
String s="Simply Coding";
System.out.println(s.indexOf('T'));
System.out.println("Welcome to"+" "+s.substring(0,10));

Ans.

-1
Welcome to Simply Cod

  1. Write a statement each to perform the following task on a string:

          String s1 = “good”, s2 = “going”;
          1 . Write coed to concat them into string s.
          2 . Write coed to replace ‘g’ with ‘f’ in string s

Ans.  s = s1.concat(s2)
          s = s.replace(‘g’, f)

  1. Give the ouput of the following string functions:

(i) “SimplyCoding”.indexOf(‘l’)+”SimplyCoding”.lastIndexOf(‘C’)
(ii) “Simply”.compareTo(“SimplyCoding”)

Ans.

(i) 10
(ii) -6

  1. State the output of the following program segment when executed:
String a = "Simply", b = "Coding";
String h = a.substring(1, 3);
String k = b.substring(2).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));

Ans.

im
false

  1. State the output of the following program segment:
String str1 = "Simply", str2 = "Coding";
System.out.println(str1.substring(0,2).concat(str2.substring(1)));
System.out.println(("HL" + (str1.substring(2).toUpperCase())));

Ans.

Sioding
HLMPLY

  1. State the value of characteristic and mantissa when the following code is executed.
String s = "6.2708";
int n = s.indexOf('.');
int characteristic = Integer.parseInt(s.substring(0,n));
int mantissa = Integer.valueOf(s.substring(n+1));

Ans.

characteristic=6
mantissa= 2708

  1. State the output of the following program segment.
String s = "SimplyCoding";
int n = s.length();
System.out.println(s.startsWith(s.substring(4, n)));
System.out.println(s.charAt(1) == s.charAt(9));

Ans.

false
true

  1. What will the following code output?
String s = "SimplyCoding";
System.out.println(s.indexOf('i'));
System.out.println(s.lastIndexOf('i'));

Ans.

1
9

  1. Give the output of the following:
String str1 = "replace";
str1.toUpperCase();
System.out.println(str1);

Ans. replace

  1. Give the output of the following:
String s1 = "Simply";
String s2 = "Coding";
System.out.println(s1.substring (0,3).concat(s2.substring(4)));
System.out.println(s1.endsWith("e"));

Ans.

Simng
False

  • Share:
author avatar
Simply Coding

Previous post

Java character class example questions
June 11, 2020

Next post

Java Arrays Exercises
June 11, 2020

You may also like

Questions on String 1
What is Java String?
3 July, 2021
String Handling Programs
String Handling Programs
22 June, 2021
Crack Java Math Problems
Forming Math Expressions
13 June, 2020

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now