Vidyalelo
Java Programming · all questions

Strings
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

107

Questions

5/6

Page

Pick an option on a question to see the right answer and solution.

What will be the output of the following Java program?
class output 
{
    public static void main(String args[])
    {
       String s1 = "Hello World";
       String s2 = s1.substring(0 , 4);
       System.out.println(s2);
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class output 
{
    public static void main(String args[])
    {
        char ch;
        ch = "hello".charAt(1);
        System.out.println(ch);
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java program?
class String_demo 
{
    public static void main(String args[])
    {
        int ascii[] = { 65, 66, 67, 68};
        String s = new String(ascii, 1, 3);
        System.out.println(s);
    }
}

Select an option to see the answer and solution.

Which of these is an incorrect statement?

Select an option to see the answer and solution.

What will be the output of the following Java program?
class output 
{
    public static void main(String args[])
    {             String s = "Hello World";
         int i = s.indexOf('o');
         int j = s.lastIndexOf('l');
         System.out.print(i + " " + j);

    }
}

Select an option to see the answer and solution.

What is the string contained in s after following lines of Java code?
StringBuffer s new StringBuffer("Hello");
s.deleteCharAt(0);

Select an option to see the answer and solution.

What will be the output of the following Java program?
class output 
{
   class output 
   {
     public static void main(String args[])
     {
        char c[]={'A', '1', 'b' ,' ' ,'a' , '0'};
        for (int i = 0; i < 5; ++i)
        {
               i++; 
               if(Character.isDigit(c[i]))
                   System.out.println(c[i]+" is a digit");
               if(Character.isWhitespace(c[i]))
                   System.out.println(c[i]+" is a Whitespace character");
               if(Character.isUpperCase(c[i]))
                   System.out.println(c[i]+" is an Upper case Letter");
               if(Character.isLowerCase(c[i]))
                   System.out.println(c[i]+" is a lower case Letter");
               i++;
        }
    }
}

Select an option to see the answer and solution.

Which of this method of class String is used to obtain a length of String object?

Select an option to see the answer and solution.

What will be the output of the following Java code?
class output 
{
    public static void main(String args[])
    {
        char c[]={'a', '1', 'b' ,' ' ,'A' , '0'};
        for (int i = 0; i < 5; ++i)
        {
               if(Character.isDigit(c[i]))
                   System.out.println(c[i]+" is a digit");
               if(Character.isWhitespace(c[i]))
                   System.out.println(c[i]+" is a Whitespace character");
               if(Character.isUpperCase(c[i]))
                   System.out.println(c[i]+" is an Upper case Letter");
               if(Character.isLowerCase(c[i]))
                   System.out.println(c[i]+" is a lower case Letter");
           i=i+3;
        }
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java program?
class output 
{
    public static void main(String args[])
    { 
         StringBuffer c = new StringBuffer("Hello");
         c.delete(0,2);
         System.out.println(c);
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class output 
{
    public static void main(String args[])
    {
         String chars[] = {"a", "b", "c", "a", "c"};
         for (int i = 0; i < chars.length; ++i)
             for (int j = i + 1; j < chars.length; ++j)
                 if(chars[i].compareTo(chars[j]) == 0)
                     System.out.print(chars[j]); 
    }
}

Select an option to see the answer and solution.

Which of these methods of class StringBuffer is used to extract a substring from a String object?

Select an option to see the answer and solution.

In the following Java code, what can directly access and change the value of the variable name?
package test;
class Target 
{
 public String name = "hello";
}

Select an option to see the answer and solution.

Which of the following statement is correct?

Select an option to see the answer and solution.

What will be the output of the following Java code?
class output 
{
    public static void main(String args[])
    {
       StringBuffer s1 = new StringBuffer("Hello World");
       s1.insert(6 , "Good ");
       System.out.println(s1);
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java program?
class output 
{
    public static void main(String args[])
    { 
         StringBuffer c = new StringBuffer("Hello");
         StringBuffer c1 = new StringBuffer(" World");
         c.append(c1);
         System.out.println(c);
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java program?
class String_demo 
{
    public static void main(String args[])
    {
        char chars[] = {'a', 'b', 'c'};
        String s = new String(chars);
        String s1 = "abcd";
        int len1 = s1.length();
        int len2 = s.length();
        System.out.println(len1 + " " + len2);
    }
}

Select an option to see the answer and solution.

Which of these method of class String is used to compare two String objects for their equality?

Select an option to see the answer and solution.

Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?

Select an option to see the answer and solution.

What will be the output of the following Java code?
public class Boxer1 
{
    Integer i;
    int x;
   public Boxer1(int y) 
   {
        x = i+y;
        System.out.println(x);
   }
   public static void main(String[] args) 
   {
       new Boxer1 (new Integer(4));
   }
}

Select an option to see the answer and solution.