MCQ

Java MCQ – Multiple Choice Questions and Answers – Strings – Part 2

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Strings”.
 

1. Which of the following constructors is used to create an empty String object?

A String()

B String(null)

C String(” “)

D None of the above

A
String() is a constructor which is used to create an empty String object. Example:

String s = new String(); // empty object is created
 

2. What is the output of the following code?
public class Q2
{
    public static void main(String []args)
    {
        char chars[] = {'b', 'o', 'b'};
        String str = new String(chars);
        System.out.println(str);
    }
}

A b

B o

C bob

D Error

C
String(chars) is a constructor of class String, which initialize the string str with the values stored in chars, therefore str contains “bob”.
 

3. What is the output of the following code?
public class Q3
{
    public static void main(String []args)
    {
        String str = "I LOVE JAVA";   
        System.out.println(str.charAt(4));
    }
}

A O

B V

C E

D L

B
charAt() is a method which provides the character specified by the index. str.charAt(4) gives 5th character.
 

4. What is the output of the following code?
public class Q4
{
    public static void main(String []args)
    {
        String str = "I LOVE JAVA";   
        System.out.println(str.length());
    }
}

A 11

B 10

C 12

D 9

A
length() method is used to get the length of a String object.
 

5. What is the output of the following code?
public class Q5
{
    public static void main(String []args)
    {
		String str1 = "Emily";
		String str2 = "Alex";
		System.out.println(str1.charAt(0) > str2.charAt(0));
    }
}

A true

B false

C 0

D 1

A
str1.charAt(0) ascii value is greater than str2.charAt(0). So it will return True.
 

6. What is the output of the following code?
public class Q6
{
    public static void main(String []args)
    {
		String a = "lorem";
		int b = 8;
		System.out.println(a += b);
    }
}

A 8lorem

B lorem8

C Compilation error

D Will throw an exception

B
a += b implies a = a + b so 8 is concatenated with lorem.
 

7. The String method compareTo() returns _______.

A 1

B -1

C false

D true

E Int value

E
The String method compareTo() compares first argument with the second argument and returns positive number, if the first argument is bigger than the second number. Returns negitive number if the first argument is less than the second argument and zero if both are equal.
 

8. What is the output of the following code?
public class Q8
{
    public static void main(String []args)
    {
        String str = "hello";
        System.out.println(str.substring(1, 4)); 
    }
}

A hel

B ell

C llo

D hell

C
substring() method returns a substring of this string. The substring starts with the character at the specified index and expands to the end of this string.
 

9. What is the output of the following code?
public class Q9
{
    public static void main(String []args)
    {
        String str = "Hello";
        System.out.println(str.indexOf('a'));
    }
}

A 0

B -1

C false

D Compilation error

B
indexOf() returns the value of index if it is present. Else it returns -1.
 

10. What is the output of the following code?
public class Q10
{
    public static void main(String []args)
    {
		String s1 = "Stack";
		String s2 = "HowTo";
		System.out.println(s1.concat(s2));
    }
}

A Stack

B HowTo

C StackHowTo

D Compilation error

C
Concat() method adds one String to the end of another.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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