Java String Basics

String Handling

    1) Which package does define String and StringBuffer classes?

        Ans : java.lang package.

    2) Which method can be used to obtain the length of the String?

        Ans : length( ) method.

    3) How do you concatenate Strings?

        Ans : By using ” + ” operator.

    4) Which method can be used to compare two strings for equality?

        Ans : equals( ) method.

    5) Which method can be used to perform a comparison between strings that ignores case differences?

        Ans : equalsIgnoreCase( ) method.

    6) What is the use of valueOf( ) method?

        Ans : valueOf( ) method converts data from its internal format into a human-readable form.

    7) What are the uses of toLowerCase( ) and toUpperCase( ) methods?

        Ans : The method toLowerCase( ) converts all the characters in a string from uppercase to

        lowercase.

        The method toUpperCase( ) converts all the characters in a string from lowercase to

        uppercase.

    8) Which method can be used to find out the total allocated capacity of a StrinBuffer?

        Ans : capacity( ) method.

    9) Which method can be used to set the length of the buffer within a StringBuffer object?

        Ans : setLength( ).

    10) What is the difference between String and StringBuffer?

        Ans : String objects are constants, whereas StringBuffer objects are not.

        String class supports constant strings, whereas StringBuffer class supports growable, modifiable strings.

    11) What are wrapper classes?

        Ans : Wrapper classes are classes that allow primitive types to be accessed as objects.

    12) Which of the following is not a wrapper class?

            String
            Integer
            Boolean
            Character

        Ans : a.

    13) What is the output of the following program?

    public class Question {

    public static void main(String args[]) {

    String s1 = “abc”;

    String s2 = “def”;

    String s3 = s1.concat(s2.toUpperCase( ) );

    System.out.println(s1+s2+s3);

    }

    }

            abcdefabcdef
            abcabcDEFDEF
            abcdefabcDEF
            None of the above

        ANS : c.

    14) Which of the following methods are methods of the String class?

            delete( )
            append( )
            reverse( )
            replace( )

        Ans : d.

    15) Which of the following methods cause the String object referenced by s to be changed?

            s.concat( )
            s.toUpperCase( )
            s.replace( )
            s.valueOf( )

        Ans : a and b.

    16) String is a wrapper class?

            True
            False

        Ans : b.

    17) If you run the code below, what gets printed out?

    String s=new String(“Bicycle”);

    int iBegin=1;

    char iEnd=3;

    System.out.println(s.substring(iBegin,iEnd));

            Bic
            ic
            icy
            error: no method matching substring(int,char)

        Ans : b.

    18) Given the following declarations

    String s1=new String(“Hello”)

    String s2=new String(“there”);

    String s3=new String();

    Which of the following are legal operations?

            s3=s1 + s2;
            s3=s1 – s2;
            s3=s1 & s2
            s3=s1 && s2

        Ans : a.

    19) Which of the following statements are true?

        a)The String class is implemented as a char array, elements are addressed using the stringname[] convention

        b) Strings are a primitive type in Java that overloads the + operator for concatenation

        c) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type

        d) The size of a string can be retrieved using the length property.

        Ans : b.

Similar Posts

  • Java Packages

    Java.lang     1) java.lang package is automatically imported into all programs.             True             False         Ans : a     2) What are the interfaces defined by java.lang?         Ans : Cloneable, Comparable and Runnable.     3) What are the constants defined by both Flaot and Double classes?         Ans : MAX_VALUE,         MIN_VALUE,        …

  • Java Basics

    Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet, Java is used…

  • Java Terminology

    Introduction to Classes and Methods     1) Which is used to get the value of the instance variables?         Ans: Dot notation.     2) The new operator creates a single instance named class and returns a reference to that object.         a)True         b)False         Ans: a.     3) A class is a template for…

  • Java Packages-2

    Networking Concepts     1) The API doesn’t list any constructors for InetAddress- How do I create an InetAddress instance?         ANSWER : In case of InetAddress the three methods getLocalHost, getByName, getByAllName can be used to create instances.         E.g.         InetAddress add1;         InetAddress add2;         try{         add1 = InetAddress.getByName(“java.sun.com”);         add2 =…

  • Java Featurs

    Exception Handling     1) What is the difference between ΓÇÿthrowΓÇÖ and ΓÇÿthrowsΓÇÖ ?And itΓÇÖs application?         Ans : Exceptions that are thrown by java runtime systems can be handled by Try and catch blocks. With throw exception we can handle the exceptions thrown by the program itself. If a method is capable of causing an…

Leave a Reply

Your email address will not be published. Required fields are marked *