Tuesday, July 30, 2024

Srtring MCQ

 In Java, which of the following best describes a string?

a) A primitive data type

b) A reference data type

c) A combination of characters

d) An array of characters


Which method in Java is used to obtain the length of a string?

a) length()

b) size()

c) count()

d) getSize()


Which method in Java is used to concatenate two strings?

a) append()

b) concat()

c) combine()

d) add()


Which method is used to compare two strings for equality in Java?

a) equals()

b) compareTo()

c) compare()

d) isEqual()


What is the purpose of the charAt() method in Java?

a) It returns the character at a specified index in a string.

b) It checks if a character exists in a string.

c) It modifies a character at a specified index in a string.

d) It returns the ASCII value of a character in a string.


Which method in Java is used to convert all characters in a string to lowercase?

a) toLower()

b) toLowerCase()

c) convertToLower()

d) caseLower()


Which method is used in Java to find the index of a specific character or substring within a string?

a) search()

b) find()

c) indexOf()

d) locate()


What is the purpose of the substring() method in Java?

a) It splits a string into multiple substrings.

b) It reverses a string.

c) It extracts a portion of a string.

d) It concatenates two strings.


Which method is used to remove leading and trailing white spaces from a string in Java?

a) removeSpaces()

b) trim()

c) strip()

d) clean()


What does the startsWith() method in Java do?

a) It checks if a string ends with a specific character or sequence.

b) It checks if a string starts with a specific character or sequence.

c) It checks if a string contains a specific character or sequence.

d) It checks if two strings are equal.


Answers:


c) A combination of characters

a) length()

b) concat()

a) equals()

a) It returns the character at a specified index in a string.

b) toLowerCase()

c) indexOf()

c) It extracts a portion of a string.

b) trim()

b) It checks if a string starts with a specific character or sequence.


MCQs on String and Methods in Java

What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Hello";

        str.concat(" World");

        System.out.println(str);

    }

}

a) Hello World

b) Hello

c) World

d) Hello World


Answer: b) Hello

Explanation: The concat method returns a new string, but it does not modify the original string.


What will the following code print?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java Programming";

        System.out.println(str.substring(5, 11));

    }

}

a) Program

b) Progra

c) Progra

d) Programming


Answer: b) Progra

Explanation: substring(5, 11) returns the substring from index 5 to 10 (the end index is exclusive).


What does the String.equalsIgnoreCase(String str) method do?


a) Compares two strings and returns true if they are equal, ignoring case differences.

b) Compares two strings and returns true if they are exactly equal.

c) Checks if one string contains another string, ignoring case differences.

d) Converts the string to lowercase.


Answer: a) Compares two strings and returns true if they are equal, ignoring case differences.

Explanation: equalsIgnoreCase compares two strings, ignoring their case.


What is the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java";

        System.out.println(str.length());

    }

}

a) 3

b) 4

c) 5

d) Java


Answer: b) 4

Explanation: The length() method returns the number of characters in the string.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "   Hello   ";

        System.out.println(str.trim());

    }

}

a) Hello

b) Hello

c) Hello

d) Hello


Answer: a) Hello

Explanation: The trim() method removes leading and trailing whitespace.


What will the following code print?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Hello World";

        System.out.println(str.indexOf("World"));

    }

}

a) 6

b) 5

c) 0

d) 11


Answer: a) 6

Explanation: indexOf("World") returns the index of the first occurrence of "World".


What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "hello";

        System.out.println(str.toUpperCase());

    }

}

a) HELLO

b) hello

c) Hello

d) HELLO


Answer: a) HELLO

Explanation: toUpperCase() converts all characters in the string to uppercase.


What is the result of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java Programming";

        System.out.println(str.replace('a', 'o'));

    }

}

a) Jovo Progroming

b) Jova Programming

c) Jovo Progromming

d) Jova Progromming


Answer: a) Jovo Progroming

Explanation: replace('a', 'o') replaces all occurrences of 'a' with 'o'.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Hello";

        System.out.println(str.charAt(2));

    }

}

a) H

b) e

c) l

d) o


Answer: c) l

Explanation: charAt(2) returns the character at index 2.


What will the following code print?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java Programming";

        System.out.println(str.substring(5));

    }

}

a) Programming

b) Java Progr

c) Programming

d) Programming


Answer: a) Programming

Explanation: substring(5) returns the substring from index 5 to the end of the string.


What will the following code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Hello World";

        String newStr = str.concat("!");

        System.out.println(newStr);

    }

}

a) Hello World

b) Hello World!

c) Hello! World

d) Hello!World


Answer: b) Hello World!

Explanation: concat("!") adds ! to the end of the string.


What does the String.split(String regex) method do?


a) Splits the string into substrings based on the given regular expression.

b) Joins an array of strings into a single string using a delimiter.

c) Replaces all occurrences of a specified substring with another substring.

d) Converts the string to lowercase.


Answer: a) Splits the string into substrings based on the given regular expression.

Explanation: split divides the string based on the delimiter regex.


What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java Programming";

        String[] words = str.split(" ");

        System.out.println(words[1]);

    }

}

a) Java

b) Programming

c)

d) Error


Answer: b) Programming

Explanation: split(" ") separates the string into words, and words[1] is Programming.


What does the String.format(String format, Object... args) method do?


a) Formats a string based on the specified format and arguments.

b) Returns a string with leading and trailing spaces removed.

c) Converts the string to uppercase.

d) Replaces all occurrences of a specified character.


Answer: a) Formats a string based on the specified format and arguments.

Explanation: String.format creates a formatted string using the given format and arguments.


What will the following code print?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Hello";

        String newStr = str + " World";

        System.out.println(newStr);

    }

}

a) Hello World

b) Hello

c) World Hello

d) HelloWorld


Answer: a) Hello World

Explanation: Concatenation of str and " World" results in Hello World.



MCQs on String Methods in Java

What does the length() method of the String class return?


a) The number of words in the string.

b) The number of characters in the string.

c) The length of the longest word in the string.

d) The index of the last character.


Answer: b) The number of characters in the string.

Explanation: length() returns the total number of characters in the string.


What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Hello World";

        System.out.println(str.toUpperCase());

    }

}

a) HELLO WORLD

b) hello world

c) Hello World

d) HELLO world


Answer: a) HELLO WORLD

Explanation: toUpperCase() converts all characters in the string to uppercase.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java Programming";

        System.out.println(str.toLowerCase());

    }

}

a) JAVA PROGRAMMING

b) java programming

c) Java Programming

d) JaVa ProGrAmMiNg


Answer: b) java programming

Explanation: toLowerCase() converts all characters in the string to lowercase.


What does the charAt(int index) method do?


a) Returns the substring starting at the specified index.

b) Returns the character at the specified index.

c) Returns the index of the specified character.

d) Converts the character at the specified index to uppercase.


Answer: b) Returns the character at the specified index.

Explanation: charAt(index) returns the character at the given index.


What will be the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java";

        System.out.println(str.indexOf('a'));

    }

}

a) 1

b) 2

c) 0

d) 3


Answer: b) 1

Explanation: indexOf('a') returns the index of the first occurrence of 'a', which is 1.


What does the lastIndexOf(String str) method return?


a) The index of the first occurrence of the string.

b) The index of the last occurrence of the string.

c) The length of the string.

d) The substring before the last occurrence.


Answer: b) The index of the last occurrence of the string.

Explanation: lastIndexOf returns the index of the last occurrence of the specified substring.


What will the following code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Programming";

        System.out.println(str.substring(3));

    }

}

a) Pro

b) gramming

c) gramming

d) Programming


Answer: b) gramming

Explanation: substring(3) returns the substring from index 3 to the end of the string.


What does substring(0, 2) do?


a) Returns the substring from index 0 to 2 (exclusive).

b) Returns the substring from index 2 to the end of the string.

c) Returns the substring from index 0 to 2 (inclusive).

d) Returns the substring from index 0 to 2 (exclusive).


Answer: a) Returns the substring from index 0 to 2 (exclusive).

Explanation: substring(0, 2) returns the substring starting from index 0 to 1.


What does the equals(Object obj) method do?


a) Compares two strings and returns true if they are equal, ignoring case.

b) Compares two strings and returns true if they are exactly equal.

c) Checks if the string contains another string.

d) Converts the string to lowercase.


Answer: b) Compares two strings and returns true if they are exactly equal.

Explanation: equals checks if two strings are exactly the same.


What will be the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str1 = "Hello";

        String str2 = "World";

        System.out.println(str1.concat(" ").concat(str2));

    }

}

a) HelloWorld

b) Hello World

c) Hello WorldWorld

d) Hello World HelloWorld


Answer: b) Hello World

Explanation: concat adds the given string at the end.


What does the contains(CharSequence sequence) method do?


a) Checks if the string starts with the specified sequence.

b) Checks if the string ends with the specified sequence.

c) Checks if the string contains the specified sequence.

d) Replaces all occurrences of the specified sequence.


Answer: c) Checks if the string contains the specified sequence.

Explanation: contains checks if the string contains the specified sequence of characters.


What will be the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java Programming";

        System.out.println(str.startsWith("Java"));

    }

}

a) true

b) false

c) Java

d) Programming


Answer: a) true

Explanation: startsWith("Java") checks if the string starts with "Java".


What does the endsWith(String suffix) method check?


a) If the string starts with the specified suffix.

b) If the string ends with the specified suffix.

c) If the string contains the specified suffix.

d) If the string matches the specified suffix.


Answer: b) If the string ends with the specified suffix.

Explanation: endsWith checks if the string ends with the specified suffix.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "   Hello World   ";

        System.out.println(str.trim());

    }

}

a) Hello World

b) Hello World

c) Hello World

d) Hello World


Answer: a) Hello World

Explanation: trim() removes leading and trailing whitespace.


What does the toCharArray() method do?


a) Converts the string to an array of integers.

b) Converts the string to an array of characters.

c) Converts the string to a character sequence.

d) Converts each character in the string to uppercase.


Answer: b) Converts the string to an array of characters.

Explanation: toCharArray() returns a new character array containing the characters of the string.


What will be the result of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "Java Programming";

        String replacedStr = str.replace('a', 'o');

        System.out.println(replacedStr);

    }

}

a) Jovo Progromming

b) Jova Programming

c) Jova Progromming

d) Jovo Programming


Answer: a) Jovo Progromming

Explanation: replace('a', 'o') replaces all occurrences of 'a' with 'o'.


What does the split(String regex) method do?


a) Splits the string based on the given regular expression.

b) Joins the array of strings into a single string using the specified delimiter.

c) Converts the string to uppercase.

d) Replaces all occurrences of the specified character.


Answer: a) Splits the string based on the given regular expression.

Explanation: split divides the string into substrings based on the provided delimiter.


What will the following code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        String str = "apple,banana,grape";

        String[] fruits = str.split(",");

        System.out.println(fruits.length);

    }

}

a) 2



No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...